Wrote a quick battery box

This commit is contained in:
DarkGuardsman 2013-11-27 11:16:43 -05:00
parent b82fc60928
commit 8a29a78e9e
10 changed files with 448 additions and 101 deletions

View file

@ -91,15 +91,14 @@ public class ItemRenderFluidCan implements IItemRenderer
{
GL11.glTranslatef(0F, -1F, 0F);
}
else if (type != ItemRenderType.EQUIPPED)
{
GL11.glTranslatef(0.5F, -0.8F, 0.5F);
}
else if (type == ItemRenderType.EQUIPPED)
{
GL11.glTranslatef(0.3F, -0.7F, 0.37F);
}
else
{
GL11.glTranslatef(0.5F, -0.8F, 0.5F);
}
CAN_MODEL.render(0.0625F);
GL11.glPopMatrix();
}

View file

@ -34,7 +34,7 @@ public class CoreRecipeLoader extends RecipeLoader
public static Block blockColorSand;
public static Block blockBasalt;
public static Block blockGlowGlass;
public static Block blockSteamGen, blockSolar;
public static Block blockSteamGen, blockSolar, blockBatBox;
public static Block blockGas;
/* ITEMS */

View file

@ -55,6 +55,7 @@ import dark.core.common.items.ItemParts;
import dark.core.common.items.ItemParts.Parts;
import dark.core.common.items.ItemReadoutTools;
import dark.core.common.items.ItemWrench;
import dark.core.common.machines.BlockEnergyStorage;
import dark.core.common.machines.BlockSmallSteamGen;
import dark.core.common.machines.BlockDebug;
import dark.core.common.machines.BlockSolarPanel;
@ -279,6 +280,7 @@ public class DarkMain extends ModPrefab
CoreRecipeLoader.blockGlowGlass = ModObjectRegistry.createNewBlock("DMBlockGlowGlass", DarkMain.MOD_ID, BlockColorGlowGlass.class, ItemBlockColored.class);
CoreRecipeLoader.blockSolar = ModObjectRegistry.createNewBlock("DMBlockSolar", DarkMain.MOD_ID, BlockSolarPanel.class, ItemBlockHolder.class);
CoreRecipeLoader.blockGas = ModObjectRegistry.createNewBlock("DMBlockGas", DarkMain.MOD_ID, BlockGasOre.class, ItemBlockHolder.class);
CoreRecipeLoader.blockBatBox = ModObjectRegistry.createNewBlock("DMBlockBatBox", DarkMain.MOD_ID, BlockEnergyStorage.class, ItemBlockHolder.class);
/* ITEMS */
CoreRecipeLoader.itemTool = ModObjectRegistry.createNewItem("DMReadoutTools", DarkMain.MOD_ID, ItemReadoutTools.class, true);

View file

@ -0,0 +1,105 @@
package dark.core.common.machines;
import java.util.List;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.UniversalElectricity;
import universalelectricity.core.block.IConductor;
import universalelectricity.core.vector.Vector3;
import dark.core.common.DarkMain;
import dark.core.helpers.MathHelper;
import dark.core.prefab.machine.BlockMachine;
/** Block for energy storage devices
*
* @author Rseifert */
public class BlockEnergyStorage extends BlockMachine
{
public static final int BATTERY_BOX_METADATA = 0;
public BlockEnergyStorage()
{
super(DarkMain.CONFIGURATION, "DMEnergyStorage", UniversalElectricity.machine);
}
public Icon getBlockTexture(IBlockAccess world, int x, int y, int z, int side)
{
Vector3 vec = new Vector3(x, y, z);
int meta = vec.getBlockMetadata(world);
if (side == 0 || side == 1)
{
return this.blockIcon;
}
if(side == (meta - BlockEnergyStorage.BATTERY_BOX_METADATA + 2))
{
return this.iconOutput;
}
return vec.clone().modifyPositionFromSide(ForgeDirection.getOrientation(side)).getTileEntity(world) instanceof IConductor ? this.iconInput : this.blockIcon;
}
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack)
{
super.onBlockPlacedBy(world, x, y, z, entityLiving, itemStack);
int metadata = world.getBlockMetadata(x, y, z);
int angle = MathHelper.floor_double((entityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
world.setBlockMetadataWithNotify(x, y, z, ((metadata / 4) * 4) + angle, 3);
}
@Override
public boolean onUseWrench(World world, int x, int y, int z, EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ)
{
if (world.getBlockMetadata(x, y, z) % 4 < 3)
{
world.setBlockMetadataWithNotify(x, y, z, world.getBlockMetadata(x, y, z) + 1, 3);
return true;
}
else
{
world.setBlockMetadataWithNotify(x, y, z, world.getBlockMetadata(x, y, z) - 3, 3);
return true;
}
}
@Override
public TileEntity createTileEntity(World world, int metadata)
{
switch (metadata / 4)
{
case 0:
return new TileEntityBatteryBox();
}
return super.createTileEntity(world, metadata);
}
@Override
public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
{
par3List.add(new ItemStack(par1, 1, 0));
}
@Override
public int damageDropped(int metadata)
{
return metadata / 4;
}
@Override
public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z)
{
return new ItemStack(this, 1, (world.getBlockMetadata(x, y, z) / 4) * 4);
}
}

