DemoXin

Untitled

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