mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-09 15:29:47 +00:00
Removed EntityInventoryChangeEvent and EntityArmorChangeEvent
there is nothing that these events do that can't be fulfilled by transactions. They complicate the internal implementation and produce unexpected behaviour for plugins when cancelled. TL;DR: Use transactions. That's what they are there for.
This commit is contained in:
parent
67affcea32
commit
dec6c9f49b
@ -28,12 +28,10 @@ use pocketmine\entity\effect\EffectInstance;
|
|||||||
use pocketmine\entity\projectile\ProjectileSource;
|
use pocketmine\entity\projectile\ProjectileSource;
|
||||||
use pocketmine\entity\utils\ExperienceUtils;
|
use pocketmine\entity\utils\ExperienceUtils;
|
||||||
use pocketmine\event\entity\EntityDamageEvent;
|
use pocketmine\event\entity\EntityDamageEvent;
|
||||||
use pocketmine\event\entity\EntityInventoryChangeEvent;
|
|
||||||
use pocketmine\event\entity\EntityRegainHealthEvent;
|
use pocketmine\event\entity\EntityRegainHealthEvent;
|
||||||
use pocketmine\event\player\PlayerExhaustEvent;
|
use pocketmine\event\player\PlayerExhaustEvent;
|
||||||
use pocketmine\event\player\PlayerExperienceChangeEvent;
|
use pocketmine\event\player\PlayerExperienceChangeEvent;
|
||||||
use pocketmine\inventory\EnderChestInventory;
|
use pocketmine\inventory\EnderChestInventory;
|
||||||
use pocketmine\inventory\Inventory;
|
|
||||||
use pocketmine\inventory\InventoryHolder;
|
use pocketmine\inventory\InventoryHolder;
|
||||||
use pocketmine\inventory\PlayerInventory;
|
use pocketmine\inventory\PlayerInventory;
|
||||||
use pocketmine\item\Consumable;
|
use pocketmine\item\Consumable;
|
||||||
@ -648,16 +646,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
|
|||||||
|
|
||||||
$this->inventory->setHeldItemIndex($nbt->getInt("SelectedInventorySlot", 0), false);
|
$this->inventory->setHeldItemIndex($nbt->getInt("SelectedInventorySlot", 0), false);
|
||||||
|
|
||||||
$this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
|
|
||||||
$ev = new EntityInventoryChangeEvent($this, $oldItem, $newItem, $slot);
|
|
||||||
$ev->call();
|
|
||||||
if($ev->isCancelled()){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ev->getNewItem();
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->setFood((float) $nbt->getInt("foodLevel", (int) $this->getFood(), true));
|
$this->setFood((float) $nbt->getInt("foodLevel", (int) $this->getFood(), true));
|
||||||
$this->setExhaustion($nbt->getFloat("foodExhaustionLevel", $this->getExhaustion(), true));
|
$this->setExhaustion($nbt->getFloat("foodExhaustionLevel", $this->getExhaustion(), true));
|
||||||
$this->setSaturation($nbt->getFloat("foodSaturationLevel", $this->getSaturation(), true));
|
$this->setSaturation($nbt->getFloat("foodSaturationLevel", $this->getSaturation(), true));
|
||||||
|
@ -26,7 +26,6 @@ namespace pocketmine\entity;
|
|||||||
use pocketmine\block\Block;
|
use pocketmine\block\Block;
|
||||||
use pocketmine\entity\effect\Effect;
|
use pocketmine\entity\effect\Effect;
|
||||||
use pocketmine\entity\effect\EffectInstance;
|
use pocketmine\entity\effect\EffectInstance;
|
||||||
use pocketmine\event\entity\EntityArmorChangeEvent;
|
|
||||||
use pocketmine\event\entity\EntityDamageByChildEntityEvent;
|
use pocketmine\event\entity\EntityDamageByChildEntityEvent;
|
||||||
use pocketmine\event\entity\EntityDamageByEntityEvent;
|
use pocketmine\event\entity\EntityDamageByEntityEvent;
|
||||||
use pocketmine\event\entity\EntityDamageEvent;
|
use pocketmine\event\entity\EntityDamageEvent;
|
||||||
@ -34,7 +33,6 @@ use pocketmine\event\entity\EntityDeathEvent;
|
|||||||
use pocketmine\event\entity\EntityEffectAddEvent;
|
use pocketmine\event\entity\EntityEffectAddEvent;
|
||||||
use pocketmine\event\entity\EntityEffectRemoveEvent;
|
use pocketmine\event\entity\EntityEffectRemoveEvent;
|
||||||
use pocketmine\inventory\ArmorInventory;
|
use pocketmine\inventory\ArmorInventory;
|
||||||
use pocketmine\inventory\Inventory;
|
|
||||||
use pocketmine\item\Armor;
|
use pocketmine\item\Armor;
|
||||||
use pocketmine\item\Consumable;
|
use pocketmine\item\Consumable;
|
||||||
use pocketmine\item\Durable;
|
use pocketmine\item\Durable;
|
||||||
@ -96,15 +94,6 @@ abstract class Living extends Entity implements Damageable{
|
|||||||
|
|
||||||
$this->armorInventory = new ArmorInventory($this);
|
$this->armorInventory = new ArmorInventory($this);
|
||||||
//TODO: load/save armor inventory contents
|
//TODO: load/save armor inventory contents
|
||||||
$this->armorInventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
|
|
||||||
$ev = new EntityArmorChangeEvent($this, $oldItem, $newItem, $slot);
|
|
||||||
$ev->call();
|
|
||||||
if($ev->isCancelled()){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ev->getNewItem();
|
|
||||||
});
|
|
||||||
|
|
||||||
$health = $this->getMaxHealth();
|
$health = $this->getMaxHealth();
|
||||||
|
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* ____ _ _ __ __ _ __ __ ____
|
|
||||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
||||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
||||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
||||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* @author PocketMine Team
|
|
||||||
* @link http://www.pocketmine.net/
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace pocketmine\event\entity;
|
|
||||||
|
|
||||||
class EntityArmorChangeEvent extends EntityInventoryChangeEvent{
|
|
||||||
|
|
||||||
}
|
|
@ -1,81 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* ____ _ _ __ __ _ __ __ ____
|
|
||||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
||||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
||||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
||||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* @author PocketMine Team
|
|
||||||
* @link http://www.pocketmine.net/
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace pocketmine\event\entity;
|
|
||||||
|
|
||||||
use pocketmine\entity\Entity;
|
|
||||||
use pocketmine\event\Cancellable;
|
|
||||||
use pocketmine\event\CancellableTrait;
|
|
||||||
use pocketmine\item\Item;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called before a slot in an entity's inventory changes.
|
|
||||||
*/
|
|
||||||
class EntityInventoryChangeEvent extends EntityEvent implements Cancellable{
|
|
||||||
use CancellableTrait;
|
|
||||||
|
|
||||||
/** @var Item */
|
|
||||||
private $oldItem;
|
|
||||||
/** @var Item */
|
|
||||||
private $newItem;
|
|
||||||
/** @var int */
|
|
||||||
private $slot;
|
|
||||||
|
|
||||||
public function __construct(Entity $entity, Item $oldItem, Item $newItem, int $slot){
|
|
||||||
$this->entity = $entity;
|
|
||||||
$this->oldItem = $oldItem;
|
|
||||||
$this->newItem = $newItem;
|
|
||||||
$this->slot = $slot;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the inventory slot number affected by the event.
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getSlot() : int{
|
|
||||||
return $this->slot;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the item which will be in the slot after the event.
|
|
||||||
* @return Item
|
|
||||||
*/
|
|
||||||
public function getNewItem() : Item{
|
|
||||||
return $this->newItem;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Item $item
|
|
||||||
*/
|
|
||||||
public function setNewItem(Item $item) : void{
|
|
||||||
$this->newItem = $item;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the item currently in the slot.
|
|
||||||
* @return Item
|
|
||||||
*/
|
|
||||||
public function getOldItem() : Item{
|
|
||||||
return $this->oldItem;
|
|
||||||
}
|
|
||||||
}
|
|
@ -134,18 +134,14 @@ abstract class BaseInventory implements Inventory{
|
|||||||
}
|
}
|
||||||
|
|
||||||
$oldItem = $this->getItem($index);
|
$oldItem = $this->getItem($index);
|
||||||
if($this->slotChangeListener !== null){
|
|
||||||
$newItem = ($this->slotChangeListener)($this, $index, $oldItem, $item);
|
|
||||||
if($newItem === null){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
$newItem = $item;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->slots[$index] = $newItem->isNull() ? null : $newItem;
|
$this->slots[$index] = $item->isNull() ? null : $item;
|
||||||
$this->onSlotChange($index, $oldItem, $send);
|
$this->onSlotChange($index, $oldItem, $send);
|
||||||
|
|
||||||
|
if($this->slotChangeListener !== null){
|
||||||
|
($this->slotChangeListener)($this, $index);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,7 +436,7 @@ abstract class BaseInventory implements Inventory{
|
|||||||
|
|
||||||
public function setSlotChangeListener(?\Closure $eventProcessor) : void{
|
public function setSlotChangeListener(?\Closure $eventProcessor) : void{
|
||||||
if($eventProcessor !== null){
|
if($eventProcessor !== null){
|
||||||
Utils::validateCallableSignature(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{}, $eventProcessor);
|
Utils::validateCallableSignature(function(Inventory $inventory, int $slot) : void{}, $eventProcessor);
|
||||||
}
|
}
|
||||||
$this->slotChangeListener = $eventProcessor;
|
$this->slotChangeListener = $eventProcessor;
|
||||||
}
|
}
|
||||||
|
@ -60,9 +60,8 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
|||||||
|
|
||||||
public function __construct(World $world, Vector3 $pos){
|
public function __construct(World $world, Vector3 $pos){
|
||||||
$this->inventory = new FurnaceInventory($this);
|
$this->inventory = new FurnaceInventory($this);
|
||||||
$this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
|
$this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot) : void{
|
||||||
$this->scheduleUpdate();
|
$this->scheduleUpdate();
|
||||||
return $newItem;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
parent::__construct($world, $pos);
|
parent::__construct($world, $pos);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user