PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
SlotChangeAction.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\inventory\transaction\action;
25
32
37 public function __construct(
38 protected Inventory $inventory,
39 private int $inventorySlot,
40 Item $sourceItem,
41 Item $targetItem
42 ){
43 parent::__construct($sourceItem, $targetItem);
44 }
45
49 public function getInventory() : Inventory{
50 return $this->inventory;
51 }
52
56 public function getSlot() : int{
57 return $this->inventorySlot;
58 }
59
65 public function validate(Player $source) : void{
66 if(!$this->inventory->slotExists($this->inventorySlot)){
67 throw new TransactionValidationException("Slot does not exist");
68 }
69 if(!$this->inventory->getItem($this->inventorySlot)->equalsExact($this->sourceItem)){
70 throw new TransactionValidationException("Slot does not contain expected original item");
71 }
72 if($this->targetItem->getCount() > $this->targetItem->getMaxStackSize()){
73 throw new TransactionValidationException("Target item exceeds item type max stack size");
74 }
75 if($this->targetItem->getCount() > $this->inventory->getMaxStackSize()){
76 throw new TransactionValidationException("Target item exceeds inventory max stack size");
77 }
78 if($this->inventory instanceof SlotValidatedInventory && !$this->targetItem->isNull()){
79 foreach($this->inventory->getSlotValidators() as $validator){
80 $ret = $validator->validate($this->inventory, $this->targetItem, $this->inventorySlot);
81 if($ret !== null){
82 throw new TransactionValidationException("Target item is not accepted by the inventory at slot #" . $this->inventorySlot . ": " . $ret->getMessage(), 0, $ret);
83 }
84 }
85 }
86 }
87
91 public function onAddToTransaction(InventoryTransaction $transaction) : void{
92 $transaction->addInventory($this->inventory);
93 }
94
98 public function execute(Player $source) : void{
99 $this->inventory->setItem($this->inventorySlot, $this->targetItem);
100 }
101}