reworked energy prefabs

Plan to do a lot more work as i want more control over how the tiles
work even at the basic layer.
This commit is contained in:
DarkGuardsman 2013-09-18 06:53:42 -04:00
parent 2009392405
commit 841d891aa3
18 changed files with 1160 additions and 851 deletions

View file

@ -22,283 +22,269 @@ import buildcraft.api.power.PowerHandler;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
/**
* A universal electricity tile used for tiles that consume or produce electricity.
/** Basic energy tile that can consume power
*
* Extend this class or use as a reference for your own implementation of compatible electrical
* tiles.
* Based off both UE universal electrical tile, and electrical tile prefabs
*
* @author micdoodle8, Calclavia
*
*/
* @author DarkGuardsman */
public abstract class TileEntityUniversalElectrical extends TileEntityElectrical implements IEnergySink, IEnergySource, IPowerReceptor
{
protected boolean isAddedToEnergyNet;
public PowerHandler bcPowerHandler;
public Type bcBlockType = Type.MACHINE;
public float maxInputEnergy = 100;
protected boolean isAddedToEnergyNet, unpowered = false;
public PowerHandler bcPowerHandler;
public Type bcBlockType = Type.MACHINE;
public float maxInputEnergy = 100;
/**
* Recharges electric item.
*/
@Override
public void recharge(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
super.recharge(itemStack);
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
IElectricItemManager manager = electricItem.getManager(itemStack);
float energy = Math.max(this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.charge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false) * Compatibility.IC2_RATIO;
this.provideElectricity(energy, true);
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float accepted = ((IChargeableItem) itemStack.getItem()).receiveEnergy(itemStack, this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
this.provideElectricity(accepted, true);
}
}
}
/** Recharges electric item. */
@Override
public void recharge(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
super.recharge(itemStack);
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
IElectricItemManager manager = electricItem.getManager(itemStack);
float energy = Math.max(this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.charge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false) * Compatibility.IC2_RATIO;
this.provideElectricity(energy, true);
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float accepted = ((IChargeableItem) itemStack.getItem()).receiveEnergy(itemStack, this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
this.provideElectricity(accepted, true);
}
}
}
/**
* Discharges electric item.
*/
@Override
public void discharge(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
super.discharge(itemStack);
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
/** Discharges electric item. */
@Override
public void discharge(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
super.discharge(itemStack);
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
if (electricItem.canProvideEnergy(itemStack))
{
IElectricItemManager manager = electricItem.getManager(itemStack);
float energy = Math.max(this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.discharge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false);
this.receiveElectricity(energy, true);
}
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float given = ((IChargeableItem) itemStack.getItem()).transferEnergy(itemStack, this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
this.receiveElectricity(given, true);
}
}
}
if (electricItem.canProvideEnergy(itemStack))
{
IElectricItemManager manager = electricItem.getManager(itemStack);
float energy = Math.max(this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.discharge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false);
this.receiveElectricity(energy, true);
}
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float given = ((IChargeableItem) itemStack.getItem()).transferEnergy(itemStack, this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
this.receiveElectricity(given, true);
}
}
}
@Override
public void initiate()
{
super.initiate();
this.initBuildCraft();
}
@Override
public void initiate()
{
super.initiate();
this.initBuildCraft();
}
@Override
public void updateEntity()
{
super.updateEntity();
@Override
public void updateEntity()
{
super.updateEntity();
// Register to the IC2 Network
if (!this.worldObj.isRemote)
{
if (!this.isAddedToEnergyNet)
{
this.initIC();
}
// Register to the IC2 Network
if (!this.worldObj.isRemote)
{
if (!this.isAddedToEnergyNet)
{
this.initIC();
}
if (this.bcPowerHandler == null)
{
this.initBuildCraft();
}
if (this.bcPowerHandler == null)
{
this.initBuildCraft();
}
if (Compatibility.isBuildcraftLoaded())
{
if (this.bcPowerHandler.getEnergyStored() > 0)
{
/**
* Cheat BuildCraft powerHandler and always empty energy inside of it.
*/
this.receiveElectricity(this.bcPowerHandler.getEnergyStored() * Compatibility.BC3_RATIO, true);
this.bcPowerHandler.setEnergy(0);
}
}
}
}
if (Compatibility.isBuildcraftLoaded())
{
if (this.bcPowerHandler.getEnergyStored() > 0)
{
/** Cheat BuildCraft powerHandler and always empty energy inside of it. */
this.receiveElectricity(this.bcPowerHandler.getEnergyStored() * Compatibility.BC3_RATIO, true);
this.bcPowerHandler.setEnergy(0);
}
}
}
}
@Override
public void produce()
{
if (!this.worldObj.isRemote)
{
for (ForgeDirection outputDirection : this.getOutputDirections())
{
this.produceUE(outputDirection);
this.produceBuildCraft(outputDirection);
}
}
}
@Override
public void produce()
{
if (!this.worldObj.isRemote)
{
for (ForgeDirection outputDirection : this.getOutputDirections())
{
this.produceUE(outputDirection);
this.produceBuildCraft(outputDirection);
}
}
}
public void produceBuildCraft(ForgeDirection outputDirection)
{
if (!this.worldObj.isRemote && outputDirection != null && outputDirection != ForgeDirection.UNKNOWN)
{
float provide = this.getProvide(outputDirection);
public void produceBuildCraft(ForgeDirection outputDirection)
{
if (!this.worldObj.isRemote && outputDirection != null && outputDirection != ForgeDirection.UNKNOWN)
{
float provide = this.getProvide(outputDirection);
if (this.getEnergyStored() >= provide && provide > 0)
{
if (Compatibility.isBuildcraftLoaded())
{
TileEntity tileEntity = new Vector3(this).modifyPositionFromSide(outputDirection).getTileEntity(this.worldObj);
if (this.getEnergyStored() >= provide && provide > 0)
{
if (Compatibility.isBuildcraftLoaded())
{
TileEntity tileEntity = new Vector3(this).modifyPositionFromSide(outputDirection).getTileEntity(this.worldObj);
if (tileEntity instanceof IPowerReceptor)
{
PowerReceiver receiver = ((IPowerReceptor) tileEntity).getPowerReceiver(outputDirection.getOpposite());
if (tileEntity instanceof IPowerReceptor)
{
PowerReceiver receiver = ((IPowerReceptor) tileEntity).getPowerReceiver(outputDirection.getOpposite());
if (receiver != null)
{
float bc3Provide = provide * Compatibility.TO_BC_RATIO;
float energyUsed = Math.min(receiver.receiveEnergy(this.bcBlockType, bc3Provide, outputDirection.getOpposite()), bc3Provide);
this.provideElectricity((bc3Provide - (energyUsed * Compatibility.TO_BC_RATIO)), true);
}
}
}
}
}
}
if (receiver != null)
{
float bc3Provide = provide * Compatibility.TO_BC_RATIO;
float energyUsed = Math.min(receiver.receiveEnergy(this.bcBlockType, bc3Provide, outputDirection.getOpposite()), bc3Provide);
this.provideElectricity((bc3Provide - (energyUsed * Compatibility.TO_BC_RATIO)), true);
}
}
}
}
}
}
/**
* IC2 Methods
*/
@Override
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
{
return this.getInputDirections().contains(direction);
}
/** IC2 Methods */
@Override
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
{
return this.getInputDirections().contains(direction);
}
@Override
public double getOfferedEnergy()
{
return this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.TO_IC2_RATIO;
}
@Override
public double getOfferedEnergy()
{
return this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.TO_IC2_RATIO;
}
@Override
public void drawEnergy(double amount)
{
this.provideElectricity((float) amount * Compatibility.IC2_RATIO, true);
}
@Override
public void drawEnergy(double amount)
{
this.provideElectricity((float) amount * Compatibility.IC2_RATIO, true);
}
@Override
public void invalidate()
{
this.unloadTileIC2();
super.invalidate();
}
@Override
public void invalidate()
{
this.unloadTileIC2();
super.invalidate();
}
@Override
public void onChunkUnload()
{
this.unloadTileIC2();
super.onChunkUnload();
}
@Override
public void onChunkUnload()
{
this.unloadTileIC2();
super.onChunkUnload();
}
protected void initIC()
{
if (Compatibility.isIndustrialCraft2Loaded())
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
}
protected void initIC()
{
if (Compatibility.isIndustrialCraft2Loaded())
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
}
this.isAddedToEnergyNet = true;
}
this.isAddedToEnergyNet = true;
}
private void unloadTileIC2()
{
if (this.isAddedToEnergyNet && this.worldObj != null)
{
if (Compatibility.isIndustrialCraft2Loaded())
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
}
private void unloadTileIC2()
{
if (this.isAddedToEnergyNet && this.worldObj != null)
{
if (Compatibility.isIndustrialCraft2Loaded())
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
}
this.isAddedToEnergyNet = false;
}
}
this.isAddedToEnergyNet = false;
}
}
@Override
public double demandedEnergyUnits()
{
return Math.ceil(this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.TO_IC2_RATIO);
}
@Override
public double demandedEnergyUnits()
{
return Math.ceil(this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.TO_IC2_RATIO);
}
@Override
public double injectEnergyUnits(ForgeDirection direction, double amount)
{
if (this.getInputDirections().contains(direction))
{
float convertedEnergy = (float) (amount * Compatibility.IC2_RATIO);
ElectricityPack toSend = ElectricityPack.getFromWatts(convertedEnergy, this.getVoltage());
float receive = this.receiveElectricity(direction, toSend, true);
@Override
public double injectEnergyUnits(ForgeDirection direction, double amount)
{
if (this.getInputDirections().contains(direction))
{
float convertedEnergy = (float) (amount * Compatibility.IC2_RATIO);
ElectricityPack toSend = ElectricityPack.getFromWatts(convertedEnergy, this.getVoltage());
float receive = this.receiveElectricity(direction, toSend, true);
// Return the difference, since injectEnergy returns left over energy, and
// receiveElectricity returns energy used.
return Math.round(amount - (receive * Compatibility.TO_IC2_RATIO));
}
// Return the difference, since injectEnergy returns left over energy, and
// receiveElectricity returns energy used.
return Math.round(amount - (receive * Compatibility.TO_IC2_RATIO));
}
return amount;
}
return amount;
}
@Override
public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction)
{
return receiver instanceof IEnergyTile && this.getOutputDirections().contains(direction);
}
@Override
public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction)
{
return receiver instanceof IEnergyTile && this.getOutputDirections().contains(direction);
}
@Override
public int getMaxSafeInput()
{
return Integer.MAX_VALUE;
}
@Override
public int getMaxSafeInput()
{
return Integer.MAX_VALUE;
}
/**
* BuildCraft power support
*/
public void initBuildCraft()
{
if (this.bcPowerHandler == null)
{
this.bcPowerHandler = new PowerHandler(this, this.bcBlockType);
}
this.bcPowerHandler.configure(0, this.maxInputEnergy, 0, (int) Math.ceil(this.getMaxEnergyStored() * Compatibility.BC3_RATIO));
}
/** BuildCraft power support */
public void initBuildCraft()
{
if (this.bcPowerHandler == null)
{
this.bcPowerHandler = new PowerHandler(this, this.bcBlockType);
}
this.bcPowerHandler.configure(0, this.maxInputEnergy, 0, (int) Math.ceil(this.getMaxEnergyStored() * Compatibility.BC3_RATIO));
}
@Override
public PowerReceiver getPowerReceiver(ForgeDirection side)
{
this.initBuildCraft();
return this.bcPowerHandler.getPowerReceiver();
}
@Override
public PowerReceiver getPowerReceiver(ForgeDirection side)
{
this.initBuildCraft();
return this.bcPowerHandler.getPowerReceiver();
}
@Override
public void doWork(PowerHandler workProvider)
{
@Override
public void doWork(PowerHandler workProvider)
{
}
}
@Override
public World getWorld()
{
return this.getWorldObj();
}
@Override
public World getWorld()
{
return this.getWorldObj();
}
}

