mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-12 08:49:42 +00:00
Encapsulate Item enchantment handling parts within a trait for scope isolation
as a happy side effect, this makes the Item class smaller, makes the code easier to read, and also makes the code more easily testable.
This commit is contained in:
parent
af73c5f2b1
commit
6aba9fadfc
@ -53,6 +53,8 @@ use function hex2bin;
|
|||||||
use function is_string;
|
use function is_string;
|
||||||
|
|
||||||
class Item implements ItemIds, \JsonSerializable{
|
class Item implements ItemIds, \JsonSerializable{
|
||||||
|
use ItemEnchantmentHandlingTrait;
|
||||||
|
|
||||||
public const TAG_ENCH = "ench";
|
public const TAG_ENCH = "ench";
|
||||||
public const TAG_DISPLAY = "display";
|
public const TAG_DISPLAY = "display";
|
||||||
public const TAG_BLOCK_ENTITY_TAG = "BlockEntityTag";
|
public const TAG_BLOCK_ENTITY_TAG = "BlockEntityTag";
|
||||||
@ -73,8 +75,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
|
|
||||||
//TODO: this stuff should be moved to itemstack properties, not mushed in with type properties
|
//TODO: this stuff should be moved to itemstack properties, not mushed in with type properties
|
||||||
|
|
||||||
/** @var EnchantmentInstance[] */
|
|
||||||
protected $enchantments = [];
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $customName = "";
|
protected $customName = "";
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
@ -143,85 +143,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
return $this->blockEntityTag;
|
return $this->blockEntityTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasEnchantments() : bool{
|
|
||||||
return !empty($this->enchantments);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Enchantment $enchantment
|
|
||||||
* @param int $level
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasEnchantment(Enchantment $enchantment, int $level = -1) : bool{
|
|
||||||
$id = $enchantment->getId();
|
|
||||||
return isset($this->enchantments[$id]) and ($level === -1 or $this->enchantments[$id]->getLevel() === $level);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Enchantment $enchantment
|
|
||||||
*
|
|
||||||
* @return EnchantmentInstance|null
|
|
||||||
*/
|
|
||||||
public function getEnchantment(Enchantment $enchantment) : ?EnchantmentInstance{
|
|
||||||
return $this->enchantments[$enchantment->getId()] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Enchantment $enchantment
|
|
||||||
* @param int $level
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function removeEnchantment(Enchantment $enchantment, int $level = -1) : Item{
|
|
||||||
$instance = $this->getEnchantment($enchantment);
|
|
||||||
if($instance !== null and ($level === -1 or $instance->getLevel() === $level)){
|
|
||||||
unset($this->enchantments[$enchantment->getId()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function removeEnchantments() : Item{
|
|
||||||
$this->enchantments = [];
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param EnchantmentInstance $enchantment
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function addEnchantment(EnchantmentInstance $enchantment) : Item{
|
|
||||||
$this->enchantments[$enchantment->getId()] = $enchantment;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return EnchantmentInstance[]
|
|
||||||
*/
|
|
||||||
public function getEnchantments() : array{
|
|
||||||
return $this->enchantments;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the level of the enchantment on this item with the specified ID, or 0 if the item does not have the
|
|
||||||
* enchantment.
|
|
||||||
*
|
|
||||||
* @param Enchantment $enchantment
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getEnchantmentLevel(Enchantment $enchantment) : int{
|
|
||||||
return ($instance = $this->getEnchantment($enchantment)) !== null ? $instance->getLevel() : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
|
116
src/pocketmine/item/ItemEnchantmentHandlingTrait.php
Normal file
116
src/pocketmine/item/ItemEnchantmentHandlingTrait.php
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
<?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\item;
|
||||||
|
|
||||||
|
|
||||||
|
use pocketmine\item\enchantment\Enchantment;
|
||||||
|
use pocketmine\item\enchantment\EnchantmentInstance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This trait encapsulates all enchantment handling needed for itemstacks.
|
||||||
|
* The primary purpose of this trait is providing scope isolation for the methods it contains.
|
||||||
|
*/
|
||||||
|
trait ItemEnchantmentHandlingTrait{
|
||||||
|
/** @var EnchantmentInstance[] */
|
||||||
|
protected $enchantments = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasEnchantments() : bool{
|
||||||
|
return !empty($this->enchantments);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Enchantment $enchantment
|
||||||
|
* @param int $level
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasEnchantment(Enchantment $enchantment, int $level = -1) : bool{
|
||||||
|
$id = $enchantment->getId();
|
||||||
|
return isset($this->enchantments[$id]) and ($level === -1 or $this->enchantments[$id]->getLevel() === $level);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Enchantment $enchantment
|
||||||
|
*
|
||||||
|
* @return EnchantmentInstance|null
|
||||||
|
*/
|
||||||
|
public function getEnchantment(Enchantment $enchantment) : ?EnchantmentInstance{
|
||||||
|
return $this->enchantments[$enchantment->getId()] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Enchantment $enchantment
|
||||||
|
* @param int $level
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function removeEnchantment(Enchantment $enchantment, int $level = -1) : self{
|
||||||
|
$instance = $this->getEnchantment($enchantment);
|
||||||
|
if($instance !== null and ($level === -1 or $instance->getLevel() === $level)){
|
||||||
|
unset($this->enchantments[$enchantment->getId()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function removeEnchantments() : self{
|
||||||
|
$this->enchantments = [];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param EnchantmentInstance $enchantment
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function addEnchantment(EnchantmentInstance $enchantment) : self{
|
||||||
|
$this->enchantments[$enchantment->getId()] = $enchantment;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return EnchantmentInstance[]
|
||||||
|
*/
|
||||||
|
public function getEnchantments() : array{
|
||||||
|
return $this->enchantments;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the level of the enchantment on this item with the specified ID, or 0 if the item does not have the
|
||||||
|
* enchantment.
|
||||||
|
*
|
||||||
|
* @param Enchantment $enchantment
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getEnchantmentLevel(Enchantment $enchantment) : int{
|
||||||
|
return ($instance = $this->getEnchantment($enchantment)) !== null ? $instance->getLevel() : 0;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user