summaryrefslogtreecommitdiff
path: root/src/main/java/ihl/processing/chemistry/CryogenicDistillerTileEntity.java
blob: 6a156db70cfa3f9c9979d438b12b2b8d11a20be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package ihl.processing.chemistry;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ic2.core.ContainerBase;
import ic2.core.block.invslot.InvSlot;
import ic2.core.block.invslot.InvSlotConsumableLiquid;
import ic2.core.block.invslot.InvSlotOutput;
import ihl.processing.invslots.InvSlotConsumableLiquidIHL;
import ihl.processing.metallurgy.BasicElectricMotorTileEntity;
import ihl.recipes.UniversalRecipeInput;
import ihl.recipes.UniversalRecipeManager;
import ihl.recipes.UniversalRecipeOutput;
import ihl.utils.IHLFluidTank;
import ihl.utils.IHLUtils;
import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;

public class CryogenicDistillerTileEntity extends BasicElectricMotorTileEntity implements IFluidHandler {

	public final InvSlotConsumableLiquidIHL fillInputSlotInput;
	public final InvSlotOutput fluidItemsSlot;
	public final InvSlotConsumableLiquidIHL fillInputSlotProducts;

	private IHLFluidTank fluidTankInput = new IHLFluidTank(8000);
	public IHLFluidTank fluidTankProducts = new IHLFluidTank(8000);
	protected static final UniversalRecipeManager recipeManager = new UniversalRecipeManager("cryogenicdistiller");
	protected static UniversalRecipeOutput airOutput = new UniversalRecipeOutput(new FluidStack[] {
			IHLUtils.getFluidStackWithSize("nitrogen", 781), IHLUtils.getFluidStackWithSize("oxygen", 209) }, null, 20,
			false);

	static {
		recipeManager.addRecipe(
				new UniversalRecipeInput(new FluidStack[] { IHLUtils.getFluidStackWithSize("air", 1000) }, null),
				airOutput);
	}

	public CryogenicDistillerTileEntity() {
		super();
		this.fillInputSlotInput = new InvSlotConsumableLiquidIHL(this, "fillInputSlotInput", -1, InvSlot.Access.I, 1,
				InvSlot.InvSide.BOTTOM, InvSlotConsumableLiquid.OpType.Fill);
		this.fillInputSlotProducts = new InvSlotConsumableLiquidIHL(this, "fillInputSlotProducts", -1, InvSlot.Access.I,
				1, InvSlot.InvSide.BOTTOM, InvSlotConsumableLiquid.OpType.Fill);
		this.fluidItemsSlot = new InvSlotOutput(this, "fluidCellsOutput", 2, 2);
	}

	@Override
	public String getInventoryName() {
		return "CryogenicDistiller";
	}

	@Override
	public void updateEntityServer() {
		super.updateEntityServer();
		IHLUtils.handleFluidSlotsBehaviour(fillInputSlotProducts, null, fluidItemsSlot, fluidTankProducts);
	}

	@Override
	public ItemStack getWrenchDrop(EntityPlayer player) {
		return IHLUtils.getThisModItemStack("cryogenicDistiller");
	}

	@Override
	@SideOnly(Side.CLIENT)
	public GuiScreen getGui(EntityPlayer player, boolean arg1) {
		return new CryogenicDistillerGui(new CryogenicDistillerContainer(player, this));
	}

	@Override
	public ContainerBase<?> getGuiContainer(EntityPlayer player) {
		return new CryogenicDistillerContainer(player, this);
	}

	@Override
	public void operate() {
		UniversalRecipeInput ri = CryogenicDistillerTileEntity.recipeManager.getRecipeInput(getInput());
		this.fluidTankProducts.fill(getOutput().getFluidOutputs(), true);
		this.fluidTankInput.drain(ri.getFluidInputs().get(0), true);
		TileEntity teOnTop = worldObj.getTileEntity(xCoord, yCoord + 1, zCoord);
		if (teOnTop instanceof IFluidHandler && this.fluidTankProducts.getLigthestFluid() != null) {
			IFluidHandler topFH = (IFluidHandler) teOnTop;
			if (topFH.canFill(ForgeDirection.DOWN, this.fluidTankProducts.getLigthestFluid().getFluid())) {
				FluidStack fsToDrain = this.fluidTankProducts.getLigthestFluid().copy();
				fsToDrain.amount = topFH.fill(ForgeDirection.DOWN, fsToDrain, true);
				this.fluidTankProducts.drain(fsToDrain, true);
			}
		}
		ForgeDirection orientation = ForgeDirection.getOrientation(this.getFacing());
		TileEntity teOnFront = worldObj.getTileEntity(xCoord + orientation.offsetX, yCoord,
				zCoord + orientation.offsetZ);
		if (teOnFront instanceof IFluidHandler && this.fluidTankProducts.getFluid() != null) {
			IFluidHandler frontFH = (IFluidHandler) teOnFront;
			if (frontFH.canFill(orientation, this.fluidTankProducts.getFluid().getFluid())) {
				FluidStack fsToDrain = this.fluidTankProducts.getFluid().copy();
				fsToDrain.amount = frontFH.fill(orientation, fsToDrain, true);
				this.fluidTankProducts.drain(fsToDrain, true);
			}
		}
	}

