diff --git a/src/main/java/icbm/classic/caps/emp/CapabilityEmpInventory.java b/src/main/java/icbm/classic/caps/emp/CapabilityEmpInventory.java index 507056a1e..beb1f4eac 100644 --- a/src/main/java/icbm/classic/caps/emp/CapabilityEmpInventory.java +++ b/src/main/java/icbm/classic/caps/emp/CapabilityEmpInventory.java @@ -5,10 +5,11 @@ import icbm.classic.api.caps.IEMPReceiver; import icbm.classic.api.explosion.IBlast; import icbm.classic.config.ConfigEMP; -import icbm.classic.lib.energy.UniversalEnergySystem; +import icbm.classic.lib.energy.system.EnergySystem; import icbm.classic.prefab.inventory.InventoryUtility; import net.minecraft.entity.Entity; import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; @@ -49,7 +50,7 @@ public float applyEmpAction(World world, double x, double y, double z, IBlast em } else if (ConfigEMP.DRAIN_ENERGY_ITEMS) { - UniversalEnergySystem.clearEnergy(itemStack, true); + EnergySystem.getSystem(itemStack, null).setEnergy(itemStack, null, 0, true); } if (!InventoryUtility.stacksMatchExact(itemStack, slotStack)) @@ -78,7 +79,12 @@ else if (ConfigEMP.DRAIN_ENERGY_ITEMS) public static class EntityInv extends CapabilityEmpInventory { - public Entity entity; + public final Entity entity; + + public EntityInv(Entity entity) + { + this.entity = entity; + } @Override protected IItemHandlerModifiable getCapability() @@ -127,5 +133,62 @@ public double y() return entity.posY; } } + + public static class TileInv extends CapabilityEmpInventory + { + public final TileEntity entity; + + public TileInv(TileEntity entity) + { + this.entity = entity; + } + + @Override + protected IItemHandlerModifiable getCapability() + { + if (ConfigEMP.ALLOW_ENTITY_INVENTORY && entity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) + { + IItemHandler handler = entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); + + //Currently only support IItemHandlerModifiable due to + // contract on IItemHandler preventing modification of returned getStack() + if (handler instanceof IItemHandlerModifiable) + { + return (IItemHandlerModifiable) handler; + } + } + return null; + } + + @Override + protected TileEntity getHost() + { + return entity; + } + + @Override + public World world() + { + return entity.getWorld(); + } + + @Override + public double z() + { + return entity.getPos().getZ() + 0.5; + } + + @Override + public double x() + { + return entity.getPos().getX() + 0.5; + } + + @Override + public double y() + { + return entity.getPos().getY() + 0.5; + } + } } diff --git a/src/main/java/icbm/classic/config/ConfigEMP.java b/src/main/java/icbm/classic/config/ConfigEMP.java index 56332688d..16e2f69f8 100644 --- a/src/main/java/icbm/classic/config/ConfigEMP.java +++ b/src/main/java/icbm/classic/config/ConfigEMP.java @@ -35,6 +35,10 @@ public class ConfigEMP @Config.Comment("Should EMP effect drain energy items that do not support EMP effect directly?") public static boolean DRAIN_ENERGY_ITEMS = true; + @Config.Name("allow_draining_energy_tiles") + @Config.Comment("Should EMP effect drain energy tiles that do not support EMP effect directly?") + public static boolean DRAIN_ENERGY_TILES = true; + @Config.Name("allow_entities") @Config.Comment("Should EMP effect run on entities?") public static boolean ALLOW_ENTITY = true; diff --git a/src/main/java/icbm/classic/content/entity/missile/EntityMissile.java b/src/main/java/icbm/classic/content/entity/missile/EntityMissile.java index 149cf534c..9da5e6e3e 100644 --- a/src/main/java/icbm/classic/content/entity/missile/EntityMissile.java +++ b/src/main/java/icbm/classic/content/entity/missile/EntityMissile.java @@ -489,51 +489,46 @@ public void setDead() @Override public void explode() + { + normalExplode(); + } + + @Override + public void normalExplode() { try { // Make sure the missile is not already exploding if (!this.isExpoding) { - if (this.explosiveID == null) + //Make sure to note we are currently exploding + this.isExpoding = true; + + //Kill the misisle entity + setDead(); + + if (!this.world.isRemote) { - if (!this.world.isRemote) + //Create TNT explosion if no explosive exists + if (this.explosiveID == null) { this.world.createExplosion(this, this.posX, this.posY, this.posZ, 5F, true); + + } + //Triger normal explosion + else + { + this.explosiveID.handler.createExplosion(this.world, new BlockPos(this.posX, this.posY, this.posZ), this); } - } - else - { - ((Explosion) this.explosiveID.handler).createExplosion(this.world, new BlockPos(this.posX, this.posY, this.posZ), this); } - this.isExpoding = true; - + //Log that the missile impacted ICBMClassic.INSTANCE.logger().info(this.getEntityName() + " (" + this.getEntityId() + ") exploded in " + (int) this.posX + ", " + (int) this.posY + ", " + (int) this.posZ); } - - setDead(); - } catch (Exception e) { - ICBMClassic.INSTANCE.logger().error("Missile failed to explode properly. Report this to the developers.", e); - } - } - - @Override - public void normalExplode() - { - if (!this.isExpoding) - { - isExpoding = true; - - if (!this.world.isRemote) - { - world.createExplosion(this, this.posX, this.posY, this.posZ, 5F, true); - } - - setDead(); + ICBMClassic.INSTANCE.logger().error("EntityMissile#normalExplode() - Unexpected error while triggering explosive on missile", e); } } diff --git a/src/main/java/icbm/classic/content/explosive/blast/BlastEMP.java b/src/main/java/icbm/classic/content/explosive/blast/BlastEMP.java index ba7a22be6..ef72317e9 100644 --- a/src/main/java/icbm/classic/content/explosive/blast/BlastEMP.java +++ b/src/main/java/icbm/classic/content/explosive/blast/BlastEMP.java @@ -4,9 +4,11 @@ import icbm.classic.api.caps.IEMPReceiver; import icbm.classic.api.events.EmpEvent; import icbm.classic.caps.emp.CapabilityEMP; +import icbm.classic.caps.emp.CapabilityEmpInventory; import icbm.classic.client.ICBMSounds; import icbm.classic.config.ConfigEMP; -import icbm.classic.lib.energy.UniversalEnergySystem; +import icbm.classic.lib.energy.system.EnergySystem; +import icbm.classic.lib.energy.system.IEnergySystem; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.tileentity.TileEntity; @@ -90,7 +92,6 @@ public void doExplode() TileEntity tileEntity = world.getTileEntity(blockPos); if (tileEntity != null) { - boolean doInventory = true; if (tileEntity.hasCapability(CapabilityEMP.EMP, null)) { @@ -101,14 +102,22 @@ public void doExplode() doInventory = receiver.shouldEmpSubObjects(world, tileEntity.getPos().getX(), tileEntity.getPos().getY(), tileEntity.getPos().getZ()); } } - else if (ConfigEMP.DRAIN_ENERGY_ENTITY) + else if (ConfigEMP.DRAIN_ENERGY_TILES) { - UniversalEnergySystem.clearEnergy(tileEntity, true); + IEnergySystem energySystem = EnergySystem.getSystem(tileEntity, null); + if (energySystem.canSetEnergyDirectly(tileEntity, null)) + { + energySystem.setEnergy(tileEntity, null, 0, true); + } + else + { + //TODO Spawn tick based effect to drain as much energy as possible over several ticks + } } if (doInventory && tileEntity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) { - powerEntity = empEntity(tileEntity, powerEntity, tileEntity.getCapability(CapabilityEMP.EMP, null)); + powerEntity = empEntity(tileEntity, powerEntity, new CapabilityEmpInventory.TileInv(tileEntity)); } } } @@ -151,12 +160,20 @@ else if (ConfigEMP.DRAIN_ENERGY_ENTITY) } else if (ConfigEMP.DRAIN_ENERGY_ENTITY) { - UniversalEnergySystem.clearEnergy(entity, true); + IEnergySystem energySystem = EnergySystem.getSystem(entity, null); + if (energySystem.canSetEnergyDirectly(entity, null)) + { + energySystem.setEnergy(entity, null, 0, true); + } + else + { + //TODO Spawn tick based effect to drain as much energy as possible over several ticks + } } if (doInventory && entity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) { - powerEntity = empEntity(entity, powerEntity, entity.getCapability(CapabilityEMP.EMP, null)); + powerEntity = empEntity(entity, powerEntity, new CapabilityEmpInventory.EntityInv(entity)); } //Fire post event to allow hooking EMP action