-
Notifications
You must be signed in to change notification settings - Fork 14
/
TileEntityCrucible.java
373 lines (314 loc) · 11.5 KB
/
TileEntityCrucible.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
366
367
368
369
370
371
372
373
package exnihiloomnia.blocks.crucibles.tileentity;
import exnihiloomnia.blocks.crucibles.tileentity.layers.CrucibleInventoryLayer;
import exnihiloomnia.registries.crucible.CrucibleRegistry;
import exnihiloomnia.registries.crucible.CrucibleRegistryEntry;
import exnihiloomnia.registries.crucible.HeatRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
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.SPacketUpdateTileEntity;
<<<<<<< HEAD
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
=======
>>>>>>> origin/master
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.common.util.Constants;
import net.minecraftforge.fluids.FluidStack;
<<<<<<< HEAD
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import javax.annotation.Nullable;
public class TileEntityCrucible extends TileEntity implements ITickable{
=======
public class TileEntityCrucible extends CrucibleInventoryLayer implements ITickable {
>>>>>>> origin/master
protected int updateTimer = 0;
protected int updateTimerMax = 8; //Sync if an update is required.
protected boolean updateQueued = false;
protected boolean updateTimerRunning = false;
protected ItemStack item;
protected int adjustedSpeed = 0;
protected int solidContent = 0;
protected int solidContentProcessed = 0;
protected int solidContentMax = 200000;
protected int solidFluidExchangeRate = 100;
<<<<<<< HEAD
private ItemStackHandler itemHandler = new ItemStackHandler(1) {
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
//ItemStack copy = stack.copy();
CrucibleRegistryEntry meltable = CrucibleRegistry.getItem(stack);
if (meltable != null) {
if (canInsertItem(stack)) {
if (hasSpaceFor(meltable.getSolidVolume())) {
stack.stackSize--;
item = stack;
//it doubles it for some reason
addSolid(meltable.getSolidVolume()/2);
sync();
return stack.stackSize == 0 ? null : stack;
}
}
}
return stack;
}
};
private FluidTank fluidTank = new FluidTank(8000) {
@Override
public FluidStack drain(FluidStack resource, boolean doDrain) {
if (!canDrainFluidType(getFluid())) {
return null;
}
return drain(resource.amount, doDrain);
}
@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
if (fluid != null) {
if (doDrain)
fluid.amount -= maxDrain;
sync();
return new FluidStack(fluid.getFluid(), maxDrain);
}
return null;
}
@Override
public int fill(FluidStack resource, boolean doFill) {
return 0;
}
@Override
public int getCapacity() {
return 8000;
}
@Override
public boolean canFill() {
return itemHandler.getStackInSlot(0) == null;
}
@Override
protected void onContentsChanged() {
markDirty();
}
};
public ItemStackHandler getItemHandler() {return itemHandler;}
public FluidTank getTank() {return fluidTank;}
public FluidStack getFluid() {return fluidTank.getFluid();}
public void addSolid(int amount) {
this.solidContent += (amount * 200);
}
public boolean hasSpaceFor(int amount) {
return solidContent + (amount * 200) <= solidContentMax;
=======
public void addSolid(int amount) {
this.solidContent += amount * 200;
}
public boolean hasSpaceFor(int amount) {
return solidContent + amount * 200 <= solidContentMax;
>>>>>>> origin/master
}
public double getSolidFullness() {
return (double) this.solidContent / (double) this.solidContentMax;
}
public double getFluidFullness() {
if (getFluid() != null)
<<<<<<< HEAD
return (double)getFluid().amount / (double) getTank().getCapacity();
else return 0;
}
public ItemStack getLastItemAdded()
{
return this.item;
}
public int getSolidContent()
{
=======
return (double) this.fluid.amount / (double) capacity;
else
return 0;
}
public ItemStack getLastItemAdded() {
return this.inventory;
}
public FluidStack getCurrentFluid() {
return this.fluid;
}
public int getSolidContent() {
>>>>>>> origin/master
return this.solidContent;
}
@Override
<<<<<<< HEAD
public void update() {
//remove stuff if is no longer valid eg config change
if (getFluid() !=null && item != null) {
=======
public void update() {
//remove stuff if is no longer valid eg config change
if (fluid != null && inventory != null) {
>>>>>>> origin/master
if (!CrucibleRegistry.containsItem(Block.getBlockFromItem(getLastItemAdded().getItem()), getLastItemAdded().getMetadata())) {
this.solidContent = 0;
this.item = null;
if (!CrucibleRegistry.containsFluid(this.getFluid().getFluid()))
getTank().setFluid(null);
}
}
//process solids
if (getFluid() == null && item != null)
getTank().setFluid(new FluidStack(CrucibleRegistry.getItem(Block.getBlockFromItem(getLastItemAdded().getItem()), getLastItemAdded().getMetadata()).fluid, 0));
if (this.solidContent <= 0)
<<<<<<< HEAD
this.item = null;
if (this.solidContent > 0 && getFluidFullness() < 1)
{
=======
this.inventory = null;
if (this.solidContent > 0 && getFluidFullness() < 1) {
>>>>>>> origin/master
int speed = this.getMeltingSpeed();
if (speed > solidContent * 2) {
speed = solidContent / 2;
}
this.adjustedSpeed = speed;
this.solidContentProcessed += speed;
this.solidContent -= speed * 2;
}
//transfer solids to fluids
if (getFluid() != null) {
while (getFluid().amount < getTank().getCapacity() && this.solidContentProcessed >= solidFluidExchangeRate) {
this.solidContentProcessed -= solidFluidExchangeRate;
getFluid().amount += CrucibleRegistry.getItem(Block.getBlockFromItem(item.getItem()), item.getMetadata()).getRatio();
}
}
//Packet throttling
if (!this.worldObj.isRemote) {
if (updateTimerRunning) {
updateTimer++;
if (updateTimer > updateTimerMax) {
updateTimer = 0;
if (updateQueued) {
updateQueued = false;
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(getPos()), getWorld().getBlockState(getPos()), 3);
}
else {
updateTimerRunning = false;
}
}
}
}
markDirty();
this.getWorld().checkLight(this.getPos());
}
//Send update packets to each client.
public void sync() {
if (getWorld() != null && !getWorld().isRemote) {
if (!updateTimerRunning) {
updateTimerRunning = true;
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(getPos()), getWorld().getBlockState(getPos()), 3);
}
else {
this.updateQueued = true;
}
}
}
public int getMeltingSpeed() {
if (getWorld().isAirBlock(getPos().down()))
return 0;
IBlockState state = getWorld().getBlockState(getPos().down());
if (HeatRegistry.containsItem(state.getBlock(), state.getBlock().getMetaFromState(state))) {
return HeatRegistry.getItem(state.getBlock(), state.getBlock().getMetaFromState(state)).value;
}
else {
return 0;
}
}
public double getTrueSpeed() {
double speed = this.adjustedSpeed;
return speed != 0 ? getLastItemAdded() != null ? ((double) 4 / 15 * speed - (double) 1 / 3) * CrucibleRegistry.getItem(Block.getBlockFromItem(getLastItemAdded().getItem()), getLastItemAdded().getMetadata()).getRatio() : 0 : 0;
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
return !oldState.getBlock().equals(newState.getBlock());
}
public boolean canInsertItem(ItemStack stack) {
if (CrucibleRegistry.containsItem(Block.getBlockFromItem(stack.getItem()), stack.getMetadata())) {
FluidStack fluid = getFluid();
CrucibleRegistryEntry block = CrucibleRegistry.getItem(Block.getBlockFromItem(stack.getItem()), stack.getMetadata());
return item == null ? fluid == null ? hasSpaceFor(block.getSolidVolume()) : fluid.getFluid() == block.fluid && hasSpaceFor(block.getSolidVolume()) : item.getItem().equals(stack.getItem()) && hasSpaceFor(block.getSolidVolume());
}
return false;
}
@Override
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
return (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY || capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ) && (facing == EnumFacing.UP || facing == null) || super.hasCapability(capability, facing);
}
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
if (facing == EnumFacing.UP || facing == null) {
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
return (T) fluidTank;
}
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return (T) itemHandler;
}
}
return super.getCapability(capability, facing);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
getTank().readFromNBT(compound);
this.solidContent = compound.getInteger("solid");
this.solidContentProcessed = compound.getInteger("processed");
NBTTagList items = compound.getTagList("items", Constants.NBT.TAG_COMPOUND);
if (items.tagCount() > 0)
{
NBTTagCompound item = items.getCompoundTagAt(0);
this.item = ItemStack.loadItemStackFromNBT(item);
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
getTank().writeToNBT(compound);
compound.setInteger("solid", solidContent);
compound.setInteger("processed", solidContentProcessed);
NBTTagList items = new NBTTagList();
if (item != null)
{
NBTTagCompound item = new NBTTagCompound();
this.item.writeToNBT(item);
items.appendTag(item);
}
compound.setTag("items", items);
return compound;
}
@Override
public NBTTagCompound getUpdateTag() {
return this.writeToNBT(new NBTTagCompound());
}
@Override
public void handleUpdateTag(NBTTagCompound tag) {
this.readFromNBT(tag);
}
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound tag = new NBTTagCompound();
this.writeToNBT(tag);
return new SPacketUpdateTileEntity(this.getPos(), this.getBlockMetadata(), tag);
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
NBTTagCompound tag = pkt.getNbtCompound();
this.readFromNBT(tag);
}
}