From 3ee78e20a5dce97859686ab205e293e3db5a00d1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 7 Dec 2024 19:28:10 +0000 Subject: [PATCH] BlockInventoryTrait: include a Block ref instead of Position --- src/block/Anvil.php | 2 +- src/block/Barrel.php | 2 +- src/block/BrewingStand.php | 2 +- src/block/CartographyTable.php | 2 +- src/block/Chest.php | 4 ++-- src/block/CraftingTable.php | 2 +- src/block/EnchantingTable.php | 2 +- src/block/EnderChest.php | 2 +- src/block/Furnace.php | 2 +- src/block/Hopper.php | 2 +- src/block/Loom.php | 2 +- src/block/ShulkerBox.php | 2 +- src/block/SmithingTable.php | 2 +- src/block/Stonecutter.php | 2 +- .../inventory/window/AnvilInventoryWindow.php | 4 ++-- .../inventory/window/BlockInventoryWindow.php | 16 +++++++--------- .../window/CartographyTableInventoryWindow.php | 4 ++-- .../window/CraftingTableInventoryWindow.php | 4 ++-- .../window/DoubleChestInventoryWindow.php | 10 +++++----- .../window/EnchantingTableInventoryWindow.php | 6 +++--- .../inventory/window/FurnaceInventoryWindow.php | 4 ++-- .../inventory/window/LoomInventoryWindow.php | 4 ++-- .../window/SmithingTableInventoryWindow.php | 4 ++-- .../window/StonecutterInventoryWindow.php | 4 ++-- src/network/mcpe/InventoryManager.php | 2 +- 25 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/block/Anvil.php b/src/block/Anvil.php index b0c7d3c8f..e2455da9b 100644 --- a/src/block/Anvil.php +++ b/src/block/Anvil.php @@ -83,7 +83,7 @@ class Anvil extends Transparent implements Fallable{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($player instanceof Player){ - $player->setCurrentWindow(new AnvilInventoryWindow($player, $this->position)); + $player->setCurrentWindow(new AnvilInventoryWindow($player, $this)); } return true; diff --git a/src/block/Barrel.php b/src/block/Barrel.php index 1b64e814c..999f82db4 100644 --- a/src/block/Barrel.php +++ b/src/block/Barrel.php @@ -89,7 +89,7 @@ class Barrel extends Opaque implements AnimatedContainer{ return true; } - $player->setCurrentWindow(new BlockInventoryWindow($player, $barrel->getInventory(), $this->position)); + $player->setCurrentWindow(new BlockInventoryWindow($player, $barrel->getInventory(), $this)); } } diff --git a/src/block/BrewingStand.php b/src/block/BrewingStand.php index dc961aa55..c43747e40 100644 --- a/src/block/BrewingStand.php +++ b/src/block/BrewingStand.php @@ -101,7 +101,7 @@ class BrewingStand extends Transparent{ if($player instanceof Player){ $stand = $this->position->getWorld()->getTile($this->position); if($stand instanceof TileBrewingStand && $stand->canOpenWith($item->getCustomName())){ - $player->setCurrentWindow(new BrewingStandInventoryWindow($player, $stand->getInventory(), $this->position)); + $player->setCurrentWindow(new BrewingStandInventoryWindow($player, $stand->getInventory(), $this)); } } diff --git a/src/block/CartographyTable.php b/src/block/CartographyTable.php index 1c3e94096..436850e83 100644 --- a/src/block/CartographyTable.php +++ b/src/block/CartographyTable.php @@ -32,7 +32,7 @@ final class CartographyTable extends Opaque{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($player !== null){ - $player->setCurrentWindow(new CartographyTableInventoryWindow($player, $this->position)); + $player->setCurrentWindow(new CartographyTableInventoryWindow($player, $this)); } return true; diff --git a/src/block/Chest.php b/src/block/Chest.php index 13712cab9..d872693bd 100644 --- a/src/block/Chest.php +++ b/src/block/Chest.php @@ -117,7 +117,7 @@ class Chest extends Transparent implements AnimatedContainer{ } if($pair !== null){ - [$left, $right] = $pairOnLeft ? [$pair->getPosition(), $this->position] : [$this->position, $pair->getPosition()]; + [$left, $right] = $pairOnLeft ? [$pair->getBlock(), $this] : [$this, $pair->getBlock()]; //TODO: we should probably construct DoubleChestInventory here directly too using the same logic //right now it uses some weird logic in TileChest which produces incorrect results @@ -125,7 +125,7 @@ class Chest extends Transparent implements AnimatedContainer{ $window = new DoubleChestInventoryWindow($player, $chest->getInventory(), $left, $right); } - $player->setCurrentWindow($window ?? new BlockInventoryWindow($player, $chest->getInventory(), $this->position)); + $player->setCurrentWindow($window ?? new BlockInventoryWindow($player, $chest->getInventory(), $this)); } } diff --git a/src/block/CraftingTable.php b/src/block/CraftingTable.php index 2b73d221a..06824824b 100644 --- a/src/block/CraftingTable.php +++ b/src/block/CraftingTable.php @@ -32,7 +32,7 @@ class CraftingTable extends Opaque{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($player instanceof Player){ - $player->setCurrentWindow(new CraftingTableInventoryWindow($player, $this->position)); + $player->setCurrentWindow(new CraftingTableInventoryWindow($player, $this)); } return true; diff --git a/src/block/EnchantingTable.php b/src/block/EnchantingTable.php index 46ecc0702..db052ecf5 100644 --- a/src/block/EnchantingTable.php +++ b/src/block/EnchantingTable.php @@ -48,7 +48,7 @@ class EnchantingTable extends Transparent{ if($player instanceof Player){ //TODO lock - $player->setCurrentWindow(new EnchantingTableInventoryWindow($player, $this->position)); + $player->setCurrentWindow(new EnchantingTableInventoryWindow($player, $this)); } return true; diff --git a/src/block/EnderChest.php b/src/block/EnderChest.php index c5901d3f4..f1fee127e 100644 --- a/src/block/EnderChest.php +++ b/src/block/EnderChest.php @@ -68,7 +68,7 @@ class EnderChest extends Transparent implements AnimatedContainer{ if($player instanceof Player){ $enderChest = $this->position->getWorld()->getTile($this->position); if($enderChest instanceof TileEnderChest && $this->getSide(Facing::UP)->isTransparent()){ - $player->setCurrentWindow(new BlockInventoryWindow($player, $player->getEnderInventory(), $this->position)); + $player->setCurrentWindow(new BlockInventoryWindow($player, $player->getEnderInventory(), $this)); } } diff --git a/src/block/Furnace.php b/src/block/Furnace.php index 2c4433413..412ac74a8 100644 --- a/src/block/Furnace.php +++ b/src/block/Furnace.php @@ -62,7 +62,7 @@ class Furnace extends Opaque{ if($player instanceof Player){ $furnace = $this->position->getWorld()->getTile($this->position); if($furnace instanceof TileFurnace && $furnace->canOpenWith($item->getCustomName())){ - $player->setCurrentWindow(new FurnaceInventoryWindow($player, $furnace->getInventory(), $this->position, $this->furnaceType)); + $player->setCurrentWindow(new FurnaceInventoryWindow($player, $furnace->getInventory(), $this, $this->furnaceType)); } } diff --git a/src/block/Hopper.php b/src/block/Hopper.php index 8c65e836c..67273b04d 100644 --- a/src/block/Hopper.php +++ b/src/block/Hopper.php @@ -85,7 +85,7 @@ class Hopper extends Transparent{ if($player !== null){ $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileHopper){ //TODO: find a way to have inventories open on click without this boilerplate in every block - $player->setCurrentWindow(new HopperInventoryWindow($player, $tile->getInventory(), $this->position)); + $player->setCurrentWindow(new HopperInventoryWindow($player, $tile->getInventory(), $this)); } return true; } diff --git a/src/block/Loom.php b/src/block/Loom.php index d19fc9449..a4d6f7da4 100644 --- a/src/block/Loom.php +++ b/src/block/Loom.php @@ -34,7 +34,7 @@ final class Loom extends Opaque{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($player !== null){ - $player->setCurrentWindow(new LoomInventoryWindow($player, $this->position)); + $player->setCurrentWindow(new LoomInventoryWindow($player, $this)); return true; } return false; diff --git a/src/block/ShulkerBox.php b/src/block/ShulkerBox.php index 170033963..c575ecae1 100644 --- a/src/block/ShulkerBox.php +++ b/src/block/ShulkerBox.php @@ -115,7 +115,7 @@ class ShulkerBox extends Opaque implements AnimatedContainer{ return true; } - $player->setCurrentWindow(new BlockInventoryWindow($player, $shulker->getInventory(), $this->position)); + $player->setCurrentWindow(new BlockInventoryWindow($player, $shulker->getInventory(), $this)); } } diff --git a/src/block/SmithingTable.php b/src/block/SmithingTable.php index b96a582d1..77816c42d 100644 --- a/src/block/SmithingTable.php +++ b/src/block/SmithingTable.php @@ -32,7 +32,7 @@ final class SmithingTable extends Opaque{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($player !== null){ - $player->setCurrentWindow(new SmithingTableInventoryWindow($player, $this->position)); + $player->setCurrentWindow(new SmithingTableInventoryWindow($player, $this)); } return true; diff --git a/src/block/Stonecutter.php b/src/block/Stonecutter.php index 3c22e74a8..20fc73da5 100644 --- a/src/block/Stonecutter.php +++ b/src/block/Stonecutter.php @@ -37,7 +37,7 @@ class Stonecutter extends Transparent{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($player !== null){ - $player->setCurrentWindow(new StonecutterInventoryWindow($player, $this->position)); + $player->setCurrentWindow(new StonecutterInventoryWindow($player, $this)); } return true; } diff --git a/src/block/inventory/window/AnvilInventoryWindow.php b/src/block/inventory/window/AnvilInventoryWindow.php index 2b994a90d..813720151 100644 --- a/src/block/inventory/window/AnvilInventoryWindow.php +++ b/src/block/inventory/window/AnvilInventoryWindow.php @@ -23,10 +23,10 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\inventory\SimpleInventory; use pocketmine\player\Player; use pocketmine\player\TemporaryInventoryWindow; -use pocketmine\world\Position; final class AnvilInventoryWindow extends BlockInventoryWindow implements TemporaryInventoryWindow{ public const SLOT_INPUT = 0; @@ -34,7 +34,7 @@ final class AnvilInventoryWindow extends BlockInventoryWindow implements Tempora public function __construct( Player $viewer, - Position $holder + Block $holder ){ parent::__construct($viewer, new SimpleInventory(2), $holder); } diff --git a/src/block/inventory/window/BlockInventoryWindow.php b/src/block/inventory/window/BlockInventoryWindow.php index 149c2e814..edb45012b 100644 --- a/src/block/inventory/window/BlockInventoryWindow.php +++ b/src/block/inventory/window/BlockInventoryWindow.php @@ -23,36 +23,34 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\block\utils\AnimatedContainer; use pocketmine\inventory\Inventory; use pocketmine\player\InventoryWindow; use pocketmine\player\Player; -use pocketmine\world\Position; class BlockInventoryWindow extends InventoryWindow{ public function __construct( Player $viewer, Inventory $inventory, - protected Position $holder + protected Block $holder ){ parent::__construct($viewer, $inventory); } - public function getHolder() : Position{ return $this->holder; } + public function getHolder() : Block{ return $this->holder; } public function onOpen() : void{ parent::onOpen(); - $block = $this->holder->getWorld()->getBlock($this->holder); - if($block instanceof AnimatedContainer){ - $block->onContainerOpen(); + if($this->holder instanceof AnimatedContainer){ + $this->holder->onContainerOpen(); } } public function onClose() : void{ - $block = $this->holder->getWorld()->getBlock($this->holder); - if($block instanceof AnimatedContainer){ - $block->onContainerClose(); + if($this->holder instanceof AnimatedContainer){ + $this->holder->onContainerClose(); } parent::onClose(); } diff --git a/src/block/inventory/window/CartographyTableInventoryWindow.php b/src/block/inventory/window/CartographyTableInventoryWindow.php index 22d2d5c43..c9998231c 100644 --- a/src/block/inventory/window/CartographyTableInventoryWindow.php +++ b/src/block/inventory/window/CartographyTableInventoryWindow.php @@ -23,16 +23,16 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\inventory\SimpleInventory; use pocketmine\player\Player; use pocketmine\player\TemporaryInventoryWindow; -use pocketmine\world\Position; final class CartographyTableInventoryWindow extends BlockInventoryWindow implements TemporaryInventoryWindow{ public function __construct( Player $viewer, - Position $holder + Block $holder ){ parent::__construct($viewer, new SimpleInventory(2), $holder); } diff --git a/src/block/inventory/window/CraftingTableInventoryWindow.php b/src/block/inventory/window/CraftingTableInventoryWindow.php index 46e5485e5..914a30796 100644 --- a/src/block/inventory/window/CraftingTableInventoryWindow.php +++ b/src/block/inventory/window/CraftingTableInventoryWindow.php @@ -23,15 +23,15 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\crafting\CraftingGrid; use pocketmine\player\Player; -use pocketmine\world\Position; final class CraftingTableInventoryWindow extends BlockInventoryWindow{ public function __construct( Player $viewer, - Position $holder + Block $holder ){ //TODO: generics would be good for this, since it has special methods parent::__construct($viewer, new CraftingGrid(CraftingGrid::SIZE_BIG), $holder); diff --git a/src/block/inventory/window/DoubleChestInventoryWindow.php b/src/block/inventory/window/DoubleChestInventoryWindow.php index 801a6ff9d..3fcf96ac5 100644 --- a/src/block/inventory/window/DoubleChestInventoryWindow.php +++ b/src/block/inventory/window/DoubleChestInventoryWindow.php @@ -23,22 +23,22 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\inventory\Inventory; use pocketmine\player\Player; -use pocketmine\world\Position; final class DoubleChestInventoryWindow extends BlockInventoryWindow{ public function __construct( Player $viewer, Inventory $inventory, - private Position $left, - private Position $right + private Block $left, + private Block $right ){ parent::__construct($viewer, $inventory, $this->left); } - public function getLeft() : Position{ return $this->left; } + public function getLeft() : Block{ return $this->left; } - public function getRight() : Position{ return $this->right; } + public function getRight() : Block{ return $this->right; } } diff --git a/src/block/inventory/window/EnchantingTableInventoryWindow.php b/src/block/inventory/window/EnchantingTableInventoryWindow.php index 294c18189..d6ea754a9 100644 --- a/src/block/inventory/window/EnchantingTableInventoryWindow.php +++ b/src/block/inventory/window/EnchantingTableInventoryWindow.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\event\player\PlayerEnchantingOptionsRequestEvent; use pocketmine\inventory\CallbackInventoryListener; use pocketmine\inventory\Inventory; @@ -32,7 +33,6 @@ use pocketmine\item\enchantment\EnchantingHelper as Helper; use pocketmine\item\enchantment\EnchantingOption; use pocketmine\item\Item; use pocketmine\player\Player; -use pocketmine\world\Position; use function array_values; use function count; @@ -47,7 +47,7 @@ final class EnchantingTableInventoryWindow extends BlockInventoryWindow{ public function __construct( Player $viewer, - Position $holder + Block $holder ){ parent::__construct($viewer, new SimpleInventory(2), $holder); @@ -75,7 +75,7 @@ final class EnchantingTableInventoryWindow extends BlockInventoryWindow{ private function regenerateOptions() : void{ $this->options = []; $item = $this->getInput(); - $options = Helper::generateOptions($this->holder, $item, $this->viewer->getEnchantmentSeed()); + $options = Helper::generateOptions($this->holder->getPosition(), $item, $this->viewer->getEnchantmentSeed()); $event = new PlayerEnchantingOptionsRequestEvent($this->viewer, $this, $options); $event->call(); diff --git a/src/block/inventory/window/FurnaceInventoryWindow.php b/src/block/inventory/window/FurnaceInventoryWindow.php index 11d36324c..9667bf631 100644 --- a/src/block/inventory/window/FurnaceInventoryWindow.php +++ b/src/block/inventory/window/FurnaceInventoryWindow.php @@ -23,11 +23,11 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\crafting\FurnaceType; use pocketmine\inventory\Inventory; use pocketmine\item\Item; use pocketmine\player\Player; -use pocketmine\world\Position; final class FurnaceInventoryWindow extends BlockInventoryWindow{ public const SLOT_INPUT = 0; @@ -37,7 +37,7 @@ final class FurnaceInventoryWindow extends BlockInventoryWindow{ public function __construct( Player $viewer, Inventory $inventory, - Position $holder, + Block $holder, private FurnaceType $furnaceType ){ parent::__construct($viewer, $inventory, $holder); diff --git a/src/block/inventory/window/LoomInventoryWindow.php b/src/block/inventory/window/LoomInventoryWindow.php index ceb27ac1a..d59050ba4 100644 --- a/src/block/inventory/window/LoomInventoryWindow.php +++ b/src/block/inventory/window/LoomInventoryWindow.php @@ -23,10 +23,10 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\inventory\SimpleInventory; use pocketmine\player\Player; use pocketmine\player\TemporaryInventoryWindow; -use pocketmine\world\Position; final class LoomInventoryWindow extends BlockInventoryWindow implements TemporaryInventoryWindow{ @@ -36,7 +36,7 @@ final class LoomInventoryWindow extends BlockInventoryWindow implements Temporar public function __construct( Player $viewer, - Position $holder + Block $holder ){ parent::__construct($viewer, new SimpleInventory(3), $holder); } diff --git a/src/block/inventory/window/SmithingTableInventoryWindow.php b/src/block/inventory/window/SmithingTableInventoryWindow.php index 7cb850ef0..4524ecb2a 100644 --- a/src/block/inventory/window/SmithingTableInventoryWindow.php +++ b/src/block/inventory/window/SmithingTableInventoryWindow.php @@ -23,13 +23,13 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\inventory\SimpleInventory; use pocketmine\player\Player; use pocketmine\player\TemporaryInventoryWindow; -use pocketmine\world\Position; final class SmithingTableInventoryWindow extends BlockInventoryWindow implements TemporaryInventoryWindow{ - public function __construct(Player $viewer, Position $holder){ + public function __construct(Player $viewer, Block $holder){ parent::__construct($viewer, new SimpleInventory(3), $holder); } } diff --git a/src/block/inventory/window/StonecutterInventoryWindow.php b/src/block/inventory/window/StonecutterInventoryWindow.php index a7a29a974..a17925110 100644 --- a/src/block/inventory/window/StonecutterInventoryWindow.php +++ b/src/block/inventory/window/StonecutterInventoryWindow.php @@ -23,15 +23,15 @@ declare(strict_types=1); namespace pocketmine\block\inventory\window; +use pocketmine\block\Block; use pocketmine\inventory\SimpleInventory; use pocketmine\player\Player; use pocketmine\player\TemporaryInventoryWindow; -use pocketmine\world\Position; final class StonecutterInventoryWindow extends BlockInventoryWindow implements TemporaryInventoryWindow{ public const SLOT_INPUT = 0; - public function __construct(Player $viewer, Position $holder){ + public function __construct(Player $viewer, Block $holder){ parent::__construct($viewer, new SimpleInventory(1), $holder); } } diff --git a/src/network/mcpe/InventoryManager.php b/src/network/mcpe/InventoryManager.php index c54a2fc22..58fccbb5f 100644 --- a/src/network/mcpe/InventoryManager.php +++ b/src/network/mcpe/InventoryManager.php @@ -382,7 +382,7 @@ class InventoryManager implements InventoryListener{ //TODO: we should be using some kind of tagging system to identify the types. Instanceof is flaky especially //if the class isn't final, not to mention being inflexible. if($window instanceof BlockInventoryWindow){ - $blockPosition = BlockPosition::fromVector3($window->getHolder()); + $blockPosition = BlockPosition::fromVector3($window->getHolder()->getPosition()); $windowType = match(true){ $window instanceof LoomInventoryWindow => WindowTypes::LOOM, $window instanceof FurnaceInventoryWindow => match($window->getFurnaceType()){