View file

@ -2,22 +2,15 @@ package universalelectricity.prefab.tile;
import net.minecraftforge.common.ForgeDirection;
/**
* The interface is applied to TileEntities that can rotate.
/** The interface is applied to TileEntities that can rotate.
*
* @author Calclavia
*
*/
* @author Calclavia */
public interface IRotatable
{
/**
* @return Gets the facing direction. Always returns the front side of the block.
*/
public ForgeDirection getDirection();
/** @return Gets the facing direction. Always returns the front side of the block. */
public ForgeDirection getDirection();
/**
* @param Sets the facing direction.
*/
public void setDirection(ForgeDirection direection);
/** @param Sets the facing direction. */
public void setDirection(ForgeDirection direction);
}

View file

@ -12,7 +12,6 @@ import cpw.mods.fml.relauncher.SideOnly;
import dark.core.client.renders.RenderBlockWire;
import dark.core.common.CommonProxy;
import dark.core.common.CoreRecipeLoader;
import dark.core.common.DarkMain;
import dark.core.common.transmit.TileEntityWire;
import dark.core.prefab.ModPrefab;

View file

@ -50,11 +50,11 @@ import dark.core.common.items.ItemTools;
import dark.core.common.items.ItemWrench;
import dark.core.common.transmit.BlockWire;
import dark.core.network.PacketHandler;
import dark.core.prefab.BlockMulti;
import dark.core.prefab.ModPrefab;
import dark.core.prefab.TileEntityMulti;
import dark.core.prefab.helpers.FluidHelper;
import dark.core.prefab.items.ItemBlockHolder;
import dark.core.prefab.machine.BlockMulti;
import dark.core.prefab.machine.TileEntityMulti;
/** @author HangCow, DarkGuardsman */
@Mod(modid = DarkMain.MOD_ID, name = DarkMain.MOD_NAME, version = DarkMain.VERSION, dependencies = "after:BuildCraft|Energy", useMetadata = true)