	public UniversalRecipeOutput getOutput() {
		return CryogenicDistillerTileEntity.recipeManager.getOutputFor(this.getInput());
	}

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List[] getInput() {
		return new List[] { Arrays.asList(new FluidStack[] { this.fluidTankInput.getLigthestFluid() }), null };
	}

	@Override
	public boolean canOperate() {
		UniversalRecipeOutput output = this.getOutput();
		if (output == airOutput) {
			ForgeDirection dir = ForgeDirection.getOrientation(this.getFacing()).getOpposite();
			Block block = worldObj.getBlock(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
			if (!block.isAir(worldObj, xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ)
					&& block != Blocks.air)
				return false;
		}
		return output != null && (this.fluidTankProducts.getCapacity() - this.fluidTankProducts.getFluidAmount()) > 0;
	}

	@Override
	public void onGuiClosed(EntityPlayer arg0) {
	}

	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound) {
		super.readFromNBT(nbttagcompound);
		this.fluidTankInput.readFromNBT(nbttagcompound.getCompoundTag("fluidTankInput"));
		this.fluidTankProducts.readFromNBT(nbttagcompound.getCompoundTag("fluidTankProducts"));
	}

	@Override
	public void writeToNBT(NBTTagCompound nbttagcompound) {
		super.writeToNBT(nbttagcompound);
		NBTTagCompound fluidTankInputTag = new NBTTagCompound();
		this.fluidTankInput.writeToNBT(fluidTankInputTag);
		nbttagcompound.setTag("fluidTankInput", fluidTankInputTag);
		NBTTagCompound fluidTankProductsTag = new NBTTagCompound();
		this.fluidTankProducts.writeToNBT(fluidTankProductsTag);
		nbttagcompound.setTag("fluidTankProducts", fluidTankProductsTag);
	}

	public static void addRecipe(FluidStack input, FluidStack output, FluidStack output2, boolean specialCondition) {
		if (output2 != null) {
			recipeManager.addRecipe(new UniversalRecipeInput(new FluidStack[] { input }, null),
					new UniversalRecipeOutput(new FluidStack[] { output, output2 }, null, 20, specialCondition));
		} else {
			recipeManager.addRecipe(new UniversalRecipeInput(new FluidStack[] { input }, null),
					new UniversalRecipeOutput(new FluidStack[] { output }, null, 20, specialCondition));
		}
	}

	public static Map<UniversalRecipeInput, UniversalRecipeOutput> getRecipes() {
		return recipeManager.getRecipes();
	}

	public boolean canProcess() {
		return this.energy >= this.energyConsume;
	}

	@Override
	public boolean canDrain(ForgeDirection dir, Fluid arg1) {
		return dir.equals(ForgeDirection.getOrientation(this.getFacing())) || dir.equals(ForgeDirection.UP);
	}

	@Override
	public boolean canFill(ForgeDirection dir, Fluid fluid) {
		return dir.equals(ForgeDirection.getOrientation(this.getFacing()).getOpposite());
	}

	@Override
	public FluidStack drain(ForgeDirection dir, FluidStack fstack, boolean doDrain) {
		if (this.canDrain(dir, null)) {
			return this.fluidTankProducts.drain(fstack, doDrain);
		}
		return null;
	}

	@Override
	public FluidStack drain(ForgeDirection dir, int amount, boolean doDrain) {
		if (this.canDrain(dir, null)) {
			if (dir.equals(ForgeDirection.UP)) {
				return this.fluidTankProducts.drainLightest(amount, doDrain);
			}
			return this.fluidTankProducts.drain(amount, doDrain);
		}
		return null;
	}

	@Override
	public int fill(ForgeDirection dir, FluidStack fstack, boolean doFill) {
		if (fstack != null && fstack.getFluid() != null && this.canFill(dir, fstack.getFluid())) {
			return this.fluidTankInput.fill(fstack, doFill);
		}
		return 0;
	}

	@Override
	public FluidTankInfo[] getTankInfo(ForgeDirection dir) {
		return new FluidTankInfo[] { this.fluidTankInput.getInfo(), this.fluidTankProducts.getInfo() };
	}

	@Override
	public void onNetworkEvent(EntityPlayer player, int event) {
		switch (event) {
		case 0:
			this.fluidTankProducts.setEmpty();
			break;
		}
	}

}