mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-19 09:54:10 +00:00
Added API method Item::canStackWith()
This commit is contained in:
parent
32d7b1e6af
commit
7ba573db77
@ -660,6 +660,7 @@ However, if we add `src-namespace-prefix: pmmp\TesterPlugin` to the `plugin.yml`
|
|||||||
- `WritableBookPage`
|
- `WritableBookPage`
|
||||||
- The following API methods have been added:
|
- The following API methods have been added:
|
||||||
- `Armor->getArmorSlot()`
|
- `Armor->getArmorSlot()`
|
||||||
|
- `Item->canStackWith()`: returns whether the two items could be contained in the same inventory slot, ignoring count and stack size limits
|
||||||
- `Potion->getType()`: returns a `PotionType` enum object containing information such as the applied effects
|
- `Potion->getType()`: returns a `PotionType` enum object containing information such as the applied effects
|
||||||
- `ProjectileItem->createEntity()`: returns a new instance of the projectile entity that will be thrown
|
- `ProjectileItem->createEntity()`: returns a new instance of the projectile entity that will be thrown
|
||||||
- The following classes have been removed:
|
- The following classes have been removed:
|
||||||
|
@ -82,7 +82,7 @@ class CraftingManager{
|
|||||||
|
|
||||||
foreach($items as $i => $item){
|
foreach($items as $i => $item){
|
||||||
foreach($result as $otherItem){
|
foreach($result as $otherItem){
|
||||||
if($item->equals($otherItem)){
|
if($item->canStackWith($otherItem)){
|
||||||
$otherItem->setCount($otherItem->getCount() + $item->getCount());
|
$otherItem->setCount($otherItem->getCount() + $item->getCount());
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
|
@ -118,7 +118,7 @@ trait InventoryHelpersTrait{
|
|||||||
$count = $item->getCount();
|
$count = $item->getCount();
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
$slot = $this->getItem($i);
|
$slot = $this->getItem($i);
|
||||||
if($item->equals($slot)){
|
if($item->canStackWith($slot)){
|
||||||
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
|
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
|
||||||
$count -= $diff;
|
$count -= $diff;
|
||||||
}
|
}
|
||||||
@ -166,7 +166,7 @@ trait InventoryHelpersTrait{
|
|||||||
$emptySlots[] = $i;
|
$emptySlots[] = $i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($slot->equals($item) and $item->getCount() < $item->getMaxStackSize()){
|
if($slot->canStackWith($item) 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());
|
||||||
if($amount > 0){
|
if($amount > 0){
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
|
@ -84,7 +84,7 @@ class CraftingTransaction extends InventoryTransaction{
|
|||||||
$recipeItem = array_pop($recipeItems);
|
$recipeItem = array_pop($recipeItems);
|
||||||
$needCount = $recipeItem->getCount();
|
$needCount = $recipeItem->getCount();
|
||||||
foreach($recipeItems as $i => $otherRecipeItem){
|
foreach($recipeItems as $i => $otherRecipeItem){
|
||||||
if($otherRecipeItem->equals($recipeItem)){ //make sure they have the same wildcards set
|
if($otherRecipeItem->canStackWith($recipeItem)){ //make sure they have the same wildcards set
|
||||||
$needCount += $otherRecipeItem->getCount();
|
$needCount += $otherRecipeItem->getCount();
|
||||||
unset($recipeItems[$i]);
|
unset($recipeItems[$i]);
|
||||||
}
|
}
|
||||||
|
@ -158,7 +158,7 @@ class InventoryTransaction{
|
|||||||
|
|
||||||
foreach($needItems as $i => $needItem){
|
foreach($needItems as $i => $needItem){
|
||||||
foreach($haveItems as $j => $haveItem){
|
foreach($haveItems as $j => $haveItem){
|
||||||
if($needItem->equals($haveItem)){
|
if($needItem->canStackWith($haveItem)){
|
||||||
$amount = min($needItem->getCount(), $haveItem->getCount());
|
$amount = min($needItem->getCount(), $haveItem->getCount());
|
||||||
$needItem->setCount($needItem->getCount() - $amount);
|
$needItem->setCount($needItem->getCount() - $amount);
|
||||||
$haveItem->setCount($haveItem->getCount() - $amount);
|
$haveItem->setCount($haveItem->getCount() - $amount);
|
||||||
|
@ -566,6 +566,13 @@ class Item implements \JsonSerializable{
|
|||||||
(!$checkCompound or $this->getNamedTag()->equals($item->getNamedTag()));
|
(!$checkCompound or $this->getNamedTag()->equals($item->getNamedTag()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether this item could stack with the given item (ignoring stack size and count).
|
||||||
|
*/
|
||||||
|
final public function canStackWith(Item $other) : bool{
|
||||||
|
return $this->equals($other, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the specified item stack has the same ID, damage, NBT and count as this item stack.
|
* Returns whether the specified item stack has the same ID, damage, NBT and count as this item stack.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user