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
|
package gregtech.api.interfaces;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.ToLongFunction;
import net.minecraft.block.Block;
import net.minecraftforge.common.util.ForgeDirection;
import com.google.common.collect.ImmutableList;
import com.gtnewhorizon.structurelib.structure.IStructureElement;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.util.GTStructureUtility;
import gregtech.api.util.IGTHatchAdder;
public interface IHatchElement<T> {
List<? extends Class<? extends IMetaTileEntity>> mteClasses();
IGTHatchAdder<? super T> adder();
String name();
long count(T t);
default <T2 extends T> IHatchElement<T2> withMteClass(Class<? extends IMetaTileEntity> aClass) {
if (aClass == null) throw new IllegalArgumentException();
return withMteClasses(Collections.singletonList(aClass));
}
@SuppressWarnings("unchecked") // can't set SafeVarargs :(
default <T2 extends T> IHatchElement<T2> withMteClasses(Class<? extends IMetaTileEntity>... aClasses) {
if (aClasses == null) throw new IllegalArgumentException();
return withMteClasses(Arrays.asList(aClasses));
}
default <T2 extends T> IHatchElement<T2> withMteClasses(List<Class<? extends IMetaTileEntity>> aClasses) {
if (aClasses == null) throw new IllegalArgumentException();
return new HatchElement<>(aClasses, null, null, null, this);
}
default <T2 extends T> IHatchElement<T2> withAdder(IGTHatchAdder<T2> aAdder) {
if (aAdder == null) throw new IllegalArgumentException();
return new HatchElement<>(null, aAdder, null, null, this);
}
default IHatchElement<T> withName(String aName) {
if (aName == null) throw new IllegalArgumentException();
return new HatchElement<>(null, null, aName, null, this);
}
default <T2 extends T> IHatchElement<T2> withCount(ToLongFunction<T2> aCount) {
if (aCount == null) throw new IllegalArgumentException();
return new HatchElement<>(null, null, null, aCount, this);
}
default <T2 extends T> IStructureElement<T2> newAny(int aCasingIndex, int aDot) {
if (aCasingIndex < 0 || aDot < 0) throw new IllegalArgumentException();
return GTStructureUtility.<T2>buildHatchAdder()
.anyOf(this)
.casingIndex(aCasingIndex)
.dot(aDot)
.continueIfSuccess()
.exclusive()
.build();
}
default <T2 extends T> IStructureElement<T2> newAnyOrCasing(int aCasingIndex, int aDot, Block casingBlock,
int casingMeta) {
if (aCasingIndex < 0 || aDot < 0) throw new IllegalArgumentException();
return GTStructureUtility.<T2>buildHatchAdder()
.anyOf(this)
.casingIndex(aCasingIndex)
.dot(aDot)
.continueIfSuccess()
.buildAndChain(com.gtnewhorizon.structurelib.structure.StructureUtility.ofBlock(casingBlock, casingMeta));
}
default <T2 extends T> IStructureElement<T2> newAny(int aCasingIndex, int aDot, ForgeDirection... allowedFacings) {
if (aCasingIndex < 0 || aDot < 0) throw new IllegalArgumentException();
return GTStructureUtility.<T2>buildHatchAdder()
.anyOf(this)
.casingIndex(aCasingIndex)
.dot(aDot)
.continueIfSuccess()
.allowOnly(allowedFacings)
.exclusive()
.build();
}
default <T2 extends T> IStructureElement<T2> newAny(int aCasingIndex, int aDot,
BiPredicate<? super T2, ? super IGregTechTileEntity> aShouldSkip) {
if (aCasingIndex < 0 || aDot < 0 || aShouldSkip == null) throw new IllegalArgumentException();
return GTStructureUtility.<T2>buildHatchAdder()
.anyOf(this)
.casingIndex(aCasingIndex)
.dot(aDot)
.shouldSkip(aShouldSkip)
.continueIfSuccess()
.build();
}
default <T2 extends T> IHatchElement<T2> or(IHatchElement<? super T2> fallback) {
return new HatchElementEither<>(this, fallback);
}
}
class HatchElementEither<T> implements IHatchElement<T> {
private final IHatchElement<? super T> first, second;
private ImmutableList<? extends Class<? extends IMetaTileEntity>> mMteClasses;
private String name;
HatchElementEither(IHatchElement<? super T> first, IHatchElement<? super T> second) {
this.first = first;
this.second = second;
}
@Override
public List<? extends Class<? extends IMetaTileEntity>> mteClasses() {
if (mMteClasses == null) mMteClasses = ImmutableList.<Class<? extends IMetaTileEntity>>builder()
.addAll(first.mteClasses())
.addAll(second.mteClasses())
.build();
return mMteClasses;
}
@Override
public IGTHatchAdder<? super T> adder() {
return ((t, te, i) -> first.adder()
.apply(t, te, i)
|| second.adder()
.apply(t, te, i));
}
@Override
public String name() {
if (name == null) name = first.name() + " or " + second.name();
return name;
}
@Override
public long count(T t) {
return first.count(t) + second.count(t);
}
}
class HatchElement<T> implements IHatchElement<T> {
private final List<Class<? extends IMetaTileEntity>> mClasses;
private final IGTHatchAdder<? super T> mAdder;
private final String mName;
private final IHatchElement<? super T> mBacking;
private final ToLongFunction<? super T> mCount;
public HatchElement(List<Class<? extends IMetaTileEntity>> aMteClasses, IGTHatchAdder<? super T> aAdder,
String aName, ToLongFunction<? super T> aCount, IHatchElement<? super T> aBacking) {
this.mClasses = aMteClasses;
this.mAdder = aAdder;
this.mName = aName;
this.mCount = aCount;
this.mBacking = aBacking;
}
@Override
public List<? extends Class<? extends IMetaTileEntity>> mteClasses() {
return mClasses == null ? mBacking.mteClasses() : mClasses;
}
@Override
public IGTHatchAdder<? super T> adder() {
return mAdder == null ? mBacking.adder() : mAdder;
}
@Override
public String name() {
return mName == null ? mBacking.name() : mName;
}
@Override
public long count(T t) {
return mCount == null ? mBacking.count(t) : mCount.applyAsLong(t);
}
@Override
public <T2 extends T> IHatchElement<T2> withMteClasses(List<Class<? extends IMetaTileEntity>> aClasses) {
if (aClasses == null) throw new IllegalArgumentException();
return new HatchElement<>(aClasses, mAdder, mName, mCount, mBacking);
}
@Override
public <T2 extends T> IHatchElement<T2> withAdder(IGTHatchAdder<T2> aAdder) {
if (aAdder == null) throw new IllegalArgumentException();
return new HatchElement<>(mClasses, aAdder, mName, mCount, mBacking);
}
@Override
public IHatchElement<T> withName(String aName) {
if (aName == null) throw new IllegalArgumentException();
return new HatchElement<>(mClasses, mAdder, aName, mCount, mBacking);
}
@Override
public <T2 extends T> IHatchElement<T2> withCount(ToLongFunction<T2> aCount) {
if (aCount == null) throw new IllegalArgumentException();
return new HatchElement<>(mClasses, mAdder, mName, aCount, mBacking);
}
}
|