View file

@ -14,9 +14,9 @@ import net.minecraftforge.common.Configuration;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.core.common.DarkMain;
import dark.core.prefab.BlockMachine;
import dark.core.prefab.IExtraObjectInfo;
import dark.core.prefab.helpers.Pair;
import dark.core.prefab.machine.BlockMachine;
public class BlockDebug extends BlockMachine implements IExtraObjectInfo
{

View file

@ -3,7 +3,7 @@ package dark.core.common.machines;
import net.minecraft.block.material.Material;
import net.minecraftforge.common.Configuration;
import dark.core.common.DarkMain;
import dark.core.prefab.BlockMachine;
import dark.core.prefab.machine.BlockMachine;
public class BlockGenerator extends BlockMachine
{

View file

@ -17,9 +17,9 @@ import universalelectricity.core.block.IConductor;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.tile.TileEntityConductor;
import dark.core.common.DarkMain;
import dark.core.prefab.BlockMachine;
import dark.core.prefab.IExtraObjectInfo;
import dark.core.prefab.helpers.Pair;
import dark.core.prefab.machine.BlockMachine;
public class BlockWire extends BlockMachine implements IExtraObjectInfo
{

View file

@ -8,9 +8,9 @@ import net.minecraft.util.MovingObjectPosition;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
import dark.core.common.DarkMain;
import dark.core.prefab.TileEntityMachine;
import dark.core.prefab.machine.TileEntityEnergyMachine;
public class TileEntityLaserEmitter extends TileEntityMachine
public class TileEntityLaserEmitter extends TileEntityEnergyMachine
{
/** Is tile set up to receive power */
private boolean receiver = false;
@ -45,7 +45,7 @@ public class TileEntityLaserEmitter extends TileEntityMachine
public void updateEntity()
{
super.updateEntity();
if (this.running)
if (this.isFunctioning())
{
this.updateRotation();
if (this.linkedEmitter != null && ticks % 20 == 0)

View file

@ -1,535 +0,0 @@
package dark.core.prefab;
import ic2.api.item.ISpecialElectricItem;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;
import thermalexpansion.api.item.IChargeableItem;
import universalelectricity.compatibility.TileEntityUniversalElectrical;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.core.item.IItemElectric;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.api.IDisableable;
import dark.api.energy.IPowerLess;
import dark.core.common.DarkMain;
import dark.core.common.ExternalModHandler;
import dark.core.interfaces.IExternalInv;
import dark.core.interfaces.IInvBox;
import dark.core.network.PacketHandler;
import dark.core.prefab.invgui.InvChest;
/** Prefab for most machines in the CoreMachine set. Provides basic power updates, packet updates,
* inventory handling, and other handy methods.
*
* @author DarkGuardsman */
public abstract class TileEntityMachine extends TileEntityUniversalElectrical implements ISidedInventory, IExternalInv, IDisableable, IPacketReceiver, IPowerLess
{
//TODO add support for attaching multi-meter to side of machine
public int playersUsingMachine = 0;
/** Forge Ore Directory name of the item to toggle infinite power mode */
public static String powerToggleItemID = "battery";
/** ticks to act dead, disabled, or not function at all */
protected int ticksDisabled = 0;
protected float WATTS_PER_TICK, MAX_WATTS;
protected boolean unpowered = false, running = false, prevRunning = false, hasGUI = false;
/** Inventory manager used by this machine */
protected IInvBox inventory;
/** Default generic packet types used by all machines */
public static enum TilePacketTypes
{
/** Normal packet data of any kind */
GENERIC("generic"),
/** Power updates */
POWER("isRunning"),
/** GUI display data update */
GUI("guiGeneral"),
/** Full tile read/write data from tile NBT */
NBT("nbtAll");
public String name;
private TilePacketTypes(String name)
{
this.name = name;
}
}
public TileEntityMachine()
{
}
public TileEntityMachine(float wattsPerTick)
{
this.WATTS_PER_TICK = wattsPerTick;
this.MAX_WATTS = wattsPerTick * 20;
}
public TileEntityMachine(float wattsPerTick, float maxEnergy)
{
this.WATTS_PER_TICK = wattsPerTick;
this.MAX_WATTS = maxEnergy;
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
boolean prevRun = this.running;
this.running = this.canRun() && this.consumePower(this.WATTS_PER_TICK, true);
if (prevRun != this.running)
{
this.sendPowerUpdate();
}
if (this.hasGUI && this.getContainer() != null && this.ticks % 5 == 0)
{
this.playersUsingMachine = 0;
for (Object entity : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(xCoord - 10, yCoord - 10, zCoord - 10, xCoord + 10, yCoord + 10, zCoord + 10)))
{
if (entity instanceof EntityPlayer && ((EntityPlayer) entity).openContainer.getClass().equals(this.getContainer()))
{
this.playersUsingMachine += 1;
this.sendGUIPacket(((EntityPlayer) entity));
}
}
}
}
if (this.ticksDisabled > 0)
{
this.ticksDisabled--;
this.whileDisable();
}
}
public void doPowerDebug()
{
System.out.println("\n CanRun: " + this.canRun());
System.out.println(" RedPower: " + this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord));
System.out.println(" IsDisabled: " + this.isDisabled());//TODO i'm going to kick myself if this is it, yep disabled
System.out.println(" HasPower: " + this.consumePower(WATTS_PER_TICK, false));
System.out.println(" IsRunning: " + this.running);
}
/** Called to consume power from the internal storage */
public boolean consumePower(float watts, boolean doDrain)
{
if (!this.runPowerLess() && this.getEnergyStored() >= watts)
{
if (doDrain)
{
this.setEnergyStored(this.getEnergyStored() - watts);
}
return true;
}
return this.runPowerLess();
}
/** Does this tile have power to run and do work */
public boolean canRun()
{
return !this.isDisabled() && (this.runPowerLess() || this.consumePower(this.WATTS_PER_TICK, false));
}
@Override
public boolean runPowerLess()
{
return this.unpowered || ExternalModHandler.runPowerLess();
}
@Override
public void setPowerLess(boolean bool)
{
this.unpowered = bool;
}
public void togglePowerMode()
{
this.setPowerLess(!this.runPowerLess());
}
/** Called when a player activates the tile's block */
public boolean onPlayerActivated(EntityPlayer player)
{
if (player != null && player.capabilities.isCreativeMode)
{
ItemStack itemStack = player.getHeldItem();
if (itemStack != null)
{
for (ItemStack stack : OreDictionary.getOres(TileEntityMachine.powerToggleItemID))
{
if (stack.isItemEqual(itemStack))
{
this.togglePowerMode();
return true;
}
}
}
}
return false;
}
@Override
public float receiveElectricity(ForgeDirection from, ElectricityPack receive, boolean doReceive)
{
if (!this.runPowerLess() && receive != null && this.canConnect(from))
{
if (receive != null && receive.voltage > (Math.sqrt(2) * this.getVoltage()) && this.worldObj.rand.nextBoolean())
{
if (doReceive)
{
this.onDisable(20 + this.worldObj.rand.nextInt(100));
}
return 0;
}
return super.receiveElectricity(from, receive, doReceive);
}
return 0;
}
@Override
public float getRequest(ForgeDirection direction)
{
return Math.max(this.getMaxEnergyStored() - this.getEnergyStored(), 0);
}
@Override
public float getProvide(ForgeDirection direction)
{
return 0;
}
@Override
public float getMaxEnergyStored()
{
return this.MAX_WATTS;
}
/** Called every tick while this tile entity is disabled. */
protected void whileDisable()
{
if (worldObj.isRemote)
{
this.renderSparks();
}
}
@SideOnly(Side.CLIENT)
private void renderSparks()
{
//TODO render sparks or call to a client-proxy method to render sparks around the block correctly
}
@Override
public void onDisable(int duration)
{
this.ticksDisabled = duration;
}
@Override
public boolean isDisabled()
{
return this.ticksDisabled > 0;
}
@Override
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
boolean packetSize = true;
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(packet.data);
DataInputStream dis = new DataInputStream(bis);
int id = dis.readInt();
int x = dis.readInt();
int y = dis.readInt();
int z = dis.readInt();
String pId = dis.readUTF();
this.simplePacket(pId, dis, player);
/** DEBUG PACKET SIZE AND INFO */
if (packetSize)
{
System.out.println("Tile>" + this.toString() + ">>>Debug>>Packet" + pId + ">>Size>>bytes>>" + packet.data.length);
}
}
catch (Exception e)
{
System.out.println("Error Reading Packet for a TileEntityAssembly");
e.printStackTrace();
}
}
/** Handles reduced data from the main packet method
*
* @param id - packet ID
* @param dis - data
* @param player - player
* @return true if the packet was used */
public boolean simplePacket(String id, DataInputStream dis, EntityPlayer player)
{
try
{
if (this.worldObj.isRemote)
{
if (id.equalsIgnoreCase(TilePacketTypes.POWER.name))
{
this.running = dis.readBoolean();
return true;
}
if (id.equalsIgnoreCase(TilePacketTypes.NBT.name))
{
this.readFromNBT(Packet.readNBTTagCompound(dis));
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/** NetworkMod channel name */
public String getChannel()
{
return DarkMain.CHANNEL;
}
/** Sends a simple true/false am running power update */
public void sendPowerUpdate()
{
if (!this.worldObj.isRemote)
{
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, TilePacketTypes.POWER.name, this.running), worldObj, new Vector3(this), 64);
}
}
/** Sends the tileEntity save data to the client */
public void sendNBTPacket()
{
if (!this.worldObj.isRemote)
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, TilePacketTypes.NBT.name, tag), worldObj, new Vector3(this), 64);
}
}
public void sendGUIPacket(EntityPlayer entity)
{
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return PacketHandler.instance().getPacket(this.getChannel(), this, TilePacketTypes.NBT.name, tag);
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.ticksDisabled = nbt.getInteger("disabledTicks");
this.unpowered = nbt.getBoolean("shouldPower");
this.running = nbt.getBoolean("isRunning");
if (nbt.hasKey("wattsReceived"))
{
this.energyStored = (float) nbt.getDouble("wattsReceived");
}
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setInteger("disabledTicks", this.ticksDisabled);
nbt.setBoolean("shouldPower", this.unpowered);
nbt.setBoolean("isRunning", this.running);
}
/*--------------------------------------------------------------
* IInventory stuff
* ------------------------------------------------------------- */
@Override
public IInvBox getInventory()
{
if (inventory == null)
{
inventory = new InvChest(this, 1);
}
return inventory;
}
@Override
public int getSizeInventory()
{
return this.getInventory().getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int i)
{
return this.getInventory().getStackInSlot(i);
}
@Override
public ItemStack decrStackSize(int i, int j)
{
return this.getInventory().decrStackSize(i, j);
}
@Override
public ItemStack getStackInSlotOnClosing(int i)
{
return this.getInventory().getStackInSlotOnClosing(i);
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
this.getInventory().setInventorySlotContents(i, itemstack);
}
@Override
public String getInvName()
{
return this.getInventory().getInvName();
}
@Override
public boolean isInvNameLocalized()
{
return this.getInventory().isInvNameLocalized();
}
@Override
public int getInventoryStackLimit()
{
return this.getInventory().getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
return this.getInventory().isUseableByPlayer(entityplayer);
}
@Override
public void openChest()
{
this.getInventory().openChest();
this.playersUsingMachine++;
}
@Override
public void closeChest()
{
this.getInventory().closeChest();
this.playersUsingMachine--;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return this.getInventory().isItemValidForSlot(i, itemstack);
}
@Override
public int[] getAccessibleSlotsFromSide(int var1)
{
return this.getInventory().getAccessibleSlotsFromSide(var1);
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canInsertItem(i, itemstack, j);
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
return this.getInventory().canExtractItem(i, itemstack, j);
}
@Override
public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
@Override
public boolean canRemove(ItemStack stack, int slot, ForgeDirection side)
{
return false;
}
public Class<? extends Container> getContainer()
{
return null;
}
public boolean isBattery(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
return true;
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
if (electricItem.canProvideEnergy(itemStack))
{
return true;
}
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
return true;
}
}
return false;
}
}

