Enchantment: more static getters, firehose magic numbers

This is similar in nature to 646fea5a4ecbbdf3f0cbfc590d874dedc1a7bfc0.

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:
Dylan K. Taylor 2019-02-20 13:45:50 +00:00
parent 646fea5a4e
commit 7170d9009d
11 changed files with 111 additions and 31 deletions

View File

@ -526,7 +526,7 @@ class Block extends Position implements BlockIds, Metadatable{
*/
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
if($this->isAffectedBySilkTouch() and $item->hasEnchantment(Enchantment::SILK_TOUCH)){
if($this->isAffectedBySilkTouch() and $item->hasEnchantment(Enchantment::SILK_TOUCH())){
return $this->getSilkTouchDrops($item);
}
@ -566,7 +566,7 @@ class Block extends Position implements BlockIds, Metadatable{
* @return int
*/
public function getXpDropForTool(Item $item) : int{
if($item->hasEnchantment(Enchantment::SILK_TOUCH) or !$this->isCompatibleWithTool($item)){
if($item->hasEnchantment(Enchantment::SILK_TOUCH()) or !$this->isCompatibleWithTool($item)){
return 0;
}

View File

@ -56,7 +56,7 @@ class Ice extends Transparent{
}
public function onBreak(Item $item, Player $player = null) : bool{
if(($player === null or $player->isSurvival()) and !$item->hasEnchantment(Enchantment::SILK_TOUCH)){
if(($player === null or $player->isSurvival()) and !$item->hasEnchantment(Enchantment::SILK_TOUCH())){
return $this->getLevel()->setBlock($this, BlockFactory::get(Block::WATER));
}
return parent::onBreak($item, $player);

View File

@ -55,7 +55,7 @@ class TNT extends Solid{
}
public function onActivate(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($item instanceof FlintSteel or $item->hasEnchantment(Enchantment::FIRE_ASPECT)){
if($item instanceof FlintSteel or $item->hasEnchantment(Enchantment::FIRE_ASPECT())){
if($item instanceof Durable){
$item->applyDamage(1);
}

View File

@ -537,12 +537,12 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
/** @var Durable[] $equipment */
$equipment = [];
if(($item = $this->inventory->getItemInHand()) instanceof Durable and $item->hasEnchantment(Enchantment::MENDING)){
if(($item = $this->inventory->getItemInHand()) instanceof Durable and $item->hasEnchantment(Enchantment::MENDING())){
$equipment[$mainHandIndex] = $item;
}
//TODO: check offhand
foreach($this->armorInventory->getContents() as $k => $item){
if($item instanceof Durable and $item->hasEnchantment(Enchantment::MENDING)){
if($item instanceof Durable and $item->hasEnchantment(Enchantment::MENDING())){
$equipment[$k] = $item;
}
}
@ -784,7 +784,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
return array_filter(array_merge(
$this->inventory !== null ? array_values($this->inventory->getContents()) : [],
$this->armorInventory !== null ? array_values($this->armorInventory->getContents()) : []
), function(Item $item) : bool{ return !$item->hasEnchantment(Enchantment::VANISHING); });
), function(Item $item) : bool{ return !$item->hasEnchantment(Enchantment::VANISHING()); });
}
public function saveNBT() : CompoundTag{

View File

@ -441,14 +441,14 @@ abstract class Living extends Entity implements Damageable{
/**
* Returns the highest level of the specified enchantment on any armour piece that the entity is currently wearing.
*
* @param int $enchantmentId
* @param Enchantment $enchantment
*
* @return int
*/
public function getHighestArmorEnchantmentLevel(int $enchantmentId) : int{
public function getHighestArmorEnchantmentLevel(Enchantment $enchantment) : int{
$result = 0;
foreach($this->armorInventory->getContents() as $item){
$result = max($result, $item->getEnchantmentLevel($enchantmentId));
$result = max($result, $item->getEnchantmentLevel($enchantment));
}
return $result;
@ -462,7 +462,7 @@ abstract class Living extends Entity implements Damageable{
}
public function setOnFire(int $seconds) : void{
parent::setOnFire($seconds - (int) min($seconds, $seconds * $this->getHighestArmorEnchantmentLevel(Enchantment::FIRE_PROTECTION) * 0.15));
parent::setOnFire($seconds - (int) min($seconds, $seconds * $this->getHighestArmorEnchantmentLevel(Enchantment::FIRE_PROTECTION()) * 0.15));
}
/**
@ -507,7 +507,7 @@ abstract class Living extends Entity implements Damageable{
if($source instanceof EntityDamageByEntityEvent){
$damage = 0;
foreach($this->armorInventory->getContents() as $k => $item){
if($item instanceof Armor and ($thornsLevel = $item->getEnchantmentLevel(Enchantment::THORNS)) > 0){
if($item instanceof Armor and ($thornsLevel = $item->getEnchantmentLevel(Enchantment::THORNS())) > 0){
if(mt_rand(0, 99) < $thornsLevel * 15){
$this->damageItem($item, 3);
$damage += ($thornsLevel > 10 ? $thornsLevel - 10 : 1 + mt_rand(0, 3));
@ -577,7 +577,7 @@ abstract class Living extends Entity implements Damageable{
//TODO: knockback should not just apply for entity damage sources
//this doesn't matter for TNT right now because the PrimedTNT entity is considered the source, not the block.
$base = $source->getKnockBack();
$source->setKnockBack($base - min($base, $base * $this->getHighestArmorEnchantmentLevel(Enchantment::BLAST_PROTECTION) * 0.15));
$source->setKnockBack($base - min($base, $base * $this->getHighestArmorEnchantmentLevel(Enchantment::BLAST_PROTECTION()) * 0.15));
}
parent::attack($source);
@ -738,7 +738,7 @@ abstract class Living extends Entity implements Damageable{
if(!$this->canBreathe()){
$this->setBreathing(false);
if(($respirationLevel = $this->armorInventory->getHelmet()->getEnchantmentLevel(Enchantment::RESPIRATION)) <= 0 or
if(($respirationLevel = $this->armorInventory->getHelmet()->getEnchantmentLevel(Enchantment::RESPIRATION())) <= 0 or
lcg_value() <= (1 / ($respirationLevel + 1))
){
$ticks -= $tickDiff;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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");

View File

@ -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);
}
}

View File

@ -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.
*