summaryrefslogtreecommitdiff
path: root/ihl/processing/chemistry/GoldChimneyKneeTileEntity.java
blob: 6c73a5b8e426f0ec0c466197a4afa8d3fd4406e0 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package ihl.processing.chemistry;

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

import ic2.api.network.INetworkDataProvider;
import ic2.api.tile.IWrenchable;
import ic2.core.IC2;
import ic2.core.network.NetworkManager;
import ihl.recipes.UniversalRecipeInput;
import ihl.recipes.UniversalRecipeManager;
import ihl.recipes.UniversalRecipeOutput;
import ihl.utils.IHLFluidTank;
import ihl.utils.IHLUtils;
import ihl.worldgen.ores.IHLFluid;
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 GoldChimneyKneeTileEntity extends TileEntity implements IWrenchable, INetworkDataProvider, IFluidHandler {
	private short facing=2;
	private short lastFacing=2;
    private final IHLFluidTank gasBuffer = new IHLFluidTank(10000);
    private final IHLFluidTank fluidTank = new IHLFluidTank(10000);
	private static UniversalRecipeManager recipeManager = new UniversalRecipeManager("chimneyknee");

    
	public GoldChimneyKneeTileEntity()
	{
		super();
	}

	@Override
	public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
		return this.facing!=side;
	}
	
	@Override
	public short getFacing() 
	{
		return facing;
	}
	
	@Override
    public List<String> getNetworkedFields()
    {
		List<String> fields = new ArrayList();
		fields.add("facing");
		return fields;
    }
	
    @Override
	public void updateEntity()
    {
        super.updateEntity();
        if(IC2.platform.isSimulating())
        {
        	if(this.facing!=this.lastFacing)
        	{
        		IC2.network.get().updateTileEntityField(this, "facing");
        		this.lastFacing=this.facing;
        	}
        	if(this.fluidTank.getFluid()!=null && this.gasBuffer.getFluid()!=null)
        	{
        		if(this.getOutput()!=null)
        		{
        			List<FluidStack> output = GoldChimneyKneeTileEntity.recipeManager.getOutputFor(getInput(), true, true).getFluidOutputs();
        			if(output.size()>0)
        			{
        				this.fillOutputApparatus(getConnectDirection(),output.get(0), true);
        			}
        		}
        	}
        	if(this.fluidTank.getFluid()!=null)
        	{
        		if(this.fillOutputApparatus(getConnectDirection(),this.fluidTank.drain(this.fluidTank.getCapacity(), false), false)>0)
        		{
        			this.fillOutputApparatus(getConnectDirection(),this.fluidTank.drain(this.fluidTank.getCapacity(), true), true);
        		}
        	}
        	if(this.gasBuffer.getFluid()!=null)
        	{
        		if(this.fillOutputApparatus(getConnectDirection(),this.gasBuffer.drain(this.gasBuffer.getCapacity(), false), false)>0)
        		{
        			this.fillOutputApparatus(getConnectDirection(),this.gasBuffer.drain(this.gasBuffer.getCapacity(), true), true);
        		}
        	}

        }
    }
	
	private int fillOutputApparatus(ForgeDirection direction,FluidStack fluidStack, boolean doFill) 
	{
		TileEntity te = worldObj.getTileEntity(xCoord+mX(),yCoord, zCoord+mZ());
		if(te instanceof IFluidHandler)
		{
			return ((IFluidHandler)te).fill(direction, fluidStack, doFill);
		}
		else
		{
			return 0;
		}
	}

	@Override
	public void setFacing(short facing1) 
	{
		facing=(short) Math.max(2,facing1);
		if(IC2.platform.isSimulating())
		{
			IC2.network.get().updateTileEntityField(this, "facing");
		}
	}

	@Override
	public boolean wrenchCanRemove(EntityPlayer entityPlayer) 
	{
		return true;
	}

	@Override
	public float getWrenchDropRate() 
	{
		return 1F;
	}

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

	@Override
	public void writeToNBT(NBTTagCompound nbttagcompound) 
	{
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setShort("facing",facing);
        NBTTagCompound fluidTankTag = new NBTTagCompound();
        this.fluidTank.writeToNBT(fluidTankTag);
        nbttagcompound.setTag("fluidTank", fluidTankTag);
        
        NBTTagCompound gasBufferTag = new NBTTagCompound();
        this.gasBuffer.writeToNBT(gasBufferTag);
        nbttagcompound.setTag("gasBuffer", gasBufferTag);
        
	}
	
	@Override
	public void readFromNBT(NBTTagCompound nbttagcompound) 
	{
        super.readFromNBT(nbttagcompound);
        facing=nbttagcompound.getShort("facing");
        this.fluidTank.readFromNBT(nbttagcompound.getCompoundTag("fluidTank"));
        this.gasBuffer.readFromNBT(nbttagcompound.getCompoundTag("gasBuffer"));
	}
	
	   protected int mX()
		{
			switch(this.getFacing())
			{
			case 4:
			return 1;
			case 5:
			return -1;
			default:
			return 0;
			}
		}
	    
	    protected int mZ()
		{
			switch(this.getFacing())
			{
			case 2:
			return 1;
			case 3:
			return -1;
			default:
			return 0;
			}
		}
	    
	    public TileEntity getConnectedTileEntity()
	    {
	    	return worldObj.getTileEntity(xCoord+mX(), yCoord, zCoord+mZ());
	    }

		public ForgeDirection getConnectDirection() 
		{
			switch(this.getFacing())
			{				
				case 2:
					return ForgeDirection.NORTH;
				case 3:
					return ForgeDirection.SOUTH;
				case 4:
					return ForgeDirection.WEST;
				case 5:
					return ForgeDirection.EAST;
				default:
					return ForgeDirection.NORTH;
			}
		}

		@Override
		public boolean canDrain(ForgeDirection arg0, Fluid arg1) 
		{
			return false;
		}

		@Override
		public boolean canFill(ForgeDirection direction, Fluid fluid) 
		{
			return fluid.isGaseous() && (direction==ForgeDirection.DOWN || direction==getConnectDirection());
		}
		
		@Override
		public int fill(ForgeDirection direction, FluidStack fluidStack, boolean doFill)
		{
			if(direction==getConnectDirection() && fluidStack!=null && fluidStack.getFluid()!=null)
			{
				if(fluidStack.getFluid().isGaseous() && fluidStack.getFluid().getDensity()<IHLFluid.maxGaseousStateVapoursDensity)
				{
					return gasBuffer.fill(fluidStack, doFill);
				}
				else
				{
					return 0;
				}
			}
			else if(direction==ForgeDirection.DOWN)
			{
				if(fluidStack.getFluid().isGaseous() && fluidStack.getFluid().getDensity()<IHLFluid.maxGaseousStateVapoursDensity)
				{
					return fluidTank.fill(fluidStack, doFill);
				}
				else
				{
					return 0;
				}
			}
			else
			{
				return 0;
			}
		}

		@Override
		public FluidStack drain(ForgeDirection arg0, FluidStack arg1, boolean arg2) 
		{
			return null;
		}

		@Override
		public FluidStack drain(ForgeDirection arg0, int arg1, boolean arg2) 
		{
			return null;
		}

		@Override
		public FluidTankInfo[] getTankInfo(ForgeDirection arg0) 
		{
			return new FluidTankInfo[]{this.fluidTank.getInfo(), this.gasBuffer.getInfo()};
		}
		
		public static void addRecipe(UniversalRecipeInput input, FluidStack fluidStackWithSize) 
		{
			recipeManager.addRecipe(input, new UniversalRecipeOutput(Arrays.asList(new FluidStack[] {fluidStackWithSize}),null,20));
		}
		
	    public UniversalRecipeOutput getOutput()
	    {
	    	return GoldChimneyKneeTileEntity.recipeManager.getOutputFor(this.getInput(), false, false);
	    }
	    
		public List[] getInput()
		{
			return new List[] {Arrays.asList(new FluidStack[] {this.gasBuffer.getFluid(),this.fluidTank.getFluid()}),null};
		}

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