aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/xmod/thaumcraft/common/tile/TileFastAlchemyFurnace.java
blob: 226c7cf39372c8231d1422297e32c31dac176633 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package gtPlusPlus.xmod.thaumcraft.common.tile;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.world.EnumSkyBlock;
import net.minecraftforge.common.util.ForgeDirection;
import thaumcraft.api.aspects.Aspect;
import thaumcraft.api.aspects.AspectList;
import thaumcraft.common.config.ConfigItems;
import thaumcraft.common.lib.crafting.ThaumcraftCraftingManager;
import thaumcraft.common.tiles.TileAlchemyFurnace;
import thaumcraft.common.tiles.TileAlembic;
import thaumcraft.common.tiles.TileBellows;

public class TileFastAlchemyFurnace extends TileAlchemyFurnace {
	private static final int[] slots_bottom = {1};
	private static final int[] slots_top = new int[0];
	private static final int[] slots_sides = {0};
	public AspectList aspects;
	public int vis;
	private int maxVis;
	public int smeltTime;
	int bellows;
	boolean speedBoost;
	private ItemStack[] furnaceItemStacks;
	public int furnaceBurnTime;
	public int currentItemBurnTime;
	public int furnaceCookTime;
	private String customName;
	int count;

	public TileFastAlchemyFurnace() {
		this.aspects = new AspectList();

		this.maxVis = 100;
		this.smeltTime = 25;
		this.bellows = -1;
		this.speedBoost = false;

		this.furnaceItemStacks = new ItemStack[2];

		this.count = 0;
	}

	public int func_70302_i_() {
		return this.furnaceItemStacks.length;
	}

	public ItemStack func_70301_a(int par1) {
		return this.furnaceItemStacks[par1];
	}

	public ItemStack func_70298_a(int par1, int par2) {
		if (this.furnaceItemStacks[par1] != null) {
			if (this.furnaceItemStacks[par1].stackSize <= par2) {
				ItemStack itemstack = this.furnaceItemStacks[par1];
				this.furnaceItemStacks[par1] = null;
				return itemstack;
			}

			ItemStack itemstack = this.furnaceItemStacks[par1].splitStack(par2);

			if (this.furnaceItemStacks[par1].stackSize == 0) {
				this.furnaceItemStacks[par1] = null;
			}

			return itemstack;
		}

		return null;
	}

	public ItemStack func_70304_b(int par1) {
		if (this.furnaceItemStacks[par1] != null) {
			ItemStack itemstack = this.furnaceItemStacks[par1];
			this.furnaceItemStacks[par1] = null;
			return itemstack;
		}

		return null;
	}

	public void func_70299_a(int par1, ItemStack par2ItemStack) {
		this.furnaceItemStacks[par1] = par2ItemStack;

		if ((par2ItemStack == null) || (par2ItemStack.stackSize <= func_70297_j_()))
			return;
		par2ItemStack.stackSize = func_70297_j_();
	}

	public String func_145825_b() {
		return ((func_145818_k_()) ? this.customName : "container.alchemyfurnace");
	}

	public boolean func_145818_k_() {
		return ((this.customName != null) && (this.customName.length() > 0));
	}

	public void setGuiDisplayName(String par1Str) {
		this.customName = par1Str;
	}

	public void readCustomNBT(NBTTagCompound nbttagcompound) {
		this.furnaceBurnTime = nbttagcompound.getShort("BurnTime");
		this.vis = nbttagcompound.getShort("Vis");
	}

	public void writeCustomNBT(NBTTagCompound nbttagcompound) {
		nbttagcompound.setShort("BurnTime", (short) this.furnaceBurnTime);
		nbttagcompound.setShort("Vis", (short) this.vis);
	}

	public void func_145839_a(NBTTagCompound nbtCompound) {
		super.func_145839_a(nbtCompound);
		NBTTagList nbttaglist = nbtCompound.getTagList("Items", 10);
		this.furnaceItemStacks = new ItemStack[func_70302_i_()];

		for (int i = 0; i < nbttaglist.tagCount(); ++i) {
			NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
			byte b0 = nbttagcompound1.getByte("Slot");

			if ((b0 < 0) || (b0 >= this.furnaceItemStacks.length))
				continue;
			this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
		}

		this.speedBoost = nbtCompound.getBoolean("speedBoost");
		this.furnaceCookTime = nbtCompound.getShort("CookTime");
		this.currentItemBurnTime = TileEntityFurnace.getItemBurnTime(this.furnaceItemStacks[1]);

		if (nbtCompound.hasKey("CustomName")) {
			this.customName = nbtCompound.getString("CustomName");
		}

		this.aspects.readFromNBT(nbtCompound);
		this.vis = this.aspects.visSize();
	}

