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

Close block windows when player is too far away #6382

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
af42942
Bump symfony/filesystem from 6.4.7 to 6.4.9
dependabot[bot] Jul 1, 2024
77db7a8
Merge pull request #6381 from pmmp/dependabot/composer/symfony/filesy…
dependabot[bot] Jul 1, 2024
279f9ba
If player is more then 12 blocks from container close container window
CoderJoeW Jul 3, 2024
f24b964
fix code style issues
CoderJoeW Jul 3, 2024
34bcd1c
Merge branch 'minor-next' into 6349-container-window-not-closed
CoderJoeW Jul 3, 2024
c58073c
hopefully fixes issue with composer.lock file being changed
CoderJoeW Jul 3, 2024
1a54372
fixes changes to composer.lock
CoderJoeW Jul 3, 2024
3db4363
change variable name to holder for consistency
CoderJoeW Jul 3, 2024
d28eb74
add max distance to inventory interface
CoderJoeW Jul 3, 2024
ca7c424
add properties and methods to override interface max distance
CoderJoeW Jul 3, 2024
cd9cc82
use max distance modifier from inventory class
CoderJoeW Jul 3, 2024
25dee10
Fix incorrect class check
CoderJoeW Jul 3, 2024
841cc21
code style fixes
CoderJoeW Jul 3, 2024
42dc7da
Create new interface for proximity restrictions
CoderJoeW Jul 5, 2024
214bb72
update code to use ProximityRestricted interface
CoderJoeW Jul 5, 2024
cd9c7da
remove old code
CoderJoeW Jul 5, 2024
de1b7c3
updated based on new interface
CoderJoeW Jul 5, 2024
fccdb19
remove unused code
CoderJoeW Jul 5, 2024
5e33cad
correct function name
CoderJoeW Jul 5, 2024
06ca641
fix typo
CoderJoeW Jul 5, 2024
77e8560
correct errors
CoderJoeW Jul 5, 2024
741f4bf
moove ProximityRestricted into block/inventory
ShockedPlot7560 Nov 18, 2024
271541c
make ProximityRestricted inventory immutable
ShockedPlot7560 Nov 18, 2024
38c99a9
make code more clear
ShockedPlot7560 Nov 18, 2024
f4c020d
improve documentation
ShockedPlot7560 Nov 18, 2024
48e3a8d
remove ProximityRestricted from BaseInventory
ShockedPlot7560 Nov 18, 2024
f6864c2
remove unrelated changes
ShockedPlot7560 Nov 18, 2024
3996449
create ProximityRestrictedTrait and implement it in the block invento…
ShockedPlot7560 Nov 18, 2024
91da21f
change conditions
ShockedPlot7560 Nov 18, 2024
caeeddc
fix PHPstan
ShockedPlot7560 Nov 18, 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
3 changes: 2 additions & 1 deletion src/block/inventory/AnvilInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/BarrelInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/BrewingStandInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/CartographyTableInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/ChestInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/CraftingTableInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/DoubleChestInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/EnchantInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/EnderChestInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/FurnaceInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/HopperInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/LoomInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions src/block/inventory/ProximityRestricted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\block\inventory;

/**
* Represents a block that has a maximum distance the player can be away from it.
*
* This need to be combined with the BlockInventory interface to be useful.
*/
interface ProximityRestricted {

public const MAX_DISTANCE = 6;

/**
* Returns the max distance the player can be away from the holder
*
* @phpstan-return positive-int
*/
public function getMaxDistance() : int;
}
33 changes: 33 additions & 0 deletions src/block/inventory/ProximityRestrictedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\block\inventory;

trait ProximityRestrictedTrait{
/** @phpstan-var positive-int */
protected int $maxDistance = ProximityRestricted::MAX_DISTANCE;

public function getMaxDistance() : int{
return $this->maxDistance;
}
}
3 changes: 2 additions & 1 deletion src/block/inventory/ShulkerBoxInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/SmithingTableInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/block/inventory/StonecutterInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 20 additions & 0 deletions src/player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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;
}

Expand All @@ -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{
Expand Down