From 32f03fb3a63218bd4f129be12087f9bf10fd9d78 Mon Sep 17 00:00:00 2001 From: Vasco Lavos Date: Thu, 19 Jun 2014 23:07:53 +0100 Subject: [PATCH] Attack pixies! --- .../common/core/handler/PixieHandler.java | 49 +++++++++++ .../common/core/proxy/CommonProxy.java | 2 + .../botania/common/entity/EntityPixie.java | 85 +++++++++---------- .../botania/common/entity/ModEntities.java | 2 +- .../armor/elementium/ItemElementiumBoots.java | 2 +- .../armor/elementium/ItemElementiumChest.java | 2 +- .../armor/elementium/ItemElementiumHelm.java | 2 +- .../armor/elementium/ItemElementiumLegs.java | 2 +- 8 files changed, 97 insertions(+), 49 deletions(-) diff --git a/MODSRC/vazkii/botania/common/core/handler/PixieHandler.java b/MODSRC/vazkii/botania/common/core/handler/PixieHandler.java index 90e33b93e..6cc642e3a 100644 --- a/MODSRC/vazkii/botania/common/core/handler/PixieHandler.java +++ b/MODSRC/vazkii/botania/common/core/handler/PixieHandler.java @@ -1,9 +1,58 @@ package vazkii.botania.common.core.handler; +import vazkii.botania.common.entity.EntityPixie; +import vazkii.botania.common.item.ModItems; +import baubles.common.container.InventoryBaubles; +import baubles.common.lib.PlayerHandler; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraftforge.event.entity.living.LivingHurtEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class PixieHandler { + @SubscribeEvent + public void onDamageTaken(LivingHurtEvent event) { + if(!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityPlayer && event.source.getEntity() != null && event.source.getEntity() instanceof EntityLivingBase) { + EntityPlayer player = (EntityPlayer) event.entityLiving; + ItemStack stack = player.getCurrentEquippedItem(); + + float chance = getChance(stack); + for(int i = 0; i < player.inventory.armorInventory.length; i++) + chance += getChance(player.inventory.armorInventory[i]); + + InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player); + for(int i = 0; i < baubles.getSizeInventory(); i++) + chance += getChance(baubles.getStackInSlot(i)); + + System.out.println(chance); + if(Math.random() < chance) { + EntityPixie pixie = new EntityPixie(player.worldObj); + pixie.setPosition(player.posX, player.posY + 2, player.posZ); + + float dmg = 4; + if(stack != null && stack.getItem() == ModItems.elementiumSword) + dmg += 2; + + pixie.setProps((EntityLivingBase) event.source.getEntity(), player, dmg); + player.worldObj.spawnEntityInWorld(pixie); + } + } + } + + float getChance(ItemStack stack) { + if(stack == null) + return 0F; + + Item item = stack.getItem(); + if(item instanceof IPixieSpawner) + return ((IPixieSpawner) item).getPixieChance(stack); + + return 0F; + } + public static interface IPixieSpawner { public float getPixieChance(ItemStack stack); diff --git a/MODSRC/vazkii/botania/common/core/proxy/CommonProxy.java b/MODSRC/vazkii/botania/common/core/proxy/CommonProxy.java index f391e7ba2..4ed46aecb 100644 --- a/MODSRC/vazkii/botania/common/core/proxy/CommonProxy.java +++ b/MODSRC/vazkii/botania/common/core/proxy/CommonProxy.java @@ -27,6 +27,7 @@ import vazkii.botania.common.core.handler.CommonTickHandler; import vazkii.botania.common.core.handler.ConfigHandler; import vazkii.botania.common.core.handler.InternalMethodHandler; import vazkii.botania.common.core.handler.ManaNetworkHandler; +import vazkii.botania.common.core.handler.PixieHandler; import vazkii.botania.common.core.helper.Vector3; import vazkii.botania.common.core.version.CommandDownloadLatest; import vazkii.botania.common.crafting.ModCrafingRecipes; @@ -73,6 +74,7 @@ public class CommonProxy { MinecraftForge.TERRAIN_GEN_BUS.register(new BiomeDecorationHandler()); MinecraftForge.EVENT_BUS.register(ManaNetworkHandler.instance); + MinecraftForge.EVENT_BUS.register(new PixieHandler()); FMLCommonHandler.instance().bus().register(new CommonTickHandler()); } diff --git a/MODSRC/vazkii/botania/common/entity/EntityPixie.java b/MODSRC/vazkii/botania/common/entity/EntityPixie.java index ec8121d5e..2f6c6d9c8 100644 --- a/MODSRC/vazkii/botania/common/entity/EntityPixie.java +++ b/MODSRC/vazkii/botania/common/entity/EntityPixie.java @@ -12,78 +12,75 @@ package vazkii.botania.common.entity; import vazkii.botania.common.Botania; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.DamageSource; import net.minecraft.util.MathHelper; import net.minecraft.world.EnumSkyBlock; import net.minecraft.world.World; public class EntityPixie extends EntityFlyingCreature { - public int courseChangeCooldown; - public double waypointX; - public double waypointY; - public double waypointZ; - + EntityPlayer player = null; + float damage = 0; + public EntityPixie(World world) { super(world); setSize(1.0F, 1.0F); } + public void setProps(EntityLivingBase target, EntityPlayer player, float damage) { + setAttackTarget(target); + this.player = player; + this.damage = damage; + } + @Override protected void updateEntityActionState() { - double d0 = waypointX - posX; - double d1 = waypointY - posY; - double d2 = waypointZ - posZ; - double d3 = d0 * d0 + d1 * d1 + d2 * d2; + EntityLivingBase target = getAttackTarget(); + System.out.println(target); + if(target != null) { + double d0 = target.posX + target.width / 2 - posX; + double d1 = target.posY + target.height / 2 - posY; + double d2 = target.posZ + target.width / 2 - posZ; + double d3 = d0 * d0 + d1 * d1 + d2 * d2; - if(d3 < 1.0D || d3 > 3600.0D) { - waypointX = posX + (double)((rand.nextFloat() * 4.0F - 2.0F) * 2.0F); - waypointY = posY + (double)((rand.nextFloat() * 4.0F - 2.0F) * 2.0F); - waypointZ = posZ + (double)((rand.nextFloat() * 4.0F - 2.0F) * 2.0F); - } + float mod = 0.45F; + motionX += d0 / d3 * mod; + motionY += d1 / d3 * mod; + motionZ += d2 / d3 * mod; - if(courseChangeCooldown-- <= 0) { - courseChangeCooldown += rand.nextInt(2) + 2; - d3 = (double)MathHelper.sqrt_double(d3); - - if(isCourseTraversable(waypointX, waypointY, waypointZ, d3)) { - motionX += d0 / d3 * 0.1D; - motionY += d1 / d3 * 0.1D; - motionZ += d2 / d3 * 0.1D; - } else { - waypointX = posX; - waypointY = posY; - waypointZ = posZ; + if(Math.sqrt(d3) < 1F) { + if(player != null) + target.attackEntityFrom(DamageSource.causePlayerDamage(player), damage); + else target.attackEntityFrom(DamageSource.causeMobDamage(this), damage); + die(); } } - + renderYawOffset = rotationYaw = -((float)Math.atan2(motionX, motionZ)) * 180.0F / (float)Math.PI; } - private boolean isCourseTraversable(double par1, double par3, double par5, double par7) { - double d4 = (waypointX - posX) / par7; - double d5 = (waypointY - posY) / par7; - double d6 = (waypointZ - posZ) / par7; - AxisAlignedBB axisalignedbb = boundingBox.copy(); - - for (int i = 1; (double)i < par7; ++i) { - axisalignedbb.offset(d4, d5, d6); - - if (!worldObj.getCollidingBoundingBoxes(this, axisalignedbb).isEmpty()) - return false; - } - - return true; - } - @Override public void onEntityUpdate() { super.onEntityUpdate(); + if(ticksExisted > 100) + die(); + if(worldObj.isRemote) for(int i = 0; i < 4; i++) Botania.proxy.sparkleFX(worldObj, posX + (Math.random() - 0.5) * 0.25, posY + 0.5 + (Math.random() - 0.5) * 0.25, posZ + (Math.random() - 0.5) * 0.25, 1F, 0.25F, 0.9F, 0.1F + (float) Math.random() * 0.25F, 12); } - + + public void die() { + setDead(); + + if(worldObj.isRemote) + for(int i = 0; i < 12; i++) + Botania.proxy.sparkleFX(worldObj, posX + (Math.random() - 0.5) * 0.25, posY + 0.5 + (Math.random() - 0.5) * 0.25, posZ + (Math.random() - 0.5) * 0.25, 1F, 0.25F, 0.9F, 1F + (float) Math.random() * 0.25F, 5); + } + } \ No newline at end of file diff --git a/MODSRC/vazkii/botania/common/entity/ModEntities.java b/MODSRC/vazkii/botania/common/entity/ModEntities.java index 64341d1dc..844900c36 100644 --- a/MODSRC/vazkii/botania/common/entity/ModEntities.java +++ b/MODSRC/vazkii/botania/common/entity/ModEntities.java @@ -21,7 +21,7 @@ public final class ModEntities { int id = 0; EntityRegistry.registerModEntity(EntityManaBurst.class, LibEntityNames.MANA_BURST, id++, Botania.instance, 64, 10, true); EntityRegistry.registerModEntity(EntitySignalFlare.class, LibEntityNames.SIGNAL_FLARE, id++, Botania.instance, 2048, 10, false); - EntityRegistry.registerModEntity(EntityPixie.class, LibEntityNames.PIXIE, id++, Botania.instance, 80, 3, true); + EntityRegistry.registerModEntity(EntityPixie.class, LibEntityNames.PIXIE, id++, Botania.instance, 16, 3, true); } } diff --git a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumBoots.java b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumBoots.java index 7c9fa4a8e..932e2eb7b 100644 --- a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumBoots.java +++ b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumBoots.java @@ -11,7 +11,7 @@ public class ItemElementiumBoots extends ItemElementiumArmor { @Override public float getPixieChance(ItemStack stack) { - return 0.041666666666666666666666666666667F; + return 4F / (25F / 24F) * 1.5F; } } diff --git a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumChest.java b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumChest.java index 5dc198f88..edd6ab8e9 100644 --- a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumChest.java +++ b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumChest.java @@ -11,7 +11,7 @@ public class ItemElementiumChest extends ItemElementiumArmor { @Override public float getPixieChance(ItemStack stack) { - return 0.083333333333333333333333333333336F; + return 8F / (25F / 24F) * 1.5F; } } diff --git a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumHelm.java b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumHelm.java index 1f5184044..7b272c18e 100644 --- a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumHelm.java +++ b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumHelm.java @@ -11,7 +11,7 @@ public class ItemElementiumHelm extends ItemElementiumArmor { @Override public float getPixieChance(ItemStack stack) { - return 0.052083333333333333333333333333335F; + return 5F / (25F / 24F) * 1.5F; } } diff --git a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumLegs.java b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumLegs.java index de8b05593..425d0eeab 100644 --- a/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumLegs.java +++ b/MODSRC/vazkii/botania/common/item/equipment/armor/elementium/ItemElementiumLegs.java @@ -11,7 +11,7 @@ public class ItemElementiumLegs extends ItemElementiumArmor { @Override public float getPixieChance(ItemStack stack) { - return 0.072916666666666666666666666666669F; + return 7F / (25F / 24F) * 1.5F; } }