Skip to content

Commit

Permalink
Include more item containers to be dropped upon destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierLeon9966 committed Aug 24, 2024
1 parent 6d47810 commit 9628fbf
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 11 deletions.
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 @@ -114,4 +114,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);
}
}
25 changes: 25 additions & 0 deletions src/block/tile/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\tile\util\ContainerHelper;
use pocketmine\inventory\Inventory;
use pocketmine\item\Item;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\world\Position;
Expand Down Expand Up @@ -63,6 +64,30 @@ protected function saveItems(CompoundTag $tag) : void{
}
}

/**
* @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
20 changes: 20 additions & 0 deletions src/block/utils/ContainerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace pocketmine\block\utils;

use pocketmine\block\tile\Container;
use pocketmine\item\Item;

trait ContainerTrait{

public function getPickedItem(bool $addUserData = false) : Item{
$item = parent::getPickedItem($addUserData);
if($addUserData){
$tile = $this->position->getWorld()->getTile($this->position);
if($tile instanceof Container){
$item->setContainedItems($tile->getRealInventory()->getContents());
}
}
return $item;
}
}

0 comments on commit 9628fbf

Please sign in to comment.