View file

@ -0,0 +1,130 @@
package dark.core.common.machines;
import java.util.EnumSet;
import net.minecraft.network.packet.Packet;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.block.IElectrical;
import universalelectricity.core.electricity.ElectricityHelper;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.core.grid.IElectricityNetwork;
import universalelectricity.core.vector.Vector3;
import universalelectricity.core.vector.VectorHelper;
import universalelectricity.prefab.network.PacketManager;
import cpw.mods.fml.common.registry.LanguageRegistry;
import dark.core.helpers.EnergyHelper;
import dark.core.prefab.machine.TileEntityEnergyMachine;
/** Simple in out battery box
*
* @author DarkGuardsman */
public class TileEntityBatteryBox extends TileEntityEnergyMachine
{
public TileEntityBatteryBox()
{
this.invSlots = 2;
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.isDisabled())
{
if (!this.worldObj.isRemote)
{
/** Recharges electric item. */
EnergyHelper.recharge(this.getStackInSlot(0), this);
/** Decharge electric item. */
EnergyHelper.discharge(this.getStackInSlot(1), this);
ForgeDirection outputDirection = ForgeDirection.getOrientation(this.getBlockMetadata() - BlockEnergyStorage.BATTERY_BOX_METADATA + 2);
TileEntity inputTile = VectorHelper.getConnectorFromSide(this.worldObj, new Vector3(this), outputDirection.getOpposite());
TileEntity outputTile = VectorHelper.getConnectorFromSide(this.worldObj, new Vector3(this), outputDirection);
IElectricityNetwork outputNetwork = ElectricityHelper.getNetworkFromTileEntity(outputTile, outputDirection);
IElectricityNetwork inputNetwork = ElectricityHelper.getNetworkFromTileEntity(inputTile, outputDirection.getOpposite());
if (outputNetwork != null)
{
if (inputNetwork != outputNetwork)
{
ElectricityPack powerRequest = outputNetwork.getRequest(this);
float outputWatts = Math.min(outputNetwork.getRequest(this).getWatts(), Math.min(this.getEnergyStored(), 10000));
if (powerRequest.getWatts() > 0)
{
ElectricityPack sendPack = ElectricityPack.min(ElectricityPack.getFromWatts(this.getEnergyStored(), this.getVoltage()), ElectricityPack.getFromWatts(outputWatts, this.getVoltage()));
float rejectedPower = outputNetwork.produce(sendPack, this);
this.provideElectricity(sendPack.getWatts() - rejectedPower, true);
}
}
}
else if (outputTile instanceof IElectrical)
{
float powerRequest = ((IElectrical) outputTile).getRequest(outputDirection.getOpposite());
float outputWatts = Math.min(powerRequest, Math.min(this.getEnergyStored(), 10000));
if (powerRequest > 0)
{
ElectricityPack sendPack = ElectricityPack.min(ElectricityPack.getFromWatts(this.getEnergyStored(), this.getVoltage()), ElectricityPack.getFromWatts(outputWatts, this.getVoltage()));
float rejectedPower = ((IElectrical) outputTile).receiveElectricity(outputDirection.getOpposite(), sendPack, true);
this.provideElectricity(sendPack.getWatts() - rejectedPower, true);
}
}
}
}
/** Gradually lose energy. */
this.setEnergyStored(this.getEnergyStored() - 0.00005f);
}
@Override
public boolean canConnect(ForgeDirection direction)
{
return direction == ForgeDirection.getOrientation(this.getBlockMetadata() - BlockEnergyStorage.BATTERY_BOX_METADATA + 2) || direction == ForgeDirection.getOrientation(this.getBlockMetadata() - BlockEnergyStorage.BATTERY_BOX_METADATA + 2).getOpposite();
}
@Override
public EnumSet<ForgeDirection> getOutputDirections()
{
return EnumSet.of(ForgeDirection.getOrientation(this.getBlockMetadata() - BlockEnergyStorage.BATTERY_BOX_METADATA + 2).getOpposite());
}
@Override
public Packet getDescriptionPacket()
{
return PacketManager.getPacket(this.getChannel(), this, this.getEnergyStored(), this.disabledTicks);
}
@Override
public String getInvName()
{
return LanguageRegistry.instance().getStringLocalization("tile.batterybox.name");
}
@Override
public int getInventoryStackLimit()
{
return 1;
}
@Override
public float getMaxEnergyStored()
{
return 5000;
}
@Override
public boolean isInvNameLocalized()
{
return true;
}
@Override
public int[] getAccessibleSlotsFromSide(int slotID)
{
return new int[] { 0, 1 };
}
}

