-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathBlockArray.java
More file actions
496 lines (428 loc) · 19.1 KB
/
BlockArray.java
File metadata and controls
496 lines (428 loc) · 19.1 KB
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*******************************************************************************
* HellFirePvP / Modular Machinery 2019
*
* This project is licensed under GNU GENERAL PUBLIC LICENSE Version 3.
* The source code is available on github: https://github.com/HellFirePvP/ModularMachinery
* For further details, see the License file there.
******************************************************************************/
package hellfirepvp.modularmachinery.common.util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import hellfirepvp.modularmachinery.client.ClientScheduler;
import hellfirepvp.modularmachinery.common.util.nbt.NBTJsonSerializer;
import hellfirepvp.modularmachinery.common.util.nbt.NBTMatchingHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Rotation;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fluids.BlockFluidBase;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
/**
* This class is part of the Modular Machinery Mod
* The complete source code for this mod can be found on github.
* Class: BlockArray
* Created by HellFirePvP
* Date: 27.06.2017 / 10:50
*/
public class BlockArray {
private static final ResourceLocation ic2TileBlock = new ResourceLocation("ic2", "te");
protected Map<BlockPos, BlockInformation> pattern = new HashMap<>();
private Vec3i min = new Vec3i(0, 0, 0), max = new Vec3i(0, 0, 0), size = new Vec3i(0, 0, 0);
public BlockArray() {}
public BlockArray(BlockArray other) {
this.pattern = new HashMap<>(other.pattern);
this.min = new Vec3i(other.min.getX(), other.min.getY(), other.min.getZ());
this.max = new Vec3i(other.max.getX(), other.max.getY(), other.max.getZ());
this.size = new Vec3i(other.size.getX(), other.size.getY(), other.size.getZ());
}
public BlockArray(BlockArray other, Vec3i offset) {
for (Map.Entry<BlockPos, BlockInformation> otherEntry : other.pattern.entrySet()) {
this.pattern.put(otherEntry.getKey().add(offset), otherEntry.getValue());
}
this.min = new Vec3i(offset.getX() + other.min.getX(), offset.getY() + other.min.getY(), offset.getZ() + other.min.getZ());
this.max = new Vec3i(offset.getX() + other.max.getX(), offset.getY() + other.max.getY(), offset.getZ() + other.max.getZ());
this.size = new Vec3i(other.size.getX(), other.size.getY(), other.size.getZ());
}
public void addBlock(int x, int y, int z, @Nonnull BlockInformation info) {
addBlock(new BlockPos(x, y, z), info);
}
public void addBlock(BlockPos offset, @Nonnull BlockInformation info) {
pattern.put(offset, info);
updateSize(offset);
}
public boolean hasBlockAt(BlockPos pos) {
return pattern.containsKey(pos);
}
public boolean isEmpty() {
return pattern.isEmpty();
}
public Vec3i getMax() {
return max;
}
public Vec3i getMin() {
return min;
}
public Vec3i getSize() {
return size;
}
private void updateSize(BlockPos addedPos) {
if(addedPos.getX() < min.getX()) {
min = new Vec3i(addedPos.getX(), min.getY(), min.getZ());
}
if(addedPos.getX() > max.getX()) {
max = new Vec3i(addedPos.getX(), max.getY(), max.getZ());
}
if(addedPos.getY() < min.getY()) {
min = new Vec3i(min.getX(), addedPos.getY(), min.getZ());
}
if(addedPos.getY() > max.getY()) {
max = new Vec3i(max.getX(), addedPos.getY(), max.getZ());
}
if(addedPos.getZ() < min.getZ()) {
min = new Vec3i(min.getX(), min.getY(), addedPos.getZ());
}
if(addedPos.getZ() > max.getZ()) {
max = new Vec3i(max.getX(), max.getY(), addedPos.getZ());
}
size = new Vec3i(max.getX() - min.getX() + 1, max.getY() - min.getY() + 1, max.getZ() - min.getZ() + 1);
}
public Map<BlockPos, BlockInformation> getPattern() {
return pattern;
}
public Map<BlockPos, BlockInformation> getPatternSlice(int slice) {
Map<BlockPos, BlockInformation> copy = new HashMap<>();
for (BlockPos pos : pattern.keySet()) {
if(pos.getY() == slice) {
copy.put(pos, pattern.get(pos));
}
}
return copy;
}
@SideOnly(Side.CLIENT)
public List<ItemStack> getAsDescriptiveStacks() {
return getAsDescriptiveStacks(Optional.empty());
}
@SideOnly(Side.CLIENT)
public List<ItemStack> getAsDescriptiveStacks(Optional<Long> snapSample) {
List<ItemStack> out = new LinkedList<>();
for (Map.Entry<BlockPos, BlockInformation> infoEntry : pattern.entrySet()) {
BlockArray.BlockInformation bi = infoEntry.getValue();
ItemStack s = bi.getDescriptiveStack(snapSample);
if(!s.isEmpty()) {
boolean found = false;
for (ItemStack stack : out) {
if(stack.getItem().getRegistryName().equals(s.getItem().getRegistryName()) && stack.getItemDamage() == s.getItemDamage()) {
stack.setCount(stack.getCount() + 1);
found = true;
break;
}
}
if(!found) {
out.add(s);
}
}
}
return out;
}
public boolean matches(World world, BlockPos center, boolean oldState, @Nullable Map<BlockPos, List<BlockInformation>> modifierReplacementPattern) {
lblPattern:
for (Map.Entry<BlockPos, BlockInformation> entry : pattern.entrySet()) {
BlockPos at = center.add(entry.getKey());
if(!entry.getValue().matches(world, at, oldState)) {
if(modifierReplacementPattern != null && modifierReplacementPattern.containsKey(entry.getKey())) {
for (BlockInformation info : modifierReplacementPattern.get(entry.getKey())) {
if (info.matches(world, at, oldState)) {
continue lblPattern;
}
}
}
return false;
}
}
return true;
}
public BlockPos getRelativeMismatchPosition(World world, BlockPos center, @Nullable Map<BlockPos, List<BlockInformation>> modifierReplacementPattern) {
for (Map.Entry<BlockPos, BlockInformation> entry : pattern.entrySet()) {
BlockPos at = center.add(entry.getKey());
if(!entry.getValue().matches(world, at, false)) {
if(modifierReplacementPattern != null && modifierReplacementPattern.containsKey(entry.getKey())) {
for (BlockInformation info : modifierReplacementPattern.get(entry.getKey())) {
if (info.matches(world, at, false)) {
continue;
}
}
}
return entry.getKey();
}
}
return null;
}
public BlockArray rotateYCCW() {
BlockArray out = new BlockArray();
for (BlockPos pos : pattern.keySet()) {
BlockInformation info = pattern.get(pos);
out.pattern.put(MiscUtils.rotateYCCW(pos), info.copyRotateYCCW());
}
return out;
}
public String serializeAsMachineJson() {
String newline = System.getProperty("line.separator");
String move = " ";
StringBuilder sb = new StringBuilder();
sb.append("{").append(newline);
sb.append(move).append("\"parts\": [").append(newline);
for (Iterator<BlockPos> iterator = this.pattern.keySet().iterator(); iterator.hasNext(); ) {
BlockPos pos = iterator.next();
sb.append(move).append(move).append("{").append(newline);
sb.append(move).append(move).append(move).append("\"x\": ").append(pos.getX()).append(",").append(newline);
sb.append(move).append(move).append(move).append("\"y\": ").append(pos.getY()).append(",").append(newline);
sb.append(move).append(move).append(move).append("\"z\": ").append(pos.getZ()).append(",").append(newline);
BlockInformation bi = this.pattern.get(pos);
/* if(bi.matchingTag != null) {
String strTag = NBTJsonSerializer.serializeNBT(bi.matchingTag);
sb.append(move).append(move).append(move).append("\"nbt\": ").append(strTag).append(",").append(newline);
} */
sb.append(move).append(move).append(move).append("\"elements\": [").append(newline);
for (Iterator<IBlockState> iterator1 = bi.samples.iterator(); iterator1.hasNext(); ) {
IBlockState descriptor = iterator1.next();
int meta = descriptor.getBlock().getMetaFromState(descriptor);
String str = descriptor.getBlock().getRegistryName().toString() + "@" + meta;
sb.append(move).append(move).append(move).append(move).append("\"").append(str).append("\"");
if(iterator1.hasNext()) {
sb.append(",");
}
sb.append(newline);
}
sb.append(move).append(move).append(move).append("]").append(newline);
sb.append(move).append(move).append("}");
if(iterator.hasNext()) {
sb.append(",");
}
sb.append(newline);
}
sb.append(move).append("]");
sb.append("}");
return sb.toString();
}
public static class BlockInformation {
public static final int CYCLE_TICK_SPEED = 30;
public List<IBlockStateDescriptor> matchingStates;
private List<IBlockState> samples = Lists.newLinkedList();
public NBTTagCompound matchingTag = null;
public BlockInformation(List<IBlockStateDescriptor> matching) {
this.matchingStates = Lists.newLinkedList(matching);
for (IBlockStateDescriptor desc : matchingStates) {
samples.addAll(desc.applicable);
}
}
public void setMatchingTag(@Nullable NBTTagCompound matchingTag) {
this.matchingTag = matchingTag;
}
public IBlockState getSampleState() {
return getSampleState(Optional.empty());
}
public IBlockState getSampleState(Optional<Long> snapTick) {
int tickSpeed = CYCLE_TICK_SPEED;
if(samples.size() > 10) {
tickSpeed *= 0.6;
}
int p = (int) (snapTick.orElse(ClientScheduler.getClientTick()) / tickSpeed);
int part = p % samples.size();
return samples.get(part);
}
@SideOnly(Side.CLIENT)
public ItemStack getDescriptiveStack(Optional<Long> snapTick) {
IBlockState state = getSampleState(snapTick);
Tuple<IBlockState, TileEntity> recovered = BlockCompatHelper.transformState(state, this.matchingTag,
new BlockArray.TileInstantiateContext(Minecraft.getMinecraft().world, BlockPos.ORIGIN));
state = recovered.getFirst();
Block type = state.getBlock();
int meta = type.getMetaFromState(state);
ItemStack s = ItemStack.EMPTY;
try {
if(ic2TileBlock.equals(type.getRegistryName())) {
s = BlockCompatHelper.tryGetIC2MachineStack(state, recovered.getSecond());
} else {
s = state.getBlock().getPickBlock(state, null, null, BlockPos.ORIGIN, null);
}
} catch (Exception exc) {}
if(s.isEmpty()) {
if(type instanceof BlockFluidBase) {
s = FluidUtil.getFilledBucket(new FluidStack(((BlockFluidBase) type).getFluid(), 1000));
} else if(type instanceof BlockLiquid) {
Material m = state.getMaterial();
if(m == Material.LAVA) {
s = new ItemStack(Items.LAVA_BUCKET);
} else if(m == Material.WATER) {
s = new ItemStack(Items.WATER_BUCKET);
} else {
s = ItemStack.EMPTY;
}
} else {
Item i = Item.getItemFromBlock(type);
if(i != Items.AIR) {
s = new ItemStack(i, 1, meta);
}
}
}
return s;
}
public static IBlockStateDescriptor getDescriptor(String strElement) throws JsonParseException {
int meta = -1;
int indexMeta = strElement.indexOf('@');
if(indexMeta != -1 && indexMeta != strElement.length() - 1) {
try {
meta = Integer.parseInt(strElement.substring(indexMeta + 1));
} catch (NumberFormatException exc) {
throw new JsonParseException("Expected a metadata number, got " + strElement.substring(indexMeta + 1), exc);
}
strElement = strElement.substring(0, indexMeta);
}
ResourceLocation res = new ResourceLocation(strElement);
Block b = ForgeRegistries.BLOCKS.getValue(res);
if(b == null || b == Blocks.AIR) {
throw new JsonParseException("Couldn't find block with registryName '" + res.toString() + "' !");
}
if(meta == -1) {
return new IBlockStateDescriptor(b);
} else {
return new IBlockStateDescriptor(b.getStateFromMeta(meta));
}
}
public BlockInformation copyRotateYCCW() {
List<IBlockStateDescriptor> newDescriptors = new ArrayList<>(this.matchingStates.size());
for (IBlockStateDescriptor desc : this.matchingStates) {
IBlockStateDescriptor copy = new IBlockStateDescriptor();
for (IBlockState applicableState : desc.applicable) {
copy.applicable.add(applicableState.withRotation(Rotation.COUNTERCLOCKWISE_90));
}
newDescriptors.add(copy);
}
BlockInformation bi = new BlockInformation(newDescriptors);
if(this.matchingTag != null) {
bi.matchingTag = this.matchingTag;
}
return bi;
}
public BlockInformation copy() {
List<IBlockStateDescriptor> descr = new ArrayList<>(this.matchingStates.size());
for (IBlockStateDescriptor desc : this.matchingStates) {
IBlockStateDescriptor copy = new IBlockStateDescriptor();
copy.applicable.addAll(desc.applicable);
descr.add(copy);
}
BlockInformation bi = new BlockInformation(descr);
if(this.matchingTag != null) {
bi.matchingTag = this.matchingTag;
}
return bi;
}
public boolean matchesState(IBlockState state) {
Block atBlock = state.getBlock();
int atMeta = atBlock.getMetaFromState(state);
for (IBlockStateDescriptor descriptor : matchingStates) {
for (IBlockState applicable : descriptor.applicable) {
Block type = applicable.getBlock();
int meta = type.getMetaFromState(applicable);
if(type.equals(state.getBlock()) && meta == atMeta) {
return true;
}
}
}
return false;
}
public boolean matches(World world, BlockPos at, boolean default_) {
if(!world.isBlockLoaded(at)) {
return default_;
}
if(matchingTag != null) {
TileEntity te = world.getTileEntity(at);
if(te != null && matchingTag.getSize() > 0) {
NBTTagCompound cmp = new NBTTagCompound();
te.writeToNBT(cmp);
if(!NBTMatchingHelper.matchNBTCompound(matchingTag, cmp)) {
return false; //No match at this position.
}
}
}
IBlockState state = world.getBlockState(at);
Block atBlock = state.getBlock();
int atMeta = atBlock.getMetaFromState(state);
for (IBlockStateDescriptor descriptor : matchingStates) {
for (IBlockState applicable : descriptor.applicable) {
Block type = applicable.getBlock();
int meta = type.getMetaFromState(applicable);
if(type.equals(state.getBlock()) && meta == atMeta) {
return true;
}
}
}
return false;
}
}
public static class IBlockStateDescriptor {
public final List<IBlockState> applicable = Lists.newArrayList();
private IBlockStateDescriptor() {}
private IBlockStateDescriptor(Block block) {
List<Integer> usedMetas = Lists.newArrayList();
if(!(block instanceof BlockLiquid) && !(block instanceof BlockFluidBase)) {
for (IBlockState state : block.getBlockState().getValidStates()) {
int meta = block.getMetaFromState(state);
if(!usedMetas.contains(meta)) {
usedMetas.add(meta);
this.applicable.add(state);
}
}
}
if(applicable.isEmpty()) {
applicable.add(block.getDefaultState());
}
}
public IBlockStateDescriptor(IBlockState state) {
this.applicable.add(state);
}
}
public static class TileInstantiateContext {
private final World world;
private final BlockPos pos;
public TileInstantiateContext(World world, BlockPos pos) {
this.world = world;
this.pos = pos;
}
public World getWorld() {
return world;
}
public BlockPos getPos() {
return pos;
}
public void apply(TileEntity te) {
if(te != null) {
te.setWorld(world);
te.setPos(pos);
}
}
}
}