-
Notifications
You must be signed in to change notification settings - Fork 497
/
ContainerGateInterface.java
478 lines (405 loc) · 13.6 KB
/
ContainerGateInterface.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* 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.transport.gui;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.TreeSet;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import cpw.mods.fml.relauncher.Side;
import buildcraft.BuildCraftCore;
import buildcraft.api.statements.IStatement;
import buildcraft.api.statements.IStatementParameter;
import buildcraft.api.statements.StatementManager;
import buildcraft.core.gui.BuildCraftContainer;
import buildcraft.core.network.BuildCraftPacket;
import buildcraft.core.network.CommandWriter;
import buildcraft.core.network.ICommandReceiver;
import buildcraft.core.network.PacketCommand;
import buildcraft.core.utils.Utils;
import buildcraft.transport.ActionActiveState;
import buildcraft.transport.Gate;
import buildcraft.transport.Pipe;
import buildcraft.transport.gates.GateDefinition;
public class ContainerGateInterface extends BuildCraftContainer implements ICommandReceiver {
public ActionActiveState[] actionsState = new ActionActiveState[8];
public GuiGateInterface gateCallback;
IInventory playerIInventory;
private final Pipe<?> pipe;
private Gate gate;
private final NavigableSet<IStatement> potentialTriggers = new TreeSet<IStatement>(new Comparator<IStatement>() {
@Override
public int compare(IStatement o1, IStatement o2) {
return o1.getUniqueTag().compareTo(o2.getUniqueTag());
}
});
private final NavigableSet<IStatement> potentialActions = new TreeSet<IStatement>(new Comparator<IStatement>() {
@Override
public int compare(IStatement o1, IStatement o2) {
return o1.getUniqueTag().compareTo(o2.getUniqueTag());
}
});
private boolean isSynchronized = false;
private boolean isNetInitialized = false;
private int lastTriggerState = 0;
public ContainerGateInterface(IInventory playerInventory, Pipe<?> pipe) {
super(0);
for (int i = 0; i < actionsState.length; ++i) {
actionsState[i] = ActionActiveState.Deactivated;
}
this.pipe = pipe;
this.playerIInventory = playerInventory;
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 9; x++) {
addSlotToContainer(new Slot(playerIInventory, x + y * 9 + 9, 8 + x * 18, 0));
}
}
for (int x = 0; x < 9; x++) {
addSlotToContainer(new Slot(playerIInventory, x, 8 + x * 18, 0));
}
}
public void init() {
if (gate == null) {
return;
}
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 9; x++) {
getSlot(x + y * 9).yDisplayPosition = gate.material.guiHeight - 84 + y * 18;
}
}
for (int x = 0; x < 9; x++) {
getSlot(x + 27).yDisplayPosition = gate.material.guiHeight - 26;
}
// Do not attempt to create a list of potential actions and triggers on
// the client.
if (!pipe.container.getWorldObj().isRemote) {
potentialTriggers.addAll(gate.getAllValidTriggers());
potentialActions.addAll(gate.getAllValidActions());
Iterator<IStatement> it = potentialTriggers.iterator();
while (it.hasNext()) {
IStatement trigger = it.next();
if (trigger.minParameters() > gate.material.numTriggerParameters) {
it.remove();
}
}
it = potentialActions.iterator();
while (it.hasNext()) {
IStatement action = it.next();
if (action.minParameters() > gate.material.numActionParameters) {
it.remove();
}
}
}
if (gateCallback != null) {
gateCallback.setGate(gate);
}
}
private static String[] statementsToStrings(Collection<IStatement> statements) {
final int size = statements.size();
String[] array = new String[size];
int pos = 0;
for (IStatement statement : statements) {
array[pos++] = statement.getUniqueTag();
}
return array;
}
private static void stringsToStatements(Collection<IStatement> statements, String[] strings) {
statements.clear();
for (String id : strings) {
statements.add(StatementManager.statements.get(id));
}
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return gate != null;
}
/**
* CLIENT SIDE *
*/
/**
* Marks client side gate container as needing to be synchronized with the
* server.
*/
public void markDirty() {
isSynchronized = false;
}
/**
* Initializes the list of triggers and actions on the gate and
* (re-)requests the current selection on the gate if needed.
*/
public void synchronize() {
if (!isNetInitialized && pipe.container.getWorldObj().isRemote) {
isNetInitialized = true;
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "initRequest", null));
}
if (!isSynchronized && pipe.container.getWorldObj().isRemote && gate != null) {
isSynchronized = true;
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "selectionRequest", null));
}
}
@Override
public void updateProgressBar(int id, int state) {
if (id == 0 /* Action state update */) {
for (int i = 0; i < 8; i++) {
/* Bit mask of triggers */
actionsState[i] = ActionActiveState.values()[(state >> (i * 2)) & 0x03];
}
}
}
/**
* SERVER SIDE *
*/
private int calculateTriggerState() {
if (gate == null) {
return 0;
}
int state = 0;
for (int i = 0; i < actionsState.length; i++) {
actionsState[i] = getActionState(i);
state |= (actionsState[i].ordinal() & 0x03) << i * 2;
}
return state;
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
int state = calculateTriggerState();
if (state != lastTriggerState) {
for (int i = 0; i < this.crafters.size(); i++) {
ICrafting viewingPlayer = (ICrafting) this.crafters.get(i);
viewingPlayer.sendProgressBarUpdate(this, 0 /* State update */, state);
}
lastTriggerState = state;
}
}
/**
* TRIGGERS *
*/
public boolean hasTriggers() {
return potentialTriggers.size() > 0;
}
public IStatement getFirstTrigger() {
if (potentialTriggers.isEmpty()) {
return null;
} else {
return potentialTriggers.first();
}
}
public IStatement getLastTrigger() {
if (potentialTriggers.isEmpty()) {
return null;
} else {
return potentialTriggers.last();
}
}
public Iterator<IStatement> getTriggerIterator(boolean descending) {
return descending ? potentialTriggers.descendingIterator() : potentialTriggers.iterator();
}
public ActionActiveState getActionState(int i) {
if (gate == null) {
return ActionActiveState.Deactivated;
} else {
return gate.actionsState [i];
}
}
public IStatementParameter getTriggerParameter(int trigger, int param) {
if (gate == null) {
return null;
} else {
return gate.getTriggerParameter(trigger, param);
}
}
/**
* ACTIONS *
*/
public boolean hasActions() {
return !potentialActions.isEmpty();
}
public IStatement getFirstAction() {
if (potentialActions.isEmpty()) {
return null;
} else {
return potentialActions.first();
}
}
public IStatement getLastAction() {
if (potentialActions.isEmpty()) {
return null;
} else {
return potentialActions.last();
}
}
public Iterator<IStatement> getActionIterator(boolean descending) {
return descending ? potentialActions.descendingIterator() : potentialActions.iterator();
}
// PACKET GENERATION
public BuildCraftPacket getStatementPacket(final String name, final int slot, final IStatement statement) {
final String statementKind = statement != null ? statement.getUniqueTag() : null;
return new PacketCommand(this, name, new CommandWriter() {
public void write(ByteBuf data) {
data.writeByte(slot);
Utils.writeUTF(data, statementKind);
}
});
}
public BuildCraftPacket getStatementParameterPacket(final String name, final int slot,
final int paramSlot, final IStatementParameter parameter) {
final String parameterName = parameter != null ? parameter.getUniqueTag() : null;
final NBTTagCompound parameterNBT = new NBTTagCompound();
if (parameter != null) {
parameter.writeToNBT(parameterNBT);
}
return new PacketCommand(this, name, new CommandWriter() {
public void write(ByteBuf data) {
data.writeByte(slot);
data.writeByte(paramSlot);
Utils.writeUTF(data, parameterName);
Utils.writeNBT(data, parameterNBT);
}
});
}
public void setGate(int direction) {
this.gate = pipe.gates[direction];
init();
}
@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
if (side.isServer()) {
EntityPlayer player = (EntityPlayer) sender;
if ("initRequest".equals(command)) {
final String[] triggerStrings = statementsToStrings(potentialTriggers);
final String[] actionStrings = statementsToStrings(potentialActions);
BuildCraftCore.instance.sendToPlayer(player, new PacketCommand(this, "init", new CommandWriter() {
public void write(ByteBuf data) {
data.writeByte(gate.getDirection().ordinal());
data.writeShort(triggerStrings.length);
data.writeShort(actionStrings.length);
for (String trigger : triggerStrings) {
Utils.writeUTF(data, trigger);
}
for (String action : actionStrings) {
Utils.writeUTF(data, action);
}
}
}));
} else if ("selectionRequest".equals(command)) {
for (int position = 0; position < gate.material.numSlots; position++) {
IStatement action = gate.getAction(position);
IStatement trigger = gate.getTrigger(position);
BuildCraftCore.instance.sendToPlayer(player, getStatementPacket("setAction", position, action));
BuildCraftCore.instance.sendToPlayer(player, getStatementPacket("setTrigger", position, trigger));
for (int p = 0; p < gate.material.numActionParameters; ++p) {
BuildCraftCore.instance.sendToPlayer(player, getStatementParameterPacket(
"setActionParameter", position, p, gate.getActionParameter(position, p)));
}
for (int q = 0; q < gate.material.numTriggerParameters; ++q) {
BuildCraftCore.instance.sendToPlayer(player, getStatementParameterPacket(
"setTriggerParameter", position, q, gate.getTriggerParameter(position, q)));
}
}
}
} else if (side.isClient()) {
if ("init".equals(command)) {
setGate(stream.readByte());
String[] triggerStrings = new String[stream.readShort()];
String[] actionStrings = new String[stream.readShort()];
for (int i = 0; i < triggerStrings.length; i++) {
triggerStrings[i] = Utils.readUTF(stream);
}
for (int i = 0; i < actionStrings.length; i++) {
actionStrings[i] = Utils.readUTF(stream);
}
stringsToStatements(this.potentialTriggers, triggerStrings);
stringsToStatements(this.potentialActions, actionStrings);
}
}
if ("setAction".equals(command)) {
setAction(stream.readUnsignedByte(), Utils.readUTF(stream), false);
} else if ("setTrigger".equals(command)) {
setTrigger(stream.readUnsignedByte(), Utils.readUTF(stream), false);
} else if ("setActionParameter".equals(command) || "setTriggerParameter".equals(command)) {
int slot = stream.readUnsignedByte();
int param = stream.readUnsignedByte();
String parameterName = Utils.readUTF(stream);
NBTTagCompound parameterData = Utils.readNBT(stream);
IStatementParameter parameter = null;
if (parameterName != null && parameterName.length() > 0) {
parameter = StatementManager.createParameter(parameterName);
}
if (parameter != null) {
parameter.readFromNBT(parameterData);
if ("setActionParameter".equals(command)) {
setActionParameter(slot, param, parameter, false);
} else {
setTriggerParameter(slot, param, parameter, false);
}
}
}
}
public void setAction(int action, String tag, boolean notifyServer) {
if (gate == null) {
return;
}
IStatement statement = null;
if (tag != null && tag.length() > 0) {
statement = (IStatement) StatementManager.statements.get(tag);
}
gate.setAction(action, statement);
if (pipe.container.getWorldObj().isRemote && notifyServer) {
BuildCraftCore.instance.sendToServer(getStatementPacket("setAction", action, statement));
}
}
public void setTrigger(int trigger, String tag, boolean notifyServer) {
if (gate == null) {
return;
}
IStatement statement = null;
if (tag != null && tag.length() > 0) {
statement = (IStatement) StatementManager.statements.get(tag);
}
gate.setTrigger(trigger, statement);
if (pipe.container.getWorldObj().isRemote && notifyServer) {
BuildCraftCore.instance.sendToServer(getStatementPacket("setTrigger", trigger, statement));
}
}
public void setActionParameter(int action, int param, IStatementParameter parameter, boolean notifyServer) {
if (gate == null) {
return;
}
gate.setActionParameter(action, param, parameter);
if (pipe.container.getWorldObj().isRemote && notifyServer) {
BuildCraftCore.instance.sendToServer(getStatementParameterPacket("setActionParameter", action, param, parameter));
}
}
public void setTriggerParameter(int trigger, int param, IStatementParameter parameter, boolean notifyServer) {
if (gate == null) {
return;
}
gate.setTriggerParameter(trigger, param, parameter);
if (pipe.container.getWorldObj().isRemote && notifyServer) {
BuildCraftCore.instance.sendToServer(getStatementParameterPacket("setTriggerParameter", trigger, param, parameter));
}
}
/**
* GATE INFORMATION *
*/
public ResourceLocation getGateGuiFile() {
return gate.material.guiFile;
}
public String getGateName() {
return GateDefinition.getLocalizedName(gate.material, gate.logic);
}
}