-
Notifications
You must be signed in to change notification settings - Fork 756
/
TileCasting.java
287 lines (242 loc) · 9.29 KB
/
TileCasting.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
package slimeknights.tconstruct.smeltery.tileentity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.SPacketParticles;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ITickable;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.wrapper.SidedInvWrapper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import slimeknights.tconstruct.common.TinkerNetwork;
import slimeknights.tconstruct.library.fluid.FluidHandlerCasting;
import slimeknights.tconstruct.library.fluid.FluidTankAnimated;
import slimeknights.tconstruct.library.smeltery.ICastingRecipe;
import slimeknights.tconstruct.library.tileentity.IProgress;
import slimeknights.tconstruct.shared.tileentity.TileTable;
import slimeknights.tconstruct.smeltery.events.TinkerCastingEvent;
import slimeknights.tconstruct.smeltery.network.FluidUpdatePacket;
public abstract class TileCasting extends TileTable implements ITickable, ISidedInventory, IProgress {
// the internal fluidtank of the casting block
public FluidTankAnimated tank;
public IFluidHandler fluidHandler;
protected int timer; // timer for recipe cooldown
protected ICastingRecipe recipe; // current recipe
public TileCasting() {
super("casting", 2, 1); // 2 slots. 0 == input, 1 == output
// initialize with empty tank
tank = new FluidTankAnimated(0, this);
fluidHandler = new FluidHandlerCasting(this, tank);
// use a SidedInventory Wrapper to respect the canInsert/Extract calls
this.itemHandler = new SidedInvWrapper(this, EnumFacing.DOWN);
}
// capability
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
return true;
}
return super.hasCapability(capability, facing);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
return (T) fluidHandler;
}
return super.getCapability(capability, facing);
}
/* Inventory Management */
public void interact(EntityPlayer player) {
// can't interact if liquid inside
if(tank.getFluidAmount() > 0) {
return;
}
// completely empty -> insert current item into input
if(!isStackInSlot(0) && !isStackInSlot(1)) {
ItemStack stack = player.inventory.decrStackSize(player.inventory.currentItem, stackSizeLimit);
setInventorySlotContents(0, stack);
}
// take item out
else {
// take out of stack 1 if something is in there, 0 otherwise
int slot = isStackInSlot(1) ? 1 : 0;
// Additional Info: Only 1 item can only be put into the casting block usually, however recipes
// can have Itemstacks with stacksize > 1 as output
// we therefore spill the whole contents on extraction
ItemStack stack = getStackInSlot(slot);
if(slot == 1) {
FMLCommonHandler.instance().firePlayerSmeltedEvent(player, stack);
}
ItemHandlerHelper.giveItemToPlayer(player, stack);
setInventorySlotContents(slot, ItemStack.EMPTY);
// send a block update for the comparator, needs to be done after the stack is removed
if(slot == 1) {
this.getWorld().notifyNeighborsOfStateChange(this.pos, this.getBlockType(), true);
}
}
}
@Nonnull
@Override
public int[] getSlotsForFace(@Nonnull EnumFacing side) {
return new int[]{0, 1};
}
@Override
public boolean canInsertItem(int index, @Nonnull ItemStack itemStackIn, @Nonnull EnumFacing direction) {
return index == 0 && !isStackInSlot(1);
}
@Override
public boolean canExtractItem(int index, @Nonnull ItemStack stack, @Nonnull EnumFacing direction) {
return index == 1;
}
/* Logic */
@Override
public void update() {
// no recipeeeh
if(recipe == null) {
return;
}
// fully filled
if(tank.getFluidAmount() == tank.getCapacity()) {
timer++;
if(!getWorld().isRemote) {
if(timer >= recipe.getTime()) {
TinkerCastingEvent.OnCasted event = TinkerCastingEvent.OnCasted.fire(recipe, this);
// done, finish!
if(event.consumeCast) {
// todo: play breaking sound and animation
setInventorySlotContents(0, ItemStack.EMPTY);
for(EntityPlayer player : getWorld().playerEntities) {
if(player.getDistanceSq(pos) < 1024 && player instanceof EntityPlayerMP) {
TinkerNetwork.sendPacket(player, new SPacketParticles(EnumParticleTypes.FLAME, false,
pos.getX() + 0.5f,
pos.getY() + 1.1f,
pos.getZ() + 0.5f,
0.25f, 0.0125f, 0.25f,
0.005f, 5));
}
}
}
// put result into output
if(event.switchOutputs) {
setInventorySlotContents(1, getStackInSlot(0));
setInventorySlotContents(0, event.output);
}
else {
setInventorySlotContents(1, event.output);
}
getWorld().playSound(null, pos, SoundEvents.BLOCK_LAVA_EXTINGUISH, SoundCategory.AMBIENT, 0.07f, 4f);
// reset state
reset();
// comparator update
getWorld().notifyNeighborsOfStateChange(this.pos, this.getBlockType(), true);
}
}
else if(getWorld().rand.nextFloat() > 0.9f) {
getWorld().spawnParticle(EnumParticleTypes.SMOKE_NORMAL, pos.getX() + getWorld().rand.nextDouble(), pos.getY() + 1.1, pos.getZ() + getWorld().rand.nextDouble(), 0.0D, 0.0D, 0.0D);
}
}
}
@Override
public float getProgress() {
if(recipe == null || tank.getFluidAmount() == 0) {
return 0f;
}
return Math.min(1f, (float) timer / (float) recipe.getTime());
}
public ItemStack getCurrentResult() {
if(recipe == null) {
return null;
}
Fluid fluid = null;
if(tank.getFluid() != null) {
fluid = tank.getFluid().getFluid();
}
return recipe.getResult(getStackInSlot(0), fluid);
}
/** Return the recipe for the current state, if one exists. Don't forget to fire the OnCasting event! */
protected abstract ICastingRecipe findRecipe(ItemStack cast, Fluid fluid);
protected ICastingRecipe findRecipe(Fluid fluid) {
ICastingRecipe recipe = findRecipe(getStackInSlot(0), fluid);
if(TinkerCastingEvent.OnCasting.fire(recipe, this)) {
return recipe;
}
// event was cancelled
return null;
}
/** Sets the state for a new casting recipe, returns the fluid amount needed for casting */
public int initNewCasting(Fluid fluid, boolean setNewRecipe) {
ICastingRecipe recipe = findRecipe(fluid);
if(recipe != null) {
if(setNewRecipe) {
this.recipe = recipe;
}
return recipe.getFluidAmount();
}
return 0;
}
/** Resets the current state completely */
public void reset() {
timer = 0;
recipe = null;
tank.setCapacity(0);
tank.setFluid(null);
tank.renderOffset = 0;
if(getWorld() != null && !getWorld().isRemote && getWorld() instanceof WorldServer) {
TinkerNetwork.sendToClients((WorldServer) getWorld(), pos, new FluidUpdatePacket(pos, null));
}
}
// called clientside to sync with the server and on load
public void updateFluidTo(FluidStack fluid) {
int oldAmount = tank.getFluidAmount();
tank.setFluid(fluid);
if(fluid == null) {
reset();
return;
}
else if(recipe == null) {
recipe = findRecipe(fluid.getFluid());
if(recipe != null) {
tank.setCapacity(recipe.getFluidAmount());
}
}
tank.renderOffset += tank.getFluidAmount() - oldAmount;
}
/**
* @return The current comparator strength based on if an output exists
*/
public int comparatorStrength() {
return isStackInSlot(1) ? 15 : 0;
}
/* Saving and Loading */
@Nonnull
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tags) {
tags = super.writeToNBT(tags);
NBTTagCompound tankTag = new NBTTagCompound();
tank.writeToNBT(tankTag);
tags.setTag("tank", tankTag);
tags.setInteger("timer", timer);
return tags;
}
@Override
public void readFromNBT(NBTTagCompound tags) {
super.readFromNBT(tags);
tank.readFromNBT(tags.getCompoundTag("tank"));
updateFluidTo(tank.getFluid());
timer = tags.getInteger("timer");
}
}