]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/common/StructureProcessor.java
Setup for 1.16.3 Fabric
[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 net.minecraft.structure.StructurePiece;
7 import net.minecraft.structure.StructureStart;
8 import net.minecraft.util.math.BlockBox;
9
10 import java.util.HashSet;
11 import java.util.Map;
12 import java.util.Set;
13
14 class StructureProcessor {
15     private static final Set<BoundingBoxType> supportedStructures = new HashSet<>();
16
17     static void registerSupportedStructure(BoundingBoxType type) {
18         supportedStructures.add(type);
19     }
20
21     StructureProcessor(BoundingBoxCache boundingBoxCache) {
22         this.boundingBoxCache = boundingBoxCache;
23     }
24
25     private final BoundingBoxCache boundingBoxCache;
26
27     private void addStructures(BoundingBoxType type, StructureStart<?> structureStart) {
28         if (structureStart == null) return;
29
30         BlockBox bb = structureStart.getBoundingBox();
31         if (bb == null) return;
32
33         AbstractBoundingBox boundingBox = buildStructure(bb, type);
34         if (boundingBoxCache.isCached(boundingBox)) return;
35
36         Set<AbstractBoundingBox> structureBoundingBoxes = new HashSet<>();
37         for (StructurePiece structureComponent : structureStart.getChildren()) {
38             structureBoundingBoxes.add(buildStructure(structureComponent.getBoundingBox(), type));
39         }
40         boundingBoxCache.addBoundingBoxes(boundingBox, structureBoundingBoxes);
41     }
42
43     private AbstractBoundingBox buildStructure(BlockBox bb, BoundingBoxType type) {
44         Coords min = new Coords(bb.minX, bb.minY, bb.minZ);
45         Coords max = new Coords(bb.maxX, bb.maxY, bb.maxZ);
46         return BoundingBoxCuboid.from(min, max, type);
47     }
48
49     void process(Map<String, StructureStart<?>> structures) {
50         if (structures.size() > 0) {
51             supportedStructures.forEach(type -> addStructures(type, structures.get(type.getName())));
52         }
53     }
54 }