Advertisement
Earthcomputer

EnchantmentDamage.java

Oct 24th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. package net.minecraft.enchantment;
  2.  
  3. import net.minecraft.entity.Entity;
  4. import net.minecraft.entity.EntityLivingBase;
  5. import net.minecraft.entity.EnumCreatureAttribute;
  6. import net.minecraft.init.MobEffects;
  7. import net.minecraft.inventory.EntityEquipmentSlot;
  8. import net.minecraft.item.ItemAxe;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.potion.PotionEffect;
  11.  
  12. public class EnchantmentDamage extends Enchantment
  13. {
  14.     /** None */
  15.     private static final String[] DAMAGE_NAMES = new String[] {"all", "undead", "arthropods"};
  16.     /** Holds the base factor of enchantability needed to be able to use the enchant. */
  17.     private static final int[] MIN_COST = new int[] {1, 5, 5};
  18.     /** None */
  19.     private static final int[] LEVEL_COST = new int[] {11, 8, 8};
  20.     /** None */
  21.     private static final int[] LEVEL_COST_SPAN = new int[] {20, 20, 20};
  22.     /** Defines the type of damage of the enchantment, 0 = all, 1 = undead, 3 = arthropods */
  23.     public final int damageType;
  24.  
  25.     public EnchantmentDamage(Enchantment.Rarity rarityIn, int damageTypeIn, EntityEquipmentSlot... slots)
  26.     {
  27.         super(rarityIn, EnumEnchantmentType.WEAPON, slots);
  28.         this.damageType = damageTypeIn;
  29.     }
  30.  
  31.     /**
  32.      * Returns the minimal value of enchantability needed on the enchantment level passed.
  33.      */
  34.     public int getMinEnchantability(int enchantmentLevel)
  35.     {
  36.         return MIN_COST[this.damageType] + (enchantmentLevel - 1) * LEVEL_COST[this.damageType];
  37.     }
  38.  
  39.     /**
  40.      * Returns the maximum value of enchantability nedded on the enchantment level passed.
  41.      */
  42.     public int getMaxEnchantability(int enchantmentLevel)
  43.     {
  44.         return this.getMinEnchantability(enchantmentLevel) + LEVEL_COST_SPAN[this.damageType];
  45.     }
  46.  
  47.     /**
  48.      * Returns the maximum level that the enchantment can have.
  49.      */
  50.     public int getMaxLevel()
  51.     {
  52.         return 5;
  53.     }
  54.  
  55.     /**
  56.      * Calculates the additional damage that will be dealt by an item with this enchantment. This alternative to
  57.      * calcModifierDamage is sensitive to the targets EnumCreatureAttribute.
  58.      */
  59.     public float calcDamageByCreature(int level, EnumCreatureAttribute creatureType)
  60.     {
  61.         if (this.damageType == 0)
  62.         {
  63.             return 1.0F + (float)Math.max(0, level - 1) * 0.5F;
  64.         }
  65.         else if (this.damageType == 1 && creatureType == EnumCreatureAttribute.UNDEAD)
  66.         {
  67.             return (float)level * 2.5F;
  68.         }
  69.         else
  70.         {
  71.             return this.damageType == 2 && creatureType == EnumCreatureAttribute.ARTHROPOD ? (float)level * 2.5F : 0.0F;
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Return the name of key in translation table of this enchantment.
  77.      */
  78.     public String getName()
  79.     {
  80.         return "enchantment.damage." + DAMAGE_NAMES[this.damageType];
  81.     }
  82.  
  83.     /**
  84.      * Determines if the enchantment passed can be applyied together with this enchantment.
  85.      */
  86.     public boolean canApplyTogether(Enchantment ench)
  87.     {
  88.         return !(ench instanceof EnchantmentDamage);
  89.     }
  90.  
  91.     /**
  92.      * Determines if this enchantment can be applied to a specific ItemStack.
  93.      */
  94.     public boolean canApply(ItemStack stack)
  95.     {
  96.         return stack.getItem() instanceof ItemAxe ? true : super.canApply(stack);
  97.     }
  98.  
  99.     /**
  100.      * Called whenever a mob is damaged with an item that has this enchantment on it.
  101.      */
  102.     public void onEntityDamaged(EntityLivingBase user, Entity target, int level)
  103.     {
  104.         if (target instanceof EntityLivingBase)
  105.         {
  106.             EntityLivingBase entitylivingbase = (EntityLivingBase)target;
  107.  
  108.             if (this.damageType == 2 && entitylivingbase.getCreatureAttribute() == EnumCreatureAttribute.ARTHROPOD)
  109.             {
  110.                 int i = 20 + user.getRNG().nextInt(10 * level);
  111.                 entitylivingbase.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, i, 3));
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement