summaryrefslogtreecommitdiff
path: root/src/main/java/ihl/processing/chemistry/DosingPumpTileEntity.java
blob: e1e49967cb781df638f86554582dc46f3b2ccd0e (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
package ihl.processing.chemistry;

import java.util.List;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
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.InvSlotConsumableLiquidIHL;
import ihl.processing.metallurgy.BasicElectricMotorTileEntity;
import ihl.utils.IHLFluidTank;
import ihl.utils.IHLUtils;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
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 DosingPumpTileEntity extends BasicElectricMotorTileEntity implements IFluidHandler {
	public final InvSlotConsumableLiquidIHL drainInputSlot;
	public final InvSlotConsumableLiquidIHL fillInputSlot;
	public final InvSlotOutput emptyFluidItemsSlot;
	private final IHLFluidTank fluidTank = new IHLFluidTank(8000);
	public int fluidAmountSetpoint = 8000;
	private boolean prevIsPowered = false;
	private boolean tickFree=false;

	public DosingPumpTileEntity() {
		super();
		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.emptyFluidItemsSlot = new InvSlotOutput(this, "fluidCellsOutput", 2, 1);
	}

	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound) {
		super.readFromNBT(nbttagcompound);
		this.fluidTank.readFromNBT(nbttagcompound.getCompoundTag("fluidTank"));
		this.fluidAmountSetpoint = nbttagcompound.getInteger("fluidAmountSetpoint");
	}

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

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

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

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

	@Override
	public void updateEntityServer() {
		super.updateEntityServer();
		this.tickFree = true;
		IHLUtils.handleFluidSlotsBehaviour(fillInputSlot, drainInputSlot, emptyFluidItemsSlot, fluidTank);
	}

	@Override
	public FluidStack drain(ForgeDirection from, int amount, boolean doDrain) {
		switch (from) {
		case UP:
			return this.fluidTank.drainLightest(amount, doDrain);
		case NORTH:
			return this.fluidTank.drainLightest(amount, doDrain);
		case SOUTH:
			return this.fluidTank.drainLightest(amount, doDrain);
		case WEST:
			return this.fluidTank.drainLightest(amount, doDrain);
		case EAST:
			return this.fluidTank.drainLightest(amount, doDrain);
		case DOWN:
			return this.fluidTank.drain(amount, doDrain);
		default:
			return this.fluidTank.drain(amount, doDrain);
		}
	}

	// 1.7.10 API
	@Override
	public boolean canDrain(ForgeDirection arg0, Fluid arg1) {
		return true;
	}

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

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

	public float getRenderLiquidLevel() {
		return (float) this.fluidTank.getFluidAmount() / (float) this.fluidTank.getCapacity();
	}

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

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

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

	@Override
	public void onGuiClosed(EntityPlayer player) {
	}

	@Override
	public boolean canOperate() {
		return false;
	}

	@Override
	public void operate() {
		int fluidAmountToDrain = fluidAmountSetpoint;
		ForgeDirection dir = ForgeDirection.getOrientation(this.getFacing());
		TileEntity te = worldObj.getTileEntity(xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ);
		if (te instanceof IFluidHandler) {
			IFluidHandler fhte = (IFluidHandler) te;
			for (int i = 0; i < this.fluidTank.getNumberOfFluids(); i++) {
				FluidStack drained = this.fluidTank.drain(fluidAmountToDrain, true);
				fluidAmountToDrain -= drained.amount;
				if (fhte.canFill(dir, drained.getFluid())) {
					fhte.fill(dir, drained, true);
				}
				if (fluidAmountToDrain <= 0) {
					break;
				}
			}
		}
		this.energy-=this.energyConsume/10;
	}

	@Override
	public FluidStack drain(ForgeDirection arg0, FluidStack fluidStack, boolean doDrain) {
		if (fluidTank.getFluid() != null && fluidTank.getFluid().containsFluid(fluidStack)) {
			return this.fluidTank.drain(fluidStack, doDrain);
		}
		return null;
	}

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

	@Override
	public FluidTankInfo[] getTankInfo(ForgeDirection arg0) {
		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 int getNumberOfFluidsInTank() {
		return this.fluidTank.getNumberOfFluids();
	}

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

	@Override
	public List<?>[] getInput() {
		return null;
	}

	public void setPowered(boolean isPowered) {
		if (isPowered && !prevIsPowered && this.energy > 0 && this.tickFree) {
			this.operate();
		}
		prevIsPowered = isPowered;
		this.tickFree = false; // Only one operation per tick max
	}

}