mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-07 02:21:46 +00:00
ItemFactory: Check the bounds of durability, instead of trying to catch exceptions
This commit is contained in:
parent
da51f106de
commit
01c3668375
@ -440,11 +440,10 @@ class ItemFactory{
|
|||||||
if(isset($this->list[$offset = self::getListOffset($id, $meta)])){
|
if(isset($this->list[$offset = self::getListOffset($id, $meta)])){
|
||||||
$item = clone $this->list[$offset];
|
$item = clone $this->list[$offset];
|
||||||
}elseif(isset($this->list[$zero = self::getListOffset($id, 0)]) and $this->list[$zero] instanceof Durable){
|
}elseif(isset($this->list[$zero = self::getListOffset($id, 0)]) and $this->list[$zero] instanceof Durable){
|
||||||
/** @var Durable $item */
|
if($meta <= $this->list[$zero]->getMaxDurability()){
|
||||||
$item = clone $this->list[$zero];
|
$item = clone $this->list[$zero];
|
||||||
try{
|
|
||||||
$item->setDamage($meta);
|
$item->setDamage($meta);
|
||||||
}catch(\InvalidArgumentException $e){
|
}else{
|
||||||
$item = new Item(new ItemIdentifier($id, $meta));
|
$item = new Item(new ItemIdentifier($id, $meta));
|
||||||
}
|
}
|
||||||
}elseif($id < 256){ //intentionally includes negatives, for extended block IDs
|
}elseif($id < 256){ //intentionally includes negatives, for extended block IDs
|
||||||
|
@ -48,4 +48,9 @@ class ItemFactoryTest extends TestCase{
|
|||||||
/** @var Sword $i2 */
|
/** @var Sword $i2 */
|
||||||
self::assertSame(1, $i2->getDamage());
|
self::assertSame(1, $i2->getDamage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetDurableItemWithTooLargeDurability() : void{
|
||||||
|
self::assertInstanceOf(Sword::class, ItemFactory::getInstance()->get(ItemIds::WOODEN_SWORD, ToolTier::WOOD()->getMaxDurability()));
|
||||||
|
self::assertInstanceOf(Item::class, ItemFactory::getInstance()->get(ItemIds::WOODEN_SWORD, ToolTier::WOOD()->getMaxDurability() + 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user