This commit is contained in:
Aidan C. Brady 2015-03-03 11:41:46 -05:00
parent 7800447c99
commit 562352d54c
6 changed files with 498 additions and 21 deletions

View file

@ -2,6 +2,8 @@ package mekanism.common.content.matrix;
import java.util.List;
import mekanism.api.Coord4D;
import mekanism.api.util.StackUtils;
import mekanism.common.Mekanism;
import mekanism.common.MekanismBlocks;
import mekanism.common.multiblock.MultiblockCache;
@ -60,6 +62,54 @@ public class MatrixUpdateProtocol extends UpdateProtocol<SynchronizedMatrixData>
@Override
protected void mergeCaches(List<ItemStack> rejectedItems, MultiblockCache<SynchronizedMatrixData> cache, MultiblockCache<SynchronizedMatrixData> merge)
{
List<ItemStack> rejects = StackUtils.getMergeRejects(((MatrixCache)cache).inventory, ((MatrixCache)merge).inventory);
if(!rejects.isEmpty())
{
rejectedItems.addAll(rejects);
}
StackUtils.merge(((MatrixCache)cache).inventory, ((MatrixCache)merge).inventory);
}
@Override
protected void onStructureCreated(SynchronizedMatrixData structure, int origX, int origY, int origZ, int xmin, int xmax, int ymin, int ymax, int zmin, int zmax)
{
for(Coord4D coord : innerNodes)
{
TileEntity tile = coord.getTileEntity(pointer.getWorldObj());
if(tile instanceof TileEntityInductionCell)
{
structure.cells.add(coord);
structure.storageCap += ((TileEntityInductionCell)tile).tier.MAX_ELECTRICITY;
}
else if(tile instanceof TileEntityInductionProvider)
{
structure.providers.add(coord);
structure.outputCap += ((TileEntityInductionProvider)tile).tier.OUTPUT;
}
}
}
@Override
protected void onStructureDestroyed(SynchronizedMatrixData structure)
{
for(Coord4D coord : structure.cells)
{
if(structure.electricityStored == 0)
{
break;
}
if(coord.getTileEntity(pointer.getWorldObj()) instanceof TileEntityInductionCell)
{
TileEntityInductionCell cell = (TileEntityInductionCell)coord.getTileEntity(pointer.getWorldObj());
double toAdd = Math.min(cell.getMaxEnergy()-cell.getEnergy(), structure.electricityStored);
cell.setEnergy(cell.getEnergy()+toAdd);
structure.electricityStored -= toAdd;
}
}
}
}

View file

@ -1,5 +1,9 @@
package mekanism.common.content.matrix;
import java.util.HashSet;
import java.util.Set;
import mekanism.api.Coord4D;
import mekanism.common.multiblock.SynchronizedData;
import net.minecraft.item.ItemStack;
@ -9,6 +13,14 @@ public class SynchronizedMatrixData extends SynchronizedData<SynchronizedMatrixD
public double electricityStored;
public Set<Coord4D> cells = new HashSet<Coord4D>();
public Set<Coord4D> providers = new HashSet<Coord4D>();
public double storageCap;
public double outputCap;
@Override
public ItemStack[] getInventory()
{

View file

@ -369,6 +369,8 @@ public abstract class UpdateProtocol<T extends SynchronizedData<T>>
protected void onStructureCreated(T structure, int origX, int origY, int origZ, int xmin, int xmax, int ymin, int ymax, int zmin, int zmax) {}
protected void onStructureDestroyed(T structure) {}
/**
* Runs the protocol and updates all nodes that make a part of the multiblock.
*/
@ -432,6 +434,7 @@ public abstract class UpdateProtocol<T extends SynchronizedData<T>>
//TODO someday: drop all items in rejectedItems
//TODO seriously this needs to happen soon
//TODO perhaps drop from pointer?
cache.apply((T)structureFound);
@ -447,9 +450,14 @@ public abstract class UpdateProtocol<T extends SynchronizedData<T>>
}
}
else {
for(TileEntity tileEntity : iteratedNodes)
for(TileEntityMultiblock<T> tileEntity : iteratedNodes)
{
((TileEntityMultiblock<T>)tileEntity).structure = null;
if(tileEntity.structure != null)
{
onStructureDestroyed(tileEntity.structure);
}
tileEntity.structure = null;
}
}
}

