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