	public void func_145841_b(NBTTagCompound nbtCompound) {
		super.func_145841_b(nbtCompound);
		nbtCompound.setBoolean("speedBoost", this.speedBoost);
		nbtCompound.setShort("CookTime", (short) this.furnaceCookTime);
		NBTTagList nbttaglist = new NBTTagList();

		for (int i = 0; i < this.furnaceItemStacks.length; ++i) {
			if (this.furnaceItemStacks[i] == null)
				continue;
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}

		nbtCompound.setTag("Items", nbttaglist);

		if (func_145818_k_()) {
			nbtCompound.setString("CustomName", this.customName);
		}

		this.aspects.writeToNBT(nbtCompound);
	}

	public int func_70297_j_() {
		return 64;
	}

	@SideOnly(Side.CLIENT)
	public int getCookProgressScaled(int par1) {
		if (this.smeltTime <= 0)
			this.smeltTime = 1;
		return (this.furnaceCookTime * par1 / this.smeltTime);
	}

	@SideOnly(Side.CLIENT)
	public int getContentsScaled(int par1) {
		return (this.vis * par1 / this.maxVis);
	}

	@SideOnly(Side.CLIENT)
	public int getBurnTimeRemainingScaled(int par1) {
		if (this.currentItemBurnTime == 0) {
			this.currentItemBurnTime = 200;
		}

		return (this.furnaceBurnTime * par1 / this.currentItemBurnTime);
	}

	public boolean isBurning() {
		return (this.furnaceBurnTime > 0);
	}

