PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
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 InventoryWindow $inventoryWindow,
39 private int $inventorySlot,
40 Item $sourceItem,
41 Item $targetItem
42 ){
43 parent::__construct($sourceItem, $targetItem);
44 }
45
50 return $this->inventoryWindow;
51 }
52
56 public function getSlot() : int{
57 return $this->inventorySlot;
58 }
59
65 public function validate(Player $source) : void{
66 $inventory = $this->inventoryWindow->getInventory();
67 if(!$inventory->slotExists($this->inventorySlot)){
68 throw new TransactionValidationException("Slot does not exist");
69 }
70 if(!$inventory->getItem($this->inventorySlot)->equalsExact($this->sourceItem)){
71 throw new TransactionValidationException("Slot does not contain expected original item");
72 }
73 if($this->targetItem->getCount() > $this->targetItem->getMaxStackSize()){
74 throw new TransactionValidationException("Target item exceeds item type max stack size");
75 }
76 if($this->targetItem->getCount() > $inventory->getMaxStackSize()){
77 throw new TransactionValidationException("Target item exceeds inventory max stack size");
78 }
79 if($inventory instanceof SlotValidatedInventory && !$this->targetItem->isNull()){
80 foreach($inventory->getSlotValidators() as $validator){
81 $ret = $validator->validate($inventory, $this->targetItem, $this->inventorySlot);
82 if($ret !== null){
83 throw new TransactionValidationException("Target item is not accepted by the inventory at slot #" . $this->inventorySlot . ": " . $ret->getMessage(), 0, $ret);
84 }
85 }
86 }
87 }
88
92 public function execute(Player $source) : void{
93 $this->inventoryWindow->getInventory()->setItem($this->inventorySlot, $this->targetItem);
94 }
95}