View file

@ -5,45 +5,6 @@ package dark.core.prefab.helpers;
* @author DarkGuardsman * */
public class MetaGroup
{
/** Gets minecraft style facing direction base
*
* @param metaData - block metadata based on 4 meta rotation
* @return 2,5,3,4 */
public static int getFacingMeta(int metaData)
{
int meta = metaData % 4;
int newMeta = 0;
switch (meta)
{
case 0:
newMeta = 2;
break;
case 1:
newMeta = 5;
break;
case 2:
newMeta = 3;
break;
case 3:
newMeta = 4;
}
return newMeta;
}
/** Gets the block's group */
public static int getGrouping(int meta)
{
return meta % 4;
}
/** Gets the starting meta of a group
*
* @param grouping - 4 meta group base
* @return metadata */
public static int getGroupStartMeta(int grouping)
{
return grouping * 4;
}
}

View file

@ -19,7 +19,6 @@ import universalelectricity.core.vector.Vector2;
import universalelectricity.prefab.vector.Region2;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.core.common.DarkMain;
import dark.core.interfaces.IScroll;
import dark.core.prefab.ModPrefab;
import dark.core.prefab.access.UserAccess;

View file

@ -1,4 +1,4 @@
package dark.core.prefab;
package dark.core.prefab.machine;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
@ -78,4 +78,5 @@ public abstract class BlockMachine extends BlockTile implements ITileEntityProvi
return super.createTileEntity(world, metadata);
}
}