View file

@ -1,5 +1,15 @@
package mekanism.common.tile;
import ic2.api.energy.EnergyNet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergyConductor;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.energy.tile.IEnergySource;
import ic2.api.energy.tile.IEnergyTile;
import ic2.api.tile.IEnergyStorage;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.EnumSet;
@ -9,38 +19,23 @@ import mekanism.api.energy.ICableOutputter;
import mekanism.api.energy.IStrictEnergyAcceptor;
import mekanism.api.energy.IStrictEnergyStorage;
import mekanism.api.transmitters.IGridTransmitter;
import mekanism.common.Upgrade;
import mekanism.common.base.ITileNetwork;
import mekanism.common.base.IUpgradeTile;
import mekanism.common.util.MekanismUtils;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import cofh.api.energy.IEnergyHandler;
import cpw.mods.fml.common.Optional.Interface;
import cpw.mods.fml.common.Optional.InterfaceList;
import cpw.mods.fml.common.Optional.Method;
import io.netty.buffer.ByteBuf;
import cofh.api.energy.IEnergyHandler;
import ic2.api.energy.EnergyNet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergyConductor;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.energy.tile.IEnergySource;
import ic2.api.energy.tile.IEnergyTile;
import ic2.api.tile.IEnergyStorage;
@InterfaceList({
@Interface(iface = "ic2.api.energy.tile.IEnergySink", modid = "IC2"),
@Interface(iface = "ic2.api.energy.tile.IEnergySource", modid = "IC2"),
@Interface(iface = "ic2.api.tile.IEnergyStorage", modid = "IC2"),
@Interface(iface = "cofh.api.energy.IEnergyHandler", modid = "CoFHAPI|energy"),
})
public abstract class TileEntityElectricBlock extends TileEntityContainerBlock implements ITileNetwork, IStrictEnergyStorage, IEnergyHandler, IEnergySink, IEnergySource, IEnergyStorage, IStrictEnergyAcceptor, ICableOutputter
public abstract class TileEntityElectricBlock extends TileEntityContainerBlock implements IStrictEnergyStorage, IEnergyHandler, IEnergySink, IEnergySource, IEnergyStorage, IStrictEnergyAcceptor, ICableOutputter
{
/** How much energy is stored in this block. */
public double electricityStored;

View file

@ -4,14 +4,17 @@ import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import mekanism.api.energy.IStrictEnergyStorage;
import mekanism.common.Tier.InductionCellTier;
import mekanism.common.util.MekanismUtils;
import net.minecraft.nbt.NBTTagCompound;
public class TileEntityInductionCell extends TileEntityBasicBlock
public class TileEntityInductionCell extends TileEntityBasicBlock implements IStrictEnergyStorage
{
public InductionCellTier tier = InductionCellTier.BASIC;
public double electricityStored;
@Override
public void onUpdate() {}
@ -25,6 +28,7 @@ public class TileEntityInductionCell extends TileEntityBasicBlock
public void handlePacketData(ByteBuf dataStream)
{
tier = InductionCellTier.values()[dataStream.readInt()];
electricityStored = dataStream.readDouble();
super.handlePacketData(dataStream);
@ -35,6 +39,7 @@ public class TileEntityInductionCell extends TileEntityBasicBlock
public ArrayList getNetworkedData(ArrayList data)
{
data.add(tier.ordinal());
data.add(electricityStored);
super.getNetworkedData(data);
@ -47,6 +52,7 @@ public class TileEntityInductionCell extends TileEntityBasicBlock
super.readFromNBT(nbtTags);
tier = InductionCellTier.values()[nbtTags.getInteger("tier")];
electricityStored = nbtTags.getDouble("electricityStored");
}
@Override
@ -55,5 +61,24 @@ public class TileEntityInductionCell extends TileEntityBasicBlock
super.writeToNBT(nbtTags);
nbtTags.setInteger("tier", tier.ordinal());
nbtTags.setDouble("electricityStored", electricityStored);
}
@Override
public double getEnergy()
{
return electricityStored;
}
@Override
public void setEnergy(double energy)
{
electricityStored = Math.min(energy, getMaxEnergy());
}
@Override
public double getMaxEnergy()
{
return tier.MAX_ELECTRICITY;
}
}

View file

@ -1,9 +1,396 @@
package mekanism.common.tile;
public class TileEntityInductionPort extends TileEntityInductionCasing
import ic2.api.energy.EnergyNet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergyConductor;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.energy.tile.IEnergySource;
import ic2.api.energy.tile.IEnergyTile;
import ic2.api.tile.IEnergyStorage;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import mekanism.api.Coord4D;
import mekanism.api.MekanismConfig.general;
import mekanism.api.energy.ICableOutputter;
import mekanism.api.energy.IStrictEnergyAcceptor;
import mekanism.api.energy.IStrictEnergyStorage;
import mekanism.api.transmitters.IGridTransmitter;
import mekanism.common.util.MekanismUtils;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import cofh.api.energy.IEnergyHandler;
import cpw.mods.fml.common.Optional.Interface;
import cpw.mods.fml.common.Optional.InterfaceList;
import cpw.mods.fml.common.Optional.Method;
@InterfaceList({
@Interface(iface = "ic2.api.energy.tile.IEnergySink", modid = "IC2"),
@Interface(iface = "ic2.api.energy.tile.IEnergySource", modid = "IC2"),
@Interface(iface = "ic2.api.tile.IEnergyStorage", modid = "IC2"),
@Interface(iface = "cofh.api.energy.IEnergyHandler", modid = "CoFHAPI|energy"),
})
public class TileEntityInductionPort extends TileEntityInductionCasing implements IStrictEnergyStorage, IEnergyHandler, IEnergySink, IEnergySource, IEnergyStorage, IStrictEnergyAcceptor, ICableOutputter
{
public boolean ic2Registered = false;
/** false = input, true = output */
public boolean mode;
public TileEntityInductionPort()
{
super("InductionPort");
}
@Override
public void onUpdate()
{
if(!ic2Registered && MekanismUtils.useIC2())
{
register();
}
}
@Override
public boolean canUpdate()
{
return true;
}
@Method(modid = "IC2")
public void register()
{
if(!worldObj.isRemote)
{
TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord);
if(registered != this)
{
if(registered instanceof IEnergyTile)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered));
}
else if(registered == null)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
ic2Registered = true;
}
}
}
}
@Method(modid = "IC2")
public void deregister()
{
if(!worldObj.isRemote)
{
TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord);
if(registered instanceof IEnergyTile)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered));
}
}
}
public double getMaxOutput()
{
return structure != null ? structure.outputCap : 0;
}
@Override
public double getEnergy()
{
return structure != null ? structure.electricityStored : 0;
}
@Override
public void setEnergy(double energy)
{
if(structure != null)
{
structure.electricityStored = Math.max(Math.min(energy, getMaxEnergy()), 0);
MekanismUtils.saveChunk(this);
}
}
@Override
public double getMaxEnergy()
{
return structure != null ? structure.storageCap : 0;
}
@Override
public void handlePacketData(ByteBuf dataStream)
{
super.handlePacketData(dataStream);
setEnergy(dataStream.readDouble());
}
@Override
public ArrayList getNetworkedData(ArrayList data)
{
super.getNetworkedData(data);
data.add(getEnergy());
return data;
}
@Override
public void onAdded()
{
super.onAdded();
if(MekanismUtils.useIC2())
{
register();
}
}
@Override
public void onChunkUnload()
{
if(MekanismUtils.useIC2())
{
deregister();
}
super.onChunkUnload();
}
@Override
public void invalidate()
{
super.invalidate();
if(MekanismUtils.useIC2())
{
deregister();
}
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
super.readFromNBT(nbtTags);
mode = nbtTags.getBoolean("mode");
}
@Override
public void writeToNBT(NBTTagCompound nbtTags)
{
super.writeToNBT(nbtTags);
nbtTags.setBoolean("mode", mode);
}
/**
* Gets the scaled energy level for the GUI.
* @param i - multiplier
* @return scaled energy
*/
public int getScaledEnergyLevel(int i)
{
return (int)(getEnergy()*i / getMaxEnergy());
}
@Override
@Method(modid = "CoFHAPI|energy")
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate)
{
if(structure != null && mode == false)
{
double toAdd = (int)Math.min(getMaxEnergy()-getEnergy(), maxReceive* general.FROM_TE);
if(!simulate)
{
setEnergy(getEnergy() + toAdd);
}
return (int)Math.round(toAdd* general.TO_TE);
}
return 0;
}
@Override
@Method(modid = "CoFHAPI|energy")
public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate)
{
if(structure != null && mode == true)
{
double toSend = Math.min(getEnergy(), Math.min(getMaxOutput(), maxExtract* general.FROM_TE));
if(!simulate)
{
setEnergy(getEnergy() - toSend);
}
return (int)Math.round(toSend* general.TO_TE);
}
return 0;
}
@Override
@Method(modid = "CoFHAPI|energy")
public boolean canConnectEnergy(ForgeDirection from)
{
return structure != null;
}
@Override
@Method(modid = "CoFHAPI|energy")
public int getEnergyStored(ForgeDirection from)
{
return (int)Math.round(getEnergy()* general.TO_TE);
}
@Override
@Method(modid = "CoFHAPI|energy")
public int getMaxEnergyStored(ForgeDirection from)
{
return (int)Math.round(getMaxEnergy()* general.TO_TE);
}
@Override
@Method(modid = "IC2")
public int getSinkTier()
{
return 4;
}
@Override
@Method(modid = "IC2")
public int getSourceTier()
{
return 4;
}
@Override
@Method(modid = "IC2")
public void setStored(int energy)
{
setEnergy(energy*general.FROM_IC2);
}
@Override
@Method(modid = "IC2")
public int addEnergy(int amount)
{
setEnergy(getEnergy() + amount*general.FROM_IC2);
return (int)Math.round(getEnergy()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public boolean isTeleporterCompatible(ForgeDirection side)
{
return structure != null && mode == true;
}
@Override
public boolean canOutputTo(ForgeDirection side)
{
return structure != null && mode == true;
}
@Override
@Method(modid = "IC2")
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
{
return structure != null && mode == false;
}
@Override
@Method(modid = "IC2")
public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction)
{
return structure != null && mode == true && receiver instanceof IEnergyConductor;
}
@Override
@Method(modid = "IC2")
public int getStored()
{
return (int)Math.round(getEnergy()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public int getCapacity()
{
return (int)Math.round(getMaxEnergy()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public int getOutput()
{
return (int)Math.round(getMaxOutput()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public double getDemandedEnergy()
{
return (getMaxEnergy() - getEnergy())*general.TO_IC2;
}
@Override
@Method(modid = "IC2")
public double getOfferedEnergy()
{
return Math.min(getEnergy(), getMaxOutput())*general.TO_IC2;
}
@Override
public boolean canReceiveEnergy(ForgeDirection side)
{
return structure != null && mode == false;
}
@Override
@Method(modid = "IC2")
public double getOutputEnergyUnitsPerTick()
{
return getMaxOutput()*general.TO_IC2;
}
@Override
@Method(modid = "IC2")
public double injectEnergy(ForgeDirection direction, double amount, double voltage)
{
if(Coord4D.get(this).getFromSide(direction).getTileEntity(worldObj) instanceof IGridTransmitter)
{
return amount;
}
return amount-transferEnergyToAcceptor(direction, amount*general.FROM_IC2)*general.TO_IC2;
}
@Override
@Method(modid = "IC2")
public void drawEnergy(double amount)
{
setEnergy(Math.max(getEnergy() - (amount*general.FROM_IC2), 0));
}
@Override
public double transferEnergyToAcceptor(ForgeDirection side, double amount)
{
if(structure == null || mode == true)
{
return 0;
}
double toUse = Math.min(getMaxEnergy()-getEnergy(), amount);
setEnergy(getEnergy() + toUse);
return toUse;
}
}