summaryrefslogtreecommitdiff
path: root/ihl/processing/chemistry/LeadOvenTileEntity.java
blob: 01f69bf13c750114bfca40ce0d0965dfe7aed10c (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidHandler;
import ic2.api.recipe.IRecipeInput;
import ic2.core.ContainerBase;
import ic2.core.IC2;
import ic2.core.IHasGui;
import ic2.core.block.TileEntityLiquidTankInventory;
import ic2.core.block.invslot.InvSlotConsumableFuel;
import ic2.core.block.invslot.InvSlot.Access;
import ihl.processing.invslots.IHLInvSlotOutput;
import ihl.recipes.UniversalRecipeInput;
import ihl.recipes.UniversalRecipeManager;
import ihl.recipes.UniversalRecipeOutput;
import ihl.utils.IHLUtils;

public class LeadOvenTileEntity extends TileEntityLiquidTankInventory implements IHasGui
{
    public final InvSlotConsumableFuel fuelSlot;
    public final ApparatusProcessableInvSlot inputSlot;
    public final IHLInvSlotOutput outputSlot;
    public short progress = 0;
    public final short maxProgress = 160;
    public int fuel = 0;
    public int maxFuel = 0;
	protected static final UniversalRecipeManager recipeManager = new UniversalRecipeManager("chemicaloven");
    private ItemStack burnedFuel;
	
    public LeadOvenTileEntity()
    {
    	super(1000);
    	this.fuelSlot = new InvSlotConsumableFuel(this, "fuel", 1, 1, true);
    	this.inputSlot = new ApparatusProcessableInvSlot(this, "input", 2, Access.IO, 2, 64);
    	this.outputSlot = new IHLInvSlotOutput(this, "output", 0, 1);
    }
    
    public static void addRecipe(UniversalRecipeInput input,UniversalRecipeOutput output)
    {
    	recipeManager.addRecipe(input, output);
    }
    
    @Override
	public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);

        try
        {
            this.fuel = nbttagcompound.getInteger("fuel");
        }
        catch (Throwable var4)
        {
            this.fuel = nbttagcompound.getShort("fuel");
        }

        try
        {
            this.maxFuel = nbttagcompound.getInteger("maxFuel");
        }
        catch (Throwable var3)
        {
            this.maxFuel = nbttagcompound.getShort("maxFuel");
        }
        this.progress = nbttagcompound.getShort("progress");
    }

    @Override
	public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setInteger("fuel", this.fuel);
        nbttagcompound.setInteger("maxFuel", this.maxFuel);
        nbttagcompound.setShort("progress", this.progress);
        NBTTagCompound gasBufferTag = new NBTTagCompound();
        nbttagcompound.setTag("gasBuffer", gasBufferTag);
    }
    
	@Override
	public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) 
	{
		return this.getFacing()!=(short)side && side!=0 && side!=1;
	}
	
	@Override
	public ItemStack getWrenchDrop(EntityPlayer entityPlayer) 
	{
		return IHLUtils.getThisModItemStack("leadOven");
	}
    
    public int gaugeProgressScaled(int i)
    {
        return this.progress * i / maxProgress;
    }

    public int gaugeFuelScaled(int i)
    {
        if (this.maxFuel == 0)
        {
            this.maxFuel = this.fuel;

            if (this.maxFuel == 0)
            {
                this.maxFuel = 160;
            }
        }

        return this.fuel * i / this.maxFuel;
    }

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

    @Override
	public void updateEntityServer()
    {
        super.updateEntityServer();
        if(this.getFluidTank().getFluid()!=null)
        {
        	FluidStack fs;
        	if(this.maxProgress>0 && this.getOutput()!=null && this.getOutput().getFluidOutputs()!=null && this.getOutput().getFluidOutputs().size()>0)
        	{
        		fs = this.getFluidTank().drain(this.getOutput().getFluidOutputs().get(0).amount/this.maxProgress, true);
        	}
        	else
        	{
        		fs = this.getFluidTank().drain(1000, true);
        	}
        	TileEntity te = worldObj.getTileEntity(xCoord, yCoord+1, zCoord);
        	if(te!=null)
        	{
        		ForgeDirection direction = ForgeDirection.DOWN;
        		if(te instanceof IFluidHandler)
        		{
        			((IFluidHandler)te).fill(direction, fs, true);
        		}
        	}
        }
        boolean needsInvUpdate = false;
        if (this.fuel <= 0 && this.canOperate())
        {
        	if(this.fuelSlot.get()!=null)
        	{
        		if(burnedFuel!=null && IHLUtils.isItemsHaveSameOreDictionaryEntry(this.fuelSlot.get(), burnedFuel))
        		{
        			burnedFuel.stackSize++;
        		}
        		else if(burnedFuel==null)
        		{
        			burnedFuel=this.fuelSlot.get().copy();
        			burnedFuel.stackSize=1;
        		}
        	}
            this.fuel = this.maxFuel = this.fuelSlot.consumeFuel();

            if (this.fuel > 0)
            {
                needsInvUpdate = true;
            }
        }

        if (this.isBurning() && this.canOperate())
        {
            ++this.progress;

            if (this.progress >= maxProgress)
            {
                this.progress = 0;
                this.operate();
                needsInvUpdate = true;
            }
        }
        else
        {
            this.progress = 0;
        }

        if (this.fuel > 0)
        {
            --this.fuel;
        }

        if (this.getActive() != this.isBurning())
        {
            this.setActive(this.isBurning());
            needsInvUpdate = true;
        }
    }
    
    public boolean isBurning()
    {
        return this.fuel > 0;
    }

    public boolean canOperate()
    {
            return this.getOutput()!=null;
    }

    /**
     * Returns the name of the inventory
     */
    @Override
	public String getInventoryName()
    {
        return "goldOven";
    }

    @Override
    public ContainerBase getGuiContainer(EntityPlayer entityPlayer)
    {
        return new LeadOvenContainer(entityPlayer, this);
    }
    
    @Override
	@SideOnly(Side.CLIENT)
    public GuiScreen getGui(EntityPlayer entityPlayer, boolean isAdmin)
    {
        return new LeadOvenGui(new LeadOvenContainer(entityPlayer, this));
    }

    @Override
	public void onGuiClosed(EntityPlayer entityPlayer) {}
    
    public int mX()
	{
		switch(this.getFacing())
		{
		case 4:
		return -1;
		case 5:
		return 1;
		default:
		return 0;
		}
	}
    
    public int mZ()
	{
		switch(this.getFacing())
		{
		case 2:
		return -1;
		case 3:
		return 1;
		default:
		return 0;
		}
	}
    
    public UniversalRecipeOutput getOutput()
    {
    	return LeadOvenTileEntity.recipeManager.getOutputFor(this.getInput(), false, false);
    }

	public List[] getInput()
	{
		if(this.inputSlot.get(1)!=null)
		{
			return new List[] {null, Arrays.asList(new ItemStack[] {this.inputSlot.get(0),this.inputSlot.get(1)})};
		}
		return new List[] {null, Arrays.asList(new ItemStack[] {this.inputSlot.get()})};
	}
	
	public void operate() 
	{
		List<IRecipeInput> rinput = LeadOvenTileEntity.recipeManager.getRecipeInput(getInput()).getItemInputs();
		UniversalRecipeOutput routput = LeadOvenTileEntity.recipeManager.getOutputFor(getInput(), false, false);
		List<FluidStack> output2 = routput.getFluidOutputs();
		if(!output2.isEmpty())
		{
			this.getFluidTank().fill(output2.get(0), true);
		}
		List itemOutputs = routput.getItemOutputs();
		if(itemOutputs!=null && !itemOutputs.isEmpty())
		{
			this.outputSlot.add(itemOutputs);
		}
		for(int i=0; i<rinput.size();i++)
		{
			if(this.burnedFuel!=null && IHLUtils.isItemsHaveSameOreDictionaryEntry(this.fuelSlot.get(), burnedFuel) && this.burnedFuel.stackSize>=rinput.get(i).getAmount())
			{
				this.burnedFuel.stackSize-=rinput.get(i).getAmount();
				if(this.burnedFuel.stackSize<=0)
				{
					this.burnedFuel=null;
				}
			}
			else
			{
				this.inputSlot.consume(i, rinput.get(i).getAmount());
			}
			if(this.inputSlot.get(i)!=null && this.inputSlot.get(i).stackSize<=0)
			{
				this.inputSlot.put(i, null);
			}
		}
		
	}

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

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

	public static void addRecipe(UniversalRecipeInput input, FluidStack fluidStackWithSize) 
	{
		recipeManager.addRecipe(input, new UniversalRecipeOutput(Arrays.asList(new FluidStack[]{fluidStackWithSize}),null,20));
	}

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

	public static void addRecipe(ItemStack input, FluidStack fluidStackOutput, ItemStack output) {
		recipeManager.addRecipe(new UniversalRecipeInput(null, Arrays.asList(new ItemStack[] {input})),new UniversalRecipeOutput(Arrays.asList(new FluidStack[] {fluidStackOutput}),Arrays.asList(new ItemStack[] {output}),20));
	}

	public static void addRecipe(ItemStack input, ItemStack output) {
		recipeManager.addRecipe(new UniversalRecipeInput(null, Arrays.asList(new ItemStack[] {input})),new UniversalRecipeOutput(null,Arrays.asList(new ItemStack[] {output}),20));
	}
}