    001package net.minecraft.enchantment;
    002
    003import net.minecraft.entity.Entity;
    004import net.minecraft.util.DamageSource;
    005import net.minecraft.util.MathHelper;
    006
    007public class EnchantmentProtection extends Enchantment
    008{
    009    /** Holds the name to be translated of each protection type. */
    010    private static final String[] protectionName = new String[] {"all", "fire", "fall", "explosion", "projectile"};
    011
    012    /**
    013     * Holds the base factor of enchantability needed to be able to use the enchant.
    014     */
    015    private static final int[] baseEnchantability = new int[] {1, 10, 5, 5, 3};
    016
    017    /**
    018     * Holds how much each level increased the enchantability factor to be able to use this enchant.
    019     */
    020    private static final int[] levelEnchantability = new int[] {11, 8, 6, 8, 6};
    021
    022    /**
    023     * Used on the formula of base enchantability, this is the 'window' factor of values to be able to use thing
    024     * enchant.
    025     */
    026    private static final int[] thresholdEnchantability = new int[] {20, 12, 10, 12, 15};
    027
    028    /**
    029     * Defines the type of protection of the enchantment, 0 = all, 1 = fire, 2 = fall (feather fall), 3 = explosion and
    030     * 4 = projectile.
    031     */
    032    public final int protectionType;
    033
    034    public EnchantmentProtection(int par1, int par2, int par3)
    035    {
    036        super(par1, par2, EnumEnchantmentType.armor);
    037        this.protectionType = par3;
    038
    039        if (par3 == 2)
    040        {
    041            this.type = EnumEnchantmentType.armor_feet;
    042        }
    043    }
    044
    045    /**
    046     * Returns the minimal value of enchantability needed on the enchantment level passed.
    047     */
    048    public int getMinEnchantability(int par1)
    049    {
    050        return baseEnchantability[this.protectionType] + (par1 - 1) * levelEnchantability[this.protectionType];
    051    }
    052
    053    /**
    054     * Returns the maximum value of enchantability nedded on the enchantment level passed.
    055     */
    056    public int getMaxEnchantability(int par1)
    057    {
    058        return this.getMinEnchantability(par1) + thresholdEnchantability[this.protectionType];
    059    }
    060
    061    /**
    062     * Returns the maximum level that the enchantment can have.
    063     */
    064    public int getMaxLevel()
    065    {
    066        return 4;
    067    }
    068
    069    /**
    070     * Calculates de damage protection of the enchantment based on level and damage source passed.
    071     */
    072    public int calcModifierDamage(int par1, DamageSource par2DamageSource)
    073    {
    074        if (par2DamageSource.canHarmInCreative())
    075        {
    076            return 0;
    077        }
    078        else
    079        {
    080            float var3 = (float)(6 + par1 * par1) / 3.0F;
    081            return this.protectionType == 0 ? MathHelper.floor_float(var3 * 0.75F) : (this.protectionType == 1 && par2DamageSource.isFireDamage() ? MathHelper.floor_float(var3 * 1.25F) : (this.protectionType == 2 && par2DamageSource == DamageSource.fall ? MathHelper.floor_float(var3 * 2.5F) : ((this.protectionType != 3 || par2DamageSource != DamageSource.explosion) && par2DamageSource != DamageSource.explosion2 ? (this.protectionType == 4 && par2DamageSource.isProjectile() ? MathHelper.floor_float(var3 * 1.5F) : 0) : MathHelper.floor_float(var3 * 1.5F))));
    082        }
    083    }
    084
    085    /**
    086     * Return the name of key in translation table of this enchantment.
    087     */
    088    public String getName()
    089    {
    090        return "enchantment.protect." + protectionName[this.protectionType];
    091    }
    092
    093    /**
    094     * Determines if the enchantment passed can be applyied together with this enchantment.
    095     */
    096    public boolean canApplyTogether(Enchantment par1Enchantment)
    097    {
    098        if (par1Enchantment instanceof EnchantmentProtection)
    099        {
    100            EnchantmentProtection var2 = (EnchantmentProtection)par1Enchantment;
    101            return var2.protectionType == this.protectionType ? false : this.protectionType == 2 || var2.protectionType == 2;
    102        }
    103        else
    104        {
    105            return super.canApplyTogether(par1Enchantment);
    106        }
    107    }
    108
    109    public static int func_92093_a(Entity par0Entity, int par1)
    110    {
    111        int var2 = EnchantmentHelper.getMaxEnchantmentLevel(Enchantment.fireProtection.effectId, par0Entity.getLastActiveItems());
    112
    113        if (var2 > 0)
    114        {
    115            par1 -= MathHelper.floor_float((float)par1 * (float)var2 * 0.15F);
    116        }
    117
    118        return par1;
    119    }
    120
    121    public static double func_92092_a(Entity par0Entity, double par1)
    122    {
    123        int var3 = EnchantmentHelper.getMaxEnchantmentLevel(Enchantment.blastProtection.effectId, par0Entity.getLastActiveItems());
    124
    125        if (var3 > 0)
    126        {
    127            par1 -= (double)MathHelper.floor_double(par1 * (double)((float)var3 * 0.15F));
    128        }
    129
    130        return par1;
    131    }
    132}