From c01d2dc7181d8e2c98115c19d85759ab990860da Mon Sep 17 00:00:00 2001 From: BrandPVP <114182697+BrandPVP@users.noreply.github.com> Date: Thu, 6 Jul 2023 13:08:13 +0300 Subject: [PATCH] CreativeInventory per Player (#5694) --- .../transaction/action/CreateItemAction.php | 3 +-- src/network/mcpe/InventoryManager.php | 3 +-- .../mcpe/handler/ItemStackRequestExecutor.php | 3 +-- src/player/Player.php | 22 +++++++++++++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/inventory/transaction/action/CreateItemAction.php b/src/inventory/transaction/action/CreateItemAction.php index 99605bf96..3ebda6900 100644 --- a/src/inventory/transaction/action/CreateItemAction.php +++ b/src/inventory/transaction/action/CreateItemAction.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace pocketmine\inventory\transaction\action; -use pocketmine\inventory\CreativeInventory; use pocketmine\inventory\transaction\TransactionValidationException; use pocketmine\item\Item; use pocketmine\item\VanillaItems; @@ -43,7 +42,7 @@ class CreateItemAction extends InventoryAction{ if($source->hasFiniteResources()){ throw new TransactionValidationException("Player has finite resources, cannot create items"); } - if(!CreativeInventory::getInstance()->contains($this->sourceItem)){ + if(!$source->getCreativeInventory()->contains($this->sourceItem)){ throw new TransactionValidationException("Creative inventory does not contain requested item"); } } diff --git a/src/network/mcpe/InventoryManager.php b/src/network/mcpe/InventoryManager.php index bdd3a892d..a94dbe946 100644 --- a/src/network/mcpe/InventoryManager.php +++ b/src/network/mcpe/InventoryManager.php @@ -35,7 +35,6 @@ use pocketmine\block\inventory\LoomInventory; use pocketmine\block\inventory\SmithingTableInventory; use pocketmine\block\inventory\StonecutterInventory; use pocketmine\crafting\FurnaceType; -use pocketmine\inventory\CreativeInventory; use pocketmine\inventory\Inventory; use pocketmine\inventory\transaction\action\SlotChangeAction; use pocketmine\inventory\transaction\InventoryTransaction; @@ -603,7 +602,7 @@ class InventoryManager{ } public function syncCreative() : void{ - $this->session->sendDataPacket(CreativeInventoryCache::getInstance()->getCache(CreativeInventory::getInstance())); + $this->session->sendDataPacket(CreativeInventoryCache::getInstance()->getCache($this->player->getCreativeInventory())); } private function newItemStackId() : int{ diff --git a/src/network/mcpe/handler/ItemStackRequestExecutor.php b/src/network/mcpe/handler/ItemStackRequestExecutor.php index f9532291c..10787f84b 100644 --- a/src/network/mcpe/handler/ItemStackRequestExecutor.php +++ b/src/network/mcpe/handler/ItemStackRequestExecutor.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace pocketmine\network\mcpe\handler; -use pocketmine\inventory\CreativeInventory; use pocketmine\inventory\Inventory; use pocketmine\inventory\transaction\action\CreateItemAction; use pocketmine\inventory\transaction\action\DestroyItemAction; @@ -327,7 +326,7 @@ class ItemStackRequestExecutor{ $this->builder->addAction(new DestroyItemAction($destroyed)); }elseif($action instanceof CreativeCreateStackRequestAction){ - $item = CreativeInventory::getInstance()->getItem($action->getCreativeItemId()); + $item = $this->player->getCreativeInventory()->getItem($action->getCreativeItemId()); if($item === null){ throw new ItemStackRequestProcessException("No such creative item index: " . $action->getCreativeItemId()); } diff --git a/src/player/Player.php b/src/player/Player.php index 11dabf7db..4a13654d7 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -80,6 +80,7 @@ use pocketmine\event\player\PlayerViewDistanceChangeEvent; use pocketmine\form\Form; use pocketmine\form\FormValidationException; use pocketmine\inventory\CallbackInventoryListener; +use pocketmine\inventory\CreativeInventory; use pocketmine\inventory\Inventory; use pocketmine\inventory\PlayerCraftingInventory; use pocketmine\inventory\PlayerCursorInventory; @@ -217,6 +218,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ protected array $permanentWindows = []; protected PlayerCursorInventory $cursorInventory; protected PlayerCraftingInventory $craftingGrid; + protected CreativeInventory $creativeInventory; protected int $messageCounter = 2; @@ -307,6 +309,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ $this->uuid = $this->playerInfo->getUuid(); $this->xuid = $this->playerInfo instanceof XboxLivePlayerInfo ? $this->playerInfo->getXuid() : ""; + $this->creativeInventory = CreativeInventory::getInstance(); + $rootPermissions = [DefaultPermissions::ROOT_USER => true]; if($this->server->isOp($this->username)){ $rootPermissions[DefaultPermissions::ROOT_OPERATOR] = true; @@ -2532,6 +2536,24 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{ return $this->craftingGrid; } + /** + * Returns the creative inventory shown to the player. + * Unless changed by a plugin, this is usually the same for all players. + */ + public function getCreativeInventory() : CreativeInventory{ + return $this->creativeInventory; + } + + /** + * To set a custom creative inventory, you need to make a clone of a CreativeInventory instance. + */ + public function setCreativeInventory(CreativeInventory $inventory) : void{ + $this->creativeInventory = $inventory; + if($this->spawned && $this->isConnected()){ + $this->getNetworkSession()->getInvManager()?->syncCreative(); + } + } + /** * @internal Called to clean up crafting grid and cursor inventory when it is detected that the player closed their * inventory.