summaryrefslogtreecommitdiff
path: root/ihl/processing/chemistry/LabElectrolyzerTileEntity.java
blob: f8c00ad8b16fd846f765f449aa09bd910d6ae311 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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 net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
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;
import ic2.core.ContainerBase;
import ic2.core.IC2;
import ic2.core.block.invslot.InvSlot;
import ic2.core.block.invslot.InvSlotConsumableLiquid;
import ic2.core.block.invslot.InvSlotOutput;
import ihl.processing.invslots.IHLInvSlotOutput;
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;

public class LabElectrolyzerTileEntity extends BasicElectricMotorTileEntity implements IFluidHandler {
	private final static UniversalRecipeManager recipeManager = new UniversalRecipeManager("labelectrolyzer");
	public final IHLInvSlotOutput outputSlot;
	public final InvSlotConsumableLiquidIHL drainInputSlot;
	public final InvSlotConsumableLiquidIHL fillInputSlot;
	public final InvSlotConsumableLiquidIHL fillInputSlotAnodeOutput;
	public final InvSlotConsumableLiquidIHL fillInputSlotCathodeOutput;
	public final InvSlotOutput emptyFluidItemsSlot;
	private final IHLFluidTank fluidTank = new IHLFluidTank(2000);
	public final IHLFluidTank fluidTankAnodeOutput = new IHLFluidTank(8000);
	public final IHLFluidTank fluidTankCathodeOutput = new IHLFluidTank(8000);
	public short temperature = 20;