View file

@ -59,12 +59,6 @@ public class TileEntitySolarPanel extends TileEntityEnergyMachine
return EnumSet.allOf(ForgeDirection.class);
}
@Override
public void discharge(ItemStack itemStack)
{
}
@Override
public float getRequest(ForgeDirection direction)
{

View file

@ -0,0 +1,92 @@
package dark.core.helpers;
import ic2.api.item.IElectricItemManager;
import ic2.api.item.ISpecialElectricItem;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
import thermalexpansion.api.item.IChargeableItem;
import universalelectricity.compatibility.Compatibility;
import universalelectricity.core.item.ElectricItemHelper;
import universalelectricity.core.item.IItemElectric;
import dark.core.prefab.machine.TileEntityEnergyMachine;
public class EnergyHelper
{
/** Recharges electric item. */
public static void recharge(ItemStack itemStack, TileEntityEnergyMachine machine)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
machine.setEnergyStored(machine.getEnergyStored() - ElectricItemHelper.chargeItem(itemStack, machine.getProvide(ForgeDirection.UNKNOWN)));
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
IElectricItemManager manager = electricItem.getManager(itemStack);
float energy = Math.max(machine.getProvide(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.charge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false) * Compatibility.IC2_RATIO;
machine.provideElectricity(energy, true);
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float accepted = ((IChargeableItem) itemStack.getItem()).receiveEnergy(itemStack, machine.getProvide(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
machine.provideElectricity(accepted, true);
}
}
}
/** Discharges electric item. */
public static void discharge(ItemStack itemStack, TileEntityEnergyMachine machine)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric)
{
machine.setEnergyStored(machine.getEnergyStored() + ElectricItemHelper.dischargeItem(itemStack, machine.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(machine.getRequest(ForgeDirection.UNKNOWN) * Compatibility.IC2_RATIO, 0);
energy = manager.discharge(itemStack, (int) (energy * Compatibility.TO_IC2_RATIO), 0, false, false);
machine.receiveElectricity(energy, true);
}
}
else if (itemStack.getItem() instanceof IChargeableItem)
{
float given = ((IChargeableItem) itemStack.getItem()).transferEnergy(itemStack, machine.getRequest(ForgeDirection.UNKNOWN) * Compatibility.BC3_RATIO, true);
machine.receiveElectricity(given, true);
}
}
}
public static boolean isBatteryItem(ItemStack itemStack)
{
if (itemStack != null)
{
if (itemStack.getItem() instanceof IItemElectric || itemStack.getItem() instanceof IChargeableItem)
{
return true;
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
if (electricItem.canProvideEnergy(itemStack))
{
return true;
}
}
}
return false;
}
}

View file

