Enchantment API changes, understandable constant names

This commit is contained in:
Dylan K. Taylor 2017-05-03 12:07:38 +01:00
parent ecba80fd63
commit 3b7fc21839
3 changed files with 110 additions and 57 deletions

View File

@ -60,7 +60,7 @@ class EnchantCommand extends VanillaCommand{
$enchantLevel = isset($args[2]) ? (int) $args[2] : 1;
$enchantment = Enchantment::getEnchantment($enchantId);
if($enchantment->getId() === Enchantment::TYPE_INVALID){
if(!($enchantment instanceof Enchantment)){
$sender->sendMessage(new TranslationContainer("commands.enchant.notFound", [$enchantId]));
return true;
}

View File

@ -543,8 +543,10 @@ class Item implements ItemIds, \JsonSerializable{
foreach($this->getNamedTag()->ench as $entry){
if($entry["id"] === $id){
$e = Enchantment::getEnchantment($entry["id"]);
$e->setLevel($entry["lvl"]);
return $e;
if($e !== null){
$e->setLevel($entry["lvl"]);
return $e;
}
}
}
@ -627,8 +629,10 @@ class Item implements ItemIds, \JsonSerializable{
if($this->hasEnchantments()){
foreach($this->getNamedTag()->ench as $entry){
$e = Enchantment::getEnchantment($entry["id"]);
$e->setLevel($entry["lvl"]);
$enchantments[] = $e;
if($e !== null){
$e->setLevel($entry["lvl"]);
$enchantments[] = $e;
}
}
}

View File

@ -26,33 +26,33 @@ namespace pocketmine\item\enchantment;
class Enchantment{
const TYPE_INVALID = -1;
const TYPE_ARMOR_PROTECTION = 0;
const TYPE_ARMOR_FIRE_PROTECTION = 1;
const TYPE_ARMOR_FALL_PROTECTION = 2;
const TYPE_ARMOR_EXPLOSION_PROTECTION = 3;
const TYPE_ARMOR_PROJECTILE_PROTECTION = 4;
const TYPE_ARMOR_THORNS = 5;
const TYPE_WATER_BREATHING = 6;
const TYPE_WATER_SPEED = 7;
const TYPE_WATER_AFFINITY = 8;
const TYPE_WEAPON_SHARPNESS = 9;
const TYPE_WEAPON_SMITE = 10;
const TYPE_WEAPON_ARTHROPODS = 11;
const TYPE_WEAPON_KNOCKBACK = 12;
const TYPE_WEAPON_FIRE_ASPECT = 13;
const TYPE_WEAPON_LOOTING = 14;
const TYPE_MINING_EFFICIENCY = 15;
const TYPE_MINING_SILK_TOUCH = 16;
const TYPE_MINING_DURABILITY = 17;
const TYPE_MINING_FORTUNE = 18;
const TYPE_BOW_POWER = 19;
const TYPE_BOW_KNOCKBACK = 20;
const TYPE_BOW_FLAME = 21;
const TYPE_BOW_INFINITY = 22;
const TYPE_FISHING_FORTUNE = 23;
const TYPE_FISHING_LURE = 24;
const PROTECTION = 0;
const FIRE_PROTECTION = 1;
const FEATHER_FALLING = 2;
const BLAST_PROTECTION = 3;
const PROJECTILE_PROTECTION = 4;
const THORNS = 5;
const RESPIRATION = 6;
const DEPTH_STRIDER = 7;
const AQUA_AFFINITY = 8;
const SHARPNESS = 9;
const SMITE = 10;
const BANE_OF_ARTHROPODS = 11;
const KNOCKBACK = 12;
const FIRE_ASPECT = 13;
const LOOTING = 14;
const EFFICIENCY = 15;
const SILK_TOUCH = 16;
const UNBREAKING = 17;
const FORTUNE = 18;
const POWER = 19;
const PUNCH = 20;
const FLAME = 21;
const INFINITY = 22;
const LUCK_OF_THE_SEA = 23;
const LURE = 24;
const FROST_WALKER = 25;
const MENDING = 26;
const RARITY_COMMON = 0;
const RARITY_UNCOMMON = 1;
@ -89,25 +89,31 @@ class Enchantment{
public static function init(){
self::$enchantments = new \SplFixedArray(256);
self::$enchantments[self::TYPE_ARMOR_PROTECTION] = new Enchantment(self::TYPE_ARMOR_PROTECTION, "%enchantment.protect.all", self::RARITY_COMMON, self::ACTIVATION_EQUIP, self::SLOT_ARMOR);
self::$enchantments[self::TYPE_ARMOR_FIRE_PROTECTION] = new Enchantment(self::TYPE_ARMOR_FIRE_PROTECTION, "%enchantment.protect.fire", self::RARITY_UNCOMMON, self::ACTIVATION_EQUIP, self::SLOT_ARMOR);
self::$enchantments[self::TYPE_ARMOR_FALL_PROTECTION] = new Enchantment(self::TYPE_ARMOR_FALL_PROTECTION, "%enchantment.protect.fall", self::RARITY_UNCOMMON, self::ACTIVATION_EQUIP, self::SLOT_FEET);
self::$enchantments[self::PROTECTION] = new Enchantment(self::PROTECTION, "%enchantment.protect.all", self::RARITY_COMMON, self::ACTIVATION_EQUIP, self::SLOT_ARMOR);
self::$enchantments[self::FIRE_PROTECTION] = new Enchantment(self::FIRE_PROTECTION, "%enchantment.protect.fire", self::RARITY_UNCOMMON, self::ACTIVATION_EQUIP, self::SLOT_ARMOR);
self::$enchantments[self::FEATHER_FALLING] = new Enchantment(self::FEATHER_FALLING, "%enchantment.protect.fall", self::RARITY_UNCOMMON, self::ACTIVATION_EQUIP, self::SLOT_FEET);
}
/**
* @param int $id
* @return $this
*
* @return Enchantment|null
*/
public static function getEnchantment($id){
public static function getEnchantment(int $id){
if(isset(self::$enchantments[$id])){
return clone self::$enchantments[(int) $id];
return clone self::$enchantments[$id];
}
return new Enchantment(self::TYPE_INVALID, "unknown", 0, 0, 0);
return null;
}
public static function getEffectByName($name){
if(defined(Enchantment::class . "::TYPE_" . strtoupper($name))){
return self::getEnchantment(constant(Enchantment::class . "::TYPE_" . strtoupper($name)));
/**
* @param string $name
*
* @return Enchantment|null
*/
public static function getEnchantmentByName(string $name){
if(defined(Enchantment::class . "::" . strtoupper($name))){
return self::getEnchantment(constant(Enchantment::class . "::" . strtoupper($name)));
}
return null;
}
@ -119,44 +125,87 @@ class Enchantment{
private $activationType;
private $slot;
private function __construct($id, $name, $rarity, $activationType, $slot){
$this->id = (int) $id;
$this->name = (string) $name;
$this->rarity = (int) $rarity;
$this->activationType = (int) $activationType;
$this->slot = (int) $slot;
/**
* @param int $id
* @param string $name
* @param int $rarity
* @param int $activationType
* @param int $slot
*/
public function __construct(int $id, string $name, int $rarity, int $activationType, int $slot){
$this->id = $id;
$this->name = $name;
$this->rarity = $rarity;
$this->activationType = $activationType;
$this->slot = $slot;
}
public function getId(){
/**
* Returns the ID of this enchantment as per Minecraft PE
* @return int
*/
public function getId() : int{
return $this->id;
}
public function getName(){
/**
* Returns a translation key for this enchantment's name.
* @return string
*/
public function getName() : string{
return $this->name;
}
public function getRarity(){
/**
* Returns an int constant indicating how rare this enchantment type is.
* @return int
*/
public function getRarity() : int{
return $this->rarity;
}
public function getActivationType(){
/**
* Returns an int constant describing what type of activation this enchantment requires. For example armor enchantments only apply when worn.
* @return int
*/
public function getActivationType() : int{
return $this->activationType;
}
public function getSlot(){
/**
* Returns an int with bitflags set to indicate what item types this enchantment can apply to.
* @return int
*/
public function getSlot() : int{
return $this->slot;
}
public function hasSlot($slot){
/**
* Returns whether this enchantment can apply to the specified item type.
* @param int $slot
*
* @return bool
*/
public function hasSlot(int $slot) : bool{
return ($this->slot & $slot) > 0;
}
public function getLevel(){
/**
* Returns the level of the enchantment.
* @return int
*/
public function getLevel(): int{
return $this->level;
}
public function setLevel($level){
$this->level = (int) $level;
/**
* Sets the level of the enchantment.
* @param int $level
*
* @return $this
*/
public function setLevel(int $level){
$this->level = $level;
return $this;
}