-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAssemblyTable.java
315 lines (250 loc) · 7.61 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
package buildcraft.silicon;
import buildcraft.core.recipes.AssemblyRecipeManager;
import buildcraft.api.gates.IAction;
import buildcraft.core.DefaultProps;
import buildcraft.core.IMachine;
import buildcraft.core.network.PacketIds;
import buildcraft.core.network.PacketNBT;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import buildcraft.core.recipes.AssemblyRecipeManager.AssemblyRecipe;
import buildcraft.core.utils.StringUtils;
import cpw.mods.fml.common.FMLCommonHandler;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.ForgeDirection;
public class TileAssemblyTable extends TileLaserTableBase implements IMachine, IInventory {
Set<AssemblyRecipe> plannedOutput = new LinkedHashSet<AssemblyRecipe>();
public AssemblyRecipe currentRecipe;
public static class SelectionMessage {
public boolean select;
public ItemStack stack;
public NBTTagCompound getNBT() {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setBoolean("s", select);
NBTTagCompound itemNBT = new NBTTagCompound();
stack.writeToNBT(itemNBT);
nbt.setCompoundTag("i", itemNBT);
return nbt;
}
public void fromNBT(NBTTagCompound nbt) {
select = nbt.getBoolean("s");
NBTTagCompound itemNBT = nbt.getCompoundTag("i");
stack = ItemStack.loadItemStackFromNBT(itemNBT);
}
}
public List<AssemblyRecipe> getPotentialOutputs() {
List<AssemblyRecipe> result = new LinkedList<AssemblyRecipe>();
for (AssemblyRecipe recipe : AssemblyRecipeManager.INSTANCE.getRecipes()) {
if (recipe.canBeDone(this))
result.add(recipe);
}
return result;
}
@Override
public boolean canUpdate() {
return !FMLCommonHandler.instance().getEffectiveSide().isClient();
}
@Override
public void updateEntity() { // WARNING: run only server-side, see canUpdate()
if (currentRecipe == null)
return;
if (!currentRecipe.canBeDone(this)) {
setNextCurrentRecipe();
if (currentRecipe == null)
return;
}
if (getEnergy() >= currentRecipe.getEnergyCost()) {
setEnergy(0);
if (currentRecipe.canBeDone(this)) {
currentRecipe.useItems(this);
ItemStack remaining = currentRecipe.output.copy();
remaining.stackSize -= Utils.addToRandomInventoryAround(worldObj, xCoord, yCoord, zCoord, remaining);
if (remaining.stackSize > 0) {
remaining.stackSize -= Utils.addToRandomPipeAround(worldObj, xCoord, yCoord, zCoord, ForgeDirection.UNKNOWN, remaining);
}
if (remaining.stackSize > 0) {
EntityItem entityitem = new EntityItem(worldObj, xCoord + 0.5, yCoord + 0.7, zCoord + 0.5, currentRecipe.output.copy());
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 getInvName() {
return StringUtils.localize("tile.assemblyTableBlock");
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList list = nbt.getTagList("planned");
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound cpt = (NBTTagCompound) list.tagAt(i);
ItemStack stack = ItemStack.loadItemStackFromNBT(cpt);
for (AssemblyRecipe r : AssemblyRecipeManager.INSTANCE.getRecipes()) {
if (r.output.itemID == stack.itemID && r.output.getItemDamage() == stack.getItemDamage()) {
plannedOutput.add(r);
break;
}
}
}
if (nbt.hasKey("recipe")) {
ItemStack stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("recipe"));
for (AssemblyRecipe r : plannedOutput) {
if (r.output.itemID == stack.itemID && r.output.getItemDamage() == stack.getItemDamage()) {
setCurrentRecipe(r);
break;
}
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
NBTTagList list = new NBTTagList();
for (AssemblyRecipe recipe : plannedOutput) {
NBTTagCompound cpt = new NBTTagCompound();
recipe.output.writeToNBT(cpt);
list.appendTag(cpt);
}
nbt.setTag("planned", list);
if (currentRecipe != null) {
NBTTagCompound recipe = new NBTTagCompound();
currentRecipe.output.writeToNBT(recipe);
nbt.setTag("recipe", recipe);
}
}
public boolean isPlanned(AssemblyRecipe recipe) {
if (recipe == null)
return false;
return plannedOutput.contains(recipe);
}
public boolean isAssembling(AssemblyRecipe recipe) {
return recipe != null && recipe == currentRecipe;
}
private void setCurrentRecipe(AssemblyRecipe recipe) {
this.currentRecipe = recipe;
}
@Override
public double getRequiredEnergy() {
if (currentRecipe != null) {
return currentRecipe.getEnergyCost();
}
return 0;
}
public void planOutput(AssemblyRecipe recipe) {
if (recipe != null && !isPlanned(recipe)) {
plannedOutput.add(recipe);
if (!isAssembling(currentRecipe) || !isPlanned(currentRecipe)) {
setCurrentRecipe(recipe);
}
}
}
public void cancelPlanOutput(AssemblyRecipe recipe) {
if (isAssembling(recipe)) {
setCurrentRecipe(null);
}
plannedOutput.remove(recipe);
if (!plannedOutput.isEmpty()) {
setCurrentRecipe(plannedOutput.iterator().next());
}
}
public void setNextCurrentRecipe() {
boolean takeNext = false;
for (AssemblyRecipe recipe : plannedOutput) {
if (recipe == currentRecipe) {
takeNext = true;
} else if (takeNext && recipe.canBeDone(this)) {
setCurrentRecipe(recipe);
return;
}
}
for (AssemblyRecipe recipe : plannedOutput) {
if (recipe.canBeDone(this)) {
setCurrentRecipe(recipe);
return;
}
}
setCurrentRecipe(null);
}
public void handleSelectionMessage(SelectionMessage message) {
for (AssemblyRecipe recipe : AssemblyRecipeManager.INSTANCE.getRecipes()) {
if (recipe.output.isItemEqual(message.stack) && ItemStack.areItemStackTagsEqual(recipe.output, message.stack)) {
if (message.select) {
planOutput(recipe);
} else {
cancelPlanOutput(recipe);
}
break;
}
}
}
public void sendSelectionTo(EntityPlayer player) {
for (AssemblyRecipe recipe : AssemblyRecipeManager.INSTANCE.getRecipes()) {
SelectionMessage message = new SelectionMessage();
message.stack = recipe.output;
if (isPlanned(recipe)) {
message.select = true;
} else {
message.select = false;
}
PacketNBT packet = new PacketNBT(PacketIds.SELECTION_ASSEMBLY_SEND, message.getNBT(), xCoord, yCoord, zCoord);
packet.posX = xCoord;
packet.posY = yCoord;
packet.posZ = zCoord;
// FIXME: This needs to be switched over to new synch system.
CoreProxy.proxy.sendToPlayers(packet.getPacket(), worldObj, (int) player.posX, (int) player.posY, (int) player.posZ,
DefaultProps.NETWORK_UPDATE_RANGE);
}
}
@Override
public boolean isActive() {
return currentRecipe != null;
}
@Override
public boolean manageFluids() {
return false;
}
@Override
public boolean manageSolids() {
return false;
}
@Override
public boolean allowAction(IAction action) {
return false;
}
@Override
public boolean canCraft() {
return currentRecipe != null;
}
@Override
public boolean isInvNameLocalized() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return true;
}
}