mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-06 11:57:10 +00:00
Improved Inventory->addItem(), fixed breaking containers duplicating the last slot, removed not necessary slot changes
This commit is contained in:
parent
a3b1d318cc
commit
6d09754ea7
@ -1628,10 +1628,13 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
$this->inventory->sendHeldItem($this);
|
$this->inventory->sendHeldItem($this);
|
||||||
}else{
|
}else{
|
||||||
$item = $this->inventory->getItemInHand();
|
$item = $this->inventory->getItemInHand();
|
||||||
|
$oldItem = clone $item;
|
||||||
//TODO: Implement adventure mode checks
|
//TODO: Implement adventure mode checks
|
||||||
if($this->level->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){
|
if($this->level->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){
|
||||||
$this->inventory->setItemInHand($item, $this);
|
if(!$item->equals($oldItem, true) or $item->getCount() !== $oldItem->getCount()){
|
||||||
$this->inventory->sendHeldItem($this->hasSpawned);
|
$this->inventory->setItemInHand($item, $this);
|
||||||
|
$this->inventory->sendHeldItem($this->hasSpawned);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1802,13 +1805,17 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
if($this->isCreative()){
|
if($this->isCreative()){
|
||||||
$item = $this->inventory->getItemInHand();
|
$item = $this->inventory->getItemInHand();
|
||||||
}else{
|
}else{
|
||||||
$item = clone $this->inventory->getItemInHand();
|
$item = $this->inventory->getItemInHand();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$oldItem = clone $item;
|
||||||
|
|
||||||
if($this->level->useBreakOn($vector, $item, $this) === true){
|
if($this->level->useBreakOn($vector, $item, $this) === true){
|
||||||
if($this->isSurvival()){
|
if($this->isSurvival()){
|
||||||
$this->inventory->setItemInHand($item, $this);
|
if(!$item->equals($oldItem, true) or $item->getCount() !== $oldItem->getCount()){
|
||||||
$this->inventory->sendHeldItem($this->hasSpawned);
|
$this->inventory->setItemInHand($item, $this);
|
||||||
|
$this->inventory->sendHeldItem($this->hasSpawned);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -233,41 +233,58 @@ abstract class BaseInventory implements Inventory{
|
|||||||
$source = null;
|
$source = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var Item[] $itemSlots */
|
||||||
/** @var Item[] $slots */
|
/** @var Item[] $slots */
|
||||||
foreach($slots as $i => $slot){
|
$itemSlots = [];
|
||||||
$slots[$i] = clone $slot;
|
foreach($slots as $slot){
|
||||||
|
if($slot->getId() !== 0 and $slot->getCount() > 0){
|
||||||
|
$itemSlots[] = clone $slot;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$emptySlots = [];
|
||||||
|
|
||||||
for($i = 0; $i < $this->getSize(); ++$i){
|
for($i = 0; $i < $this->getSize(); ++$i){
|
||||||
$item = $this->getItem($i);
|
$item = $this->getItem($i);
|
||||||
foreach($slots as $index => $slot){
|
if($item->getId() === Item::AIR or $item->getCount() <= 0){
|
||||||
if($item->getID() === Item::AIR or $item->getCount() <= 0){
|
$emptySlots[] = $i;
|
||||||
$amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
|
}
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
|
||||||
$item = clone $slot;
|
foreach($itemSlots as $index => $slot){
|
||||||
$item->setCount($amount);
|
if($slot->equals($item, true) and $item->getCount() < $item->getMaxStackSize()){
|
||||||
$this->setItem($i, $item, $source);
|
|
||||||
$item = $this->getItem($i);
|
|
||||||
if($slot->getCount() <= 0){
|
|
||||||
unset($slots[$index]);
|
|
||||||
}
|
|
||||||
}elseif($slot->equals($item, true) and $item->getCount() < $item->getMaxStackSize()){
|
|
||||||
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
|
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
if($amount > 0){
|
||||||
$item->setCount($item->getCount() + $amount);
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
$this->setItem($i, $item, $source);
|
$item->setCount($item->getCount() + $amount);
|
||||||
if($slot->getCount() <= 0){
|
$this->setItem($i, $item, $source);
|
||||||
unset($slots[$index]);
|
if($slot->getCount() <= 0){
|
||||||
|
unset($itemSlots[$index]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count($slots) === 0){
|
if(count($itemSlots) === 0){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $slots;
|
if(count($itemSlots) > 0 and count($emptySlots) > 0){
|
||||||
|
foreach($emptySlots as $slotIndex){
|
||||||
|
foreach($itemSlots as $index => $slot){
|
||||||
|
$amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
|
||||||
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
|
$item = clone $slot;
|
||||||
|
$item->setCount($amount);
|
||||||
|
$this->setItem($slotIndex, $item, $source);
|
||||||
|
if($slot->getCount() <= 0){
|
||||||
|
unset($itemSlots[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $itemSlots;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeItem(...$slots){
|
public function removeItem(...$slots){
|
||||||
@ -279,31 +296,39 @@ abstract class BaseInventory implements Inventory{
|
|||||||
$source = null;
|
$source = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var Item[] $itemSlots */
|
||||||
/** @var Item[] $slots */
|
/** @var Item[] $slots */
|
||||||
|
$itemSlots = [];
|
||||||
|
foreach($slots as $slot){
|
||||||
|
if($slot->getId() !== 0 and $slot->getCount() > 0){
|
||||||
|
$itemSlots[] = clone $slot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for($i = 0; $i < $this->getSize(); ++$i){
|
for($i = 0; $i < $this->getSize(); ++$i){
|
||||||
$item = $this->getItem($i);
|
$item = $this->getItem($i);
|
||||||
if($item->getID() === Item::AIR){
|
if($item->getId() === Item::AIR or $item->getCount() <= 0){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($slots as $index => $slot){
|
foreach($itemSlots as $index => $slot){
|
||||||
if($slot->equals($item, $slot->getDamage() === null ? false : true)){
|
if($slot->equals($item, $slot->getDamage() === null ? false : true)){
|
||||||
$amount = min($item->getCount(), $slot->getCount());
|
$amount = min($item->getCount(), $slot->getCount());
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
$item->setCount($item->getCount() - $amount);
|
$item->setCount($item->getCount() - $amount);
|
||||||
$this->setItem($i, $item, $source);
|
$this->setItem($i, $item, $source);
|
||||||
if($slot->getCount() <= 0){
|
if($slot->getCount() <= 0){
|
||||||
unset($slots[$index]);
|
unset($itemSlots[$index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count($slots) === 0){
|
if(count($itemSlots) === 0){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $slots;
|
return $itemSlots;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clear($index, $source = null){
|
public function clear($index, $source = null){
|
||||||
|
@ -625,7 +625,7 @@ class Item{
|
|||||||
}
|
}
|
||||||
|
|
||||||
final public function __toString(){
|
final public function __toString(){
|
||||||
return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")";
|
return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")x".$this->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDestroySpeed(Block $block, Player $player){
|
public function getDestroySpeed(Block $block, Player $player){
|
||||||
|
@ -1091,8 +1091,8 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
$tile->unpair();
|
$tile->unpair();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($tile->getInventory()->getContents() as $item){
|
foreach($tile->getInventory()->getContents() as $chestItem){
|
||||||
$this->dropItem($target, $item);
|
$this->dropItem($target, $chestItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user