View file

@ -1,4 +1,4 @@
package dark.core.prefab;
package dark.core.prefab.machine;
import java.util.Random;

View file

@ -0,0 +1,608 @@
package dark.core.prefab.machine;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.energy.tile.IEnergySource;
import ic2.api.energy.tile.IEnergyTile;
import ic2.api.item.IElectricItemManager;
import ic2.api.item.ISpecialElectricItem;
import java.util.EnumSet;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.oredict.OreDictionary;
import thermalexpansion.api.item.IChargeableItem;
import universalelectricity.compatibility.Compatibility;
import universalelectricity.core.block.IElectrical;
import universalelectricity.core.block.IElectricalStorage;
import universalelectricity.core.electricity.ElectricityHelper;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.core.grid.IElectricityNetwork;
import universalelectricity.core.item.ElectricItemHelper;
import universalelectricity.core.item.IItemElectric;
import universalelectricity.core.vector.Vector3;
import universalelectricity.core.vector.VectorHelper;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
import dark.api.energy.IPowerLess;
import dark.core.common.ExternalModHandler;
/** Basic energy tile that can consume power
*
* Based off both UE universal electrical tile, and electrical tile prefabs
*
* @author DarkGuardsman */
public class TileEntityEnergyMachine extends TileEntityMachine implements IElectrical, IElectricalStorage, IEnergySink, IEnergySource, IPowerReceptor, IPowerLess
{
/** Forge Ore Directory name of the item to toggle infinite power mode */
public static String powerToggleItemID = "battery";
protected float WATTS_PER_TICK, MAX_WATTS, maxInputEnergy = 100, energyStored = 0;
protected boolean isAddedToEnergyNet, consumeEnergy = true;
public PowerHandler bcPowerHandler;
public Type bcBlockType = Type.MACHINE;
public TileEntityEnergyMachine()
{
}
public TileEntityEnergyMachine(float wattsPerTick)
{
this.WATTS_PER_TICK = wattsPerTick;
this.MAX_WATTS = wattsPerTick * 20;
}
public TileEntityEnergyMachine(float wattsPerTick, float maxEnergy)
{
this.WATTS_PER_TICK = wattsPerTick;
this.MAX_WATTS = maxEnergy;
}
/** Called to consume power from the internal storage */
public boolean consumePower(float watts, boolean doDrain)
{
if (!this.runPowerLess() && this.getEnergyStored() >= watts)
{
if (doDrain)
{
this.setEnergyStored(this.getEnergyStored() - watts);
}
return true;
}
return this.runPowerLess();
}
/** Does this tile have power to run and do work */
public boolean canFunction()
{
return !this.isDisabled() && (this.runPowerLess() || this.consumePower(this.WATTS_PER_TICK, false));
}
@Override
public boolean runPowerLess()
{
return !consumeEnergy || ExternalModHandler.runPowerLess();
}
@Override
public void setPowerLess(boolean bool)
{
consumeEnergy = !bool;
}
public void togglePowerMode()
{
this.setPowerLess(!this.runPowerLess());
}
/** Called when a player activates the tile's block */
public boolean onPlayerActivated(EntityPlayer player)
{
if (player != null && player.capabilities.isCreativeMode)
{
ItemStack itemStack = player.getHeldItem();
if (itemStack != null)
{
for (ItemStack stack : OreDictionary.getOres(powerToggleItemID))
{
if (stack.isItemEqual(itemStack))
{
this.togglePowerMode();
return true;
}
}
}
}
return false;
}
/** Recharges electric item. */
public void recharge(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
this.setEnergyStored(this.getEnergyStored() - ElectricItemHelper.chargeItem(itemStack, this.getProvide(ForgeDirection.UNKNOWN)));
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
IElectricItemManager manager = electricItem.getManager(itemStack);
float energy = Math.max(this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.charge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false) * Compatibility.IC2_RATIO;
this.provideElectricity(energy, true);
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float accepted = ((IChargeableItem) itemStack.getItem()).receiveEnergy(itemStack, this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
this.provideElectricity(accepted, true);
}
}
}
/** Discharges electric item. */
public void discharge(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
this.setEnergyStored(this.getEnergyStored() + ElectricItemHelper.dischargeItem(itemStack, this.getRequest(ForgeDirection.UNKNOWN)));
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
if (electricItem.canProvideEnergy(itemStack))
{
IElectricItemManager manager = electricItem.getManager(itemStack);
float energy = Math.max(this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.discharge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false);
this.receiveElectricity(energy, true);
}
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float given = ((IChargeableItem) itemStack.getItem()).transferEnergy(itemStack, this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
this.receiveElectricity(given, true);
}
}
}
public boolean isBatteryItem(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
return true;
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
if (electricItem.canProvideEnergy(itemStack))
{
return true;
}
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
return true;
}
}
return false;
}
@Override
public void initiate()
{
super.initiate();
this.initBuildCraft();
}
@Override
public void updateEntity()
{
super.updateEntity();
// Register to the IC2 Network
if (!this.worldObj.isRemote)
{
if (!this.isAddedToEnergyNet)
{
this.initIC();
}
if (this.bcPowerHandler == null)
{
this.initBuildCraft();
}
if (Compatibility.isBuildcraftLoaded())
{
if (this.bcPowerHandler.getEnergyStored() > 0)
{
/** Cheat BuildCraft powerHandler and always empty energy inside of it. */
this.receiveElectricity(this.bcPowerHandler.getEnergyStored() * Compatibility.BC3_RATIO, true);
this.bcPowerHandler.setEnergy(0);
}
}
}
}
/** Produces energy on all sides */
public void produceAllSides()
{
if (!this.worldObj.isRemote)
{
for (ForgeDirection outputDirection : this.getOutputDirections())
{
this.produceDirection(outputDirection);
}
}
}
/** Produces energy only on the given side */
public void produceDirection(ForgeDirection side)
{
this.produceUE(side);
this.produceBuildCraft(side);
}
public void produceBuildCraft(ForgeDirection outputDirection)
{
if (!this.worldObj.isRemote && outputDirection != null && outputDirection != ForgeDirection.UNKNOWN)
{
float provide = this.getProvide(outputDirection);
if (this.getEnergyStored() >= provide && provide > 0)
{
if (Compatibility.isBuildcraftLoaded())
{
TileEntity tileEntity = new Vector3(this).modifyPositionFromSide(outputDirection).getTileEntity(this.worldObj);
if (tileEntity instanceof IPowerReceptor)
{
PowerReceiver receiver = ((IPowerReceptor) tileEntity).getPowerReceiver(outputDirection.getOpposite());
if (receiver != null)
{
float bc3Provide = provide * Compatibility.TO_BC_RATIO;
float energyUsed = Math.min(receiver.receiveEnergy(this.bcBlockType, bc3Provide, outputDirection.getOpposite()), bc3Provide);
this.provideElectricity((bc3Provide - (energyUsed * Compatibility.TO_BC_RATIO)), true);
}
}
}
}
}
}
/** IC2 Methods */
@Override
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
{
return this.getInputDirections().contains(direction);
}
@Override
public double getOfferedEnergy()
{
return this.getProvide(ForgeDirection.UNKNOWN) * Compatibility.TO_IC2_RATIO;
}
@Override
public void drawEnergy(double amount)
{
this.provideElectricity((float) amount * Compatibility.IC2_RATIO, true);
}
@Override
public void invalidate()
{
this.unloadTileIC2();
super.invalidate();
}
@Override
public void onChunkUnload()
{
this.unloadTileIC2();
super.onChunkUnload();
}
protected void initIC()
{
if (Compatibility.isIndustrialCraft2Loaded())
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
}
this.isAddedToEnergyNet = true;
}
private void unloadTileIC2()
{
if (this.isAddedToEnergyNet && this.worldObj != null)
{
if (Compatibility.isIndustrialCraft2Loaded())
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
}
this.isAddedToEnergyNet = false;
}
}
@Override
public double demandedEnergyUnits()
{
return Math.ceil(this.getRequest(ForgeDirection.UNKNOWN) * Compatibility.TO_IC2_RATIO);
}
@Override
public double injectEnergyUnits(ForgeDirection direction, double amount)
{
if (this.getInputDirections().contains(direction))
{
float convertedEnergy = (float) (amount * Compatibility.IC2_RATIO);
ElectricityPack toSend = ElectricityPack.getFromWatts(convertedEnergy, this.getVoltage());
float receive = this.receiveElectricity(direction, toSend, true);
// Return the difference, since injectEnergy returns left over energy, and
// receiveElectricity returns energy used.
return Math.round(amount - (receive * Compatibility.TO_IC2_RATIO));
}
return amount;
}
@Override
public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction)
{
return receiver instanceof IEnergyTile && this.getOutputDirections().contains(direction);
}
@Override
public int getMaxSafeInput()
{
return Integer.MAX_VALUE;
}
/** BuildCraft power support */
public void initBuildCraft()
{
if (this.bcPowerHandler == null)
{
this.bcPowerHandler = new PowerHandler(this, this.bcBlockType);
}
this.bcPowerHandler.configure(0, this.maxInputEnergy, 0, (int) Math.ceil(this.getMaxEnergyStored() * Compatibility.BC3_RATIO));
}
@Override
public PowerReceiver getPowerReceiver(ForgeDirection side)
{
this.initBuildCraft();
return this.bcPowerHandler.getPowerReceiver();
}
@Override
public void doWork(PowerHandler workProvider)
{
}
@Override
public World getWorld()
{
return this.getWorldObj();
}
/** Produces UE power towards a specific direction.
*
* @param outputDirection - The output direction. */
public void produceUE(ForgeDirection outputDirection)
{
if (!this.worldObj.isRemote && outputDirection != null && outputDirection != ForgeDirection.UNKNOWN)
{
float provide = this.getProvide(outputDirection);
if (provide > 0)
{
TileEntity outputTile = VectorHelper.getConnectorFromSide(this.worldObj, new Vector3(this), outputDirection);
IElectricityNetwork outputNetwork = ElectricityHelper.getNetworkFromTileEntity(outputTile, outputDirection);
if (outputNetwork != null)
{
ElectricityPack powerRequest = outputNetwork.getRequest(this);
if (powerRequest.getWatts() > 0)
{
ElectricityPack sendPack = ElectricityPack.min(ElectricityPack.getFromWatts(this.getEnergyStored(), this.getVoltage()), ElectricityPack.getFromWatts(provide, this.getVoltage()));
float rejectedPower = outputNetwork.produce(sendPack, this);
this.provideElectricity(sendPack.getWatts() - rejectedPower, true);
}
}
}
}
}
/** The electrical input direction.
*
* @return The direction that electricity is entered into the tile. Return null for no input. By
* default you can accept power from all sides. */
public EnumSet<ForgeDirection> getInputDirections()
{
return EnumSet.allOf(ForgeDirection.class);
}
/** The electrical output direction.
*
* @return The direction that electricity is output from the tile. Return null for no output. By
* default it will return an empty EnumSet. */
public EnumSet<ForgeDirection> getOutputDirections()
{
return EnumSet.noneOf(ForgeDirection.class);
}
@Override
public float receiveElectricity(ForgeDirection from, ElectricityPack receive, boolean doReceive)
{
if (!this.runPowerLess() && receive != null && this.canConnect(from))
{
if (receive != null && receive.voltage > (Math.sqrt(2) * this.getVoltage()) && this.worldObj.rand.nextBoolean())
{
if (doReceive)
{
this.onDisable(20 + this.worldObj.rand.nextInt(100));
}
return 0;
}
return this.receiveElectricity(receive.getWatts(), doReceive);
}
return 0;
}
@Override
public ElectricityPack provideElectricity(ForgeDirection from, ElectricityPack request, boolean doProvide)
{
if (this.getOutputDirections().contains(from))
{
return this.provideElectricity(request, doProvide);
}
return new ElectricityPack();
}
/** A non-side specific version of receiveElectricity for you to optionally use it internally. */
public float receiveElectricity(ElectricityPack receive, boolean doReceive)
{
if (receive != null)
{
float prevEnergyStored = this.getEnergyStored();
float newStoredEnergy = Math.min(this.getEnergyStored() + receive.getWatts(), this.getMaxEnergyStored());
if (doReceive)
{
this.setEnergyStored(newStoredEnergy);
}
return Math.max(newStoredEnergy - prevEnergyStored, 0);
}
return 0;
}
public float receiveElectricity(float energy, boolean doReceive)
{
return this.receiveElectricity(ElectricityPack.getFromWatts(energy, this.getVoltage()), doReceive);
}
/** A non-side specific version of provideElectricity for you to optionally use it internally. */
public ElectricityPack provideElectricity(ElectricityPack request, boolean doProvide)
{
if (request != null)
{
float requestedEnergy = Math.min(request.getWatts(), this.energyStored);
if (doProvide)
{
this.setEnergyStored(this.energyStored - requestedEnergy);
}
return ElectricityPack.getFromWatts(requestedEnergy, this.getVoltage());
}
return new ElectricityPack();
}
public ElectricityPack provideElectricity(float energy, boolean doProvide)
{
return this.provideElectricity(ElectricityPack.getFromWatts(energy, this.getVoltage()), doProvide);
}
@Override
public void setEnergyStored(float energy)
{
this.energyStored = Math.max(Math.min(energy, this.getMaxEnergyStored()), 0);
}
@Override
public float getEnergyStored()
{
return this.energyStored;
}
@Override
public boolean canConnect(ForgeDirection direction)
{
if (direction == null || direction.equals(ForgeDirection.UNKNOWN))
{
return false;
}
return this.getInputDirections().contains(direction) || this.getOutputDirections().contains(direction);
}
@Override
public float getVoltage()
{
return 0.120F;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.energyStored = nbt.getFloat("energyStored");
consumeEnergy = !nbt.getBoolean("shouldPower");
this.functioning = nbt.getBoolean("isRunning");
if (nbt.hasKey("wattsReceived"))
{
this.energyStored = (float) nbt.getDouble("wattsReceived");
}
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setBoolean("shouldPower", !consumeEnergy);
nbt.setFloat("energyStored", this.energyStored);
nbt.setBoolean("isRunning", this.functioning);
}
@Override
public float getMaxEnergyStored()
{
return this.MAX_WATTS;
}
@Override
public float getRequest(ForgeDirection direction)
{
return Math.max(this.getMaxEnergyStored() - this.getEnergyStored(), 0);
}
@Override
public float getProvide(ForgeDirection direction)
{
return 0;
}
}

