-
Notifications
You must be signed in to change notification settings - Fork 755
/
FurnaceLogic.java
355 lines (313 loc) · 8.83 KB
/
FurnaceLogic.java
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
package tconstruct.blocks.logic;
import cpw.mods.fml.common.registry.GameRegistry;
import mantle.blocks.BlockUtils;
import mantle.blocks.abstracts.InventoryLogic;
import mantle.blocks.iface.*;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.*;
import net.minecraft.inventory.Container;
import net.minecraft.item.*;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.*;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import tconstruct.inventory.FurnaceContainer;
/* Slots
* 0: Input
* 1: Fuel
* 2: Output
* 3-16: Chest
*/
public class FurnaceLogic extends InventoryLogic implements IActiveLogic, IFacingLogic
{
boolean active;
public int fuel;
public int fuelGague;
public int progress;
public int fuelScale = 200;
byte direction;
public FurnaceLogic()
{
super(3);
active = false;
}
@Override
public String getDefaultName ()
{
return "container.furnace";
}
@Override
public boolean getActive ()
{
return active;
}
@Override
public void setActive (boolean flag)
{
active = flag;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
/* Fuel gauge */
public int gaugeProgressScaled (int scale)
{
return (progress * scale) / fuelScale;
}
public int gaugeFuelScaled (int scale)
{
if (fuelGague == 0)
{
fuelGague = fuel;
if (fuelGague == 0)
{
fuelGague = fuelScale;
}
}
return (fuel * scale) / fuelGague;
}
/* Item cooking */
public void updateEntity ()
{
boolean burning = isBurning();
boolean updateInventory = false;
if (fuel <= 0 && canSmelt())
{
fuel = fuelGague = (int) (getItemBurnTime(inventory[1]));
if (fuel > 0)
{
if (inventory[1].getItem().hasContainerItem()) //Fuel slot
{
inventory[1] = new ItemStack(inventory[1].getItem().getContainerItem());
}
else
{
inventory[1].stackSize--;
}
if (inventory[1].stackSize <= 0)
{
inventory[1] = null;
}
updateInventory = true;
}
}
if (isBurning() && canSmelt())
{
progress++;
if (progress >= fuelScale)
{
progress = 0;
cookItems();
updateInventory = true;
}
}
else
{
progress = 0;
}
if (fuel > 0)
{
fuel--;
}
if (burning != isBurning())
{
setActive(isBurning());
updateInventory = true;
}
if (updateInventory)
{
onInventoryChanged();
}
}
public void cookItems ()
{
if (this.canSmelt())
{
ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.inventory[0]);
if (this.inventory[2] == null)
{
this.inventory[2] = itemstack.copy();
}
else if (this.inventory[2].isItemEqual(itemstack))
{
inventory[2].stackSize += itemstack.stackSize;
}
--this.inventory[0].stackSize;
if (this.inventory[0].stackSize <= 0)
{
this.inventory[0] = null;
}
}
}
public boolean canSmelt ()
{
if (inventory[0] == null) //Nothing here!
return false;
else
{
ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.inventory[0]);
if (itemstack == null)
return false;
if (this.inventory[2] == null)
return true;
if (!this.inventory[2].isItemEqual(itemstack))
return false;
int result = inventory[2].stackSize + itemstack.stackSize;
return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
}
}
public boolean isBurning ()
{
return fuel > 0;
}
public ItemStack getResultFor (ItemStack stack)
{
ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(stack);
if (result != null) //Only valid for food
return result.copy();
return null;
}
public static int getItemBurnTime (ItemStack stack)
{
if (stack == null)
{
return 0;
}
else
{
Item item = stack.getItem();
if (stack.getItem() instanceof ItemBlock && BlockUtils.getBlockFromItem(item) != null)
{
Block block = BlockUtils.getBlockFromItem(item);
if (block == Blocks.wooden_slab)
{
return 150;
}
if (block == Blocks.log)
{
return 1200;
}
if (block.getMaterial() == Material.wood)
{
return 300;
}
if (block == Blocks.coal_block)
{
return 16000;
}
}
if (item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD"))
return 200;
if (item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD"))
return 200;
if (item instanceof ItemHoe && ((ItemHoe) item).getToolMaterialName().equals("WOOD"))
return 200;
if (item == Items.stick)
return 100;
if (item == Items.coal)
return 1600;
if (item == Items.lava_bucket)
return 20000;
if (item == new ItemStack(Blocks.sapling).getItem())
return 100;
if (item == Items.blaze_rod)
return 2400;
return GameRegistry.getFuelValue(stack);
}
}
/* NBT */
public void readFromNBT (NBTTagCompound tags)
{
super.readFromNBT(tags);
active = tags.getBoolean("Active");
fuel = tags.getInteger("Fuel");
fuelGague = tags.getInteger("FuelGague");
readNetworkNBT(tags);
}
public void writeToNBT (NBTTagCompound tags)
{
super.writeToNBT(tags);
tags.setBoolean("Active", active);
tags.setInteger("Fuel", fuel);
tags.setInteger("FuelGague", fuelGague);
writeNetworkNBT(tags);
}
public void readNetworkNBT (NBTTagCompound tags)
{
direction = tags.getByte("Direction");
}
public void writeNetworkNBT (NBTTagCompound tags)
{
tags.setByte("Direction", direction);
}
/* Packets */
@Override
public Packet getDescriptionPacket ()
{
NBTTagCompound tag = new NBTTagCompound();
writeNetworkNBT(tag);
return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
}
@Override
public void onDataPacket (NetworkManager net, S35PacketUpdateTileEntity packet)
{
readNetworkNBT(packet.func_148857_g());
worldObj.func_147479_m(xCoord, yCoord, zCoord);
}
@Override
public Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z)
{
return new FurnaceContainer(inventoryplayer, this);
}
@Override
public byte getRenderDirection ()
{
return direction;
}
@Override
public ForgeDirection getForgeDirection ()
{
return ForgeDirection.VALID_DIRECTIONS[direction];
}
@Override
@Deprecated
public void setDirection (int side)
{
//Nope!
}
@Override
@Deprecated
public void setDirection (float yaw, float pitch, EntityLivingBase player)
{
int facing = MathHelper.floor_double((double) (yaw / 360) + 0.5D) & 3;
switch (facing)
{
case 0:
direction = 2;
break;
case 1:
direction = 5;
break;
case 2:
direction = 3;
break;
case 3:
direction = 4;
break;
}
}
@Override
public String getInventoryName ()
{
return getDefaultName();
}
@Override
public boolean hasCustomInventoryName ()
{
return false;
}
}