Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Items that have contents now drops them on destroy #4768

Open
wants to merge 21 commits into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/block/Barrel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\tile\Barrel as TileBarrel;
use pocketmine\block\utils\AnyFacingTrait;
use pocketmine\block\utils\ContainerTrait;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Item;
use pocketmine\math\Facing;
Expand All @@ -35,6 +36,7 @@

class Barrel extends Opaque{
use AnyFacingTrait;
use ContainerTrait;

protected bool $open = false;

Expand Down
2 changes: 2 additions & 0 deletions src/block/BrewingStand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\tile\BrewingStand as TileBrewingStand;
use pocketmine\block\utils\BrewingStandSlot;
use pocketmine\block\utils\ContainerTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Item;
Expand All @@ -37,6 +38,7 @@
use function spl_object_id;

class BrewingStand extends Transparent{
use ContainerTrait;

/**
* @var BrewingStandSlot[]
Expand Down
2 changes: 2 additions & 0 deletions src/block/Chest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace pocketmine\block;

use pocketmine\block\tile\Chest as TileChest;
use pocketmine\block\utils\ContainerTrait;
use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\event\block\ChestPairEvent;
Expand All @@ -35,6 +36,7 @@

class Chest extends Transparent{
use FacesOppositePlacingPlayerTrait;
use ContainerTrait;

/**
* @return AxisAlignedBB[]
Expand Down
2 changes: 2 additions & 0 deletions src/block/ChiseledBookshelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\tile\ChiseledBookshelf as TileChiseledBookshelf;
use pocketmine\block\utils\ChiseledBookshelfSlot;
use pocketmine\block\utils\ContainerTrait;
use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\data\runtime\RuntimeDataDescriber;
Expand All @@ -41,6 +42,7 @@
class ChiseledBookshelf extends Opaque{
use HorizontalFacingTrait;
use FacesOppositePlacingPlayerTrait;
use ContainerTrait;

/**
* @var ChiseledBookshelfSlot[]
Expand Down
2 changes: 2 additions & 0 deletions src/block/Furnace.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace pocketmine\block;

use pocketmine\block\tile\Furnace as TileFurnace;
use pocketmine\block\utils\ContainerTrait;
use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
use pocketmine\block\utils\LightableTrait;
use pocketmine\crafting\FurnaceType;
Expand All @@ -36,6 +37,7 @@
class Furnace extends Opaque{
use FacesOppositePlacingPlayerTrait;
use LightableTrait;
use ContainerTrait;

protected FurnaceType $furnaceType;

Expand Down
2 changes: 2 additions & 0 deletions src/block/Hopper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace pocketmine\block;

use pocketmine\block\tile\Hopper as TileHopper;
use pocketmine\block\utils\ContainerTrait;
use pocketmine\block\utils\PoweredByRedstoneTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
Expand All @@ -36,6 +37,7 @@

class Hopper extends Transparent{
use PoweredByRedstoneTrait;
use ContainerTrait;

private int $facing = Facing::DOWN;

Expand Down
5 changes: 1 addition & 4 deletions src/block/ShulkerBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Blo
}

private function addDataFromTile(TileShulkerBox $tile, Item $item) : void{
$shulkerNBT = $tile->getCleanedNBT();
if($shulkerNBT !== null){
$item->setNamedTag($shulkerNBT);
}
$item->setContainedItems($tile->getRealInventory()->getContents());
if($tile->hasName()){
$item->setCustomName($tile->getName());
}
Expand Down
10 changes: 9 additions & 1 deletion src/block/tile/Barrel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
namespace pocketmine\block\tile;

use pocketmine\block\inventory\BarrelInventory;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\world\World;

class Barrel extends Spawnable implements Container, Nameable{
use NameableTrait;
use NameableTrait {
copyDataFromItem as copyNameFromItem;
}
use ContainerTrait;

protected BarrelInventory $inventory;
Expand All @@ -49,6 +52,11 @@ protected function writeSaveData(CompoundTag $nbt) : void{
$this->saveItems($nbt);
}

public function copyDataFromItem(Item $item) : void{
$this->copyNameFromItem($item);
$this->copyContentsFromItem($item);
}

public function close() : void{
if(!$this->closed){
$this->inventory->removeAllViewers();
Expand Down
6 changes: 6 additions & 0 deletions src/block/tile/BrewingStand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
class BrewingStand extends Spawnable implements Container, Nameable{
use NameableTrait {
addAdditionalSpawnData as addNameSpawnData;
copyDataFromItem as copyNameFromItem;
}
use ContainerTrait;

Expand Down Expand Up @@ -92,6 +93,11 @@ protected function writeSaveData(CompoundTag $nbt) : void{
$nbt->setShort(self::TAG_REMAINING_FUEL_TIME_PE, $this->remainingFuelTime);
}

public function copyDataFromItem(Item $item) : void{
$this->copyNameFromItem($item);
$this->copyContentsFromItem($item);
}

protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
$this->addNameSpawnData($nbt);

Expand Down
10 changes: 9 additions & 1 deletion src/block/tile/Chest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\inventory\ChestInventory;
use pocketmine\block\inventory\DoubleChestInventory;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
Expand All @@ -35,8 +36,10 @@
class Chest extends Spawnable implements Container, Nameable{
use NameableTrait {
addAdditionalSpawnData as addNameSpawnData;
copyDataFromItem as copyNameFromItem;
}
use ContainerTrait {
getCleanedNBT as getContainerNBT;
onBlockDestroyedHook as containerTraitBlockDestroyedHook;
}

Expand Down Expand Up @@ -83,14 +86,19 @@ protected function writeSaveData(CompoundTag $nbt) : void{
}

public function getCleanedNBT() : ?CompoundTag{
$tag = parent::getCleanedNBT();
$tag = $this->getContainerNBT();
if($tag !== null){
//TODO: replace this with a purpose flag on writeSaveData()
$tag->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
}
return $tag;
}

public function copyDataFromItem(Item $item) : void{
$this->copyNameFromItem($item);
$this->copyContentsFromItem($item);
}

public function close() : void{
if(!$this->closed){
$this->inventory->removeAllViewers();
Expand Down
5 changes: 5 additions & 0 deletions src/block/tile/ChiseledBookshelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,9 @@ protected function saveItems(CompoundTag $tag) : void{
$tag->setString(Container::TAG_LOCK, $this->lock);
}
}

public function copyDataFromItem(Item $item) : void{
parent::copyDataFromItem($item);
$this->copyContentsFromItem($item);
}
}
50 changes: 28 additions & 22 deletions src/block/tile/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@

namespace pocketmine\block\tile;

use pocketmine\data\bedrock\item\SavedItemStackData;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\block\tile\utils\ContainerHelper;
use pocketmine\inventory\Inventory;
use pocketmine\item\Item;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\world\Position;

Expand All @@ -43,22 +40,12 @@ trait ContainerTrait{
abstract public function getRealInventory() : Inventory;

protected function loadItems(CompoundTag $tag) : void{
if(($inventoryTag = $tag->getTag(Container::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){
$newContents = ContainerHelper::deserializeContents($tag);
if($newContents !== null){
$inventory = $this->getRealInventory();
$listeners = $inventory->getListeners()->toArray();
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization

$newContents = [];
/** @var CompoundTag $itemNBT */
foreach($inventoryTag as $itemNBT){
try{
$newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT);
}catch(SavedDataLoadingException $e){
//TODO: not the best solution
\GlobalLogger::get()->logException($e);
continue;
}
}
$inventory->setContents($newContents);

$inventory->getListeners()->add(...$listeners);
Expand All @@ -70,18 +57,37 @@ protected function loadItems(CompoundTag $tag) : void{
}

protected function saveItems(CompoundTag $tag) : void{
$items = [];
foreach($this->getRealInventory()->getContents() as $slot => $item){
$items[] = $item->nbtSerialize($slot);
}

$tag->setTag(Container::TAG_ITEMS, new ListTag($items, NBT::TAG_Compound));
ContainerHelper::serializeContents($tag, $this->getRealInventory()->getContents());

if($this->lock !== null){
$tag->setString(Container::TAG_LOCK, $this->lock);
}
}

/**
* @see Tile::getCleanedNBT()
*/
public function getCleanedNBT() : ?CompoundTag{
$tag = parent::getCleanedNBT();
if($tag !== null){
$tag->removeTag(Container::TAG_ITEMS);
}
return $tag;
}

/**
* @see Tile::copyDataFromItem()
*/
public function copyContentsFromItem(Item $item) : void{
$inventory = $this->getRealInventory();
$listeners = $inventory->getListeners()->toArray();
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization

$inventory->setContents($item->getContainedItems());

$inventory->getListeners()->add(...$listeners);
}

/**
* @see Container::canOpenWith()
*/
Expand Down
9 changes: 8 additions & 1 deletion src/block/tile/Furnace.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
use function max;

abstract class Furnace extends Spawnable implements Container, Nameable{
use NameableTrait;
use NameableTrait {
copyDataFromItem as copyNameFromItem;
}
use ContainerTrait;

public const TAG_BURN_TIME = "BurnTime";
Expand Down Expand Up @@ -84,6 +86,11 @@ public function readSaveData(CompoundTag $nbt) : void{
}
}

public function copyDataFromItem(Item $item) : void{
$this->copyNameFromItem($item);
$this->copyContentsFromItem($item);
}

protected function writeSaveData(CompoundTag $nbt) : void{
$nbt->setShort(self::TAG_BURN_TIME, $this->remainingFuelTime);
$nbt->setShort(self::TAG_COOK_TIME, $this->cookTime);
Expand Down
10 changes: 9 additions & 1 deletion src/block/tile/Hopper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
namespace pocketmine\block\tile;

use pocketmine\block\inventory\HopperInventory;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\world\World;

class Hopper extends Spawnable implements Container, Nameable{

use NameableTrait {
copyDataFromItem as copyNameFromItem;
}
use ContainerTrait;
use NameableTrait;

private const TAG_TRANSFER_COOLDOWN = "TransferCooldown";

Expand All @@ -57,6 +60,11 @@ protected function writeSaveData(CompoundTag $nbt) : void{
$nbt->setInt(self::TAG_TRANSFER_COOLDOWN, $this->transferCooldown);
}

public function copyDataFromItem(Item $item) : void{
$this->copyNameFromItem($item);
$this->copyContentsFromItem($item);
}

public function close() : void{
if(!$this->closed){
$this->inventory->removeAllViewers();
Expand Down
8 changes: 5 additions & 3 deletions src/block/tile/ShulkerBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class ShulkerBox extends Spawnable implements Container, Nameable{
use NameableTrait {
addAdditionalSpawnData as addNameSpawnData;
}
use ContainerTrait;
use ContainerTrait {
getCleanedNBT as getContainerNBT;
}

public const TAG_FACING = "facing";

Expand All @@ -60,7 +62,7 @@ protected function writeSaveData(CompoundTag $nbt) : void{
}

public function copyDataFromItem(Item $item) : void{
$this->readSaveData($item->getNamedTag());
$this->copyContentsFromItem($item);
if($item->hasCustomName()){
$this->setName($item->getCustomName());
}
Expand All @@ -78,7 +80,7 @@ protected function onBlockDestroyedHook() : void{
}

public function getCleanedNBT() : ?CompoundTag{
$nbt = parent::getCleanedNBT();
$nbt = $this->getContainerNBT();
if($nbt !== null){
$nbt->removeTag(self::TAG_FACING);
}
Expand Down
Loading