DemoXin

Untitled

Nov 21st, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.39 KB | None | 0 0
  1. package com.demoxin.minecraft.moreenchants;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.ListIterator;
  7. import java.util.Map;
  8. import java.util.UUID;
  9.  
  10. import net.minecraft.enchantment.EnchantmentHelper;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.SharedMonsterAttributes;
  14. import net.minecraft.entity.WatchableObject;
  15. import net.minecraft.entity.ai.attributes.AttributeInstance;
  16. import net.minecraft.entity.ai.attributes.AttributeModifier;
  17. import net.minecraft.entity.item.EntityXPOrb;
  18. import net.minecraft.entity.monster.EntityCreeper;
  19. import net.minecraft.entity.player.EntityPlayer;
  20. import net.minecraft.entity.projectile.EntityArrow;
  21. import net.minecraft.item.ItemStack;
  22. import net.minecraft.potion.Potion;
  23. import net.minecraft.potion.PotionEffect;
  24. import net.minecraft.util.AxisAlignedBB;
  25. import net.minecraft.util.DamageSource;
  26. import net.minecraft.util.MathHelper;
  27. import net.minecraft.util.Vec3;
  28. import net.minecraftforge.event.ForgeSubscribe;
  29. import net.minecraftforge.event.entity.EntityJoinWorldEvent;
  30. import net.minecraftforge.event.entity.PlaySoundAtEntityEvent;
  31. import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
  32. import net.minecraftforge.event.entity.living.LivingHurtEvent;
  33.  
  34. public class EnchantProcessor {
  35.     private final int MENDING = 1;
  36.     private final int MENDING2 = 2;
  37.     private final int POISON = 4;
  38.    
  39.     private static Map<EntityLivingBase, AttributeModifier> fleetfootWatcher = new HashMap<EntityLivingBase, AttributeModifier>();
  40.     public static UUID fleetfootUUID = UUID.fromString("edff168f-32d7-438b-8d29-189e9405e032");
  41.    
  42.     @ForgeSubscribe
  43.     public void Handler_EntitySpawn(EntityJoinWorldEvent fEvent)
  44.     {
  45.         // Arrow manipulation for bow enchants
  46.         // Only interested in arrows
  47.         if(fEvent.entity instanceof EntityArrow)
  48.         {
  49.             // Let's check and see if our owner is something that's alive.
  50.             if(((EntityArrow)fEvent.entity).shootingEntity instanceof EntityLivingBase)
  51.             {
  52.                 // Let's grab their bow
  53.                 EntityLivingBase shooter = (EntityLivingBase)((EntityArrow)fEvent.entity).shootingEntity;
  54.                 ItemStack itemBow = shooter.getHeldItem();
  55.                
  56.                 // Make sure our data int is nice and set-up.
  57.                 fEvent.entity.getDataWatcher().addObject(24, Integer.valueOf(0));
  58.                
  59.                 // Mending Enchant
  60.                 int mendingLevel = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantMending.effectId, itemBow);
  61.                 if(mendingLevel > 0)
  62.                 {
  63.                     // Set our fancy bitmask
  64.                     fEvent.entity.getDataWatcher().updateObject(24, fEvent.entity.getDataWatcher().getWatchableObjectInt(24) + Integer.valueOf(MENDING));
  65.                     if(mendingLevel == 2)
  66.                         fEvent.entity.getDataWatcher().updateObject(24, fEvent.entity.getDataWatcher().getWatchableObjectInt(24) + Integer.valueOf(MENDING2));
  67.                    
  68.                     // Remove knockback completely
  69.                     ((EntityArrow)fEvent.entity).setKnockbackStrength(-1);
  70.                 }
  71.                
  72.                 // Poison Enchant
  73.                 if(EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantPoison.effectId, itemBow) > 0)
  74.                     fEvent.entity.getDataWatcher().updateObject(24, fEvent.entity.getDataWatcher().getWatchableObjectInt(24) + Integer.valueOf(POISON));
  75.                
  76.             }
  77.         }
  78.         // End Arrow modification
  79.        
  80.         // Speed mods hack prevention
  81.         if(fEvent.entity instanceof EntityLivingBase)
  82.         {
  83.             EntityLivingBase fEntity = (EntityLivingBase)fEvent.entity;
  84.             AttributeInstance attrSpeed = fEntity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.movementSpeed);
  85.             AttributeModifier modSpeed = new AttributeModifier(fleetfootUUID, "FleetfootedBoots", 0, 1);
  86.             attrSpeed.removeModifier(modSpeed);
  87.         }
  88.        
  89.     }
  90.    
  91.     @ForgeSubscribe
  92.     public void Handler_PlaySoundAtEntity(PlaySoundAtEntityEvent fEvent)
  93.     {
  94.         // This is our SUPER hacky way of granting our experience bonus to players with the "Knowledge" enchant.
  95.    
  96.         if(fEvent.name != "random.orb")
  97.             return;
  98.        
  99.         if(!(fEvent.entity instanceof EntityXPOrb))
  100.             return;
  101.        
  102.         // This is an XP sound, let's figure out who our target player is.
  103.         EntityPlayer player = fEvent.entity.worldObj.getClosestPlayerToEntity(fEvent.entity, 2.0D);
  104.        
  105.         // Let's check if they've got a Knowledge enchanted helm on.
  106.         if(player == null)
  107.             return;
  108.        
  109.         ItemStack armorHelm = player.getCurrentItemOrArmor(4);
  110.        
  111.         if(armorHelm == null)
  112.             return;
  113.        
  114.         // Look for Knowledge
  115.         if(EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantKnowledge.effectId, armorHelm) > 0)
  116.         {
  117.             // They have knowledge, let's give them double.
  118.             player.addExperience(((EntityXPOrb)fEvent.entity).getXpValue());
  119.         }
  120.     }
  121.    
  122.     @ForgeSubscribe
  123.     public void Handler_LivingUpdateTick(LivingUpdateEvent fEvent)
  124.     {
  125.         if(fEvent.entity instanceof EntityLivingBase)
  126.         {          
  127.             // Check for Fleetfooted
  128.             // We know this is an EntityLivingBase at least already
  129.             EntityLivingBase entity = (EntityLivingBase)fEvent.entity;
  130.             ItemStack boots = entity.getCurrentItemOrArmor(1);
  131.            
  132.             if(boots == null)
  133.             {
  134.                 RemoveSpeedBuff(entity);
  135.                 return;
  136.             }
  137.            
  138.             // Look for the enchant on them.
  139.             int level = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantFleetfoot.effectId, boots);
  140.                        
  141.             if(level > 0)
  142.                 AddSpeedBuff(entity);
  143.             else
  144.                 RemoveSpeedBuff(entity);
  145.  
  146.         }
  147.     }
  148.    
  149.     private void AddSpeedBuff(EntityLivingBase fEntity)
  150.     {
  151.         if(fleetfootWatcher.get(fEntity) != null)
  152.             return;
  153.  
  154.         if(fEntity instanceof EntityPlayer)
  155.             ((EntityPlayer)fEntity).addChatMessage("Newly Detected Speed Boots");
  156.        
  157.         AttributeModifier modSpeed = new AttributeModifier(fleetfootUUID, "FleetfootedBoots", 0.5D, 1);
  158.         AttributeInstance speedAttr = fEntity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.movementSpeed);
  159.        
  160.         // Creatures and Players use 2 different values here
  161.         speedAttr.applyModifier(modSpeed);
  162.            
  163.         fleetfootWatcher.put(fEntity, modSpeed);
  164.     }
  165.    
  166.     private void RemoveSpeedBuff(EntityLivingBase fEntity)
  167.     {
  168.         if(fleetfootWatcher.get(fEntity) == null)
  169.             return;
  170.        
  171.         fEntity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.movementSpeed).removeModifier(fleetfootWatcher.get(fEntity));
  172.        
  173.         fleetfootWatcher.remove(fEntity);
  174.     }
  175.    
  176.     @ForgeSubscribe
  177.     public void Handler_EntityHurt(LivingHurtEvent fEvent)
  178.     {
  179.         if(fEvent.source != DamageSource.magic && fEvent.source.damageType != "player" && fEvent.source.damageType != "mob" && fEvent.source.damageType != "arrow")
  180.             return;
  181.        
  182.         // We're here, so this could be an event we're interested in.
  183.        
  184.         // Bow Enchants
  185.         if(fEvent.source.damageType == "arrow")
  186.         {
  187.             EntityArrow strikingArrow = (EntityArrow)fEvent.source.getSourceOfDamage();
  188.             // This is an arrow hit. We need to look for mending and poison.
  189.            
  190.             // Make sure we're getting bits safely.
  191.             boolean bitsafe = false;
  192.             @SuppressWarnings("unchecked")
  193.             List<WatchableObject> watched = strikingArrow.getDataWatcher().getAllWatched();
  194.             for(WatchableObject obj : watched)
  195.             {
  196.                 if(obj.getDataValueId() == 24)
  197.                 {
  198.                     bitsafe = true;
  199.                     break;
  200.                 }
  201.             }
  202.             // Our necessary value isn't there, add it.
  203.             if(!bitsafe)
  204.                 strikingArrow.getDataWatcher().addObject(24, Integer.valueOf(0));
  205.            
  206.             int infoBits = strikingArrow.getDataWatcher().getWatchableObjectInt(24);
  207.             if((infoBits & MENDING) != 0)
  208.             {
  209.                 // This is a mending arrow.
  210.                 // First thing, cancel the normal stuff associated with this arrow.
  211.                 fEvent.setCanceled(true);
  212.                
  213.                 // Stop the arrow dead so that it doesn't move our "victim" around.
  214.                 fEvent.entity.setVelocity(0, 0, 0);
  215.                
  216.                 // Next, figure out our damage modifier.
  217.                 float dmgMod = 0.3F;
  218.                 if((infoBits & MENDING2) != 0)
  219.                     dmgMod = dmgMod + 0.2F;
  220.                
  221.                 ((EntityLivingBase)fEvent.entity).heal(fEvent.ammount * dmgMod);
  222.             }
  223.            
  224.             if((infoBits & POISON) != 0)
  225.             {
  226.                 // We've got the poison mod.  We need to generate an effect based on the mending status of our bow.
  227.                 PotionEffect poisonResult;
  228.                 if((infoBits & MENDING) != 0)
  229.                     poisonResult = new PotionEffect(Potion.regeneration.getId(), 100, 1);
  230.                 else
  231.                     poisonResult = new PotionEffect(Potion.poison.getId(), 120, 1);
  232.                
  233.                 ((EntityLivingBase)fEvent.entity).addPotionEffect(poisonResult);
  234.                
  235.             }
  236.         }
  237.        
  238.         // Melee Weapons
  239.         if(fEvent.source.damageType == "player" || fEvent.source.damageType == "mob")
  240.         {
  241.                         // The meat and potatoes of our damage enchants, we're looking for our damage-oriented enchants here, so let's get started.
  242.             ItemStack dmgSource = ((EntityLivingBase)fEvent.source.getSourceOfDamage()).getHeldItem();
  243.             if(dmgSource != null)
  244.             {              
  245.                 // Our attacker is holding and item, we need to check it for each of our enchants.
  246.                 int levelMending = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantMending.effectId, dmgSource);
  247.                 int levelVenom = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantVenom.effectId, dmgSource);
  248.                 int levelDefusing = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantDefusing.effectId, dmgSource);
  249.                 int levelCleave = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantCleave.effectId, dmgSource);
  250.                
  251.                 // Mending
  252.                 if(levelMending > 0)
  253.                 {
  254.                     // Mending enchant, convert to damage.
  255.                     fEvent.setCanceled(true);
  256.                    
  257.                     float healMod = 0.2F + (0.1F * levelMending);
  258.                    
  259.                     ((EntityLivingBase)fEvent.entity).heal(fEvent.ammount * healMod);
  260.                     return;
  261.                 }
  262.                
  263.                 // Venom
  264.                 if(levelVenom > 0)
  265.                 {
  266.                     // Process venom here.
  267.                     // We want to make our poison more toxic by its level, so our Venom level will be our amplifier.
  268.                     ((EntityLivingBase)fEvent.entity).addPotionEffect(new PotionEffect(Potion.poison.getId(), 60, levelVenom));
  269.                 }
  270.                
  271.                 // Defusing
  272.                 if(levelDefusing > 0)
  273.                 {
  274.                     // Creeper killer enchant, we need to increase the damage, but only if the victim is a creeper.
  275.                     if(fEvent.entity instanceof EntityCreeper)
  276.                     {              
  277.                         fEvent.ammount = fEvent.ammount + (2.5F * levelDefusing);
  278.                         if(levelDefusing > 4)
  279.                             fEvent.ammount = fEvent.ammount + 0.5F;
  280.                     }
  281.                 }
  282.                
  283.                 // Cleave
  284.                 if(levelCleave > 0)
  285.                 {
  286.                     // We have a cleaving level, let's figure out our damage value.
  287.                     float splashDamage = fEvent.ammount * (levelCleave * 0.25F);
  288.                    
  289.                     // Next, find our entities to hit.
  290.                     EntityLivingBase attacker = (EntityLivingBase)fEvent.source.getSourceOfDamage();
  291.                     AxisAlignedBB boundBox = AxisAlignedBB.getBoundingBox(attacker.posX-5, attacker.posY-5, attacker.posZ-5, attacker.posX+5, attacker.posY+5, attacker.posZ+5);
  292.                     @SuppressWarnings("unchecked")
  293.                     ArrayList<Entity> targetEntities = new ArrayList<Entity>(attacker.worldObj.getEntitiesWithinAABBExcludingEntity(fEvent.entity, boundBox));
  294.                    
  295.                     // Let's remove all the entries that aren't within range of our attacker
  296.                     ListIterator<Entity> itr = targetEntities.listIterator();
  297.                     while(itr.hasNext())
  298.                     {
  299.                         Entity target = itr.next();
  300.                        
  301.                         if(target == attacker)
  302.                             continue;
  303.                        
  304.                         if(target.getDistanceToEntity(attacker) > 3.5F)
  305.                             continue;
  306.                        
  307.                         Vec3 attackerCheck = Vec3.createVectorHelper(target.posX-attacker.posX, target.posY-attacker.posY, target.posZ-attacker.posZ);
  308.                         double angle = Math.toDegrees(Math.acos(attackerCheck.normalize().dotProduct(attacker.getLookVec())));
  309.                        
  310.                         if(angle < 60.0D)
  311.                         {
  312.                             // This is within our arc, let's deal our damage.
  313.                             target.attackEntityFrom(DamageSource.generic, splashDamage);
  314.                         }
  315.                     }
  316.                 }
  317.             }
  318.         }
  319.         // End Melee Weapons
  320.        
  321.         // Poison/Magic Protection
  322.         if(fEvent.source == DamageSource.magic)
  323.         {
  324.             if(fEvent.entity instanceof EntityLivingBase)
  325.             {
  326.                 // This is a living thing, so let's look through its armor slots and start reducing incoming damage.
  327.                 EntityLivingBase victim = (EntityLivingBase)fEvent.entity;
  328.                 int[] levelProtect = new int[4];
  329.                 levelProtect[0] = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantPoisonProtect.effectId, victim.getCurrentItemOrArmor(4));
  330.                 levelProtect[1] = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantPoisonProtect.effectId, victim.getCurrentItemOrArmor(3));
  331.                 levelProtect[2] = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantPoisonProtect.effectId, victim.getCurrentItemOrArmor(2));
  332.                 levelProtect[3] = EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantPoisonProtect.effectId, victim.getCurrentItemOrArmor(1));
  333.                
  334.                 for(int i = 0; i < levelProtect.length; ++i)
  335.                 {
  336.                     if(levelProtect[i] > 0)
  337.                     {
  338.                         float dmgMod = (float)(6 + levelProtect[i] * levelProtect[i]) / 3.0F;
  339.                         dmgMod = MathHelper.floor_float(dmgMod * 1.25F);
  340.                         dmgMod = dmgMod / 100;
  341.                         dmgMod = dmgMod * 5;
  342.                         dmgMod = 1.0F - dmgMod;
  343.                         fEvent.ammount = fEvent.ammount * dmgMod;
  344.                     }
  345.                 }
  346.             }
  347.         }
  348.         // End Poison Protection
  349.        
  350.         // Berserking
  351.         if(fEvent.source.damageType == "player" || fEvent.source.damageType == "mob")
  352.         {
  353.             EntityLivingBase attacker = (EntityLivingBase)fEvent.source.getSourceOfDamage();
  354.             // Grab their chest armor
  355.             ItemStack armorChest = attacker.getCurrentItemOrArmor(3);
  356.            
  357.             if(armorChest == null)
  358.                 return;
  359.            
  360.             // See if they have berserking.  Berserking only needs to respect its existence.
  361.             if(EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantBerserk.effectId, armorChest) != 0)
  362.             {
  363.                 // We have berserking, let's go ahead and figure out how much to amplify the damage by.
  364.                 float attackerHealthPercent = attacker.getHealth() / attacker.getMaxHealth();
  365.                 float dmgMod = 1.0f - attackerHealthPercent;
  366.                 dmgMod = 1.0F + dmgMod;
  367.                
  368.                 // Modify the event damage
  369.                 fEvent.ammount = fEvent.ammount * dmgMod;
  370.             }
  371.         }
  372.         // End Berserking
  373.        
  374.         // Agility
  375.         if(fEvent.source.damageType == "player" || fEvent.source.damageType == "mob")
  376.         {
  377.             EntityLivingBase victim = (EntityLivingBase)fEvent.entity;
  378.             // Grab their chest armor
  379.             ItemStack armorLegs = victim.getCurrentItemOrArmor(2);
  380.            
  381.             if(armorLegs == null)
  382.                 return;
  383.            
  384.             // See if we have agility.  Only should be 1 level
  385.             if(EnchantmentHelper.getEnchantmentLevel(MoreEnchants.enchantAgility.effectId, armorLegs) != 0)
  386.             {
  387.                 if(fEvent.entity.worldObj.rand.nextInt(100) < 10)
  388.                 {
  389.                     // We hit our dodge, cancel the event.
  390.                     // Add sounds eventually maybe?
  391.                     //fEvent.entity.playSound("moreenchants:sounds/dodge", 1.0F, 1.0F);
  392.                     fEvent.setCanceled(true);
  393.                 }
  394.             }
  395.         }
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment