Skip to content

Commit

Permalink
Added support for item inventories to EMP
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGuardsman committed Mar 13, 2018
1 parent 4fee71e commit d31173e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 36 deletions.
111 changes: 76 additions & 35 deletions src/main/java/icbm/classic/caps/emp/CapabilityEmpInventory.java
@@ -1,7 +1,6 @@
package icbm.classic.caps.emp;

import icbm.classic.ICBMClassic;
import icbm.classic.api.IWorldPosition;
import icbm.classic.api.caps.IEMPReceiver;
import icbm.classic.api.explosion.IBlast;
import icbm.classic.config.ConfigEMP;
Expand All @@ -16,11 +15,17 @@
import net.minecraftforge.items.IItemHandlerModifiable;

/**
* Used to wrapper an inventory capability to provide support to EMP the contents.
* <p>
* By default {@link icbm.classic.content.explosive.blast.BlastEMP} will generate this object
* for the target of the EMP.
*
* @see <a href="https://github.com/BuiltBrokenModding/VoltzEngine/blob/development/license.md">License</a> for what you can and can't do with the code.
* Created by Dark(DarkGuardsman, Robert) on 3/12/2018.
*/
public abstract class CapabilityEmpInventory<H extends Object> implements IEMPReceiver, IWorldPosition
public abstract class CapabilityEmpInventory<H extends Object> implements IEMPReceiver
{
@Override
public float applyEmpAction(World world, double x, double y, double z, IBlast emp_blast, float power, boolean doAction)
{
final IItemHandlerModifiable iItemHandler = getCapability();
Expand All @@ -34,6 +39,7 @@ public float applyEmpAction(World world, double x, double y, double z, IBlast em
//Check to make sure its not a placeholder
if (!slotStack.isEmpty())
{
boolean doInventory = ConfigEMP.ALLOW_ITEM_INVENTORY;
//Copy stack for editing
final ItemStack itemStack = slotStack.copy();

Expand All @@ -46,11 +52,24 @@ public float applyEmpAction(World world, double x, double y, double z, IBlast em
{
//Apply effect
power = cap.applyEmpAction(world, x, y, z, emp_blast, power, true);
doInventory = cap.shouldEmpSubObjects(world, x, y, z) && doInventory;
}
}
else if (ConfigEMP.DRAIN_ENERGY_ITEMS)
{
EnergySystem.getSystem(itemStack, null).setEnergy(itemStack, null, 0, true);
EnergySystem.getSystem(itemStack, null).setEnergy(itemStack, null, 0, true);
}

if (doInventory && itemStack.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
{
if (getHost() instanceof Entity)
{
power = new ItemInvInEntity((Entity) getHost(), itemStack).applyEmpAction(world, x, y, z, emp_blast, power, doAction);
}
else if (getHost() instanceof TileEntity)
{
power = new ItemInvInTile((TileEntity) getHost(), itemStack).applyEmpAction(world, x, y, z, emp_blast, power, doAction);
}
}

if (!InventoryUtility.stacksMatchExact(itemStack, slotStack))
Expand Down Expand Up @@ -108,47 +127,58 @@ protected Entity getHost()
{
return entity;
}
}

@Override
public World world()
{
return entity.world;
}
public static class TileInv extends CapabilityEmpInventory<TileEntity>
{
public final TileEntity entity;

@Override
public double z()
public TileInv(TileEntity entity)
{
return entity.posZ;
this.entity = entity;
}

@Override
public double x()
protected IItemHandlerModifiable getCapability()
{
return entity.posX;
if (ConfigEMP.ALLOW_TILE_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
public double y()
protected TileEntity getHost()
{
return entity.posY;
return entity;
}
}

public static class TileInv extends CapabilityEmpInventory<TileEntity>
public static class ItemInvInEntity extends CapabilityEmpInventory<Entity>
{
public final TileEntity entity;
public final ItemStack item;
public final Entity host;

public TileInv(TileEntity entity)
public ItemInvInEntity(Entity host, ItemStack item)
{
this.entity = entity;
this.host = host;
this.item = item;
}

@Override
protected IItemHandlerModifiable getCapability()
{
if (ConfigEMP.ALLOW_ENTITY_INVENTORY && entity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
if (ConfigEMP.ALLOW_ITEM_INVENTORY && item.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
{
IItemHandler handler = entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
IItemHandler handler = item.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

//Currently only support IItemHandlerModifiable due to
// contract on IItemHandler preventing modification of returned getStack()
Expand All @@ -161,33 +191,44 @@ protected IItemHandlerModifiable getCapability()
}

@Override
protected TileEntity getHost()
protected Entity getHost()
{
return entity;
return host;
}
}

@Override
public World world()
{
return entity.getWorld();
}
public static class ItemInvInTile extends CapabilityEmpInventory<TileEntity>
{
public final ItemStack item;
public final TileEntity host;

@Override
public double z()
public ItemInvInTile(TileEntity host, ItemStack item)
{
return entity.getPos().getZ() + 0.5;
this.host = host;
this.item = item;
}

@Override
public double x()
protected IItemHandlerModifiable getCapability()
{
return entity.getPos().getX() + 0.5;
if (ConfigEMP.ALLOW_ITEM_INVENTORY && item.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
{
IItemHandler handler = item.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
public double y()
protected TileEntity getHost()
{
return entity.getPos().getY() + 0.5;
return host;
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/icbm/classic/config/ConfigEMP.java
Expand Up @@ -24,9 +24,17 @@ public class ConfigEMP
public static boolean ALLOW_MISSILE_DROPS = true;

@Config.Name("allow_entity_inventory")
@Config.Comment("Should EMP effect run on entity inventories?")
@Config.Comment("Should EMP effect run on entity inventories? (Eg. Player, Cart)")
public static boolean ALLOW_ENTITY_INVENTORY = true;

@Config.Name("allow_tile_inventory")
@Config.Comment("Should EMP effect run on block/tile inventories? (Eg. Chest, Hopper, Machine)")
public static boolean ALLOW_TILE_INVENTORY = true;

@Config.Name("allow_item_inventory")
@Config.Comment("Should EMP effect run on item inventories? (Eg. Bag, Backpack)")
public static boolean ALLOW_ITEM_INVENTORY = true;

@Config.Name("allow_draining_energy_entity")
@Config.Comment("Should EMP effect drain energy entities that do not support EMP effect directly?")
public static boolean DRAIN_ENERGY_ENTITY = true;
Expand Down

0 comments on commit d31173e

Please sign in to comment.