]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/common/StructureProcessor.java
f1681cec64f5d14e6ed38933224919d68caef74b
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / common / StructureProcessor.java
1 package com.irtimaled.bbor.common;
2
3 import com.irtimaled.bbor.common.models.AbstractBoundingBox;
4 import com.irtimaled.bbor.common.models.BoundingBoxCuboid;
5 import com.irtimaled.bbor.common.models.Coords;
6 import com.irtimaled.bbor.mixin.access.IStructureStart;
7 import net.minecraft.structure.StructurePiece;
8 import net.minecraft.structure.StructureStart;
9 import net.minecraft.util.math.BlockBox;
10
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Set;
14
15 class StructureProcessor {
16     private static final Set<BoundingBoxType> supportedStructures = new HashSet<>();
17
18     static void registerSupportedStructure(BoundingBoxType type) {
19         supportedStructures.add(type);
20     }
21
22     StructureProcessor(BoundingBoxCache boundingBoxCache) {
23         this.boundingBoxCache = boundingBoxCache;
24     }
25
26     private final BoundingBoxCache boundingBoxCache;
27
28     private void addStructures(BoundingBoxType type, StructureStart<?> structureStart) {
29         if (structureStart == null) return;
30
31         try {
32             structureStart.getBoundingBox();
33         } catch (Throwable ignored) {
34         }
35
36         BlockBox bb = ((IStructureStart) structureStart).getBoundingBox1();
37         if (bb == null) return;
38
39         AbstractBoundingBox boundingBox = buildStructure(bb, type);
40         if (boundingBoxCache.isCached(boundingBox)) return;
41
42         Set<AbstractBoundingBox> structureBoundingBoxes = new HashSet<>();
43         for (StructurePiece structureComponent : structureStart.getChildren()) {
44             structureBoundingBoxes.add(buildStructure(structureComponent.getBoundingBox(), type));
45         }
46         boundingBoxCache.addBoundingBoxes(boundingBox, structureBoundingBoxes);
47     }
48
49     private AbstractBoundingBox buildStructure(BlockBox bb, BoundingBoxType type) {
50         Coords min = new Coords(bb.getMinX(), bb.getMinY(), bb.getMinZ());
51         Coords max = new Coords(bb.getMaxX(), bb.getMaxY(), bb.getMaxZ());
52         return BoundingBoxCuboid.from(min, max, type);
53     }
54
55     void process(Map<String, StructureStart<?>> structures) {
56         if (structures.size() > 0) {
57             supportedStructures.forEach(type -> addStructures(type, structures.get(type.getName())));
58         }
59     }
60 }