	public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		if (this.worldObj != null)
			this.worldObj.updateLightByType(EnumSkyBlock.Block, this.xCoord, this.yCoord,
					this.zCoord);
	}

	public boolean canUpdate() {
		return true;
	}

	public void func_145845_h() {
		boolean flag = this.furnaceBurnTime > 0;
		boolean flag1 = false;
		this.count += 1;
		if (this.furnaceBurnTime > 0) {
			this.furnaceBurnTime -= 1;
		}

		if (!(this.worldObj.isRemote)) {
			if (this.bellows < 0)
				getBellows();

			if ((this.count % ((this.speedBoost) ? 20 : 40) == 0) && (this.aspects.size() > 0)) {
				AspectList exlude = new AspectList();
				int deep = 0;
				TileEntity tile = null;
				while (deep < 5) {
					++deep;
					tile = this.worldObj.getTileEntity(this.xCoord, this.yCoord + deep,
							this.zCoord);
					if (!(tile instanceof TileAlembic))
						break;
					TileAlembic alembic = (TileAlembic) tile;
					if ((alembic.aspect != null) && (alembic.amount < alembic.maxAmount)
							&& (this.aspects.getAmount(alembic.aspect) > 0)) {
						takeFromContainer(alembic.aspect, 1);
						alembic.addToContainer(alembic.aspect, 1);
						exlude.merge(alembic.aspect, 1);
						this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord,
								this.zCoord);
						this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord + deep,
								this.zCoord);
					}
					tile = null;
				}

				deep = 0;
				while (deep < 5) {
					++deep;
					tile = this.worldObj.getTileEntity(this.xCoord, this.yCoord + deep,
							this.zCoord);
					if (!(tile instanceof TileAlembic))
						break;
					TileAlembic alembic = (TileAlembic) tile;
					if ((alembic.aspect == null) || (alembic.amount == 0))
						;
					Aspect as = null;
					if (alembic.aspectFilter == null) {
						as = takeRandomAspect(exlude);
					} else if (takeFromContainer(alembic.aspectFilter, 1)) {
						as = alembic.aspectFilter;
					}

					if (as != null) {
						alembic.addToContainer(as, 1);
						this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord,
								this.zCoord);
						this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord + deep,
								this.zCoord);
						break;
					}

				}

			}

			if ((this.furnaceBurnTime == 0) && (canSmelt())) {
				this.currentItemBurnTime = (this.furnaceBurnTime = TileEntityFurnace
						.getItemBurnTime(this.furnaceItemStacks[1]));

				if (this.furnaceBurnTime > 0) {
					flag1 = true;
					this.speedBoost = false;

					if (this.furnaceItemStacks[1] != null) {
						if (this.furnaceItemStacks[1].isItemEqual(new ItemStack(ConfigItems.itemResource, 1, 0))) {
							this.speedBoost = true;
						}
						this.furnaceItemStacks[1].stackSize -= 1;

						if (this.furnaceItemStacks[1].stackSize == 0) {
							this.furnaceItemStacks[1] = this.furnaceItemStacks[1].getItem()
									.getContainerItem(this.furnaceItemStacks[1]);
						}
					}
				}
			}

			if ((isBurning()) && (canSmelt())) {
				this.furnaceCookTime += 1;

				if (this.furnaceCookTime >= this.smeltTime) {
					this.furnaceCookTime = 0;
					smeltItem();
					flag1 = true;
				}
			} else {
				this.furnaceCookTime = 0;
			}

			if (flag != this.furnaceBurnTime > 0) {
				flag1 = true;
				this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
			}
		}

		if (!(flag1))
			return;
		markDirty();
	}

	private boolean canSmelt() {
		if (this.furnaceItemStacks[0] == null) {
			return false;
		}

		AspectList al = ThaumcraftCraftingManager.getObjectTags(this.furnaceItemStacks[0]);
		al = ThaumcraftCraftingManager.getBonusTags(this.furnaceItemStacks[0], al);

		if ((al == null) || (al.size() == 0))
			return false;
		int vs = al.visSize();
		if (vs > this.maxVis - this.vis)
			return false;
		this.smeltTime = (int) (vs * 10 * (1.0F - (0.125F * this.bellows)));
		return true;
	}

	public void getBellows() {
		this.bellows = TileBellows.getBellows(this.worldObj, this.xCoord, this.yCoord,
				this.zCoord, ForgeDirection.VALID_DIRECTIONS);
	}

	public void smeltItem() {
		if (!(canSmelt())) {
			return;
		}
		AspectList al = ThaumcraftCraftingManager.getObjectTags(this.furnaceItemStacks[0]);
		al = ThaumcraftCraftingManager.getBonusTags(this.furnaceItemStacks[0], al);

		for (Aspect a : al.getAspects()) {
			this.aspects.add(a, al.getAmount(a));
		}

		this.vis = this.aspects.visSize();

		this.furnaceItemStacks[0].stackSize -= 1;

		if (this.furnaceItemStacks[0].stackSize > 0)
			return;
		this.furnaceItemStacks[0] = null;
	}

	public static boolean isItemFuel(ItemStack par0ItemStack) {
		return (TileEntityFurnace.getItemBurnTime(par0ItemStack) > 0);
	}

	public boolean func_70300_a(EntityPlayer par1EntityPlayer) {
		return (this.worldObj.getTileEntity(this.xCoord, this.yCoord,
				this.zCoord) == this);
	}

	public void func_70295_k_() {
	}

	public void func_70305_f() {
	}

	public boolean func_94041_b(int par1, ItemStack par2ItemStack) {
		if (par1 == 0) {
			AspectList al = ThaumcraftCraftingManager.getObjectTags(par2ItemStack);
			al = ThaumcraftCraftingManager.getBonusTags(par2ItemStack, al);
			if ((al != null) && (al.size() > 0))
				return true;
		}
		return ((par1 == 1) ? isItemFuel(par2ItemStack) : false);
	}

	public int[] func_94128_d(int par1) {
		return ((par1 == 1) ? slots_top : (par1 == 0) ? slots_bottom : slots_sides);
	}

	public boolean func_102007_a(int par1, ItemStack par2ItemStack, int par3) {
		return ((par3 == 1) ? false : func_94041_b(par1, par2ItemStack));
	}

	public boolean func_102008_b(int par1, ItemStack par2ItemStack, int par3) {
		return ((par3 != 0) || (par1 != 1) || (par2ItemStack.getItem() == Items.bucket));
	}

	public Aspect takeRandomAspect(AspectList exlude) {
		if (this.aspects.size() > 0) {
			AspectList temp = this.aspects.copy();
			if (exlude.size() > 0)
				for (Aspect a : exlude.getAspects())
					temp.remove(a);
			if (temp.size() > 0) {
				Aspect tag = temp.getAspects()[this.worldObj.rand.nextInt(temp.getAspects().length)];
				this.aspects.remove(tag, 1);
				this.vis -= 1;
				return tag;
			}
		}
		return null;
	}

	public boolean takeFromContainer(Aspect tag, int amount) {
		if ((this.aspects != null) && (this.aspects.getAmount(tag) >= amount)) {
			this.aspects.remove(tag, amount);
			this.vis -= amount;
			return true;
		}
		return false;
	}
}