View file

@ -1,6 +1,7 @@
package dark.core.prefab;
package dark.core.prefab.machine;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@ -27,6 +28,12 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
return inventory;
}
/** Gets the container class that goes with this tileEntity when creating a gui */
public Class<? extends Container> getContainer()
{
return null;
}
@Override
public int getSizeInventory()
{

View file

@ -0,0 +1,289 @@
package dark.core.prefab.machine;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.tile.IRotatable;
import com.google.common.io.ByteArrayDataInput;
import dark.api.IDisableable;
import dark.core.common.DarkMain;
import dark.core.interfaces.IExternalInv;
import dark.core.interfaces.IInvBox;
import dark.core.network.PacketHandler;
public class TileEntityMachine extends TileEntityInv implements ISidedInventory, IExternalInv, IDisableable, IPacketReceiver, IRotatable
{
protected int disabledTicks = 0, playersUsingMachine = 0;
protected boolean functioning = false, prevFunctioning = false, hasGUI = false, rotateByMetaGroup = false, canBeDisabled = false;
/** Inventory manager used by this machine */
protected IInvBox inventory;
/** Default generic packet types used by all machines */
public static enum SimplePacketTypes
{
/** Normal packet data of any kind */
GENERIC("generic"),
/** Power updates */
RUNNING("isRunning"),
/** GUI display data update */
GUI("guiGeneral"),
/** Full tile read/write data from tile NBT */
NBT("nbtAll");
public String name;
private SimplePacketTypes(String name)
{
this.name = name;
}
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
this.prevFunctioning = this.functioning;
this.functioning = this.isFunctioning();
if (prevFunctioning != this.functioning)
{
this.sendPowerUpdate();
}
if (this.hasGUI && this.getContainer() != null && this.ticks % 5 == 0)
{
this.playersUsingMachine = 0;
for (Object entity : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(xCoord - 10, yCoord - 10, zCoord - 10, xCoord + 10, yCoord + 10, zCoord + 10)))
{
if (entity instanceof EntityPlayer && ((EntityPlayer) entity).openContainer.getClass().equals(this.getContainer()))
{
this.playersUsingMachine += 1;
this.sendGUIPacket(((EntityPlayer) entity));
}
}
}
}
if (this.disabledTicks > 0)
{
this.disabledTicks--;
this.whileDisable();
}
}
/** Can this tile function, or run threw normal processes */
public boolean canFunction()
{
return !this.isDisabled();
}
public boolean isFunctioning()
{
if (this.worldObj.isRemote)
{
return this.functioning;
}
else
{
return this.canFunction();
}
}
public void doRunningDebug()
{
System.out.println("\n CanRun: " + this.canFunction());
System.out.println(" RedPower: " + this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord));
System.out.println(" IsDisabled: " + this.isDisabled());//TODO i'm going to kick myself if this is it, yep disabled
System.out.println(" IsRunning: " + this.functioning);
}
/** Called every tick while this tile entity is disabled. */
protected void whileDisable()
{
}
@Override
public ForgeDirection getDirection()
{
if (this.rotateByMetaGroup)
{
switch (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) % 4)
{
case 0:
return ForgeDirection.NORTH;
case 1:
return ForgeDirection.SOUTH;
case 2:
return ForgeDirection.SOUTH;
default:
return ForgeDirection.WEST;
}
}
return ForgeDirection.UNKNOWN;
}
@Override
public void setDirection(ForgeDirection direction)
{
if (this.rotateByMetaGroup)
{
switch (direction)
{
case NORTH:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4, 3);
case WEST:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4) + 1, 3);
case SOUTH:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4) + 2, 3);
default:
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord) / 4) + 3, 3);
}
}
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.disabledTicks = nbt.getInteger("disabledTicks");
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setInteger("disabledTicks", this.disabledTicks);
}
@Override
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
boolean packetSize = true;
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(packet.data);
DataInputStream dis = new DataInputStream(bis);
int id = dis.readInt();
int x = dis.readInt();
int y = dis.readInt();
int z = dis.readInt();
String pId = dis.readUTF();
this.simplePacket(pId, dis, player);
/** DEBUG PACKET SIZE AND INFO */
if (packetSize)
{
System.out.println("Tile>" + this.toString() + ">>>Debug>>Packet" + pId + ">>Size>>bytes>>" + packet.data.length);
}
}
catch (Exception e)
{
System.out.println("Error Reading Packet for a TileEntityAssembly");
e.printStackTrace();
}
}
/** Handles reduced data from the main packet method
*
* @param id - packet ID
* @param dis - data
* @param player - player
* @return true if the packet was used */
public boolean simplePacket(String id, DataInputStream dis, EntityPlayer player)
{
try
{
if (this.worldObj.isRemote)
{
if (id.equalsIgnoreCase(SimplePacketTypes.RUNNING.name))
{
this.functioning = dis.readBoolean();
return true;
}
if (id.equalsIgnoreCase(SimplePacketTypes.NBT.name))
{
this.readFromNBT(Packet.readNBTTagCompound(dis));
return true;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
/** Sends the tileEntity save data to the client */
public void sendNBTPacket()
{
if (!this.worldObj.isRemote)
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.NBT.name, tag), worldObj, new Vector3(this), 64);
}
}
/** Sends a simple true/false am running power update */
public void sendPowerUpdate()
{
if (!this.worldObj.isRemote)
{
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.RUNNING.name, this.functioning), worldObj, new Vector3(this), 64);
}
}
public void sendGUIPacket(EntityPlayer entity)
{
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.NBT.name, tag);
}
/** NetworkMod channel name */
public String getChannel()
{
return DarkMain.CHANNEL;
}
@Override
public void onDisable(int duration)
{
if (this.canBeDisabled)
{
this.disabledTicks = duration;
}
}
@Override
public boolean isDisabled()
{
return !this.canBeDisabled && this.disabledTicks > 0;
}
}

View file

@ -1,4 +1,4 @@
package dark.core.prefab;
package dark.core.prefab.machine;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
@ -13,6 +13,7 @@ import universalelectricity.prefab.network.IPacketReceiver;
import com.google.common.io.ByteArrayDataInput;
import dark.core.network.PacketHandler;
import dark.core.prefab.IMultiBlock;
/** This is a multiblock to be used for blocks that are bigger than one block.
*

View file

@ -20,13 +20,13 @@ import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import dark.core.network.PacketHandler;
import dark.core.prefab.TileEntityMachine;
import dark.core.prefab.access.AccessLevel;
import dark.core.prefab.access.ISpecialAccess;
import dark.core.prefab.access.UserAccess;
import dark.core.prefab.machine.TileEntityEnergyMachine;
/** @author Calclavia, DarkGuardsman */
public abstract class TileEntityTerminal extends TileEntityMachine implements ISpecialAccess, IPacketReceiver, ITerminal
public abstract class TileEntityTerminal extends TileEntityEnergyMachine implements ISpecialAccess, IPacketReceiver, ITerminal
{
public enum PacketType