-
Notifications
You must be signed in to change notification settings - Fork 497
/
ActionIterator.java
executable file
·101 lines (82 loc) · 2.25 KB
/
ActionIterator.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
/**
* 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.gates;
import java.util.Iterator;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.transport.Gate;
import buildcraft.transport.Pipe;
public class ActionIterator implements Iterable<StatementSlot> {
private Pipe pipe;
public ActionIterator(Pipe iPipe) {
pipe = iPipe;
}
@Override
public Iterator<StatementSlot> iterator() {
return new It();
}
private class It implements Iterator<StatementSlot> {
private ForgeDirection curDir = ForgeDirection.values()[0];
private int index = 0;
private StatementSlot next;
public It() {
while (!isValid()) {
if (curDir == ForgeDirection.UNKNOWN) {
break;
} else if (pipe.gates[curDir.ordinal()] == null
|| index >= pipe.gates[curDir.ordinal()].activeActions.size() - 1) {
index = 0;
curDir = ForgeDirection.values()[curDir.ordinal() + 1];
} else {
index++;
}
}
if (isValid()) {
next = pipe.gates[curDir.ordinal()].activeActions.get(index);
}
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public StatementSlot next() {
StatementSlot result = next;
while (true) {
if (index < Gate.MAX_STATEMENTS - 1) {
index++;
} else if (curDir != ForgeDirection.UNKNOWN) {
index = 0;
curDir = ForgeDirection.values()[curDir.ordinal() + 1];
} else {
break;
}
if (isValid()) {
break;
}
}
if (isValid()) {
next = new StatementSlot();
next.statement = pipe.gates[curDir.ordinal()].actions[index];
next.parameters = pipe.gates[curDir.ordinal()].actionParameters[index];
} else {
next = null;
}
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not supported.");
}
private boolean isValid() {
return curDir != ForgeDirection.UNKNOWN
&& pipe.gates[curDir.ordinal()] != null
&& index < pipe.gates[curDir.ordinal()].activeActions.size();
}
}
}