From 726e2cba23043b0d2640c09638d6e1f696094bf1 Mon Sep 17 00:00:00 2001 From: ShockedPlot7560 Date: Sat, 10 Aug 2024 23:26:07 +0200 Subject: [PATCH] Add anvil event --- src/event/player/PlayerUseAnvilEvent.php | 86 +++++++++++++++++++ .../transaction/AnvilTransaction.php | 20 +++++ 2 files changed, 106 insertions(+) create mode 100644 src/event/player/PlayerUseAnvilEvent.php diff --git a/src/event/player/PlayerUseAnvilEvent.php b/src/event/player/PlayerUseAnvilEvent.php new file mode 100644 index 000000000..4a7c4ca8d --- /dev/null +++ b/src/event/player/PlayerUseAnvilEvent.php @@ -0,0 +1,86 @@ +player = $player; + } + + /** + * Returns the item that the player is using as the base item (left slot). + */ + public function getBaseItem() : Item{ + return $this->baseItem; + } + + /** + * Returns the item that the player is using as the material item (right slot), or null if there is no material item + * (e.g. when renaming an item). + */ + public function getMaterialItem() : ?Item{ + return $this->materialItem; + } + + /** + * Returns the item that the player will receive as a result of the anvil operation. + */ + public function getResultItem() : Item{ + return $this->resultItem; + } + + /** + * Returns the custom name that the player is setting on the item, or null if the player is not renaming the item. + * + * This value is defined when the base item is already renamed. + */ + public function getCustomName() : ?string{ + return $this->customName; + } + + /** + * Returns the amount of XP levels that the player will spend on this anvil operation. + */ + public function getXpCost() : int{ + return $this->xpCost; + } +} diff --git a/src/inventory/transaction/AnvilTransaction.php b/src/inventory/transaction/AnvilTransaction.php index 7301df982..849d87077 100644 --- a/src/inventory/transaction/AnvilTransaction.php +++ b/src/inventory/transaction/AnvilTransaction.php @@ -25,12 +25,18 @@ namespace pocketmine\inventory\transaction; use pocketmine\block\utils\AnvilHelper; use pocketmine\block\utils\AnvilResult; +use pocketmine\event\player\PlayerUseAnvilEvent; use pocketmine\item\Item; use pocketmine\item\VanillaItems; use pocketmine\player\Player; +use pocketmine\utils\AssumptionFailedError; use function count; class AnvilTransaction extends InventoryTransaction{ + private ?Item $baseItem = null; + private ?Item $materialItem = null; + private ?Item $resultItem = null; + public function __construct( Player $source, private readonly AnvilResult $expectedResult, @@ -57,6 +63,10 @@ class AnvilTransaction extends InventoryTransaction{ return null; } + $this->baseItem = $base; + $this->materialItem = $material; + $this->resultItem = $expectedOutput; + return $calculAttempt; } @@ -104,4 +114,14 @@ class AnvilTransaction extends InventoryTransaction{ $this->source->getXpManager()->subtractXpLevels($this->expectedResult->getRepairCost()); } } + + protected function callExecuteEvent() : bool{ + if($this->baseItem === null){ + throw new AssumptionFailedError("Expected that baseItem are not null before executing the event"); + } + + $ev = new PlayerUseAnvilEvent($this->source, $this->baseItem, $this->materialItem, $this->expectedResult->getResult(), $this->customName, $this->expectedResult->getRepairCost()); + $ev->call(); + return !$ev->isCancelled(); + } }