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
|
#include "stdafx.h"
#include "com.mojang.nbt.h"
#include "net.minecraft.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.item.crafting.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "Material.h"
#include "FurnaceTileEntity.h"
int furnaceSlotsForUp [] = { FurnaceTileEntity::SLOT_INPUT };
int furnaceSlotsForDown [] = { FurnaceTileEntity::SLOT_RESULT, FurnaceTileEntity::SLOT_FUEL };
int furnaceSlotsForSides [] = { FurnaceTileEntity::SLOT_FUEL };
const intArray FurnaceTileEntity::SLOTS_FOR_UP = intArray(furnaceSlotsForUp, 1);
const intArray FurnaceTileEntity::SLOTS_FOR_DOWN = intArray(furnaceSlotsForDown, 2);
const intArray FurnaceTileEntity::SLOTS_FOR_SIDES = intArray(furnaceSlotsForSides, 1);
const int FurnaceTileEntity::BURN_INTERVAL = 10 * 20;
// 4J Stu - Need a ctor to initialise member variables
FurnaceTileEntity::FurnaceTileEntity() : TileEntity()
{
items = ItemInstanceArray(3);
litTime = 0;
litDuration = 0;
tickCount = 0;
m_charcoalUsed = false;
name = L"";
}
FurnaceTileEntity::~FurnaceTileEntity()
{
delete[] items.data;
}
unsigned int FurnaceTileEntity::getContainerSize()
{
return items.length;
}
shared_ptr<ItemInstance> FurnaceTileEntity::getItem(unsigned int slot)
{
return items[slot];
}
shared_ptr<ItemInstance> FurnaceTileEntity::removeItem(unsigned int slot, int count)
{
m_charcoalUsed = false;
if (items[slot] != nullptr)
{
if (items[slot]->count <= count)
{
shared_ptr<ItemInstance> item = items[slot];
items[slot] = nullptr;
// 4J Stu - Fix for duplication glitch
if(item->count <= 0) return nullptr;
return item;
}
else
{
shared_ptr<ItemInstance> i = items[slot]->remove(count);
if (items[slot]->count == 0) items[slot] = nullptr;
// 4J Stu - Fix for duplication glitch
if(i->count <= 0) return nullptr;
return i;
}
}
return nullptr;
}
shared_ptr<ItemInstance> FurnaceTileEntity::removeItemNoUpdate(int slot)
{
m_charcoalUsed = false;
if (items[slot] != nullptr)
{
shared_ptr<ItemInstance> item = items[slot];
items[slot] = nullptr;
return item;
}
return nullptr;
}
void FurnaceTileEntity::setItem(unsigned int slot, shared_ptr<ItemInstance> item)
{
items[slot] = item;
if (item != nullptr && item->count > getMaxStackSize()) item->count = getMaxStackSize();
}
wstring FurnaceTileEntity::getName()
{
return hasCustomName() ? name : app.GetString(IDS_TILE_FURNACE);
}
wstring FurnaceTileEntity::getCustomName()
{
return hasCustomName() ? name : L"";
}
bool FurnaceTileEntity::hasCustomName()
{
return !name.empty();
}
void FurnaceTileEntity::setCustomName(const wstring &name)
{
this->name = name;
}
void FurnaceTileEntity::load(CompoundTag *base)
{
TileEntity::load(base);
ListTag<CompoundTag> *inventoryList = (ListTag<CompoundTag> *) base->getList(L"Items");
delete[] items.data;
items = ItemInstanceArray(getContainerSize());
for (int i = 0; i < inventoryList->size(); i++)
{
CompoundTag *tag = inventoryList->get(i);
unsigned int slot = tag->getByte(L"Slot");
if (slot >= 0 && slot < items.length) items[slot] = ItemInstance::fromTag(tag);
}
litTime = base->getShort(L"BurnTime");
tickCount = base->getShort(L"CookTime");
litDuration = getBurnDuration(items[SLOT_FUEL]);
if (base->contains(L"CustomName")) name = base->getString(L"CustomName");
m_charcoalUsed = base->getBoolean(L"CharcoalUsed");
}
void FurnaceTileEntity::save(CompoundTag *base)
{
TileEntity::save(base);
base->putShort(L"BurnTime", static_cast<short>(litTime));
base->putShort(L"CookTime", static_cast<short>(tickCount));
ListTag<CompoundTag> *listTag = new ListTag<CompoundTag>();
for (unsigned int i = 0; i < items.length; i++)
{
if (items[i] != nullptr)
{
CompoundTag *tag = new CompoundTag();
tag->putByte(L"Slot", static_cast<byte>(i));
items[i]->save(tag);
listTag->add(tag);
}
}
base->put(L"Items", listTag);
if (hasCustomName()) base->putString(L"CustomName", name);
base->putBoolean(L"CharcoalUsed", m_charcoalUsed);
}
int FurnaceTileEntity::getMaxStackSize() const
{
return Container::LARGE_MAX_STACK_SIZE;
}
int FurnaceTileEntity::getBurnProgress(int max)
{
return tickCount * max / BURN_INTERVAL;
}
int FurnaceTileEntity::getLitProgress(int max)
{
if (litDuration == 0) litDuration = BURN_INTERVAL;
return litTime * max / litDuration;
}
bool FurnaceTileEntity::isLit()
{
return litTime > 0;
}
void FurnaceTileEntity::tick()
{
bool wasLit = litTime > 0;
bool changed = false;
if (litTime > 0)
{
litTime--;
}
if ( level != nullptr && !level->isClientSide)
{
if (litTime == 0 && canBurn())
{
litDuration = litTime = getBurnDuration(items[SLOT_FUEL]);
if (litTime > 0)
{
changed = true;
if (items[SLOT_FUEL] != nullptr)
{
// 4J Added: Keep track of whether charcoal was used in production of current stack.
if ( items[SLOT_FUEL]->getItem()->id == Item::coal_Id
&& items[SLOT_FUEL]->getAuxValue() == CoalItem::CHAR_COAL)
{
m_charcoalUsed = true;
}
items[SLOT_FUEL]->count--;
if (items[SLOT_FUEL]->count == 0)
{
Item *remaining = items[SLOT_FUEL]->getItem()->getCraftingRemainingItem();
items[SLOT_FUEL] = remaining != nullptr ? std::make_shared<ItemInstance>(remaining) : nullptr;
}
}
}
}
if (isLit() && canBurn())
{
tickCount++;
if (tickCount == BURN_INTERVAL)
{
tickCount = 0;
burn();
changed = true;
}
}
else
{
tickCount = 0;
}
if (wasLit != litTime > 0)
{
changed = true;
FurnaceTile::setLit(litTime > 0, level, x, y, z);
}
}
if (changed) setChanged();
}
bool FurnaceTileEntity::canBurn()
{
if (items[SLOT_INPUT] == nullptr) return false;
const ItemInstance *burnResult = FurnaceRecipes::getInstance()->getResult(items[SLOT_INPUT]->getItem()->id);
if (burnResult == nullptr) return false;
if (items[SLOT_RESULT] == nullptr) return true;
if (!items[SLOT_RESULT]->sameItem_not_shared(burnResult)) return false;
if (items[SLOT_RESULT]->count < getMaxStackSize() && items[SLOT_RESULT]->count < items[SLOT_RESULT]->getMaxStackSize()) return true;
if (items[SLOT_RESULT]->count < burnResult->getMaxStackSize()) return true;
return false;
}
void FurnaceTileEntity::burn()
{
if (!canBurn()) return;
const ItemInstance *result = FurnaceRecipes::getInstance()->getResult(items[SLOT_INPUT]->getItem()->id);
if (items[SLOT_RESULT] == nullptr) items[SLOT_RESULT] = result->copy();
else if (items[SLOT_RESULT]->id == result->id) items[SLOT_RESULT]->count++;
items[SLOT_INPUT]->count--;
if (items[SLOT_INPUT]->count <= 0) items[SLOT_INPUT] = nullptr;
}
int FurnaceTileEntity::getBurnDuration(shared_ptr<ItemInstance> itemInstance)
{
if (itemInstance == nullptr) return 0;
int id = itemInstance->getItem()->id;
Item *item = itemInstance->getItem();
if (id < 256 && Tile::tiles[id] != nullptr)
{
Tile *tile = Tile::tiles[id];
if (tile == Tile::woodSlabHalf)
{
return BURN_INTERVAL * 3 / 4;
}
if (tile->material == Material::wood)
{
return BURN_INTERVAL * 3 / 2;
}
if (tile == Tile::coalBlock)
{
return BURN_INTERVAL * 8 * 10;
}
}
if (dynamic_cast<DiggerItem *>(item) && static_cast<DiggerItem *>(item)->getTier() == Item::Tier::WOOD)
{
return BURN_INTERVAL;
}
else if (dynamic_cast<WeaponItem *>(item) && static_cast<WeaponItem *>(item)->getTier() == Item::Tier::WOOD)
{
return BURN_INTERVAL;
}
else if (dynamic_cast<HoeItem *>(item) && static_cast<HoeItem *>(item)->getTier() == Item::Tier::WOOD)
{
return BURN_INTERVAL;
}
if (id == Item::stick->id)
{
return BURN_INTERVAL / 2;
}
if (id == Item::coal->id) return BURN_INTERVAL * 8;
if (id == Item::bucket_lava->id) return BURN_INTERVAL * 100;
if (id == Tile::sapling_Id) return BURN_INTERVAL / 2;
if (id == Item::blazeRod_Id) return BURN_INTERVAL * 12;
return 0;
}
bool FurnaceTileEntity::isFuel(shared_ptr<ItemInstance> item)
{
return getBurnDuration(item) > 0;
}
bool FurnaceTileEntity::stillValid(shared_ptr<Player> player)
{
if (level->getTileEntity(x, y, z) != shared_from_this() ) return false;
if (player->distanceToSqr(x + 0.5, y + 0.5, z + 0.5) > 8 * 8) return false;
return true;
}
void FurnaceTileEntity::setChanged()
{
return TileEntity::setChanged();
}
void FurnaceTileEntity::startOpen()
{
}
void FurnaceTileEntity::stopOpen()
{
}
bool FurnaceTileEntity::canPlaceItem(int slot, shared_ptr<ItemInstance> item)
{
if (slot == SLOT_RESULT) return false;
if (slot == SLOT_FUEL) return isFuel(item);
return true;
}
intArray FurnaceTileEntity::getSlotsForFace(int face)
{
if (face == Facing::DOWN)
{
return SLOTS_FOR_DOWN;
}
else if (face == Facing::UP)
{
return SLOTS_FOR_UP;
}
else
{
return SLOTS_FOR_SIDES;
}
}
bool FurnaceTileEntity::canPlaceItemThroughFace(int slot, shared_ptr<ItemInstance> item, int face)
{
return canPlaceItem(slot, item);
}
bool FurnaceTileEntity::canTakeItemThroughFace(int slot, shared_ptr<ItemInstance> item, int face)
{
if (face == Facing::DOWN && slot == SLOT_FUEL)
{
if (item->id != Item::bucket_empty_Id) return false;
}
return true;
}
// 4J Added
shared_ptr<TileEntity> FurnaceTileEntity::clone()
{
shared_ptr<FurnaceTileEntity> result = std::make_shared<FurnaceTileEntity>();
TileEntity::clone(result);
result->litTime = litTime;
result->tickCount = tickCount;
result->litDuration = litDuration;
for (unsigned int i = 0; i < items.length; i++)
{
if (items[i] != nullptr)
{
result->items[i] = ItemInstance::clone(items[i]);
}
}
return result;
}
|