mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-15 02:09:42 +00:00
Fixed wrong reported slot in PlayerItemHeldEvent, fixed assert failures due to negative slot numbers in the hotbar
fixes some issues reported in #525
This commit is contained in:
parent
8779259734
commit
38b8f14758
@ -64,14 +64,22 @@ class PlayerInventory extends BaseInventory{
|
||||
*/
|
||||
public function equipItem(int $hotbarSlot, $inventorySlot = null) : bool{
|
||||
if($inventorySlot === null){
|
||||
$inventorySlot = $this->getHotbarSlotIndex($this->getHeldItemIndex());
|
||||
$inventorySlot = $this->getHotbarSlotIndex($hotbarSlot);
|
||||
}
|
||||
|
||||
if($hotbarSlot < 0 or $hotbarSlot >= $this->getHotbarSize() or $inventorySlot < -1 or $inventorySlot >= $this->getSize()){
|
||||
$this->sendContents($this->getHolder());
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->getHolder()->getLevel()->getServer()->getPluginManager()->callEvent($ev = new PlayerItemHeldEvent($this->getHolder(), $this->getItem($inventorySlot), $inventorySlot, $hotbarSlot));
|
||||
if($inventorySlot === -1){
|
||||
$item = Item::get(Item::AIR, 0, 0);
|
||||
}else{
|
||||
$item = $this->getItem($inventorySlot);
|
||||
}
|
||||
|
||||
$this->getHolder()->getLevel()->getServer()->getPluginManager()->callEvent($ev = new PlayerItemHeldEvent($this->getHolder(), $item, $inventorySlot, $hotbarSlot));
|
||||
|
||||
if($ev->isCancelled()){
|
||||
$this->sendContents($this->getHolder());
|
||||
return false;
|
||||
@ -103,16 +111,36 @@ class PlayerInventory extends BaseInventory{
|
||||
* @param int $inventorySlot
|
||||
*/
|
||||
public function setHotbarSlotIndex($hotbarSlot, $inventorySlot){
|
||||
if($hotbarSlot >= 0 and $hotbarSlot < $this->getHotbarSize() and $inventorySlot >= -1 and $inventorySlot < $this->getSize()){
|
||||
if($inventorySlot !== -1 and ($alreadyEquippedIndex = array_search($inventorySlot, $this->hotbar)) !== false){
|
||||
/* Swap the slots
|
||||
* This assumes that the equipped slot can only be equipped in one other slot
|
||||
* it will not account for ancient bugs where the same slot ended up linked to several hotbar slots.
|
||||
* Such bugs will require a hotbar reset to default.
|
||||
*/
|
||||
$this->hotbar[$alreadyEquippedIndex] = $this->hotbar[$hotbarSlot];
|
||||
}
|
||||
$this->hotbar[$hotbarSlot] = $inventorySlot;
|
||||
if($hotbarSlot < 0 or $hotbarSlot >= $this->getHotbarSize()){
|
||||
throw new \InvalidArgumentException("Hotbar slot index \"$index\" is out of range");
|
||||
}elseif($inventorySlot < -1 or $inventorySlot >= $this->getSize()){
|
||||
throw new \InvalidArgumentException("Inventory slot index \"$index\" is out of range");
|
||||
}
|
||||
|
||||
if($inventorySlot !== -1 and ($alreadyEquippedIndex = array_search($inventorySlot, $this->hotbar)) !== false){
|
||||
/* Swap the slots
|
||||
* This assumes that the equipped slot can only be equipped in one other slot
|
||||
* it will not account for ancient bugs where the same slot ended up linked to several hotbar slots.
|
||||
* Such bugs will require a hotbar reset to default.
|
||||
*/
|
||||
$this->hotbar[$alreadyEquippedIndex] = $this->hotbar[$hotbarSlot];
|
||||
}
|
||||
|
||||
$this->hotbar[$hotbarSlot] = $inventorySlot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the item in the slot linked to the specified hotbar slot, or Air if the slot is not linked to any hotbar slot.
|
||||
* @param int $hotbarSlotIndex
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function getHotbarSlotItem(int $hotbarSlotIndex) : Item{
|
||||
$inventorySlot = $this->getHotbarSlotIndex($hotbarSlotIndex);
|
||||
if($inventorySlot !== -1){
|
||||
return $this->getItem($inventorySlot);
|
||||
}else{
|
||||
return Item::get(Item::AIR, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,6 +179,8 @@ class PlayerInventory extends BaseInventory{
|
||||
}
|
||||
|
||||
$this->sendHeldItem($this->getHolder()->getViewers());
|
||||
}else{
|
||||
throw new \InvalidArgumentException("Hotbar slot index \"$index\" is out of range");
|
||||
}
|
||||
}
|
||||
|
||||
@ -160,12 +190,7 @@ class PlayerInventory extends BaseInventory{
|
||||
* @return Item
|
||||
*/
|
||||
public function getItemInHand(){
|
||||
$item = $this->getItem($this->getHeldItemSlot());
|
||||
if($item instanceof Item){
|
||||
return $item;
|
||||
}else{
|
||||
return Item::get(Item::AIR, 0, 0);
|
||||
}
|
||||
return $this->getHotbarSlotItem($this->itemInHandIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user