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
|
package gregtech.api.util;
import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.gtnewhorizon.structurelib.structure.IItemSource;
import com.gtnewhorizon.structurelib.structure.IStructureElement;
import com.gtnewhorizon.structurelib.structure.IStructureElementNoPlacement;
import com.gtnewhorizon.structurelib.structure.StructureUtility;
import com.gtnewhorizon.structurelib.util.ItemStackPredicate;
import gregtech.api.GregTech_API;
import gregtech.api.enums.HeatingCoilLevel;
import gregtech.api.enums.Materials;
import gregtech.api.enums.OrePrefixes;
import gregtech.api.interfaces.IHeatingCoil;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.BaseMetaPipeEntity;
import gregtech.api.metatileentity.implementations.GT_MetaPipeEntity_Frame;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_EnhancedMultiBlockBase;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_TieredMachineBlock;
import gregtech.common.blocks.GT_Block_Casings5;
import gregtech.common.blocks.GT_Item_Machines;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;
public class GT_StructureUtility {
// private static final Map<Class<?>, String> customNames = new HashMap<>();
private GT_StructureUtility() {
throw new AssertionError("Not instantiable");
}
public static boolean hasMTE(IGregTechTileEntity aTile, Class<? extends IMetaTileEntity> clazz) {
return aTile != null && clazz.isInstance(aTile.getMetaTileEntity());
}
public static <T> IStructureElementNoPlacement<T> ofHatchAdder(IGT_HatchAdder<T> aHatchAdder, int aTextureIndex, int aDots) {
return ofHatchAdder(aHatchAdder, aTextureIndex, StructureLibAPI.getBlockHint(), aDots - 1);
}
public static <T> IStructureElement<T> ofFrame(Materials aFrameMaterial) {
if (aFrameMaterial == null) throw new IllegalArgumentException();
return new IStructureElement<T>() {
private IIcon[] mIcons;
@Override
public boolean check(T t, World world, int x, int y, int z) {
TileEntity tBase = world.getTileEntity(x, y, z);
if (tBase instanceof BaseMetaPipeEntity) {
BaseMetaPipeEntity tPipeBase = (BaseMetaPipeEntity) tBase;
if (tPipeBase.isInvalidTileEntity()) return false;
if (tPipeBase.getMetaTileEntity() instanceof GT_MetaPipeEntity_Frame)
return aFrameMaterial == ((GT_MetaPipeEntity_Frame) tPipeBase.getMetaTileEntity()).mMaterial;
}
return false;
}
@Override
public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) {
if (mIcons == null) {
mIcons = new IIcon[6];
Arrays.fill(mIcons, aFrameMaterial.mIconSet.mTextures[OrePrefixes.frameGt.mTextureIndex].getIcon());
}
StructureLibAPI.hintParticleTinted(world, x, y, z, mIcons, aFrameMaterial.mRGBa);
return true;
}
@Override
public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) {
ItemStack tFrameStack = GT_OreDictUnificator.get(OrePrefixes.frameGt, aFrameMaterial, 1);
if (!GT_Utility.isStackValid(tFrameStack) || !(tFrameStack.getItem() instanceof ItemBlock))
return false;
ItemBlock tFrameStackItem = (ItemBlock) tFrameStack.getItem();
return tFrameStackItem.placeBlockAt(tFrameStack, null, world, x, y, z, 6, 0, 0, 0, Items.feather.getDamage(tFrameStack));
}
@Override
public PlaceResult survivalPlaceBlock(T t, World world, int x, int y, int z, ItemStack trigger, IItemSource s, EntityPlayerMP actor, Consumer<IChatComponent> chatter) {
if (check(t, world, x, y, z)) return PlaceResult.SKIP;
ItemStack tFrameStack = GT_OreDictUnificator.get(OrePrefixes.frameGt, aFrameMaterial, 1);
if (!GT_Utility.isStackValid(tFrameStack) || !(tFrameStack.getItem() instanceof ItemBlock))
return PlaceResult.REJECT; // honestly, this is more like a programming error or pack issue
return StructureUtility.survivalPlaceBlock(tFrameStack, ItemStackPredicate.NBTMode.IGNORE_KNOWN_INSIGNIFICANT_TAGS, null, false, world, x, y, z, s, actor, chatter);
}
};
}
public static <T> GT_HatchElementBuilder<T> buildHatchAdder() {
return GT_HatchElementBuilder.builder();
}
/**
* Completely equivalent to {@link #buildHatchAdder()}, except it plays nicer with type inference when statically imported
*/
public static <T> GT_HatchElementBuilder<T> buildHatchAdder(Class<T> typeToken) {
return GT_HatchElementBuilder.builder();
}
public static <T> IStructureElementNoPlacement<T> ofHatchAdder(IGT_HatchAdder<T> aHatchAdder, int aTextureIndex, Block aHintBlock, int aHintMeta) {
if (aHatchAdder == null || aHintBlock == null) {
throw new IllegalArgumentException();
}
return new IStructureElementNoPlacement<T>() {
@Override
public boolean check(T t, World world, int x, int y, int z) {
TileEntity tileEntity = world.getTileEntity(x, y, z);
return tileEntity instanceof IGregTechTileEntity && aHatchAdder.apply(t, (IGregTechTileEntity) tileEntity, (short) aTextureIndex);
}
@Override
public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) {
StructureLibAPI.hintParticle(world, x, y, z, aHintBlock, aHintMeta);
return true;
}
};
}
public static <T> IStructureElement<T> ofHatchAdder(IGT_HatchAdder<T> aHatchAdder, int aTextureIndex, Block aHintBlock, int aHintMeta, BiPredicate<T, IGregTechTileEntity> shouldSkip, Function<T, Class<? extends IMetaTileEntity>> aMetaId, final IStructureElement.PlaceResult acceptType) {
if (aHatchAdder == null) {
throw new IllegalArgumentException();
}
return new IStructureElement<T>() {
@Override
public boolean check(T t, World world, int x, int y, int z) {
TileEntity tileEntity = world.getTileEntity(x, y, z);
return tileEntity instanceof IGregTechTileEntity && aHatchAdder.apply(t, (IGregTechTileEntity) tileEntity, (short) aTextureIndex);
}
@Override
public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) {
StructureLibAPI.hintParticle(world, x, y, z, aHintBlock, aHintMeta);
return true;
}
@Override
public boolean placeBlock(T t, World world, int i, int i1, int i2, ItemStack itemStack) {
// TODO
return false;
}
@Override
public PlaceResult survivalPlaceBlock(T t, World world, int x, int y, int z, ItemStack trigger, IItemSource s, EntityPlayerMP actor, Consumer<IChatComponent> chatter) {
if (shouldSkip != null) {
TileEntity tileEntity = world.getTileEntity(x, y, z);
if (tileEntity instanceof IGregTechTileEntity && shouldSkip.test(t, (IGregTechTileEntity) tileEntity))
return PlaceResult.SKIP;
}
if (!StructureLibAPI.isBlockTriviallyReplaceable(world, x, y, z, actor))
return PlaceResult.REJECT;
Class<? extends IMetaTileEntity> clazz = aMetaId.apply(t);
if (clazz == null) return PlaceResult.REJECT;
ItemStack taken = s.takeOne(is -> clazz.isInstance(GT_Item_Machines.getMetaTileEntity(is)), true);
if (GT_Utility.isStackInvalid(taken)) {
chatter.accept(new ChatComponentTranslation("GT5U.autoplace.error.no_mte.class_name", clazz.getSimpleName()));
return PlaceResult.REJECT;
}
if (StructureUtility.survivalPlaceBlock(taken, ItemStackPredicate.NBTMode.IGNORE, null, true, world, x, y, z, s, actor) == PlaceResult.ACCEPT)
return acceptType;
return PlaceResult.REJECT;
}
};
}
public static <T> IStructureElement<T> ofHatchAdder(IGT_HatchAdder<T> aHatchAdder, int aTextureIndex, Block aHintBlock, int aHintMeta, BiPredicate<T, IGregTechTileEntity> shouldSkip, ToIntFunction<T> aMetaId) {
if (aHatchAdder == null) {
throw new IllegalArgumentException();
}
return new IStructureElement<T>() {
@Override
public boolean check(T t, World world, int x, int y, int z) {
TileEntity tileEntity = world.getTileEntity(x, y, z);
return tileEntity instanceof IGregTechTileEntity && aHatchAdder.apply(t, (IGregTechTileEntity) tileEntity, (short) aTextureIndex);
}
@Override
public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) {
StructureLibAPI.hintParticle(world, x, y, z, aHintBlock, aHintMeta);
return true;
}
@Override
public boolean placeBlock(T t, World world, int i, int i1, int i2, ItemStack itemStack) {
// TODO
return false;
}
@Override
public PlaceResult survivalPlaceBlock(T t, World world, int x, int y, int z, ItemStack trigger, IItemSource s, EntityPlayerMP actor, Consumer<IChatComponent> chatter) {
if (shouldSkip != null) {
TileEntity tileEntity = world.getTileEntity(x, y, z);
if (tileEntity instanceof IGregTechTileEntity && shouldSkip.test(t, (IGregTechTileEntity) tileEntity))
return PlaceResult.SKIP;
}
if (!StructureLibAPI.isBlockTriviallyReplaceable(world, x, y, z, actor))
return PlaceResult.REJECT;
GT_Item_Machines item = (GT_Item_Machines) Item.getItemFromBlock(GregTech_API.sBlockMachines);
int meta = aMetaId.applyAsInt(t);
if (meta < 0) return PlaceResult.REJECT;
ItemStack taken = s.takeOne(ItemStackPredicate.from(item).setMeta(meta), true);
if (GT_Utility.isStackInvalid(taken)) {
chatter.accept(new ChatComponentTranslation("GT5U.autoplace.error.no_mte.id", meta));
return PlaceResult.REJECT;
}
return StructureUtility.survivalPlaceBlock(taken, ItemStackPredicate.NBTMode.IGNORE, null, true, world, x, y, z, s, actor) == PlaceResult.ACCEPT ? PlaceResult.ACCEPT_STOP : PlaceResult.REJECT;
}
};
}
public static <T> IStructureElement<T> ofHatchAdderOptional(IGT_HatchAdder<T> aHatchAdder, int textureIndex, int dots, Block placeCasing, int placeCasingMeta) {
return ofHatchAdderOptional(aHatchAdder, textureIndex, StructureLibAPI.getBlockHint(), dots - 1, placeCasing, placeCasingMeta);
}
public static <T> IStructureElement<T> ofHatchAdderOptional(IGT_HatchAdder<T> aHatchAdder, int aTextureIndex, Block aHintBlock, int hintMeta, Block placeCasing, int placeCasingMeta) {
if (aHatchAdder == null || aHintBlock == null) {
throw new IllegalArgumentException();
}
return new IStructureElement<T>() {
@Override
public boolean check(T t, World world, int x, int y, int z) {
TileEntity tileEntity = world.getTileEntity(x, y, z);
Block worldBlock = world.getBlock(x, y, z);
return (tileEntity instanceof IGregTechTileEntity &&
aHatchAdder.apply(t, (IGregTechTileEntity) tileEntity, (short) aTextureIndex)) ||
(worldBlock == placeCasing && worldBlock.getDamageValue(world, x, y, z) == placeCasingMeta);
}
@Override
public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) {
StructureLibAPI.hintParticle(world, x, y, z, aHintBlock, hintMeta);
return true;
}
@Override
public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) {
world.setBlock(x, y, z, placeCasing, placeCasingMeta, 2);
return true;
}
@Override
public PlaceResult survivalPlaceBlock(T t, World world, int x, int y, int z, ItemStack trigger, IItemSource s, EntityPlayerMP actor, Consumer<IChatComponent> chatter) {
if (check(t, world, x, y, z)) return PlaceResult.SKIP;
return StructureUtility.survivalPlaceBlock(placeCasing, placeCasingMeta, world, x, y, z, s, actor, chatter);
}
};
}
/**
* Assume all coils accepted.
*
* @see #ofCoil(BiPredicate, Function)
*/
public static <T> IStructureElement<T> ofCoil(BiConsumer<T, HeatingCoilLevel> aHeatingCoilSetter, Function<T, HeatingCoilLevel> aHeatingCoilGetter) {
return ofCoil((t, l) -> {
aHeatingCoilSetter.accept(t, l);
return true;
}, aHeatingCoilGetter);
}
/**
* Heating coil structure element.
*
* @param aHeatingCoilSetter Notify the controller of this new coil.
* Got called exactly once per coil.
* Might be called less times if structure test fails.
* If the setter returns false then it assumes the coil is rejected.
* @param aHeatingCoilGetter Get the current heating level. Null means no coil recorded yet.
*/
public static <T> IStructureElement<T> ofCoil(BiPredicate<T, HeatingCoilLevel> aHeatingCoilSetter, Function<T, HeatingCoilLevel> aHeatingCoilGetter) {
if (aHeatingCoilSetter == null || aHeatingCoilGetter == null) {
throw new IllegalArgumentException();
}
return new IStructureElement<T>() {
@Override
public boolean check(T t, World world, int x, int y, int z) {
Block block = world.getBlock(x, y, z);
if (!(block instanceof IHeatingCoil))
return false;
HeatingCoilLevel existingLevel = aHeatingCoilGetter.apply(t),
newLevel = ((IHeatingCoil) block).getCoilHeat(world.getBlockMetadata(x, y, z));
if (existingLevel == null || existingLevel == HeatingCoilLevel.None) {
return aHeatingCoilSetter.test(t, newLevel);
} else {
return newLevel == existingLevel;
}
}
@Override
public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) {
StructureLibAPI.hintParticle(world, x, y, z, GregTech_API.sBlockCasings5, getMetaFromHint(trigger));
return true;
}
private int getMetaFromHint(ItemStack trigger) {
return GT_Block_Casings5.getMetaFromCoilHeat(getHeatFromHint(trigger));
}
private HeatingCoilLevel getHeatFromHint(ItemStack trigger) {
return HeatingCoilLevel.getFromTier((byte) Math.min(HeatingCoilLevel.getMaxTier() , Math.max(0, trigger.stackSize - 1)));
}
@Override
public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) {
return world.setBlock(x, y, z, GregTech_API.sBlockCasings5, getMetaFromHint(trigger), 3);
}
@Override
public PlaceResult survivalPlaceBlock(T t, World world, int x, int y, int z, ItemStack trigger, IItemSource s, EntityPlayerMP actor, Consumer<IChatComponent> chatter) {
Block block = world.getBlock(x, y, z);
boolean isCoil = block instanceof IHeatingCoil && ((IHeatingCoil) block).getCoilHeat(world.getBlockMetadata(x, y, z)) == getHeatFromHint(trigger);
if (isCoil) return PlaceResult.SKIP;
return StructureUtility.survivalPlaceBlock(GregTech_API.sBlockCasings5, getMetaFromHint(trigger), world, x, y, z, s, actor, chatter);
}
};
}
@Nonnull
public static Predicate<ItemStack> filterByMTEClass(List<? extends Class<? extends IMetaTileEntity>> list) {
return is -> {
IMetaTileEntity tile = GT_Item_Machines.getMetaTileEntity(is);
return tile != null && list.stream().anyMatch(c -> c.isInstance(tile));
};
}
@Nonnull
public static Predicate<ItemStack> filterByMTETier(int aMinTier, int aMaxTier) {
return is -> {
IMetaTileEntity tile = GT_Item_Machines.getMetaTileEntity(is);
return tile instanceof GT_MetaTileEntity_TieredMachineBlock &&
((GT_MetaTileEntity_TieredMachineBlock) tile).mTier <= aMaxTier &&
((GT_MetaTileEntity_TieredMachineBlock) tile).mTier >= aMinTier;
};
}
}
|