	public LabElectrolyzerTileEntity() {
		super();
		this.energyConsume *= 10;
		this.outputSlot = new IHLInvSlotOutput(this, "output", 0, 2);
		this.drainInputSlot = new InvSlotConsumableLiquidIHL(this, "drainInput", -1, InvSlot.Access.I, 1,
				InvSlot.InvSide.TOP, InvSlotConsumableLiquid.OpType.Drain);
		this.fillInputSlot = new InvSlotConsumableLiquidIHL(this, "fillInput", -1, InvSlot.Access.I, 1,
				InvSlot.InvSide.BOTTOM, InvSlotConsumableLiquid.OpType.Fill);
		this.fillInputSlotAnodeOutput = new InvSlotConsumableLiquidIHL(this, "fillInputAnodeOutput", -1,
				InvSlot.Access.I, 1, InvSlot.InvSide.BOTTOM, InvSlotConsumableLiquid.OpType.Fill);
		this.fillInputSlotCathodeOutput = new InvSlotConsumableLiquidIHL(this, "fillInputCathodeOutput", -1,
				InvSlot.Access.I, 1, InvSlot.InvSide.BOTTOM, InvSlotConsumableLiquid.OpType.Fill);
		this.emptyFluidItemsSlot = new InvSlotOutput(this, "fluidCellsOutput", 2, 3);
	}

	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound) {
		super.readFromNBT(nbttagcompound);
		this.fluidTank.readFromNBT(nbttagcompound.getCompoundTag("fluidTank"));
		this.fluidTankAnodeOutput.readFromNBT(nbttagcompound.getCompoundTag("fluidTankAnodeOutput"));
		this.fluidTankCathodeOutput.readFromNBT(nbttagcompound.getCompoundTag("fluidTankCathodeOutput"));
	}

	@Override
	public void writeToNBT(NBTTagCompound nbttagcompound) {
		super.writeToNBT(nbttagcompound);
		NBTTagCompound fluidTankTag = new NBTTagCompound();
		this.fluidTank.writeToNBT(fluidTankTag);
		nbttagcompound.setTag("fluidTank", fluidTankTag);

		NBTTagCompound fluidTankAnodeTag = new NBTTagCompound();
		this.fluidTankAnodeOutput.writeToNBT(fluidTankAnodeTag);
		nbttagcompound.setTag("fluidTankAnodeOutput", fluidTankAnodeTag);

		NBTTagCompound fluidTankCathodeTag = new NBTTagCompound();
		this.fluidTankCathodeOutput.writeToNBT(fluidTankCathodeTag);
		nbttagcompound.setTag("fluidTankCathodeOutput", fluidTankCathodeTag);
	}

	@Override
	public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
		return this.getFacing() != side;
	}

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

	@Override
	public boolean enableUpdateEntity() {
		return IC2.platform.isSimulating();
	}

	@Override
	public void updateEntityServer() {
		super.updateEntityServer();
		IHLUtils.handleFluidSlotsBehaviour(fillInputSlot, drainInputSlot, emptyFluidItemsSlot, fluidTank);
		IHLUtils.handleFluidSlotsBehaviour(fillInputSlotAnodeOutput, null, emptyFluidItemsSlot, fluidTankAnodeOutput);
		IHLUtils.handleFluidSlotsBehaviour(fillInputSlotCathodeOutput, null, emptyFluidItemsSlot,
				fluidTankCathodeOutput);
	}

	@Override
	public FluidStack drain(ForgeDirection direction, int amount, boolean doDrain) {
		if (this.canDrain(direction, null)) {
			if (direction.equals(ForgeDirection.getOrientation(this.getFacing()).getRotation(ForgeDirection.UP))) {
				return this.fluidTankAnodeOutput.drain(amount, doDrain);
			}
			if (direction.equals(ForgeDirection.getOrientation(this.getFacing()).getRotation(ForgeDirection.DOWN))) {
				return this.fluidTankCathodeOutput.drain(amount, doDrain);
			}
			return this.fluidTank.drain(amount, doDrain);
		}
		return null;
	}

	// 1.7.10 API
	@Override
	public boolean canDrain(ForgeDirection direction, Fluid arg1) {
		return direction.equals(ForgeDirection.getOrientation(this.getFacing()).getRotation(ForgeDirection.UP))
				|| direction.equals(ForgeDirection.getOrientation(this.getFacing()).getRotation(ForgeDirection.DOWN))
				|| direction.equals(ForgeDirection.DOWN);
	}

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

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

	@Override
	public int gaugeProgressScaled(int i) {
		return this.progress * i / operationLength;
	}

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

	@Override
	public ContainerBase<?> getGuiContainer(EntityPlayer player) {
		this.fluidTank.sortFluidsByDensity();
		return new LabElectrolyzerContainer(player, this);
	}

	@Override
	public void onGuiClosed(EntityPlayer player) {
	}

	@Override
	public boolean canOperate() {
		UniversalRecipeOutput output = getOutput();
		if (output != null) {
			if (getOutput().getItemOutputs() != null && !getOutput().getItemOutputs().isEmpty()
					&& getOutput().getItemOutputs().get(0) != null) {
				return this.outputSlot.canAdd(getOutput().getItemOutputs());
			} else {
				return this.fluidTankAnodeOutput.getFluidAmount() < this.fluidTankAnodeOutput.getCapacity()
						&& this.fluidTankAnodeOutput.getFluidAmount() < this.fluidTankCathodeOutput.getCapacity();
			}
		}
		return false;
	}

	public UniversalRecipeOutput getOutput() {
		return LabElectrolyzerTileEntity.recipeManager.getOutputFor(this.getInput(), false, false);
	}

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

	@Override
	public void operate() {
		UniversalRecipeInput recipeInput = LabElectrolyzerTileEntity.recipeManager.getRecipeInput(getInput());
		UniversalRecipeOutput output1 = getOutput();
		this.fluidTank.drain(recipeInput.getFluidInputs().get(0), true);
		if (output1.getFluidOutputs().size() > 0)
			this.fluidTankAnodeOutput.fill(output1.getFluidOutputs().get(0).copy(), true);
		if (output1.getFluidOutputs().size() > 1)
			this.fluidTankCathodeOutput.fill(output1.getFluidOutputs().get(1).copy(), true);
		if (!output1.getItemOutputs().isEmpty() && output1.getItemOutputs().get(0) != null)
			this.outputSlot.add(output1.getItemOutputs());
	}

	@Override
	public FluidStack drain(ForgeDirection direction, FluidStack fluidStack, boolean doDrain) {
		if (this.fluidTankAnodeOutput.getFluid().containsFluid(fluidStack)) {
			return this.fluidTankAnodeOutput.drain(fluidStack.amount, doDrain);
		} else if (this.fluidTankCathodeOutput.getFluid().containsFluid(fluidStack)) {
			return this.fluidTankCathodeOutput.drain(fluidStack.amount, doDrain);
		}
		return null;
	}

	@Override
	public int fill(ForgeDirection arg0, FluidStack arg1, boolean arg2) {
		return this.fluidTank.fill(arg1, arg2);
	}

	@Override
	public FluidTankInfo[] getTankInfo(ForgeDirection direction) {
		if (direction.equals(ForgeDirection.getOrientation(this.getFacing()).getRotation(ForgeDirection.UP))) {
			return new FluidTankInfo[] { this.fluidTankAnodeOutput.getInfo() };
		}
		if (direction.equals(ForgeDirection.getOrientation(this.getFacing()).getRotation(ForgeDirection.DOWN))) {
			return new FluidTankInfo[] { this.fluidTankCathodeOutput.getInfo() };
		}
		return new FluidTankInfo[] { this.fluidTank.getInfo() };

	}

	public boolean needsFluid() {
		return this.fluidTank.getFluidAmount() <= this.fluidTank.getCapacity();
	}

	public FluidStack getFluidStackfromTank() {
		return this.fluidTank.getFluid();
	}

	public int getTankAmount() {
		return this.fluidTank.getFluidAmount();
	}

	public int gaugeLiquidScaled(int i, int index) {
		return this.fluidTank.getFluidAmount() <= 0 ? 0
				: this.fluidTank.getFluidAmount(index) * i / this.fluidTank.getCapacity();
	}

	public static void addRecipe(UniversalRecipeInput input, UniversalRecipeOutput output) {
		recipeManager.addRecipe(input, output);
	}

	public int getNumberOfFluidsInTank() {
		return this.fluidTank.getNumberOfFluids();
	}

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

	public static void addRecipe(FluidStack fluidStackInput1, FluidStack fluidStackOutputAnode,
			FluidStack fluidStackOutputCathode, ItemStack itemStackOutput1) {
		if (itemStackOutput1 != null) {
			addRecipe(new UniversalRecipeInput((new FluidStack[] { fluidStackInput1 }), null),
					new UniversalRecipeOutput((new FluidStack[] { fluidStackOutputAnode, fluidStackOutputCathode }),
							(new ItemStack[] { itemStackOutput1 }), 200));
		} else {
			addRecipe(new UniversalRecipeInput((new FluidStack[] { fluidStackInput1 }), null),
					new UniversalRecipeOutput((new FluidStack[] { fluidStackOutputAnode, fluidStackOutputCathode }),
							null, 200));
		}
	}

	public IHLFluidTank getFluidTank() {
		return this.fluidTank;
	}

}