Validate transaction slots (#6304)

This commit is contained in:
ShockedPlot7560
2024-09-09 09:48:38 +02:00
committed by GitHub
parent 8cb2e577a1
commit f6e2a1ecce
6 changed files with 170 additions and 1 deletions

View File

@ -23,8 +23,13 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\block\BlockTypeIds;
use pocketmine\entity\Living;
use pocketmine\inventory\transaction\action\validator\CallbackSlotValidator;
use pocketmine\inventory\transaction\TransactionValidationException;
use pocketmine\item\Armor;
use pocketmine\item\Item;
use pocketmine\item\ItemBlock;
class ArmorInventory extends SimpleInventory{
public const SLOT_HEAD = 0;
@ -36,6 +41,8 @@ class ArmorInventory extends SimpleInventory{
protected Living $holder
){
parent::__construct(4);
$this->validators->add(new CallbackSlotValidator($this->validate(...)));
}
public function getHolder() : Living{
@ -73,4 +80,20 @@ class ArmorInventory extends SimpleInventory{
public function setBoots(Item $boots) : void{
$this->setItem(self::SLOT_FEET, $boots);
}
private function validate(Inventory $inventory, Item $item, int $slot) : ?TransactionValidationException{
if($item instanceof Armor){
if($item->getArmorSlot() !== $slot){
return new TransactionValidationException("Armor item is in wrong slot");
}
}else{
if(!($slot === ArmorInventory::SLOT_HEAD && $item instanceof ItemBlock && (
$item->getBlock()->getTypeId() === BlockTypeIds::CARVED_PUMPKIN ||
$item->getBlock()->getTypeId() === BlockTypeIds::MOB_HEAD
))){
return new TransactionValidationException("Item is not accepted in an armor slot");
}
}
return null;
}
}