/
TileMelter.java
365 lines (304 loc) · 10 KB
/
TileMelter.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
356
357
358
359
360
361
362
363
364
365
package knightminer.tcomplement.feature.tileentity;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import knightminer.tcomplement.common.TCompNetwork;
import knightminer.tcomplement.feature.client.GuiMelter;
import knightminer.tcomplement.feature.inventory.ContainerMelter;
import knightminer.tcomplement.feature.multiblock.MultiblockMelter;
import knightminer.tcomplement.feature.network.MelterFuelUpdatePacket;
import knightminer.tcomplement.library.TCompRegistry;
import knightminer.tcomplement.library.tanks.FluidHandlerDrainOnlyWrapper;
import knightminer.tcomplement.library.tanks.MelterTank;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.common.IInventoryGui;
import slimeknights.tconstruct.common.TinkerNetwork;
import slimeknights.tconstruct.library.TinkerRegistry;
import slimeknights.tconstruct.library.fluid.FluidTankAnimated;
import slimeknights.tconstruct.library.materials.Material;
import slimeknights.tconstruct.library.smeltery.MeltingRecipe;
import slimeknights.tconstruct.smeltery.block.BlockTank;
import slimeknights.tconstruct.smeltery.multiblock.MultiblockDetection;
import slimeknights.tconstruct.smeltery.network.HeatingStructureFuelUpdatePacket;
import slimeknights.tconstruct.smeltery.tileentity.TileHeatingStructureFuelTank;
import slimeknights.tconstruct.smeltery.tileentity.TileTank;
public class TileMelter extends TileHeatingStructureFuelTank<MultiblockMelter> implements ITickable, IInventoryGui {
private int tick;
// liquid stored inside
private FluidTankAnimated tank;
private IFluidHandler tankWrapper;
protected static final int CAPACITY = Material.VALUE_Ingot * 16;
public TileMelter() {
super("gui.tcomplement.melter.name", 3, 1);
setMultiblock(new MultiblockMelter(this));
tank = new MelterTank(CAPACITY, this);
tankWrapper = new FluidHandlerDrainOnlyWrapper(tank);
}
public FluidTankAnimated getTank() {
return tank;
}
public boolean isSolidFuel() {
return hasFuel() && currentFuel == null;
}
public TileHeater getSolidHeater() {
TileEntity te = world.getTileEntity(pos.down());
if(te instanceof TileHeater) {
return (TileHeater) te;
}
return null;
}
@Override
public void update() {
if(isClientWorld()) {
return;
}
// are we fully formed?
if(!isActive()) {
if(tick % 20 == 0) {
checkMultiblockStructure();
}
} else {
if(tick % 4 == 0) {
heatItems();
}
if(needsFuel) {
consumeFuel();
}
}
tick = (tick + 1) % 20;
}
@Override
protected void consumeFuel() {
// no need to consume fuel
if(hasFuel()) {
return;
}
// consume fuel!
// catch world being null as this is called during loading
World world = getWorld();
if(world == null) {
return;
}
currentTank = this.pos.down();
TileEntity te = world.getTileEntity(currentTank);
if(te instanceof TileTank) {
IFluidTank tank = ((TileTank) te).getInternalTank();
FluidStack liquid = tank.getFluid();
if(liquid != null) {
FluidStack in = liquid.copy();
int bonusFuel = TinkerRegistry.consumeSmelteryFuel(in);
int amount = liquid.amount - in.amount;
FluidStack drained = tank.drain(amount, false);
// we can drain. actually drain and add the fuel
if(drained != null && drained.amount == amount) {
tank.drain(amount, true);
currentFuel = drained.copy();
fuelQuality = bonusFuel;
addFuel(bonusFuel, drained.getFluid().getTemperature(drained) - 300); // convert to degree celcius
// notify client of fuel/temperature changes
if(isServerWorld()) {
TinkerNetwork.sendToAll(new HeatingStructureFuelUpdatePacket(pos, currentTank, temperature, currentFuel));
}
return;
}
}
fuelQuality = 0;
} else if (te instanceof TileHeater) {
TileHeater heater = (TileHeater) te;
ItemStack stack = heater.getStackInSlot(0);
if(!stack.isEmpty()) {
ItemStack fuel = stack.copy();
int time = TileEntityFurnace.getItemBurnTime(fuel) / 2;
if(time > 0) {
currentFuel = null;
fuelQuality = time;
// just about enough to melt clay or most metals, but not iron
// also, about the temperature of a conventional oven I guess
addFuel(time, 200);
fuel.shrink(1);
// if the stack is now empty, return the container
if(fuel.isEmpty()) {
fuel = stack.getItem().getContainerItem(fuel);
}
// set the heater fuel
heater.setInventorySlotContents(0, fuel);
// notify client of fuel/temperature changes
if(isServerWorld()) {
TCompNetwork.sendToAll(new MelterFuelUpdatePacket(pos, temperature));
}
return;
}
}
fuelQuality = 0;
}
}
@Override
protected void updateStructureInfo(MultiblockDetection.MultiblockStructure structure) {
if(structure.blocks.size() < 2) {
return;
}
tanks.clear();
BlockPos down = pos.down();
if(getWorld().getBlockState(down).getBlock() instanceof BlockTank) {
// find all tanks for input
tanks.add(down);
if(isSolidFuel()) {
this.fuel = 0;
this.temperature = 0;
this.needsFuel = true;
}
}
this.resize(3);
}
@Override
protected int getUpdatedInventorySize(int width, int height, int depth) {
return 3;
}
/* Smeltery logic */
@Override
protected void updateHeatRequired(int index) {
ItemStack stack = getStackInSlot(index);
if(!stack.isEmpty()) {
MeltingRecipe melting = TCompRegistry.getMelting(stack);
if(melting != null) {
FluidStack current = tank.getFluid();
if(current == null || current.getFluid() == melting.getResult().getFluid()) {
setHeatRequiredForSlot(index, Math.max(5, melting.getUsableTemperature()));
// instantly consume fuel if required
if(!hasFuel()) {
consumeFuel();
}
} else {
setHeatRequiredForSlot(index, Math.max(5, melting.getUsableTemperature()));
}
return;
}
}
setHeatRequiredForSlot(index, 0);
}
// melt stuff
@Override
protected boolean onItemFinishedHeating(ItemStack stack, int slot) {
MeltingRecipe recipe = TCompRegistry.getMelting(stack);
if(recipe == null) {
return false;
}
// fluid is different
FluidStack fluid = recipe.output.copy();
FluidStack current = getTank().getFluid();
if(current != null && current.getFluid() != fluid.getFluid()) {
itemTemperatures[slot] = itemTempRequired[slot] * 3 + 1;
return false;
}
// unfortunately, the event requires a smeltery, so not fired
//TinkerSmelteryEvent.OnMelting event = TinkerSmelteryEvent.OnMelting.fireEvent(this, stack, recipe.output.copy());
int filled = getTank().fill(fluid.copy(), false);
if(filled == fluid.amount) {
getTank().fill(fluid, true);
// only clear out items n stuff if it was successful
setInventorySlotContents(slot, ItemStack.EMPTY);
return true;
}
else {
// can't fill into the melter, set error state
itemTemperatures[slot] = itemTempRequired[slot] * 2 + 1;
}
return false;
}
/* Fluid interactions */
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
return true;
}
return super.hasCapability(capability, facing);
}
@Nonnull
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
// only allow extraction
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.cast(tankWrapper);
}
return super.getCapability(capability, facing);
}
/* Client sync */
// called only clientside to sync with the server
@SideOnly(Side.CLIENT)
public void updateFluidTo(FluidStack fluid) {
int oldAmount = tank.getFluidAmount();
tank.setFluid(fluid);
tank.renderOffset += tank.getFluidAmount() - oldAmount;
}
/* GUI */
@Override
public Container createContainer(InventoryPlayer inventoryplayer, World world, BlockPos pos) {
return new ContainerMelter(inventoryplayer, this);
}
@Override
@SideOnly(Side.CLIENT)
public GuiContainer createGui(InventoryPlayer inventoryplayer, World world, BlockPos pos) {
return new GuiMelter((ContainerMelter)createContainer(inventoryplayer, world, pos), this);
}
@Override
@SideOnly(Side.CLIENT)
public FuelInfo getFuelDisplay() {
FuelInfo info = new FuelInfo();
// just return the current contents of the tank below, don't worry about last fluid
if(hasFuel() && currentFuel != null) {
info.fluid = currentFuel.copy();
info.heat = this.temperature + 300;
info.maxCap = currentFuel.amount;
} else {
IFluidTank tank = getTankAt(pos.down());
if(tank != null) {
FluidStack fuel = tank.getFluid();
if(fuel != null) {
info.fluid = fuel.copy();
}
info.heat = this.temperature + 300;
info.maxCap = tank.getCapacity();
}
}
return info;
}
/**
* Grabs the tank at the given location (if present)
*/
private IFluidTank getTankAt(BlockPos pos) {
TileEntity te = getWorld().getTileEntity(pos);
if(te instanceof TileTank) {
return ((TileTank) te).getInternalTank();
}
return null;
}
/* NBT */
@Nonnull
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound = super.writeToNBT(compound);
getTank().writeToNBT(compound);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
getTank().readFromNBT(compound);
}
}