-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAssemblyTable.java
366 lines (294 loc) · 8.97 KB
/
TileAssemblyTable.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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.silicon;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.BuildCraftCore;
import buildcraft.api.recipes.CraftingResult;
import buildcraft.api.recipes.IFlexibleCrafter;
import buildcraft.api.recipes.IFlexibleRecipe;
import buildcraft.api.tiles.IControllable;
import buildcraft.core.network.CommandWriter;
import buildcraft.core.network.ICommandReceiver;
import buildcraft.core.network.PacketCommand;
import buildcraft.core.recipes.AssemblyRecipeManager;
import buildcraft.core.robots.EntityRobot;
import buildcraft.core.robots.ResourceIdAssemblyTable;
import buildcraft.core.robots.RobotRegistry;
import buildcraft.core.utils.StringUtils;
import buildcraft.core.utils.Utils;
public class TileAssemblyTable extends TileLaserTableBase implements IInventory, IFlexibleCrafter, ICommandReceiver {
public String currentRecipeId = "";
public IFlexibleRecipe<ItemStack> currentRecipe;
private HashSet<String> plannedOutput = new HashSet<String>();
public List<CraftingResult<ItemStack>> getPotentialOutputs() {
List<CraftingResult<ItemStack>> result = new LinkedList<CraftingResult<ItemStack>>();
for (IFlexibleRecipe recipe : AssemblyRecipeManager.INSTANCE.getRecipes()) {
CraftingResult<ItemStack> r = recipe.craft(this, true);
if (r != null) {
result.add(r);
}
}
return result;
}
@Override
public boolean canUpdate() {
return !FMLCommonHandler.instance().getEffectiveSide().isClient();
}
@Override
public void updateEntity() { // WARNING: run only server-side, see canUpdate()
super.updateEntity();
if (currentRecipe == null) {
return;
}
if (!currentRecipe.canBeCrafted(this)) {
setNextCurrentRecipe();
if (currentRecipe == null) {
return;
}
}
if (getEnergy() >= currentRecipe.craft(this, true).energyCost
&& lastMode != IControllable.Mode.Off) {
setEnergy(0);
if (currentRecipe.canBeCrafted(this)) {
ItemStack remaining = currentRecipe.craft(this, false).crafted.copy();
EntityRobot robot = RobotRegistry.getRegistry(worldObj)
.robotTaking(new ResourceIdAssemblyTable(this));
if (robot != null) {
remaining = robot.receiveItem(this, remaining);
}
if (remaining != null && remaining.stackSize > 0) {
remaining.stackSize -= Utils
.addToRandomInventoryAround(worldObj, xCoord, yCoord, zCoord, remaining);
}
if (remaining != null && remaining.stackSize > 0) {
remaining.stackSize -= Utils.addToRandomPipeAround(worldObj, xCoord, yCoord, zCoord, ForgeDirection.UNKNOWN, remaining);
}
if (remaining != null && remaining.stackSize > 0) {
EntityItem entityitem = new EntityItem(worldObj, xCoord + 0.5, yCoord + 0.7, zCoord + 0.5,
remaining);
worldObj.spawnEntityInWorld(entityitem);
}
setNextCurrentRecipe();
}
}
}
/* IINVENTORY */
@Override
public int getSizeInventory() {
return 12;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
super.setInventorySlotContents(slot, stack);
if (currentRecipe == null) {
setNextCurrentRecipe();
}
}
@Override
public String getInventoryName() {
return StringUtils.localize("tile.assemblyTableBlock.name");
}
@Override
public void readData(ByteBuf stream) {
super.readData(stream);
currentRecipeId = Utils.readUTF(stream);
plannedOutput.clear();
int size = stream.readUnsignedByte();
for (int i = 0; i < size; i++) {
plannedOutput.add(Utils.readUTF(stream));
}
}
@Override
public void writeData(ByteBuf stream) {
super.writeData(stream);
Utils.writeUTF(stream, currentRecipeId);
stream.writeByte(plannedOutput.size());
for (String s: plannedOutput) {
Utils.writeUTF(stream, s);
}
currentRecipe = AssemblyRecipeManager.INSTANCE.getRecipe(currentRecipeId);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList list = nbt.getTagList("plannedIds", Constants.NBT.TAG_STRING);
for (int i = 0; i < list.tagCount(); ++i) {
IFlexibleRecipe<ItemStack> recipe = AssemblyRecipeManager.INSTANCE.getRecipe(list.getStringTagAt(i));
if (recipe != null) {
plannedOutput.add(recipe.getId());
}
}
if (nbt.hasKey("recipeId")) {
IFlexibleRecipe<ItemStack> recipe = AssemblyRecipeManager.INSTANCE.getRecipe(nbt.getString("recipeId"));
if (recipe != null) {
setCurrentRecipe(recipe);
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
NBTTagList list = new NBTTagList();
for (String recipe : plannedOutput) {
list.appendTag(new NBTTagString(recipe));
}
nbt.setTag("plannedIds", list);
if (currentRecipe != null) {
nbt.setString("recipeId", currentRecipe.getId());
}
}
public boolean isPlanned(IFlexibleRecipe recipe) {
if (recipe == null) {
return false;
}
return plannedOutput.contains(recipe.getId());
}
public boolean isAssembling(IFlexibleRecipe recipe) {
return recipe != null && recipe == currentRecipe;
}
private void setCurrentRecipe(IFlexibleRecipe<ItemStack> recipe) {
currentRecipe = recipe;
if (recipe != null) {
currentRecipeId = recipe.getId();
} else {
currentRecipeId = "";
}
if (worldObj != null && !worldObj.isRemote) {
sendNetworkUpdate();
}
}
@Override
public int getRequiredEnergy() {
if (currentRecipe != null) {
CraftingResult<ItemStack> result = currentRecipe.craft(this, true);
if (result != null) {
return result.energyCost;
} else {
return 0;
}
} else {
return 0;
}
}
public void planOutput(IFlexibleRecipe<ItemStack> recipe) {
if (recipe != null && !isPlanned(recipe)) {
plannedOutput.add(recipe.getId());
if (!isAssembling(currentRecipe) || !isPlanned(currentRecipe)) {
setCurrentRecipe(recipe);
}
}
}
public void cancelPlanOutput(IFlexibleRecipe<ItemStack> recipe) {
if (isAssembling(recipe)) {
setCurrentRecipe(null);
}
plannedOutput.remove(recipe.getId());
if (!plannedOutput.isEmpty()) {
setCurrentRecipe(AssemblyRecipeManager.INSTANCE.getRecipe(plannedOutput.iterator().next()));
}
}
public void setNextCurrentRecipe() {
boolean takeNext = false;
for (String recipeId : plannedOutput) {
IFlexibleRecipe<ItemStack> recipe = AssemblyRecipeManager.INSTANCE.getRecipe(recipeId);
if (recipe == currentRecipe) {
takeNext = true;
} else if (takeNext && recipe.canBeCrafted(this)) {
setCurrentRecipe(recipe);
return;
}
}
for (String recipeId : plannedOutput) {
IFlexibleRecipe<ItemStack> recipe = AssemblyRecipeManager.INSTANCE.getRecipe(recipeId);
if (recipe.canBeCrafted(this)) {
setCurrentRecipe(recipe);
return;
}
}
setCurrentRecipe(null);
}
public void rpcSelectRecipe(final String id, final boolean select) {
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "select", new CommandWriter() {
public void write(ByteBuf data) {
Utils.writeUTF(data, id);
data.writeBoolean(select);
}
}));
}
@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
if (side.isServer() && "select".equals(command)) {
String id = Utils.readUTF(stream);
boolean select = stream.readBoolean();
IFlexibleRecipe<ItemStack> recipe = AssemblyRecipeManager.INSTANCE.getRecipe(id);
if (recipe != null) {
if (select) {
planOutput(recipe);
} else {
cancelPlanOutput(recipe);
}
}
sendNetworkUpdate();
}
}
@Override
public boolean hasWork() {
return currentRecipe != null && super.hasWork();
}
@Override
public boolean canCraft() {
return hasWork();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return true;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getCraftingItemStackSize() {
return getSizeInventory();
}
@Override
public ItemStack getCraftingItemStack(int slotid) {
return getStackInSlot(slotid);
}
@Override
public ItemStack decrCraftingItemStack(int slotid, int val) {
return decrStackSize(slotid, val);
}
@Override
public FluidStack getCraftingFluidStack(int tankid) {
return null;
}
@Override
public FluidStack decrCraftingFluidStack(int tankid, int val) {
return null;
}
@Override
public int getCraftingFluidStackSize() {
return 0;
}
}