Moved everything that can use EU and FE to using joules as an internal storage unit

This commit is contained in:
malte0811 2018-04-06 17:29:26 +02:00
parent 92a114d29d
commit bd0c941ac8
13 changed files with 202 additions and 220 deletions

View file

@ -79,7 +79,7 @@ public class IWConfig {
public static class HVStuff {
@Comment({"The amount of EU a Jacobs Ladder uses per tick, sorted by size of the ladder"})
public static double[] jacobsUsageEU = {20, 50, 100};
public static double[] jacobsUsageWatt = {40, 300, 2000};
@Comment({"The damage dealt by a small Jacobs Ladder. Normal Ladders deal twice this damage, huge ones 3 times as much"})
public static float jacobsBaseDmg = 5;
@Comment({"The effect of standing somewhat close to a Marx generator discharge.",

View file

@ -16,7 +16,10 @@
package malte0811.industrialWires.blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public interface ISyncReceiver {
@SideOnly(Side.CLIENT)
void onSync(NBTTagCompound nbt);
}

View file

@ -73,19 +73,19 @@ public class TileEntityIEMotor extends TileEntityIWBase implements ITickable, IF
@Override
public void readNBT(NBTTagCompound in, boolean updatePacket) {
dir = EnumFacing.VALUES[in.getByte(DIR_TAG)];
energy.readFromNBT(in.getCompoundTag(ENERGY_TAG));
dir = EnumFacing.VALUES[in.getByte(DIRECTION)];
energy.readFromNBT(in.getCompoundTag(ENERGY));
receiver = null;
rotBuffer = in.getDouble(BUFFER_TAG);
rotBuffer = in.getDouble(BUFFER);
}
@Override
public void writeNBT(NBTTagCompound out, boolean updatePacket) {
out.setByte(DIR_TAG, (byte) dir.getIndex());
out.setByte(DIRECTION, (byte) dir.getIndex());
NBTTagCompound nbt = new NBTTagCompound();
energy.writeToNBT(nbt);
out.setTag(ENERGY_TAG, nbt);
out.setDouble(BUFFER_TAG, rotBuffer);
out.setTag(ENERGY, nbt);
out.setDouble(BUFFER, rotBuffer);
}
// Flux energy

View file

@ -29,8 +29,8 @@ import net.minecraft.util.math.BlockPos;
import javax.annotation.Nonnull;
import static malte0811.industrialWires.util.NBTKeys.BUFFER_TAG;
import static malte0811.industrialWires.util.NBTKeys.DIR_TAG;
import static malte0811.industrialWires.util.NBTKeys.BUFFER;
import static malte0811.industrialWires.util.NBTKeys.DIRECTION;
public class TileEntityMechICtoIE extends TileEntityIWBase implements IDirectionalTile, ITickable {
EnumFacing dir = EnumFacing.DOWN;
@ -68,14 +68,14 @@ public class TileEntityMechICtoIE extends TileEntityIWBase implements IDirection
@Override
public void writeNBT(NBTTagCompound out, boolean updatePacket) {
out.setByte(DIR_TAG, (byte) dir.getIndex());
out.setInteger(BUFFER_TAG, kinBuffer);
out.setByte(DIRECTION, (byte) dir.getIndex());
out.setInteger(BUFFER, kinBuffer);
}
@Override
public void readNBT(NBTTagCompound in, boolean updatePacket) {
dir = EnumFacing.VALUES[in.getByte(DIR_TAG)];
kinBuffer = in.getInteger(BUFFER_TAG);
dir = EnumFacing.VALUES[in.getByte(DIRECTION)];
kinBuffer = in.getInteger(BUFFER);
to = null;
from = null;
}

View file

@ -26,8 +26,8 @@ import net.minecraft.util.EnumFacing;
import javax.annotation.Nonnull;
import static malte0811.industrialWires.util.NBTKeys.BUFFER_TAG;
import static malte0811.industrialWires.util.NBTKeys.DIR_TAG;
import static malte0811.industrialWires.util.NBTKeys.BUFFER;
import static malte0811.industrialWires.util.NBTKeys.DIRECTION;
public class TileEntityMechIEtoIC extends TileEntityIWBase implements IDirectionalTile, IRotationAcceptor, IKineticSource {
EnumFacing dir = EnumFacing.DOWN;
@ -37,14 +37,14 @@ public class TileEntityMechIEtoIC extends TileEntityIWBase implements IDirection
@Override
public void writeNBT(NBTTagCompound out, boolean updatePacket) {
out.setByte(DIR_TAG, (byte) dir.getIndex());
out.setDouble(BUFFER_TAG, rotBuffer);
out.setByte(DIRECTION, (byte) dir.getIndex());
out.setDouble(BUFFER, rotBuffer);
}
@Override
public void readNBT(NBTTagCompound in, boolean updatePacket) {
dir = EnumFacing.VALUES[in.getByte(DIR_TAG)];
rotBuffer = in.getDouble(BUFFER_TAG);
dir = EnumFacing.VALUES[in.getByte(DIRECTION)];
rotBuffer = in.getDouble(BUFFER);
}
// Directional

View file

@ -253,7 +253,7 @@ public class TileEntityMultiblockConverter extends TileEntityIWMultiblock implem
out.setTag(PARTS, mechParts);
out.setDouble(SPEED, energyState.getSpeed());
}
out.setInteger(STRUCTURE_VERSION, structureVersion);
out.setInteger(VERSION, structureVersion);
}
@Override
@ -273,7 +273,7 @@ public class TileEntityMultiblockConverter extends TileEntityIWMultiblock implem
shouldInitWorld = true;
}
}
structureVersion = in.getInteger(STRUCTURE_VERSION);
structureVersion = in.getInteger(VERSION);
rBB = null;
aabb = null;
}

View file

@ -33,7 +33,8 @@ import malte0811.industrialWires.blocks.IHasDummyBlocksIW;
import malte0811.industrialWires.blocks.ISyncReceiver;
import malte0811.industrialWires.network.MessageTileSyncIW;
import malte0811.industrialWires.util.Beziers;
import malte0811.industrialWires.util.DualEnergyStorage;
import malte0811.industrialWires.util.ConversionUtil;
import malte0811.industrialWires.util.JouleEnergyStorage;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
@ -55,7 +56,6 @@ import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -73,7 +73,7 @@ import static malte0811.industrialWires.util.MiscUtils.interpolate;
public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickable, IHasDummyBlocksIW, ISyncReceiver,
IEnergySink, IBlockBoundsIW, IDirectionalTile, IColoredLight, IEBlockInterfaces.IPlayerInteraction {
public EnumFacing facing = EnumFacing.NORTH;
private DualEnergyStorage energy;
private JouleEnergyStorage energy;
public LadderSize size;
public Vec3d[] controls;
@ -85,7 +85,7 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
private int dummy = 0;
public int timeTillActive = -1;
private double tStep = 0;
private double consumtionEU;
private double consumtionJoule;
private boolean addedToIC2Net = false;
private int soundPhase;
private Vec3d soundPos;
@ -106,8 +106,9 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
controlControls = new Vec3d[size.arcPoints - 2][size.movementPoints];
controlMovement = new Vec3d[size.arcPoints];
int sizeId = size.ordinal();
consumtionEU = IWConfig.HVStuff.jacobsUsageEU[sizeId];
energy = new DualEnergyStorage(20 * consumtionEU, 2 * consumtionEU);
consumtionJoule = IWConfig.HVStuff.jacobsUsageWatt[sizeId];
energy = new JouleEnergyStorage(20 * consumtionJoule,
40 * consumtionJoule);
}
@Override
@ -120,7 +121,8 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
if (hasIC2&&!addedToIC2Net) {
addToIC2Net();
}
if ((controlControls[0][0] == null || timeTillActive == -1 || t >= 1) && energy.getEnergyStoredEU() >= 2 * consumtionEU) {
if ((controlControls[0][0] == null || timeTillActive == -1 || t >= 1)
&& energy.getEnergyStoredJ() >= 2 * consumtionJoule) {
for (int j = 0; j < size.movementPoints; j++) {
double y = j * (size.height + size.extraHeight) / (double) (size.movementPoints - 1) + size.innerPointOffset;
double width = widthFromHeight(y);
@ -137,9 +139,9 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
tStep = 1D / (int) (.875 * size.tickToTop + world.rand.nextInt(size.tickToTop / 4));
IndustrialWires.packetHandler.sendToAll(new MessageTileSyncIW(this, writeArcStarter()));
} else if (timeTillActive == 0 && t < 1) {
double extracted = energy.extractEU(consumtionEU, false);
if (extracted >= consumtionEU) {
energy.extractEU(consumtionEU, true);
double extracted = energy.extract(consumtionJoule, 1, true);
if (extracted >= consumtionJoule) {
energy.extract(consumtionJoule, 1, false);
} else {
timeTillActive = -1 - size.delay;
NBTTagCompound nbt = new NBTTagCompound();
@ -248,6 +250,7 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
return nbt;
}
@SideOnly(Side.CLIENT)
private void readArcStarter(NBTTagCompound nbt) {
controlControls = read2DVecArray(nbt.getTagList("ctrlCtrl", 9));
tStep = nbt.getDouble("tStep");
@ -320,6 +323,7 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
}
@Override
@SideOnly(Side.CLIENT)
public void onSync(NBTTagCompound nbt) {
if (nbt.hasKey("salt")) {
salt = nbt.getDouble("salt");
@ -419,7 +423,7 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
@Override
@Optional.Method(modid = "ic2")
public double getDemandedEnergy() {
return energy.getEURequested();
return energy.getRequested(ConversionUtil.euPerJoule());
}
@Override
@ -431,7 +435,7 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
@Override
@Optional.Method(modid = "ic2")
public double injectEnergy(EnumFacing dir, double amount, double voltage) {
return amount - energy.insertEU(amount, true);
return amount - energy.insert(amount, ConversionUtil.joulesPerEu(), true);
}
@Override
@ -445,13 +449,12 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
return !isDummy() && from == facing && capability == CapabilityEnergy.ENERGY;
}
private EnergyCap energyCap = new EnergyCap();
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
if (hasCapability(capability, facing)) {
if (capability == CapabilityEnergy.ENERGY) {
return CapabilityEnergy.ENERGY.cast(energyCap);
return CapabilityEnergy.ENERGY.cast(energy);
}
}
return null;
@ -646,37 +649,4 @@ public class TileEntityJacobsLadder extends TileEntityIEBase implements ITickabl
return name().toLowerCase();
}
}
public class EnergyCap implements IEnergyStorage {
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
return (int) energy.insertIF(maxReceive, !simulate);
}
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
return 0;
}
@Override
public int getEnergyStored() {
return (int) energy.getEnergyStoredIF();
}
@Override
public int getMaxEnergyStored() {
return (int) energy.getMaxStoredIF();
}
@Override
public boolean canExtract() {
return false;
}
@Override
public boolean canReceive() {
return true;
}
}
}

View file

@ -38,7 +38,8 @@ import malte0811.industrialWires.blocks.TileEntityIWMultiblock;
import malte0811.industrialWires.hv.IMarxTarget;
import malte0811.industrialWires.hv.MarxOreHandler;
import malte0811.industrialWires.network.MessageTileSyncIW;
import malte0811.industrialWires.util.DualEnergyStorage;
import malte0811.industrialWires.util.ConversionUtil;
import malte0811.industrialWires.util.JouleEnergyStorage;
import malte0811.industrialWires.util.MiscUtils;
import malte0811.industrialWires.wires.IC2Wiretype;
import net.minecraft.block.Block;
@ -103,8 +104,8 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
private int stageCount = 0;
public FiringState state = FiringState.CHARGING;
public Discharge dischargeData;
// Voltage=100*storedEU
private DualEnergyStorage storage = new DualEnergyStorage(50_000, 32_000);
// Voltage=10*storedJ
private JouleEnergyStorage storage = new JouleEnergyStorage(5_000, 3_200);
private boolean hasConnection;
private double[] capVoltages;
private int voltageControl = 0;
@ -126,7 +127,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
out.setInteger(TYPE, type.ordinal());
out.setInteger(STAGES, stageCount);
out.setBoolean(HAS_CONN, hasConnection);
storage.writeToNbt(out, ENERGY_TAG);
storage.writeToNbt(out, ENERGY);
NBTTagList voltages = new NBTTagList();
if (capVoltages != null) {
for (int i = 0; i < stageCount; i++) {
@ -147,7 +148,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
for (int i = 0;i<stageCount;i++) {
capVoltages[i] = voltages.getDoubleAt(i);
}
storage.readFromNBT(in.getCompoundTag(ENERGY_TAG));
storage.readFromNBT(in.getCompoundTag(ENERGY));
hasConnection = in.getBoolean(HAS_CONN);
collisionAabb = null;
renderAabb = null;
@ -223,7 +224,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
}
//charge bottom cap from storage
double setVoltage = MAX_VOLTAGE * voltageControl / 255F;
double u0 = Math.min(setVoltage, 100 * storage.getEnergyStoredEU());
double u0 = Math.min(setVoltage, 10 * storage.getEnergyStoredJ());
if (u0 < 0) {
u0 = 0;
}
@ -232,8 +233,8 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
}
double tmp = u0 - (u0 - oldBottomVoltage) * timeFactorBottom;
double energyUsed = .5 * (tmp * tmp - oldBottomVoltage * oldBottomVoltage) * CAPACITANCE;
if (energyUsed > 0 && storage.extractEU(energyUsed, false) == energyUsed) {// energyUsed can be negative when discharging the caps
storage.extractEU(energyUsed, true);
if (energyUsed > 0 && storage.extract(energyUsed, 1, true) == energyUsed) {// energyUsed can be negative when discharging the caps
storage.extract(energyUsed, 1, false);
capVoltages[0] = tmp;
} else if (energyUsed <= 0) {
capVoltages[0] = tmp;
@ -248,7 +249,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
state = FiringState.NEXT_TICK;
}
}
leftover = storage.getMaxInputIF();
leftover = storage.getMaxInPerTick();
}
private void fire() {
@ -523,7 +524,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
public int outputEnergy(int amount, boolean simulate, int energyType) {
TileEntityMarx master = master(this);
if (master!=null && amount>0) {
double ret = master.storage.insertIF(amount, master.leftover, !simulate);
double ret = master.storage.insert(amount, ConversionUtil.joulesPerIf(), simulate, master.leftover);
master.leftover -= ret;
return (int) ret;
} else {
@ -535,7 +536,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
public double insertEnergy(double eu, boolean simulate) {
TileEntityMarx master = master(this);
if (master!=null) {
double ret = master.storage.insertEU(eu, master.leftover, !simulate);
double ret = master.storage.insert(eu, ConversionUtil.joulesPerEu(), simulate, master.leftover);
master.leftover -= ret;
return eu-ret;
} else {

View file

@ -205,7 +205,7 @@ public class ClientProxy extends CommonProxy {
return ~0;
}, IndustrialWires.panelComponent);
Config.manual_doubleA.put("iwJacobsUsage", IWConfig.HVStuff.jacobsUsageEU);
Config.manual_doubleA.put("iwJacobsUsage", IWConfig.HVStuff.jacobsUsageWatt);
Config.manual_int.put("iwKeysOnRing", IWConfig.maxKeysOnRing);
m.addEntry("industrialwires.jacobs", IndustrialWires.MODID,
new ManualPages.CraftingMulti(m, "industrialwires.jacobs0", new ItemStack(IndustrialWires.jacobsLadder, 1, 0), new ItemStack(IndustrialWires.jacobsLadder, 1, 1), new ItemStack(IndustrialWires.jacobsLadder, 1, 2)),

View file

@ -253,9 +253,7 @@ public class MechPartCommutator extends MechMBPart implements IMBPartElectric {
TileEntity te = w.getTileEntity(BlockPos.ORIGIN);
if (te!=null) {
ResourceLocation loc = TileEntity.getKey(te.getClass());
if (loc!=null&&loc.equals(KINETIC_GEN_KEY)) {
return true;
}
return loc != null && loc.equals(KINETIC_GEN_KEY);
}
return false;
}
@ -267,7 +265,7 @@ public class MechPartCommutator extends MechMBPart implements IMBPartElectric {
@Override
public void disassemble(boolean failed, MechEnergy energy) {
if (IndustrialWires.ic2TeBlock!=null) {
if (!failed&&IndustrialWires.ic2TeBlock!=null) {
NBTTagCompound dummyNbt = new NBTTagCompound();
dummyNbt.setString("id", KINETIC_GEN_KEY.toString());
world.setBlockState(BlockPos.ORIGIN, IndustrialWires.ic2TeBlock.getDefaultState());

View file

@ -1,132 +0,0 @@
/*
* This file is part of Industrial Wires.
* Copyright (C) 2016-2018 malte0811
* Industrial Wires is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Industrial Wires is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
*/
package malte0811.industrialWires.util;
import net.minecraft.nbt.NBTTagCompound;
public class DualEnergyStorage {
private double storedEU;
private double maxEU;
private double maxOutEU;
private double maxInEU;
public DualEnergyStorage(double maxEU, double maxInEU, double maxOutEU) {
this(0, maxEU, maxInEU, maxOutEU);
}
public DualEnergyStorage(double storedEU, double maxEU, double maxInEU, double maxOutEU) {
this.maxEU = maxEU;
this.maxInEU = maxInEU;
this.maxOutEU = maxOutEU;
this.storedEU = storedEU;
}
public DualEnergyStorage(double maxEU, double maxIoEU) {
this(maxEU, maxIoEU, maxIoEU);
}
public DualEnergyStorage(double maxEU) {
this(maxEU, maxEU, maxEU);
}
public DualEnergyStorage(int maxIF, int maxInIF, int maxOutIF) {
this(ConversionUtil.euPerIfIdeal() * maxIF, ConversionUtil.euPerIfIdeal() * maxInIF, ConversionUtil.euPerIfIdeal() * maxOutIF);
}
public DualEnergyStorage(int maxIF, int maxIoIF) {
this(maxIF, maxIoIF, maxIoIF);
}
public DualEnergyStorage(int maxIF) {
this(maxIF, maxIF, maxIF);
}
public double extractEU(double extractMax, boolean doExtract) {
double extr = Math.min(storedEU, extractMax);
if (doExtract) {
storedEU -= extr;
}
return extr;
}
public void extractEURaw(double extract) {
storedEU -= extract;
}
public double extractIF(int extractMax, boolean doExtract) {
double eu = extractMax * ConversionUtil.euPerIfIdeal();
return ConversionUtil.ifPerEuIdeal() * extractEU(eu, doExtract);
}
public double insertEU(double insertMax, boolean doInsert) {
return insertEU(insertMax, maxInEU, doInsert);
}
public double insertEU(double insertMax, double leftover, boolean doInsert) {
double ins = Math.min(Math.min(insertMax, maxEU - storedEU), leftover);
if (doInsert) {
storedEU += ins;
}
return ins;
}
public double insertIF(int insertMax, boolean doInsert) {
return insertIF(insertMax, ConversionUtil.ifPerEuIdeal()*maxInEU, doInsert);
}
public double insertIF(int insertMax, double leftover, boolean doInsert) {
double eu = insertMax * ConversionUtil.euPerIfIdeal();
double euMax = leftover* ConversionUtil.euPerIfIdeal();
return ConversionUtil.ifPerEuIdeal() * insertEU(eu, euMax, doInsert);
}
public double getEnergyStoredEU() {
return storedEU;
}
public double getMaxStoredEU() {
return maxEU;
}
public double getEnergyStoredIF() {
return storedEU * ConversionUtil.ifPerEuIdeal();
}
public double getMaxStoredIF() {
return maxEU * ConversionUtil.ifPerEuIdeal();
}
public double getEURequested() {
return Math.min(maxInEU, maxEU - storedEU);
}
public void writeToNbt(NBTTagCompound nbtOuter, String key) {
NBTTagCompound nbt = key == null ? nbtOuter : new NBTTagCompound();
nbt.setDouble("stored", storedEU);
nbt.setDouble("maxStored", maxEU);
nbt.setDouble("maxIn", maxInEU);
nbt.setDouble("maxOut", maxOutEU);
if (key != null) {
nbtOuter.setTag(key, nbt);
}
}
public void readFromNBT(NBTTagCompound nbt) {
storedEU = nbt.getDouble("stored");
}
public double getMaxInputIF() {
return maxInEU*ConversionUtil.ifPerEuIdeal();
}
}

View file

@ -0,0 +1,142 @@
/*
* This file is part of Industrial Wires.
* Copyright (C) 2016-2018 malte0811
* Industrial Wires is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Industrial Wires is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
*/
package malte0811.industrialWires.util;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.energy.IEnergyStorage;
import static malte0811.industrialWires.util.NBTKeys.ENERGY;
import static malte0811.industrialWires.util.NBTKeys.VERSION;
public class JouleEnergyStorage implements IEnergyStorage {
private double energyStored;
private final double maxStored;
private final double maxWattIn;
private final double maxWattOut;
public JouleEnergyStorage(double energyStored, double maxStored, double maxWattIn, double maxWattOut) {
this.energyStored = energyStored;
this.maxStored = maxStored;
this.maxWattIn = maxWattIn;
this.maxWattOut = maxWattOut;
}
public JouleEnergyStorage(double maxStored, double maxWattIn, double maxWattOut) {
this(0, maxStored, maxWattIn, maxWattOut);
}
public JouleEnergyStorage(double maxStored, double maxWattIO) {
this(maxStored, maxWattIO, maxWattIO);
}
public double getEnergyStoredJ() {
return energyStored;
}
public void setEnergyStoredJ(double energyStored) {
this.energyStored = energyStored;
}
public double getMaxInPerTick() {
return maxWattIn/20;
}
public double getMaxOutPerTick() {
return maxWattOut/20;
}
//conversionFactor*amount is amount in joules
//returns amount of energy inserted/extracted
public double insert(double amount, double conversionFactor, boolean simulate) {
return insert(amount, conversionFactor, simulate, Double.POSITIVE_INFINITY);
}
public double insert(double amount, double conversionFactor, boolean simulate, double maximum) {
double joules = amount*conversionFactor;
joules = Math.min(joules, getMaxInPerTick());
joules = Math.min(joules, maxStored-energyStored);
joules = Math.min(joules, maximum);
if (!simulate) {
energyStored += joules;
}
return joules/conversionFactor;
}
public double extract(double amount, double conversionFactor, boolean simulate) {
double joules = amount*conversionFactor;
joules = Math.min(joules, getMaxOutPerTick());
joules = Math.min(joules, energyStored);
if (!simulate) {
energyStored -= joules;
}
return joules/conversionFactor;
}
public double getRequested(double conversion) {
return conversion*Math.min(getMaxInPerTick(), maxStored-energyStored);
}
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
return MathHelper.ceil(insert(maxReceive, ConversionUtil.joulesPerIf(), simulate));
}
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
return MathHelper.floor(extract(maxExtract, ConversionUtil.joulesPerIf(), simulate));
}
@Override
public int getEnergyStored() {
return (int) (ConversionUtil.ifPerJoule()*energyStored);
}
@Override
public int getMaxEnergyStored() {
return (int) (ConversionUtil.ifPerJoule()*maxStored);
}
@Override
public boolean canExtract() {
return maxWattOut>0;
}
@Override
public boolean canReceive() {
return maxWattIn>0;
}
public void writeToNbt(NBTTagCompound nbtOuter, String key) {
NBTTagCompound nbt = key == null ? nbtOuter : new NBTTagCompound();
nbt.setDouble(ENERGY, energyStored);
nbt.setByte(VERSION, (byte) 1);
if (key != null) {
nbtOuter.setTag(key, nbt);
}
}
public void readFromNBT(NBTTagCompound nbt) {
byte b = nbt.getByte(VERSION);
switch (b) {
case 0://Old EU storage
setEnergyStoredJ(nbt.getDouble("stored"));
break;
case 1:
setEnergyStoredJ(nbt.getDouble(ENERGY));
break;
}
}
}

View file

@ -18,13 +18,13 @@ package malte0811.industrialWires.util;
public final class NBTKeys {
//General
public static final String ENERGY_TAG = "energy";
public static final String BUFFER_TAG = "buffer";
public static final String DIR_TAG = "dir";
public static final String ENERGY = "energy";
public static final String BUFFER = "buffer";
public static final String DIRECTION = "dir";
public static final String TYPE = "type";
public static final String BUFFER_IN = "inBuffer";
public static final String BUFFER_OUT = "outBuffer";
public static final String STRUCTURE_VERSION = "structureVersion";
public static final String VERSION = "version";
//Control panels
public static final String HEIGHT = "height";