-
Notifications
You must be signed in to change notification settings - Fork 497
/
ContainerGateInterface.java
410 lines (342 loc) · 10.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
/**
* 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 net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ResourceLocation;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IStatementParameter;
import buildcraft.api.gates.IStatement;
import buildcraft.api.gates.ITrigger;
import buildcraft.api.gates.IStatementParameter;
import buildcraft.api.gates.StatementManager;
import buildcraft.core.gui.BuildCraftContainer;
import buildcraft.core.network.RPC;
import buildcraft.core.network.RPCHandler;
import buildcraft.core.network.RPCMessageInfo;
import buildcraft.core.network.RPCSide;
import buildcraft.transport.ActionActiveState;
import buildcraft.transport.Gate;
import buildcraft.transport.Pipe;
import buildcraft.transport.gates.GateDefinition;
public class ContainerGateInterface extends BuildCraftContainer {
public ActionActiveState[] actionsState = new ActionActiveState[8];
public GuiGateInterface gateCallback;
IInventory playerIInventory;
private final Pipe<?> pipe;
private Gate gate;
private final NavigableSet<ITrigger> potentialTriggers = new TreeSet<ITrigger>(new Comparator<ITrigger>() {
@Override
public int compare(ITrigger o1, ITrigger o2) {
return o1.getUniqueTag().compareTo(o2.getUniqueTag());
}
});
private final NavigableSet<IAction> potentialActions = new TreeSet<IAction>(new Comparator<IAction>() {
@Override
public int compare(IAction o1, IAction 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());
if (gate.material.numTriggerParameters == 0) {
Iterator<ITrigger> it = potentialTriggers.iterator();
while (it.hasNext()) {
ITrigger trigger = it.next();
if (trigger.minParameters() > 0) {
it.remove();
}
}
}
if (gate.material.numActionParameters == 0) {
Iterator<IAction> it = potentialActions.iterator();
while (it.hasNext()) {
IAction action = it.next();
if (action.minParameters() > 0) {
it.remove();
}
}
}
}
if (gateCallback != null) {
gateCallback.setGate(gate);
}
}
private static <T extends IStatement> String[] statementsToStrings(Collection<T> statements) {
final int size = statements.size();
String[] array = new String[size];
int pos = 0;
for (T statement : statements) {
array[pos++] = statement.getUniqueTag();
}
return array;
}
private static <T extends IStatement> void stringsToStatements(Collection<T> statements, String[] strings) {
statements.clear();
for (String id : strings) {
statements.add((T) 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;
RPCHandler.rpcServer(this, "initRequest");
}
if (!isSynchronized && pipe.container.getWorldObj().isRemote && gate != null) {
isSynchronized = true;
RPCHandler.rpcServer(this, "selectionRequest");
}
}
@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;
}
}
/**
* Sends gate info to the client
*/
@RPC(RPCSide.SERVER)
public void initRequest(RPCMessageInfo info) {
EntityPlayer player = info.sender;
RPCHandler.rpcPlayer(player, this, "setGate", gate.getDirection().ordinal());
RPCHandler.rpcPlayer(player, this, "setPotential", statementsToStrings(potentialActions), statementsToStrings(potentialTriggers));
}
@RPC(RPCSide.CLIENT)
public void setPotential(String[] potentialActions, String[] potentialTriggers) {
stringsToStatements(this.potentialActions, potentialActions);
stringsToStatements(this.potentialTriggers, potentialTriggers);
}
@RPC(RPCSide.CLIENT)
public void setGate(int direction) {
this.gate = pipe.gates[direction];
init();
}
@RPC(RPCSide.SERVER)
public void selectionRequest(RPCMessageInfo info) {
EntityPlayer player = info.sender;
for (int position = 0; position < gate.material.numSlots; position++) {
IAction action = gate.getAction(position);
ITrigger trigger = gate.getTrigger(position);
RPCHandler.rpcPlayer(player, this, "setAction", position, action != null ? action.getUniqueTag() : null, false);
RPCHandler.rpcPlayer(player, this, "setTrigger", position, trigger != null ? trigger.getUniqueTag() : null, false);
for (int p = 0; p < 3; ++p) {
RPCHandler.rpcPlayer(player, this, "setActionParameter", position, p,
gate.getActionParameter(position, p), false);
RPCHandler.rpcPlayer(player, this, "setTriggerParameter", position, p,
gate.getTriggerParameter(position, p), false);
}
}
}
/**
* TRIGGERS *
*/
public boolean hasTriggers() {
return potentialTriggers.size() > 0;
}
public ITrigger getFirstTrigger() {
if (potentialTriggers.isEmpty()) {
return null;
} else {
return potentialTriggers.first();
}
}
public ITrigger getLastTrigger() {
if (potentialTriggers.isEmpty()) {
return null;
} else {
return potentialTriggers.last();
}
}
public Iterator<ITrigger> 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];
}
}
@RPC(RPCSide.BOTH)
public void setTrigger(int trigger, String tag, boolean notifyServer) {
if (gate == null) {
return;
}
if (tag != null) {
gate.setTrigger(trigger, (ITrigger) StatementManager.statements.get(tag));
} else {
gate.setTrigger(trigger, null);
}
if (pipe.container.getWorldObj().isRemote && notifyServer) {
RPCHandler.rpcServer(this, "setTrigger", trigger, tag, false);
}
}
@RPC(RPCSide.BOTH)
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) {
RPCHandler.rpcServer(this, "setTriggerParameter", trigger, param, parameter, false);
}
}
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 IAction getFirstAction() {
if (potentialActions.isEmpty()) {
return null;
} else {
return potentialActions.first();
}
}
public IAction getLastAction() {
if (potentialActions.isEmpty()) {
return null;
} else {
return potentialActions.last();
}
}
public Iterator<IAction> getActionIterator(boolean descending) {
return descending ? potentialActions.descendingIterator() : potentialActions.iterator();
}
@RPC(RPCSide.BOTH)
public void setAction(int action, String tag, boolean notifyServer) {
if (gate == null) {
return;
}
if (tag != null) {
gate.setAction(action, (IAction) StatementManager.statements.get(tag));
} else {
gate.setAction(action, null);
}
if (pipe.container.getWorldObj().isRemote && notifyServer) {
RPCHandler.rpcServer(this, "setAction", action, tag, false);
}
}
@RPC(RPCSide.BOTH)
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) {
RPCHandler.rpcServer(this, "setActionParameter", action, param, parameter, false);
}
}
/**
* GATE INFORMATION *
*/
public ResourceLocation getGateGuiFile() {
return gate.material.guiFile;
}
public String getGateName() {
return GateDefinition.getLocalizedName(gate.material, gate.logic);
}
}