@ -30,7 +30,7 @@ import dark.core.registration.ModObjectRegistry.BlockBuildData;
/** Basic TileEntity Container class designed to be used by generic machines. It is suggested that
* each mod using this create there own basic block extending this to reduce need to use build data
* per block.
*
*
* @author Darkguardsman */
public abstract class BlockMachine extends BlockTile implements IExtraBlockInfo
{
@ -38,6 +38,8 @@ public abstract class BlockMachine extends BlockTile implements IExtraBlockInfo
public boolean zeroAnimation, zeroSound, zeroRendering;
public int guiID = -1;
public Icon iconInput, iconOutput;
public BlockMachine(BlockBuildData data)
{
super(data.config.getBlock(data.blockName, ModPrefab.getNextID()).getInt(), data.blockMaterial);
@ -61,6 +63,8 @@ public abstract class BlockMachine extends BlockTile implements IExtraBlockInfo
public void registerIcons(IconRegister iconReg)
{
this.blockIcon = iconReg.registerIcon(DarkMain.getInstance().PREFIX + "machine");
this.iconInput = iconReg.registerIcon(DarkMain.getInstance().PREFIX + "machine_input");
this.iconOutput = iconReg.registerIcon(DarkMain.getInstance().PREFIX + "machine_output");
}
@Override

View file

@ -0,0 +1,104 @@
package dark.core.prefab.machine;
import universalelectricity.core.block.IElectricalStorage;
import universalelectricity.core.electricity.ElectricityPack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
/** Basic energy bank class designed to remove most of the energy buffer code from a machine */
public class EnergyBank implements IElectricalStorage
{
protected float capacity;
protected float energyStored;
protected TileEntity tile;
public EnergyBank(int capacity)
{
this.capacity = capacity;
}
public EnergyBank(int amount, int capacity)
{
this(capacity);
this.energyStored = amount;
}
/** 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, float voltage, boolean doReceive)
{
return this.receiveElectricity(ElectricityPack.getFromWatts(energy, voltage), 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, request.voltage);
}
return new ElectricityPack();
}
public ElectricityPack provideElectricity(float energy, float voltage, boolean doProvide)
{
return this.provideElectricity(ElectricityPack.getFromWatts(energy, voltage), doProvide);
}
public EnergyBank readFromNBT(NBTTagCompound nbt)
{
this.energyStored = nbt.getFloat("energyStored");
return this;
}
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
{
nbt.setFloat("energyStored", this.energyStored);
return nbt;
}
@Override
public void setEnergyStored(float energy)
{
this.energyStored = energy;
}
@Override
public float getEnergyStored()
{
return this.energyStored;
}
@Override
public float getMaxEnergyStored()
{
return this.capacity;
}
}

View file

@ -1,8 +1,5 @@
package dark.core.prefab.machine;
import ic2.api.item.IElectricItemManager;
import ic2.api.item.ISpecialElectricItem;
import java.util.EnumSet;
import net.minecraft.entity.player.EntityPlayer;
@ -11,15 +8,11 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
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.PowerHandler;
@ -28,9 +21,9 @@ 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 abstract class TileEntityEnergyMachine extends TileEntityMachine implements IElectrical, IElectricalStorage, IPowerLess
{
@ -118,83 +111,6 @@ public abstract class TileEntityEnergyMachine extends TileEntityMachine implemen
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 || itemStack.getItem() instanceof IChargeableItem)
{
return true;
}
else if (itemStack.getItem() instanceof ISpecialElectricItem)
{
ISpecialElectricItem electricItem = (ISpecialElectricItem) itemStack.getItem();
if (electricItem.canProvideEnergy(itemStack))
{
return true;
}
}
}
return false;
}
@Override
public void updateEntity()
{
@ -223,6 +139,7 @@ public abstract class TileEntityEnergyMachine extends TileEntityMachine implemen
/** Produces energy only on the given side */
public void produceDirection(ForgeDirection outputDirection)
{
//TODO detect machines and power them if they are directly next to this machine
if (!this.worldObj.isRemote && outputDirection != null && outputDirection != ForgeDirection.UNKNOWN)
{
float provide = this.getProvide(outputDirection);
@ -248,7 +165,7 @@ public abstract class TileEntityEnergyMachine extends TileEntityMachine implemen
}
/** 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()
@ -257,7 +174,7 @@ public abstract class TileEntityEnergyMachine extends TileEntityMachine implemen
}
/** 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()