1 package com.irtimaled.bbor.common;
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;
15 import java.util.HashSet;
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<>());
23 public static void registerSupportedStructure(BoundingBoxType type) {
24 supportedStructures.put(type.getName(), type);
27 StructureProcessor(BoundingBoxCache boundingBoxCache) {
28 this.boundingBoxCache = boundingBoxCache;
31 private final BoundingBoxCache boundingBoxCache;
33 private void addStructures(BoundingBoxType type, StructureStart structureStart) {
34 if (structureStart == null) return;
37 structureStart.getBoundingBox();
38 } catch (Throwable ignored) {
41 BlockBox bb = ((IStructureStart) structureStart).getBoundingBox1();
42 if (bb == null) return;
44 AbstractBoundingBox boundingBox = buildStructure(bb, type);
45 if (boundingBoxCache.isCached(boundingBox)) return;
47 Set<AbstractBoundingBox> structureBoundingBoxes = new HashSet<>();
48 for (StructurePiece structureComponent : structureStart.getChildren()) {
49 structureBoundingBoxes.add(buildStructure(structureComponent.getBoundingBox(), type));
51 boundingBoxCache.addBoundingBoxes(boundingBox, structureBoundingBoxes);
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);
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());
64 addStructures(type, entry.getValue());