Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ce9a2b9
Items that have contents now drops them on death
JavierLeon9966 Jan 24, 2022
027e8e1
Merge branch 'stable' into item-drop-contents
dktapps Sep 28, 2022
4b918aa
Added `Item::getContainedItems()` and `Item::setContainedItems()`
JavierLeon9966 Oct 3, 2022
1168d47
Fixed syntax error
JavierLeon9966 Oct 3, 2022
135781c
Fixed CS
JavierLeon9966 Oct 3, 2022
1ab9e4b
Merge branch 'next-minor' into item-drop-contents
JavierLeon9966 Oct 3, 2022
f1701a5
Merge remote-tracking branch 'origin/minor-next' into item-drop-contents
JavierLeon9966 Aug 22, 2024
ee86b51
Fixed items not preserving their slot positions
JavierLeon9966 Aug 23, 2024
28ae2d7
Extracted item contents serialization code
JavierLeon9966 Aug 23, 2024
bbbaf7e
Fixed PHPStan error
JavierLeon9966 Aug 23, 2024
d9fcbc7
Fix CS
JavierLeon9966 Aug 23, 2024
8d9e14d
Fix CS (again)
JavierLeon9966 Aug 23, 2024
6d47810
Make chiseled bookshelves use their own items serialization
JavierLeon9966 Aug 24, 2024
9628fbf
Include more item containers to be dropped upon destroy
JavierLeon9966 Aug 24, 2024
90d2432
Fix CS
JavierLeon9966 Aug 24, 2024
074cf27
Fix CS
JavierLeon9966 Aug 24, 2024
b0f1347
Merge remote-tracking branch 'fork/item-drop-contents' into item-drop…
JavierLeon9966 Aug 24, 2024
039105f
Renamed "util" folder to "utils" for consistency
JavierLeon9966 Aug 24, 2024
1582f95
Merge branch 'minor-next' into item-drop-contents
dktapps Nov 15, 2024
1ba57de
Clone the contained items before setting them
JavierLeon9966 Nov 29, 2024
d3e45f7
Merge branch 'minor-next' into item-drop-contents
JavierLeon9966 Nov 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/entity/object/ItemEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,10 @@ public function onCollideWithPlayer(Player $player) : void{
}
$this->flagForDespawn();
}

protected function onDeath() : void{
foreach($this->item->getContainedItems() as $item){
$this->getWorld()->dropItem($this->location, $item);
}
}
}
38 changes: 38 additions & 0 deletions src/item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use pocketmine\block\Block;
use pocketmine\block\BlockBreakInfo;
use pocketmine\block\BlockToolType;
use pocketmine\block\tile\Container;
use pocketmine\block\VanillaBlocks;
use pocketmine\data\bedrock\EnchantmentIdMap;
use pocketmine\data\bedrock\item\ItemTypeDeserializeException;
Expand Down Expand Up @@ -100,6 +101,9 @@ class Item implements \JsonSerializable{

protected bool $keepOnDeath = false;

/** @var Item[] $containedItems */
protected array $containedItems = [];

/**
* Constructs a new Item type. This constructor should ONLY be used when constructing a new item TYPE to register
* into the index.
Expand Down Expand Up @@ -238,6 +242,21 @@ public function setKeepOnDeath(bool $keepOnDeath) : void{
$this->keepOnDeath = $keepOnDeath;
}

/**
* @return Item[]
*/
public function getContainedItems() : array{
return $this->containedItems;
}

/**
* @param Item[] $items
*/
public function setContainedItems(array $items) : void{
Utils::validateArrayValueType($items, static function(Item $_) : void{});
$this->containedItems = $items;
}

/**
* Returns whether this Item has a non-empty NBT.
*/
Expand Down Expand Up @@ -338,6 +357,15 @@ protected function deserializeCompoundTag(CompoundTag $tag) : void{
}

$this->keepOnDeath = $tag->getByte(self::TAG_KEEP_ON_DEATH, 0) !== 0;

$this->containedItems = [];
$containedItems = $tag->getListTag(Container::TAG_ITEMS);
if($containedItems !== null && $containedItems->getTagType() === NBT::TAG_Compound){
/** @var CompoundTag $itemNBT */
foreach($containedItems as $itemNBT){
$this->containedItems[] = Item::nbtDeserialize($itemNBT);
}
}
}

protected function serializeCompoundTag(CompoundTag $tag) : void{
Expand Down Expand Up @@ -406,6 +434,16 @@ protected function serializeCompoundTag(CompoundTag $tag) : void{
}else{
$tag->removeTag(self::TAG_KEEP_ON_DEATH);
}

if(count($this->containedItems) > 0){
$containedItems = new ListTag();
foreach($this->containedItems as $item){
$containedItems->push($item->nbtSerialize());
}
$tag->setTag(Container::TAG_ITEMS, $containedItems);
}else{
$tag->removeTag(Container::TAG_ITEMS);
}
}

public function getCount() : int{
Expand Down