mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Enchantment: more static getters, firehose magic numbers
This is similar in nature to 646fea5a4e
.
On a side note: Migrating this way is a pain in the ass due to lack of types. What the heck is int supposed to mean?!?!?!?! At least if we wanted to go _back_ to magic numbers, it would be easy to locate everything with an Enchantment typehint...
This commit is contained in:
@ -84,7 +84,7 @@ abstract class Armor extends Durable{
|
||||
}
|
||||
|
||||
protected function getUnbreakingDamageReduction(int $amount) : int{
|
||||
if(($unbreakingLevel = $this->getEnchantmentLevel(Enchantment::UNBREAKING)) > 0){
|
||||
if(($unbreakingLevel = $this->getEnchantmentLevel(Enchantment::UNBREAKING())) > 0){
|
||||
$negated = 0;
|
||||
|
||||
$chance = 1 / ($unbreakingLevel + 1);
|
||||
|
@ -67,17 +67,17 @@ class Bow extends Tool{
|
||||
/** @var ArrowEntity $entity */
|
||||
$entity = EntityFactory::create(ArrowEntity::class, $player->getLevel(), $nbt, $player, $baseForce >= 1);
|
||||
|
||||
$infinity = $this->hasEnchantment(Enchantment::INFINITY);
|
||||
$infinity = $this->hasEnchantment(Enchantment::INFINITY());
|
||||
if($infinity){
|
||||
$entity->setPickupMode(ArrowEntity::PICKUP_CREATIVE);
|
||||
}
|
||||
if(($punchLevel = $this->getEnchantmentLevel(Enchantment::PUNCH)) > 0){
|
||||
if(($punchLevel = $this->getEnchantmentLevel(Enchantment::PUNCH())) > 0){
|
||||
$entity->setPunchKnockback($punchLevel);
|
||||
}
|
||||
if(($powerLevel = $this->getEnchantmentLevel(Enchantment::POWER)) > 0){
|
||||
if(($powerLevel = $this->getEnchantmentLevel(Enchantment::POWER())) > 0){
|
||||
$entity->setBaseDamage($entity->getBaseDamage() + (($powerLevel + 1) / 2));
|
||||
}
|
||||
if($this->hasEnchantment(Enchantment::FLAME)){
|
||||
if($this->hasEnchantment(Enchantment::FLAME())){
|
||||
$entity->setOnFire(intdiv($entity->getFireTicks(), 20) + 100);
|
||||
}
|
||||
$ev = new EntityShootBowEvent($player, $this, $entity, $baseForce * 3);
|
||||
|
@ -85,7 +85,7 @@ abstract class Durable extends Item{
|
||||
}
|
||||
|
||||
protected function getUnbreakingDamageReduction(int $amount) : int{
|
||||
if(($unbreakingLevel = $this->getEnchantmentLevel(Enchantment::UNBREAKING)) > 0){
|
||||
if(($unbreakingLevel = $this->getEnchantmentLevel(Enchantment::UNBREAKING())) > 0){
|
||||
$negated = 0;
|
||||
|
||||
$chance = 1 / ($unbreakingLevel + 1);
|
||||
|
@ -249,16 +249,17 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param int $level
|
||||
* @param Enchantment $enchantment
|
||||
* @param int $level
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasEnchantment(int $id, int $level = -1) : bool{
|
||||
public function hasEnchantment(Enchantment $enchantment, int $level = -1) : bool{
|
||||
$ench = $this->getNamedTagEntry(self::TAG_ENCH);
|
||||
if(!($ench instanceof ListTag)){
|
||||
return false;
|
||||
}
|
||||
$id = $enchantment->getId();
|
||||
|
||||
/** @var CompoundTag $entry */
|
||||
foreach($ench as $entry){
|
||||
@ -271,16 +272,17 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param Enchantment $enchantment
|
||||
*
|
||||
* @return EnchantmentInstance|null
|
||||
*/
|
||||
public function getEnchantment(int $id) : ?EnchantmentInstance{
|
||||
public function getEnchantment(Enchantment $enchantment) : ?EnchantmentInstance{
|
||||
$ench = $this->getNamedTagEntry(self::TAG_ENCH);
|
||||
if(!($ench instanceof ListTag)){
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = $enchantment->getId();
|
||||
/** @var CompoundTag $entry */
|
||||
foreach($ench as $entry){
|
||||
if($entry->getShort("id") === $id){
|
||||
@ -295,17 +297,18 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param int $level
|
||||
* @param Enchantment $enchantment
|
||||
* @param int $level
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function removeEnchantment(int $id, int $level = -1) : Item{
|
||||
public function removeEnchantment(Enchantment $enchantment, int $level = -1) : Item{
|
||||
$ench = $this->getNamedTagEntry(self::TAG_ENCH);
|
||||
if(!($ench instanceof ListTag)){
|
||||
return $this;
|
||||
}
|
||||
|
||||
$id = $enchantment->getId();
|
||||
/** @var CompoundTag $entry */
|
||||
foreach($ench as $k => $entry){
|
||||
if($entry->getShort("id") === $id and ($level === -1 or $entry->getShort("lvl") === $level)){
|
||||
@ -390,14 +393,15 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
* Returns the level of the enchantment on this item with the specified ID, or 0 if the item does not have the
|
||||
* enchantment.
|
||||
*
|
||||
* @param int $enchantmentId
|
||||
* @param Enchantment $enchantment
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getEnchantmentLevel(int $enchantmentId) : int{
|
||||
public function getEnchantmentLevel(Enchantment $enchantment) : int{
|
||||
$ench = $this->getNamedTag()->getListTag(self::TAG_ENCH);
|
||||
if($ench !== null){
|
||||
/** @var CompoundTag $entry */
|
||||
$enchantmentId = $enchantment->getId();
|
||||
foreach($ench as $entry){
|
||||
if($entry->getShort("id") === $enchantmentId){
|
||||
return $entry->getShort("lvl");
|
||||
|
@ -36,7 +36,7 @@ abstract class Tool extends Durable{
|
||||
$efficiency = 1;
|
||||
if(($block->getToolType() & $this->getBlockToolType()) !== 0){
|
||||
$efficiency = $this->getBaseMiningEfficiency();
|
||||
if(($enchantmentLevel = $this->getEnchantmentLevel(Enchantment::EFFICIENCY)) > 0){
|
||||
if(($enchantmentLevel = $this->getEnchantmentLevel(Enchantment::EFFICIENCY())) > 0){
|
||||
$efficiency += ($enchantmentLevel ** 2 + 1);
|
||||
}
|
||||
}
|
||||
|
@ -140,6 +140,82 @@ class Enchantment{
|
||||
self::registerEnchantment(new Enchantment(self::VANISHING, "%enchantment.curse.vanishing", self::RARITY_MYTHIC, self::SLOT_NONE, self::SLOT_ALL, 1));
|
||||
}
|
||||
|
||||
public static function BLAST_PROTECTION() : Enchantment{
|
||||
return self::getEnchantment(self::BLAST_PROTECTION);
|
||||
}
|
||||
|
||||
public static function EFFICIENCY() : Enchantment{
|
||||
return self::getEnchantment(self::EFFICIENCY);
|
||||
}
|
||||
|
||||
public static function FEATHER_FALLING() : Enchantment{
|
||||
return self::getEnchantment(self::FEATHER_FALLING);
|
||||
}
|
||||
|
||||
public static function FIRE_ASPECT() : Enchantment{
|
||||
return self::getEnchantment(self::FIRE_ASPECT);
|
||||
}
|
||||
|
||||
public static function FIRE_PROTECTION() : Enchantment{
|
||||
return self::getEnchantment(self::FIRE_PROTECTION);
|
||||
}
|
||||
|
||||
public static function FLAME() : Enchantment{
|
||||
return self::getEnchantment(self::FLAME);
|
||||
}
|
||||
|
||||
public static function INFINITY() : Enchantment{
|
||||
return self::getEnchantment(self::INFINITY);
|
||||
}
|
||||
|
||||
public static function KNOCKBACK() : Enchantment{
|
||||
return self::getEnchantment(self::KNOCKBACK);
|
||||
}
|
||||
|
||||
public static function MENDING() : Enchantment{
|
||||
return self::getEnchantment(self::MENDING);
|
||||
}
|
||||
|
||||
public static function POWER() : Enchantment{
|
||||
return self::getEnchantment(self::POWER);
|
||||
}
|
||||
|
||||
public static function PROJECTILE_PROTECTION() : Enchantment{
|
||||
return self::getEnchantment(self::PROJECTILE_PROTECTION);
|
||||
}
|
||||
|
||||
public static function PROTECTION() : Enchantment{
|
||||
return self::getEnchantment(self::PROTECTION);
|
||||
}
|
||||
|
||||
public static function PUNCH() : Enchantment{
|
||||
return self::getEnchantment(self::PUNCH);
|
||||
}
|
||||
|
||||
public static function RESPIRATION() : Enchantment{
|
||||
return self::getEnchantment(self::RESPIRATION);
|
||||
}
|
||||
|
||||
public static function SHARPNESS() : Enchantment{
|
||||
return self::getEnchantment(self::SHARPNESS);
|
||||
}
|
||||
|
||||
public static function SILK_TOUCH() : Enchantment{
|
||||
return self::getEnchantment(self::SILK_TOUCH);
|
||||
}
|
||||
|
||||
public static function THORNS() : Enchantment{
|
||||
return self::getEnchantment(self::THORNS);
|
||||
}
|
||||
|
||||
public static function UNBREAKING() : Enchantment{
|
||||
return self::getEnchantment(self::UNBREAKING);
|
||||
}
|
||||
|
||||
public static function VANISHING() : Enchantment{
|
||||
return self::getEnchantment(self::VANISHING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an enchantment type.
|
||||
*
|
||||
|
Reference in New Issue
Block a user