Skip to content

Commit

Permalink
Implemented new energy system wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGuardsman committed Mar 13, 2018
1 parent 7166225 commit 4fee71e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 38 deletions.
69 changes: 66 additions & 3 deletions src/main/java/icbm/classic/caps/emp/CapabilityEmpInventory.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -78,7 +79,12 @@ else if (ConfigEMP.DRAIN_ENERGY_ITEMS)

public static class EntityInv extends CapabilityEmpInventory<Entity>
{
public Entity entity;
public final Entity entity;

public EntityInv(Entity entity)
{
this.entity = entity;
}

@Override
protected IItemHandlerModifiable getCapability()
Expand Down Expand Up @@ -127,5 +133,62 @@ public double y()
return entity.posY;
}
}

public static class TileInv extends CapabilityEmpInventory<TileEntity>
{
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;
}
}
}

4 changes: 4 additions & 0 deletions src/main/java/icbm/classic/config/ConfigEMP.java
Expand Up @@ -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;
Expand Down
Expand Up @@ -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);
}
}

Expand Down
31 changes: 24 additions & 7 deletions src/main/java/icbm/classic/content/explosive/blast/BlastEMP.java
Expand Up @@ -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;
Expand Down Expand Up @@ -90,7 +92,6 @@ public void doExplode()
TileEntity tileEntity = world.getTileEntity(blockPos);
if (tileEntity != null)
{

boolean doInventory = true;
if (tileEntity.hasCapability(CapabilityEMP.EMP, null))
{
Expand All @@ -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));
}
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4fee71e

Please sign in to comment.