diff --git a/src/block/inventory/AnvilInventory.php b/src/block/inventory/AnvilInventory.php index 7d906a6326e..d874032eb26 100644 --- a/src/block/inventory/AnvilInventory.php +++ b/src/block/inventory/AnvilInventory.php @@ -27,8 +27,9 @@ use pocketmine\inventory\TemporaryInventory; use pocketmine\world\Position; -class AnvilInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{ +class AnvilInventory extends SimpleInventory implements BlockInventory, TemporaryInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public const SLOT_INPUT = 0; public const SLOT_MATERIAL = 1; diff --git a/src/block/inventory/BarrelInventory.php b/src/block/inventory/BarrelInventory.php index 0d17d2a3e5b..aa3c184c345 100644 --- a/src/block/inventory/BarrelInventory.php +++ b/src/block/inventory/BarrelInventory.php @@ -30,8 +30,9 @@ use pocketmine\world\sound\BarrelOpenSound; use pocketmine\world\sound\Sound; -class BarrelInventory extends SimpleInventory implements BlockInventory{ +class BarrelInventory extends SimpleInventory implements BlockInventory, ProximityRestricted{ use AnimatedBlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct(Position $holder){ $this->holder = $holder; diff --git a/src/block/inventory/BrewingStandInventory.php b/src/block/inventory/BrewingStandInventory.php index 8bab4ba97b0..b71ae300060 100644 --- a/src/block/inventory/BrewingStandInventory.php +++ b/src/block/inventory/BrewingStandInventory.php @@ -26,8 +26,9 @@ use pocketmine\inventory\SimpleInventory; use pocketmine\world\Position; -class BrewingStandInventory extends SimpleInventory implements BlockInventory{ +class BrewingStandInventory extends SimpleInventory implements BlockInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public const SLOT_INGREDIENT = 0; public const SLOT_BOTTLE_LEFT = 1; diff --git a/src/block/inventory/CartographyTableInventory.php b/src/block/inventory/CartographyTableInventory.php index 7bd9146ac6f..0e3ce7fc6a9 100644 --- a/src/block/inventory/CartographyTableInventory.php +++ b/src/block/inventory/CartographyTableInventory.php @@ -27,8 +27,9 @@ use pocketmine\inventory\TemporaryInventory; use pocketmine\world\Position; -final class CartographyTableInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{ +final class CartographyTableInventory extends SimpleInventory implements BlockInventory, TemporaryInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct(Position $holder){ $this->holder = $holder; diff --git a/src/block/inventory/ChestInventory.php b/src/block/inventory/ChestInventory.php index b61fab57c35..7bf0961245c 100644 --- a/src/block/inventory/ChestInventory.php +++ b/src/block/inventory/ChestInventory.php @@ -31,8 +31,9 @@ use pocketmine\world\sound\ChestOpenSound; use pocketmine\world\sound\Sound; -class ChestInventory extends SimpleInventory implements BlockInventory{ +class ChestInventory extends SimpleInventory implements BlockInventory, ProximityRestricted{ use AnimatedBlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct(Position $holder){ $this->holder = $holder; diff --git a/src/block/inventory/CraftingTableInventory.php b/src/block/inventory/CraftingTableInventory.php index 767e8a5f49c..124cf1c635b 100644 --- a/src/block/inventory/CraftingTableInventory.php +++ b/src/block/inventory/CraftingTableInventory.php @@ -27,8 +27,9 @@ use pocketmine\inventory\TemporaryInventory; use pocketmine\world\Position; -final class CraftingTableInventory extends CraftingGrid implements BlockInventory, TemporaryInventory{ +final class CraftingTableInventory extends CraftingGrid implements BlockInventory, TemporaryInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct(Position $holder){ $this->holder = $holder; diff --git a/src/block/inventory/DoubleChestInventory.php b/src/block/inventory/DoubleChestInventory.php index a7eb4a4398c..a666ea6f16c 100644 --- a/src/block/inventory/DoubleChestInventory.php +++ b/src/block/inventory/DoubleChestInventory.php @@ -30,8 +30,9 @@ use pocketmine\world\sound\ChestOpenSound; use pocketmine\world\sound\Sound; -class DoubleChestInventory extends BaseInventory implements BlockInventory, InventoryHolder{ +class DoubleChestInventory extends BaseInventory implements BlockInventory, InventoryHolder, ProximityRestricted{ use AnimatedBlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct( private ChestInventory $left, diff --git a/src/block/inventory/EnchantInventory.php b/src/block/inventory/EnchantInventory.php index b726dbedf32..0c34650b077 100644 --- a/src/block/inventory/EnchantInventory.php +++ b/src/block/inventory/EnchantInventory.php @@ -33,8 +33,9 @@ use function array_values; use function count; -class EnchantInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{ +class EnchantInventory extends SimpleInventory implements BlockInventory, TemporaryInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public const SLOT_INPUT = 0; public const SLOT_LAPIS = 1; diff --git a/src/block/inventory/EnderChestInventory.php b/src/block/inventory/EnderChestInventory.php index c1d7c5401d3..693cc8ba836 100644 --- a/src/block/inventory/EnderChestInventory.php +++ b/src/block/inventory/EnderChestInventory.php @@ -38,10 +38,11 @@ /** * EnderChestInventory is not a real inventory; it's just a gateway to the player's ender inventory. */ -class EnderChestInventory extends DelegateInventory implements BlockInventory{ +class EnderChestInventory extends DelegateInventory implements BlockInventory, ProximityRestricted{ use AnimatedBlockInventoryTrait { onClose as animatedBlockInventoryTrait_onClose; } + use ProximityRestrictedTrait; public function __construct( Position $holder, diff --git a/src/block/inventory/FurnaceInventory.php b/src/block/inventory/FurnaceInventory.php index ff44d6b7063..13d38bd05fc 100644 --- a/src/block/inventory/FurnaceInventory.php +++ b/src/block/inventory/FurnaceInventory.php @@ -28,8 +28,9 @@ use pocketmine\item\Item; use pocketmine\world\Position; -class FurnaceInventory extends SimpleInventory implements BlockInventory{ +class FurnaceInventory extends SimpleInventory implements BlockInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public const SLOT_INPUT = 0; public const SLOT_FUEL = 1; diff --git a/src/block/inventory/HopperInventory.php b/src/block/inventory/HopperInventory.php index a20e9ae1a18..c7e2250d63f 100644 --- a/src/block/inventory/HopperInventory.php +++ b/src/block/inventory/HopperInventory.php @@ -26,8 +26,9 @@ use pocketmine\inventory\SimpleInventory; use pocketmine\world\Position; -class HopperInventory extends SimpleInventory implements BlockInventory{ +class HopperInventory extends SimpleInventory implements BlockInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct(Position $holder, int $size = 5){ $this->holder = $holder; diff --git a/src/block/inventory/LoomInventory.php b/src/block/inventory/LoomInventory.php index fd34620a05f..a234307faa3 100644 --- a/src/block/inventory/LoomInventory.php +++ b/src/block/inventory/LoomInventory.php @@ -27,8 +27,9 @@ use pocketmine\inventory\TemporaryInventory; use pocketmine\world\Position; -final class LoomInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{ +final class LoomInventory extends SimpleInventory implements BlockInventory, TemporaryInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public const SLOT_BANNER = 0; public const SLOT_DYE = 1; diff --git a/src/block/inventory/ProximityRestricted.php b/src/block/inventory/ProximityRestricted.php new file mode 100644 index 00000000000..2a4ad149a2a --- /dev/null +++ b/src/block/inventory/ProximityRestricted.php @@ -0,0 +1,41 @@ +maxDistance; + } +} diff --git a/src/block/inventory/ShulkerBoxInventory.php b/src/block/inventory/ShulkerBoxInventory.php index d915a995182..677ff1fc508 100644 --- a/src/block/inventory/ShulkerBoxInventory.php +++ b/src/block/inventory/ShulkerBoxInventory.php @@ -34,8 +34,9 @@ use pocketmine\world\sound\ShulkerBoxOpenSound; use pocketmine\world\sound\Sound; -class ShulkerBoxInventory extends SimpleInventory implements BlockInventory{ +class ShulkerBoxInventory extends SimpleInventory implements BlockInventory, ProximityRestricted{ use AnimatedBlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct(Position $holder){ $this->holder = $holder; diff --git a/src/block/inventory/SmithingTableInventory.php b/src/block/inventory/SmithingTableInventory.php index 2f67ac9d2dd..110481b4a68 100644 --- a/src/block/inventory/SmithingTableInventory.php +++ b/src/block/inventory/SmithingTableInventory.php @@ -27,8 +27,9 @@ use pocketmine\inventory\TemporaryInventory; use pocketmine\world\Position; -final class SmithingTableInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{ +final class SmithingTableInventory extends SimpleInventory implements BlockInventory, TemporaryInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public function __construct(Position $holder){ $this->holder = $holder; diff --git a/src/block/inventory/StonecutterInventory.php b/src/block/inventory/StonecutterInventory.php index 4ed644ff2c3..2b20a7538ac 100644 --- a/src/block/inventory/StonecutterInventory.php +++ b/src/block/inventory/StonecutterInventory.php @@ -27,8 +27,9 @@ use pocketmine\inventory\TemporaryInventory; use pocketmine\world\Position; -class StonecutterInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{ +class StonecutterInventory extends SimpleInventory implements BlockInventory, TemporaryInventory, ProximityRestricted{ use BlockInventoryTrait; + use ProximityRestrictedTrait; public const SLOT_INPUT = 0; diff --git a/src/player/Player.php b/src/player/Player.php index 0cefbe71f22..0465744dda3 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -26,6 +26,8 @@ use pocketmine\block\BaseSign; use pocketmine\block\Bed; use pocketmine\block\BlockTypeTags; +use pocketmine\block\inventory\BlockInventory; +use pocketmine\block\inventory\ProximityRestricted; use pocketmine\block\UnknownBlock; use pocketmine\block\VanillaBlocks; use pocketmine\command\CommandSender; @@ -296,6 +298,8 @@ public static function isValidUserName(?string $name) : bool{ protected ?SurvivalBlockBreakHandler $blockBreakHandler = null; + protected ?Vector3 $containerOpenPosition = null; + public function __construct(Server $server, NetworkSession $session, PlayerInfo $playerInfo, bool $authenticated, Location $spawnLocation, ?CompoundTag $namedtag){ $username = TextFormat::clean($playerInfo->getUsername()); $this->logger = new \PrefixedLogger($server->getLogger(), "Player: $username"); @@ -1374,6 +1378,12 @@ protected function processMostRecentMovements() : void{ $this->logger->debug("Exceeded movement rate limit, forcing to last accepted position"); $this->sendPosition($this->location, $this->location->getYaw(), $this->location->getPitch(), MovePlayerPacket::MODE_RESET); } + if($this->containerOpenPosition !== null && + $this->currentWindow instanceof ProximityRestricted && + $this->location->distance($this->containerOpenPosition) > $this->currentWindow->getMaxDistance() + ){ + $this->removeCurrentWindow(); + } } protected function revertMovement(Location $from) : void{ @@ -2646,6 +2656,14 @@ public function setCurrentWindow(Inventory $inventory) : bool{ $inventoryManager->onCurrentWindowChange($inventory); $inventory->onOpen($this); $this->currentWindow = $inventory; + + if($inventory instanceof ProximityRestricted && + $inventory instanceof BlockInventory && + $inventory->getMaxDistance() > 0 + ){ + $this->containerOpenPosition = $inventory->getHolder()->asPosition(); + } + return true; } @@ -2661,6 +2679,8 @@ public function removeCurrentWindow() : void{ $this->currentWindow = null; (new InventoryCloseEvent($currentWindow, $this))->call(); } + + $this->containerOpenPosition = null; } protected function addPermanentInventories(Inventory ...$inventories) : void{