]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/common/StructureProcessor.java
1.18.2-rc1
[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 it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
8 import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
9 import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
10 import it.unimi.dsi.fastutil.objects.ObjectSets;
11 import net.minecraft.structure.StructurePiece;
12 import net.minecraft.structure.StructureStart;
13 import net.minecraft.util.math.BlockBox;
14
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18
19 public class StructureProcessor {
20     private static final Map<String, BoundingBoxType> supportedStructures = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
21     public static final Set<String> supportedStructureIds = ObjectSets.synchronize(new ObjectOpenHashSet<>());
22
23     public static void registerSupportedStructure(BoundingBoxType type) {
24         supportedStructures.put(type.getName(), type);
25     }
26
27     StructureProcessor(BoundingBoxCache boundingBoxCache) {
28         this.boundingBoxCache = boundingBoxCache;
29     }
30
31     private final BoundingBoxCache boundingBoxCache;
32
33     private void addStructures(BoundingBoxType type, StructureStart structureStart) {
34         if (structureStart == null) return;
35
36         try {
37             structureStart.getBoundingBox();
38         } catch (Throwable ignored) {
39         }
40
41         BlockBox bb = ((IStructureStart) structureStart).getBoundingBox1();
42         if (bb == null) return;
43
44         AbstractBoundingBox boundingBox = buildStructure(bb, type);
45         if (boundingBoxCache.isCached(boundingBox)) return;
46
47         Set<AbstractBoundingBox> structureBoundingBoxes = new HashSet<>();
48         for (StructurePiece structureComponent : structureStart.getChildren()) {
49             structureBoundingBoxes.add(buildStructure(structureComponent.getBoundingBox(), type));
50         }
51         boundingBoxCache.addBoundingBoxes(boundingBox, structureBoundingBoxes);
52     }
53
54     private AbstractBoundingBox buildStructure(BlockBox bb, BoundingBoxType type) {
55         Coords min = new Coords(bb.getMinX(), bb.getMinY(), bb.getMinZ());
56         Coords max = new Coords(bb.getMaxX(), bb.getMaxY(), bb.getMaxZ());
57         return BoundingBoxCuboid.from(min, max, type);
58     }
59
60     void process(Map<String, StructureStart> structures) {
61         for (Map.Entry<String, StructureStart> entry : structures.entrySet()) {
62             final BoundingBoxType type = supportedStructures.get("structure:" + entry.getKey());
63             if (type != null) {
64                 addStructures(type, entry.getValue());
65             }
66         }
67     }
68 }