aboutsummaryrefslogtreecommitdiff
path: root/src/Java/miscutil/enderio/conduit/GregTech
diff options
context:
space:
mode:
authorDraknyte1 <Draknyte1@hotmail.com>2016-02-27 01:09:45 +1000
committerDraknyte1 <Draknyte1@hotmail.com>2016-02-27 01:09:45 +1000
commitdccabf64c5675d34da85dda7fb8b764ae1d1becc (patch)
tree14250806641072852b0b2b332c6af6d3d5dee668 /src/Java/miscutil/enderio/conduit/GregTech
parent2d3e33a26084e068756852ee9d5de4d65ffd91ac (diff)
downloadGT5-Unofficial-dccabf64c5675d34da85dda7fb8b764ae1d1becc.tar.gz
GT5-Unofficial-dccabf64c5675d34da85dda7fb8b764ae1d1becc.tar.bz2
GT5-Unofficial-dccabf64c5675d34da85dda7fb8b764ae1d1becc.zip
Initial conduit work started based off of Mekinisms Gas Conduits. (Thanks tterag)
Diffstat (limited to 'src/Java/miscutil/enderio/conduit/GregTech')
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/AbstractGtConduit.java73
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduit.java161
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduitNetwork.java64
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java153
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/GtConduit.java265
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/GtConduitNetwork.java310
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/GtConduitRenderer.java180
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/GtOutput.java49
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/GtUtil.java69
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/IGtConduit.java12
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/ItemGtConduit.java76
-rw-r--r--src/Java/miscutil/enderio/conduit/GregTech/PacketGtLevel.java43
12 files changed, 1455 insertions, 0 deletions
diff --git a/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtConduit.java b/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtConduit.java
new file mode 100644
index 0000000000..de0fc13371
--- /dev/null
+++ b/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtConduit.java
@@ -0,0 +1,73 @@
+package miscutil.enderio.conduit.GregTech;
+
+import mekanism.api.gas.Gas;
+import mekanism.api.gas.GasStack;
+import net.minecraft.item.ItemStack;
+import net.minecraft.util.IIcon;
+import net.minecraftforge.common.util.ForgeDirection;
+import crazypants.enderio.conduit.AbstractConduitNetwork;
+import crazypants.enderio.conduit.gas.AbstractGasConduit;
+import crazypants.enderio.conduit.geom.CollidableComponent;
+
+public class AbstractGtConduit extends AbstractGasConduit{
+
+ @Override
+ public ItemStack createItem() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public AbstractConduitNetwork<?, ?> getNetwork() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public boolean setNetwork(AbstractConduitNetwork<?, ?> network) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public IIcon getTextureForState(CollidableComponent component) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public IIcon getTransmitionTextureForState(CollidableComponent component) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public int receiveGas(ForgeDirection side, GasStack stack) {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public GasStack drawGas(ForgeDirection side, int amount) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public boolean canReceiveGas(ForgeDirection side, Gas type) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean canDrawGas(ForgeDirection side, Gas type) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ protected boolean canJoinNeighbour(IGtConduit n) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+}
diff --git a/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduit.java b/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduit.java
new file mode 100644
index 0000000000..1b440aa0bb
--- /dev/null
+++ b/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduit.java
@@ -0,0 +1,161 @@
+package miscutil.enderio.conduit.GregTech;
+
+import java.util.List;
+
+import mekanism.api.gas.GasStack;
+import mekanism.api.gas.IGasHandler;
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.nbt.NBTTagCompound;
+import net.minecraftforge.common.util.ForgeDirection;
+import crazypants.enderio.conduit.AbstractConduitNetwork;
+import crazypants.enderio.conduit.ConduitUtil;
+import crazypants.enderio.conduit.ConnectionMode;
+import crazypants.enderio.conduit.RaytraceResult;
+import crazypants.enderio.tool.ToolUtil;
+import crazypants.util.BlockCoord;
+
+public abstract class AbstractGtTankConduit extends AbstractGtConduit {
+
+ protected ConduitGtTank tank = new ConduitGtTank(0);
+ protected boolean stateDirty = false;
+ protected long lastEmptyTick = 0;
+ protected int numEmptyEvents = 0;
+
+ @Override
+ public boolean onBlockActivated(EntityPlayer player, RaytraceResult res, List<RaytraceResult> all) {
+ if(player.getCurrentEquippedItem() == null) {
+ return false;
+ }
+ if(ToolUtil.isToolEquipped(player)) {
+
+ if(!getBundle().getEntity().getWorldObj().isRemote) {
+
+ if(res != null && res.component != null) {
+
+ ForgeDirection connDir = res.component.dir;
+ ForgeDirection faceHit = ForgeDirection.getOrientation(res.movingObjectPosition.sideHit);
+
+ if(connDir == ForgeDirection.UNKNOWN || connDir == faceHit) {
+
+ if(getConnectionMode(faceHit) == ConnectionMode.DISABLED) {
+ setConnectionMode(faceHit, getNextConnectionMode(faceHit));
+ return true;
+ }
+
+ BlockCoord loc = getLocation().getLocation(faceHit);
+ IGtConduit n = ConduitUtil.getConduit(getBundle().getEntity().getWorldObj(), loc.x, loc.y, loc.z, IGtConduit.class);
+ if(n == null) {
+ return false;
+ }
+ if(!canJoinNeighbour(n)) {
+ return false;
+ }
+ if(!(n instanceof AbstractGtTankConduit)) {
+ return false;
+ }
+ AbstractGtTankConduit neighbour = (AbstractGtTankConduit) n;
+ if(neighbour.getGasType() == null || getGasType() == null) {
+ GasStack type = getGasType();
+ type = type != null ? type : neighbour.getGasType();
+ neighbour.setGasTypeOnNetwork(neighbour, type);
+ setGasTypeOnNetwork(this, type);
+ }
+ return ConduitUtil.joinConduits(this, faceHit);
+ } else if(containsExternalConnection(connDir)) {
+ // Toggle extraction mode
+ setConnectionMode(connDir, getNextConnectionMode(connDir));
+ } else if(containsConduitConnection(connDir)) {
+ GasStack curGasType = null;
+ if(getTankNetwork() != null) {
+ curGasType = getTankNetwork().getGasType();
+ }
+ ConduitUtil.disconectConduits(this, connDir);
+ setGasType(curGasType);
+
+ }
+ }
+ }
+ return true;
+
+ }
+
+ return false;
+ }
+
+ private void setGasTypeOnNetwork(AbstractGtTankConduit con, GasStack type) {
+ AbstractConduitNetwork<?, ?> n = con.getNetwork();
+ if(n != null) {
+ AbstractGtTankConduitNetwork<?> network = (AbstractGtTankConduitNetwork<?>) n;
+ network.setGasType(type);
+ }
+
+ }
+
+ protected abstract boolean canJoinNeighbour(IGtConduit n);
+
+ public abstract AbstractGtTankConduitNetwork<? extends AbstractGtTankConduit> getTankNetwork();
+
+ public void setGasType(GasStack gasType) {
+ if(tank.getGas() != null && tank.getGas().isGasEqual(gasType)) {
+ return;
+ }
+ if(gasType != null) {
+ gasType = gasType.copy();
+ } else if(tank.getGas() == null) {
+ return;
+ }
+ tank.setGas(gasType);
+ stateDirty = true;
+ }
+
+ public ConduitGtTank getTank() {
+ return tank;
+ }
+
+ public GasStack getGasType() {
+ GasStack result = null;
+ if(getTankNetwork() != null) {
+ result = getTankNetwork().getGasType();
+ }
+ if(result == null) {
+ result = tank.getGas();
+ }
+ return result;
+ }
+
+ @Override
+ public boolean canOutputToDir(ForgeDirection dir) {
+ if (super.canOutputToDir(dir)) {
+ IGasHandler ext = getExternalHandler(dir);
+ return ext != null && ext.canReceiveGas(dir.getOpposite(), tank.getGasType());
+ }
+ return false;
+ }
+
+ protected abstract void updateTank();
+
+ @Override
+ public void readFromNBT(NBTTagCompound nbtRoot, short nbtVersion) {
+ super.readFromNBT(nbtRoot, nbtVersion);
+ updateTank();
+ if(nbtRoot.hasKey("tank")) {
+ GasStack gas = GasStack.readFromNBT(nbtRoot.getCompoundTag("tank"));
+ tank.setGas(gas);
+ } else {
+ tank.setGas(null);
+ }
+ }
+
+ @Override
+ public void writeToNBT(NBTTagCompound nbtRoot) {
+ super.writeToNBT(nbtRoot);
+ GasStack gt = getGasType();
+ if(GtUtil.isGasValid(gt)) {
+ updateTank();
+ gt = gt.copy();
+ gt.amount = tank.getStored();
+ nbtRoot.setTag("tank", gt.write(new NBTTagCompound()));
+ }
+ }
+
+}
diff --git a/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduitNetwork.java b/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduitNetwork.java
new file mode 100644
index 0000000000..8cd720fa18
--- /dev/null
+++ b/src/Java/miscutil/enderio/conduit/GregTech/AbstractGtTankConduitNetwork.java
@@ -0,0 +1,64 @@
+package miscutil.enderio.conduit.GregTech;
+
+import mekanism.api.gas.GasStack;
+import crazypants.enderio.conduit.AbstractConduitNetwork;
+
+public class AbstractGtTankConduitNetwork<T extends AbstractGtTankConduit> extends AbstractConduitNetwork<IGtConduit, T> {
+
+ protected GasStack gasType;
+
+ protected AbstractGtTankConduitNetwork(Class<T> cl) {
+ super(cl);
+ }
+
+ public GasStack getGasType() {
+ return gasType;
+ }
+
+ @Override
+ public Class<IGtConduit> getBaseConduitType() {
+ return IGtConduit.class;
+ }
+
+ @Override
+ public void addConduit(T con) {
+ super.addConduit(con);
+ con.setGasType(gasType);
+ }
+
+ public boolean setGasType(GasStack newType) {
+ if(gasType != null && gasType.isGasEqual(newType)) {
+ return false;
+ }
+ if(newType != null) {
+ gasType = newType.copy();
+ gasType.amount = 0;
+ } else {
+ gasType = null;
+ }
+ for (AbstractGtTankConduit conduit : conduits) {
+ conduit.setGasType(gasType);
+ }
+ return true;
+ }
+
+ public boolean canAcceptGas(GasStack acceptable) {
+ return areGassCompatable(gasType, acceptable);
+ }
+
+ public static boolean areGassCompatable(GasStack a, GasStack b) {
+ if(a == null || b == null) {
+ return true;
+ }
+ return a.isGasEqual(b);
+ }
+
+ public int getTotalVolume() {
+ int totalVolume = 0;
+ for (T con : conduits) {
+ totalVolume += con.getTank().getStored();
+ }
+ return totalVolume;
+ }
+
+}
diff --git a/src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java b/src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java
new file mode 100644
index 0000000000..d1a81f32d2
--- /dev/null
+++ b/src/Java/miscutil/enderio/conduit/GregTech/ConduitGtTank.java
@@ -0,0 +1,153 @@
+package miscutil.enderio.conduit.GregTech;
+
+import mekanism.api.gas.GasStack;
+import mekanism.api.gas.GasTank;
+import net.minecraft.nbt.NBTTagCompound;
+
+public class ConduitGtTank extends GasTank {
+
+ private int capacity;
+
+ ConduitGtTank(int capacity) {
+ super(capacity);
+ this.capacity = capacity;
+ }
+
+ public float getFilledRatio() {
+ if(getStored() <= 0) {
+ return 0;
+ }
+ if(getMaxGas() <= 0) {
+ return -1;
+ }
+ float res = (float) getStored() / getMaxGas();
+ return res;
+ }
+
+ public boolean isFull() {
+ return getStored() >= getMaxGas();
+ }
+
+ public void setAmount(int amount) {
+ if(stored != null) {
+ stored.amount = amount;
+ }
+ }
+
+ public int getAvailableSpace() {
+ return getMaxGas() - getStored();
+ }
+
+ public void addAmount(int amount) {
+ setAmount(getStored() + amount);
+ }
+
+ @Override
+ public int getMaxGas() {
+ return this.capacity;
+ }
+
+ public void setCapacity(int capacity) {
+ this.capacity = capacity;
+ if(getStored() > capacity) {
+ setAmount(capacity);
+ }
+ }
+
+ @Override
+ public int receive(GasStack resource, boolean doReceive) {
+ if(resource == null || resource.getGas().getID() < 0) {
+ return 0;
+ }
+
+ if(stored == null || stored.getGas().getID() < 0) {
+ if(resource.amount <= capacity) {
+ if(doReceive) {
+ setGas(resource.copy());
+ }
+ return resource.amount;
+ } else {
+ if(doReceive) {
+ stored = resource.copy();
+ stored.amount = capacity;
+ }
+ return capacity;
+ }
+ }
+
+ if(!stored.isGasEqual(resource)) {
+ return 0;
+ }
+
+ int space = capacity - stored.amount;
+ if(resource.amount <= space) {
+ if(doReceive) {
+ addAmount(resource.amount);
+ }
+ return resource.amount;
+ } else {
+ if(doReceive) {
+ stored.amount = capacity;
+ }
+ return space;
+ }
+
+ }
+
+ @Override
+ public GasStack draw(int maxDrain, boolean doDraw) {
+ if(stored == null || stored.getGas().getID() < 0) {
+ return null;
+ }
+ if(stored.amount <= 0) {
+ return null;
+ }
+
+ int used = maxDrain;
+ if(stored.amount < used) {
+ used = stored.amount;
+ }
+
+ if(doDraw) {
+ addAmount(-used);
+ }
+
+ GasStack drained = new GasStack(stored.getGas().getID(), used);
+
+ if(stored.amount < 0) {
+ stored.amount = 0;
+ }
+ return drained;
+ }
+
+ public String getGasName() {
+ return stored != null ? stored.getGas().getLocalizedName() : null;
+ }
+
+ public boolean containsValidGas() {
+ return GtUtil.isGasValid(stored);
+ }
+
+ public NBTTagCompound write(NBTTagCompound nbt) {
+ if(containsValidGas()) {
+ stored.write(nbt);
+ } else {
+ nbt.setBoolean("emptyGasTank", true);
+ }
+ return nbt;
+ }
+
+ public void read(NBTTagCompound nbt) {
+ if(!nbt.hasKey("emptyGasTank")) {
+ GasStack gas = GasStack.readFromNBT(nbt);
+ if(gas != null) {
+ setGas(gas);
+ }
+ }
+ }
+
+ public boolean isEmpty() {
+ return stored == null || stored.amount == 0;
+ }
+
+}
diff --git a/src/Java/miscutil/enderio/conduit/GregTech/GtConduit.java b/src/Java/miscutil/enderio/conduit/GregTech/GtConduit.java
new file mode 100644
index 0000000000..3af9f089e4
--- /dev/null
+++ b/src/Java/miscutil/enderio/conduit/GregTech/GtConduit.java
@@ -0,0 +1,265 @@
+package miscutil.enderio.conduit.GregTech;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import mekanism.api.gas.Gas;
+import mekanism.api.gas.GasStack;
+import net.minecraft.client.renderer.texture.IIconRegister;
+import net.minecraft.item.ItemStack;
+import net.minecraft.util.IIcon;
+import net.minecraft.world.World;
+import net.minecraftforge.common.util.ForgeDirection;
+import cpw.mods.fml.relauncher.Side;
+import cpw.mods.fml.relauncher.SideOnly;
+import crazypants.enderio.EnderIO;
+import crazypants.enderio.conduit.AbstractConduitNetwork;
+import crazypants.enderio.conduit.ConnectionMode;
+import crazypants.enderio.conduit.IConduit;
+import crazypants.enderio.conduit.geom.CollidableComponent;
+import crazypants.enderio.config.Config;
+import crazypants.render.IconUtil;
+import crazypants.util.BlockCoord;
+
+public class GtConduit extends AbstractGtTankConduit {
+
+ public static final int CONDUIT_VOLUME = 1000;
+
+ public static final String ICON_KEY = "enderio:gasConduit";
+ public static final String ICON_CORE_KEY = "enderio:gasConduitCore";
+ public static final String ICON_EXTRACT_KEY = "enderio:gasConduitInput";
+ public static final String ICON_INSERT_KEY = "enderio:gasConduitOutput";
+ public static final String ICON_EMPTY_EDGE = "enderio:gasConduitEdge";
+
+ static final Map<String, IIcon> ICONS = new HashMap<String, IIcon>();
+
+ @SideOnly(Side.CLIENT)
+ public static void initIcons() {
+ IconUtil.addIconProvider(new IconUtil.IIconProvider() {
+
+ @Override
+ public void registerIcons(IIconRegister register) {
+ ICONS.put(ICON_KEY, register.registerIcon(ICON_KEY));
+ ICONS.put(ICON_CORE_KEY, register.registerIcon(ICON_CORE_KEY));
+ ICONS.put(ICON_EXTRACT_KEY, register.registerIcon(ICON_EXTRACT_KEY));
+ ICONS.put(ICON_INSERT_KEY, register.registerIcon(ICON_INSERT_KEY));
+ ICONS.put(ICON_EMPTY_EDGE, register.registerIcon(ICON_EMPTY_EDGE));
+ }
+
+ @Override
+ public int getTextureType() {
+ return 0;
+ }
+
+ });
+ }
+
+ private GtConduitNetwork network;
+
+ private long ticksSinceFailedExtract = 0;
+
+ public static final int MAX_EXTRACT_PER_TICK = Config.gasConduitExtractRate;
+
+ public static final int MAX_IO_PER_TICK = Config.gasConduitMaxIoRate;
+
+ public GtConduit() {
+ updateTank();
+ }
+
+ @Override
+ public void updateEntity(World world) {
+ super.updateEntity(world);
+ if(world.isRemote) {
+ return;
+ }
+ doExtract();
+ if(stateDirty) {
+ getBundle().dirty();
+ stateDirty = false;
+ }
+ }
+
+ private void doExtract() {
+ BlockCoord loc = getLocation();
+ if(!hasConnectionMode(ConnectionMode.INPUT)) {
+ return;
+ }
+ if(network == null) {
+ return;
+ }
+
+ // assume failure, reset to 0 if we do extract
+ ticksSinceFailedExtract++;
+ if(ticksSinceFailedExtract > 25 && ticksSinceFailedExtract % 10 != 0) {
+ // after 25 ticks of failing, only check every 10 ticks
+ return;
+ }
+
+ Gas f = tank.getGas() == null ? null : tank.getGas().getGas();
+ for (ForgeDirection dir : externalConnections) {
+ if(autoExtractForDir(dir)) {
+ if(network.extractFrom(this, dir, MAX_EXTRACT_PER_TICK)) {
+ ticksSinceFailedExtract = 0;
+ }
+ }
+ }
+
+ }
+
+ @Override
+ protected void updateTank() {
+ tank.setCapacity(CONDUIT_VOLUME);
+ if(network != null) {
+ network.updateConduitVolumes();
+ }
+ }
+
+ @Override
+ public ItemStack createItem() {
+ return new ItemStack(EnderIO.itemGasConduit);
+ }
+
+ @Override
+ public AbstractConduitNetwork<?, ?> getNetwork() {
+ return network;
+ }
+
+ @Override
+ public boolean setNetwork(AbstractConduitNetwork<?, ?> network) {
+ if(network == null) {
+ this.network = null;
+ return true;
+ }
+ if(!(network instanceof GtConduitNetwork)) {
+ return false;
+ }
+
+ GtConduitNetwork n = (GtConduitNetwork) network;
+ if(tank.getGas() == null) {
+ tank.setGas(n.getGasType() == null ? null : n.getGasType().copy());
+ } else if(n.getGasType() == null) {
+ n.setGasType(tank.getGas());
+ } else if(!tank.getGas().isGasEqual(n.getGasType())) {
+ return false;
+ }
+ this.network = n;
+ return true;
+
+ }
+
+ @Override
+ public boolean canConnectToConduit(ForgeDirection direction, IConduit con) {
+ if(!super.canConnectToConduit(direction, con)) {
+ return false;
+ }
+ if(!(con instanceof GtConduit)) {
+ return false;
+ }
+ if(getGasType() != null && ((GtConduit) con).getGasType() == null) {
+ return false;
+ }
+ return GtConduitNetwork.areGassCompatable(getGasType(), ((GtConduit) con).getGasType());
+ }
+
+ @Override
+ public void setConnectionMode(ForgeDirection dir, ConnectionMode mode) {
+ super.setConnectionMode(dir, mode);
+ refreshInputs(dir);
+ }
+
+ private void refreshInputs(ForgeDirection dir) {
+ if(network == null) {
+ return;
+ }
+ GtOutput lo = new GtOutput(getLocation().getLocation(dir), dir.getOpposite());
+ network.removeInput(lo);
+ if(getConnectionMode(dir).acceptsOutput() && containsExternalConnection(dir)) {
+ network.addInput(lo);
+ }
+ }
+
+ @Override
+ public void externalConnectionAdded(ForgeDirection fromDirection) {
+ super.externalConnectionAdded(fromDirection);
+ refreshInputs(fromDirection);
+ }
+
+ @Override
+ public void externalConnectionRemoved(ForgeDirection fromDirection) {
+ super.externalConnectionRemoved(fromDirection);
+ refreshInputs(fromDirection);
+ }
+
+ @Override
+ public IIcon getTextureForState(CollidableComponent component) {
+ if(component.dir == ForgeDirection.UNKNOWN) {
+ return ICONS.get(ICON_CORE_KEY);
+ }
+ return ICONS.get(ICON_KEY);
+ }
+
+ public IIcon getTextureForInputMode() {
+ return ICONS.get(ICON_EXTRACT_KEY);
+ }
+
+ public IIcon getTextureForOutputMode() {
+ return ICONS.get(ICON_INSERT_KEY);
+ }
+
+ public IIcon getNotSetEdgeTexture() {
+ return ICONS.get(ICON_EMPTY_EDGE);
+ }
+
+ @Override
+ public IIcon getTransmitionTextureForState(CollidableComponent component) {
+ if(isActive() && tank.containsValidGas()) {
+ return tank.getGas().getGas().getIcon();
+ }
+ return null;
+ }
+
+ // ------------------------------------------- Gas API
+
+ @Override
+ public int receiveGas(ForgeDirection from, GasStack resource) {
+ if(network == null || !getConnectionMode(from).acceptsInput()) {
+ return 0;
+ }
+ return network.fill(from, resource, true);
+ }
+
+ @Override
+ public GasStack drawGas(ForgeDirection from, int maxDrain) {
+ if(network == null || !getConnectionMode(from).acceptsOutput()) {
+ return null;
+ }
+ return network.drain(from, maxDrain, true);
+ }
+
+ @Override
+ public boolean canReceiveGas(ForgeDirection from, Gas gas) {
+ if(network == null) {
+ return false;
+ }
+ return getConnectionMode(from).acceptsInput() && GtConduitNetwork.areGassCompatable(getGasType(), new GasStack(gas, 0));
+ }
+
+ @Override
+ public boolean canDrawGas(ForgeDirection from, Gas gas) {
+ if(network == null) {
+ return false;
+ }
+ return getConnectionMode(from).acceptsOutput() && GtConduitNetwork.areGassCompatable(getGasType(), new GasStack(gas, 0));
+ }
+
+ @Override
+ protected boolean canJoinNeighbour(IGtConduit n) {
+ return n instanceof GtConduit;
+ }
+
+ @Override
+ public AbstractGtTankConduitNetwork<? extends AbstractGtTankConduit> getTankNetwork() {
+ return network;
+ }
+
+}
diff --git a/src/Java/miscutil/enderio/conduit/GregTech/GtConduitNetwork.java b/src/Java/miscutil/enderio/conduit/GregTech/GtConduitNetwork.java
new file mode 100644
index 0000000000..7855547a7e
--- /dev/null
+++ b/src/Java/miscutil/enderio/conduit/GregTech/GtConduitNetwork.java
@@ -0,0 +1,310 @@
+package miscutil.enderio.conduit.GregTech;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import mekanism.api.gas.GasStack;
+import mekanism.api.gas.IGasHandler;
+import miscutil.enderio.conduit.ConduitGTHandler;
+import net.minecraft.tileentity.TileEntity;
+import net.minecraft.world.World;
+import net.minecraftforge.common.util.ForgeDirection;
+import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
+import crazypants.enderio.conduit.ConduitNetworkTickHandler;
+import crazypants.enderio.conduit.ConduitNetworkTickHandler.TickListener;
+import crazypants.enderio.conduit.IConduit;
+import crazypants.util.BlockCoord;
+
+public class GtConduitNetwork extends AbstractGtTankConduitNetwork<GtConduit> {
+
+ private final ConduitGtTank tank = new ConduitGtTank(0);
+
+ private final Set<GtOutput> outputs = new HashSet<GtOutput>();
+
+ private Iterator<GtOutput> outputIterator;
+
+ private int ticksActiveUnsynced;
+
+ private boolean lastSyncedActive = false;
+
+ private int lastSyncedVolume = -1;
+
+ private long timeAtLastApply;
+
+ private final InnerTickHandler tickHandler = new InnerTickHandler();
+
+ public GtConduitNetwork() {
+ super(GtConduit.class);
+ }
+
+ @Override
+ public Class<IGtConduit> getBaseConduitType() {
+ return IGtConduit.class;
+ }
+
+ @Override
+ public void addConduit(GtConduit con) {
+ tank.setCapacity(tank.getMaxGas() + GtConduit.CONDUIT_VOLUME);
+ if(con.getTank().containsValidGas()) {
+ tank.addAmount(con.getTank().getStored());
+ }
+ for (ForgeDirection dir : con.getExternalConnections()) {
+ if(con.getConnectionMode(dir).acceptsOutput()) {
+ outputs.add(new GtOutput(con.getLocation().getLocation(dir), dir.getOpposite()));
+ }
+ }
+ outputIterator = null;
+ super.addConduit(con);
+ }
+
+ @Override
+ public boolean setGasType(GasStack newType) {
+ if(super.setGasType(newType)) {
+
+ GasStack ft = getGasType();
+ tank.setGas(ft == null ? null : ft.copy());
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public void destroyNetwork() {
+ setConduitVolumes();
+ outputs.clear();
+ super.destroyNetwork();
+ }
+
+ private void setConduitVolumes() {
+ if(tank.containsValidGas() && !conduits.isEmpty()) {
+ GasStack gasPerConduit = tank.getGas().copy();
+ int numCons = conduits.size();
+ int leftOvers = gasPerConduit.amount % numCons;
+ gasPerConduit.amount = gasPerConduit.amount / numCons;
+
+ for (GtConduit con : conduits) {
+ GasStack f = gasPerConduit.copy();
+ if(leftOvers > 0) {
+ f.amount += 1;
+ leftOvers--;
+ }
+ con.getTank().setGas(f);
+ BlockCoord bc = con.getLocation();
+ con.getBundle().getEntity().getWorldObj().markTileEntityChunkModified(bc.x, bc.y, bc.z, con.getBundle().getEntity());
+ }
+
+ }
+ }
+
+ @Override
+ public void onUpdateEntity(IConduit conduit) {
+ World world = conduit.getBundle().getEntity().getWorldObj();
+ if(world == null) {
+ return;
+ }
+ if(world.isRemote) {
+ return;
+ }
+
+ long curTime = world.getTotalWorldTime();
+ if(curTime > 0 && curTime != timeAtLastApply) {
+ timeAtLastApply = curTime;
+ ConduitNetworkTickHandler.instance.addListener(tickHandler);
+ }
+
+ }
+
+ private void doTick() {
+ if(gasType == null || outputs.isEmpty() || !tank.containsValidGas() || tank.isEmpty()) {
+ updateActiveState();
+ return;
+ }
+
+ if(outputIterator == null || !outputIterator.hasNext()) {
+ outputIterator = outputs.iterator();
+ }
+
+ updateActiveState();
+
+ int numVisited = 0;
+ while (!tank.isEmpty() && numVisited < outputs.size()) {
+ if(!outputIterator.hasNext()) {
+ outputIterator = outputs.iterator();
+ }
+ GtOutput output = outputIterator.next();
+ if(output != null) {
+ ConduitGTHandler cont = (ConduitGTHandler) getTankContainer(output.location);
+ if(cont != null) {
+ GasStack offer = tank.getGas().copy();
+ int filled = cont.receiveGas(output.dir, offer);
+ if(filled > 0) {
+ tank.addAmount(-filled);
+
+ }
+ }
+ }
+ numVisited++;
+ }
+
+ }
+
+ private void updateActiveState() {
+ boolean isActive = tank.containsValidGas() && !tank.isEmpty();
+ if(lastSyncedActive != isActive) {
+ ticksActiveUnsynced++;
+ } else {
+ ticksActiveUnsynced = 0;
+ }
+ if(ticksActiveUnsynced >= 10 || ticksActiveUnsynced > 0 && isActive) {
+ if(!isActive) {
+ setGasType(null);
+ }
+ for (IConduit con : conduits) {
+ con.setActive(isActive);
+ }
+ lastSyncedActive = isActive;
+ ticksActiveUnsynced = 0;
+ }
+ }
+
+ public int fill(ForgeDirection from, GasStack resource, boolean doFill) {
+ if(resource == null) {
+ return 0;
+ }
+ resource.amount = Math.min(resource.amount, GtConduit.MAX_IO_PER_TICK);
+ boolean gasWasValid = tank.containsValidGas();
+ int res = tank.receive(resource, doFill);
+ if(doFill && res > 0 && gasWasValid) {
+ int vol = tank.getStored();
+ setGasType(resource);
+ tank.setAmount(vol);
+ }
+ return res;
+ }
+
+ public GasStack drain(ForgeDirection from, GasStack resource, boolean doDrain) {
+ if(resource == null || tank.isEmpty() || !tank.containsValidGas() || !GtConduitNetwork.areGassCompatable(getGasType(), resource)) {
+ return null;
+ }
+ int amount = Math.min(resource.amount, tank.getStored());
+ amount = Math.min(amount, GtConduit.MAX_IO_PER_TICK);
+ GasStack result = resource.copy();
+ result.amount = amount;
+ if(doDrain) {
+ tank.addAmount(-amount);
+ }
+ return result;
+ }
+
+ public GasStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
+ if(tank.isEmpty() || !tank.containsValidGas()) {
+ return null;
+ }
+ int amount = Math.min(maxDrain, tank.getStored());
+ GasStack result = tank.getGas().copy();
+ result.amount = amount;
+ if(doDrain) {
+ tank.addAmount(-amount);
+ }
+ return result;
+ }
+
+ public boolean extractFrom(GtConduit advancedGtConduit, ForgeDirection dir, int maxExtractPerTick) {
+
+ if(tank.isFull()) {
+ return false;
+ }
+
+ IGasHandler extTank = getTankContainer(advancedGtConduit, dir);
+ if(extTank != null) {
+ int maxExtract = Math.min(maxExtractPerTick, tank.getAvailableSpace());
+
+ if(gasType == null || !tank.containsValidGas()) {
+ GasStack drained = extTank.drawGas(dir.getOpposite(), maxExtract);
+ if(drained == null || drained.amount <= 0) {
+ return false;
+ }
+ setGasType(drained);
+ tank.setGas(drained.copy());
+ return true;
+ }
+
+ GasStack couldDrain = gasType.copy();
+ couldDrain.amount = maxExtract;
+
+ // GasStack drained = extTank.drain(dir.getOpposite(), couldDrain, true);
+ // if(drained == null || drained.amount <= 0) {
+ // return false;
+ // }
+ // tank.addAmount(drained.amount);
+
+ //Have to use this 'double handle' approach to work around an issue with TiC
+ GasStack drained = extTank.drawGas(dir.getOpposite(), maxExtract);
+ if(drained == null || drained.amount == 0) {
+ return false;
+ } else {
+ if(drained.isGasEqual(getGasType())) {
+ tank.addAmount(drained.amount);
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public IGasHandler getTankContainer(BlockCoord bc) {
+ World w = getWorld();