]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blobdiff - src/main/java/com/irtimaled/bbor/client/interop/NBTStructureLoader.java
Get 1.17.1 building
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / interop / NBTStructureLoader.java
index b8455d2d44cbe9de8f0b6e8ded3cf0fe6d19806d..b0c76ec3582e7760650a5fc097fb5e9b17c03423 100644 (file)
@@ -1,78 +1,92 @@
 package com.irtimaled.bbor.client.interop;
 
 import com.irtimaled.bbor.common.EventBus;
+import com.irtimaled.bbor.common.ReflectionHelper;
 import com.irtimaled.bbor.common.events.StructuresLoaded;
-import net.minecraft.nbt.CompoundNBT;
-import net.minecraft.nbt.ListNBT;
+import com.irtimaled.bbor.common.models.DimensionId;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.nbt.NbtCompound;
+import net.minecraft.nbt.NbtList;
+import net.minecraft.server.world.ServerWorld;
+import net.minecraft.structure.StructureManager;
+import net.minecraft.structure.StructurePiece;
+import net.minecraft.structure.StructureStart;
+import net.minecraft.util.math.BlockBox;
+import net.minecraft.util.math.BlockPos;
 import net.minecraft.util.math.ChunkPos;
-import net.minecraft.util.math.MutableBoundingBox;
-import net.minecraft.world.IWorld;
+import net.minecraft.util.registry.DynamicRegistryManager;
+import net.minecraft.world.*;
 import net.minecraft.world.biome.Biome;
-import net.minecraft.world.chunk.storage.RegionFileCache;
 import net.minecraft.world.dimension.DimensionType;
-import net.minecraft.world.gen.ChunkGenerator;
-import net.minecraft.world.gen.feature.structure.LegacyStructureDataUtil;
-import net.minecraft.world.gen.feature.structure.StructurePiece;
-import net.minecraft.world.gen.feature.structure.StructureStart;
-import net.minecraft.world.gen.feature.template.TemplateManager;
-import net.minecraft.world.storage.DimensionSavedDataManager;
-import net.minecraft.world.storage.SaveHandler;
+import net.minecraft.world.gen.StructureAccessor;
+import net.minecraft.world.gen.chunk.ChunkGenerator;
+import net.minecraft.world.gen.feature.FeatureConfig;
+import net.minecraft.world.level.storage.LevelStorage;
+import net.minecraft.world.storage.RegionBasedStorage;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
+import java.util.function.BiFunction;
 
 class NBTStructureLoader {
-    private final int dimensionId;
+    private final DimensionId dimensionId;
     private final Set<String> loadedChunks = new HashSet<>();
 
-    private LegacyStructureDataUtil legacyStructureDataUtil = null;
-    private SaveHandler saveHandler = null;
+    private FeatureUpdater legacyStructureDataUtil = null;
+    private LevelStorage.Session saveHandler = null;
     private File chunkSaveLocation = null;
     private ChunkLoader chunkLoader;
 
-    NBTStructureLoader(int dimensionId, SaveHandler saveHandler, File worldDirectory) {
+    NBTStructureLoader(DimensionId dimensionId, LevelStorage.Session saveHandler, File worldDirectory) {
         this.dimensionId = dimensionId;
         this.configure(saveHandler, worldDirectory);
     }
 
     void clear() {
-        this.saveHandler = null;
+        this.legacyStructureDataUtil = null;
         this.chunkSaveLocation = null;
         this.loadedChunks.clear();
+        close(this.saveHandler, this.chunkLoader);
+        this.saveHandler = null;
+        this.chunkLoader = null;
+    }
 
-        if (this.chunkLoader == null) return;
-        try {
-            this.chunkLoader.close();
-        } catch (IOException ignored) {
+    private void close(AutoCloseable... closeables) {
+        for (AutoCloseable closeable : closeables) {
+            if(closeable == null) continue;
+            try {
+                closeable.close();
+            } catch (Exception ignored) {
+            }
         }
     }
 
-    void configure(SaveHandler saveHandler, File worldDirectory) {
+    void configure(LevelStorage.Session saveHandler, File worldDirectory) {
         this.saveHandler = saveHandler;
         if (worldDirectory != null) {
-            this.chunkSaveLocation = new File(DimensionType.getById(dimensionId).getDirectory(worldDirectory), "region");
+            this.chunkSaveLocation = new File(DimensionType.getSaveDirectory(this.dimensionId.getDimensionType(), worldDirectory), "region");
             this.chunkLoader = new ChunkLoader(this.chunkSaveLocation);
         }
     }
 
-    private LegacyStructureDataUtil getLegacyStructureDataUtil() {
+    private FeatureUpdater getLegacyStructureDataUtil() {
         if (this.legacyStructureDataUtil == null) {
-            File dataFolder = new File(DimensionType.OVERWORLD.getDirectory(this.saveHandler.getWorldDirectory()), "data");
-            this.legacyStructureDataUtil = LegacyStructureDataUtil.func_215130_a(DimensionType.getById(dimensionId),
-                    new DimensionSavedDataManager(dataFolder, this.saveHandler.getFixer()));
+            File dataFolder = new File(this.saveHandler.getWorldDirectory(World.OVERWORLD), "data");
+            this.legacyStructureDataUtil = FeatureUpdater.create(dimensionId.getDimensionType(),
+                    new PersistentStateManager(dataFolder, MinecraftClient.getInstance().getDataFixer()));
         }
         return this.legacyStructureDataUtil;
     }
 
-    private CompoundNBT loadStructureStarts(int chunkX, int chunkZ) {
+    private NbtCompound loadStructureStarts(int chunkX, int chunkZ) {
         try {
-            CompoundNBT compound = this.chunkLoader.readChunk(chunkX, chunkZ);
+            NbtCompound compound = this.chunkLoader.readChunk(chunkX, chunkZ);
             if (compound == null) return null;
             int dataVersion = compound.contains("DataVersion", 99) ? compound.getInt("DataVersion") : -1;
             if (dataVersion < 1493) {
                 if (compound.getCompound("Level").getBoolean("hasLegacyStructureData")) {
-                    compound = getLegacyStructureDataUtil().func_212181_a(compound);
+                    compound = getLegacyStructureDataUtil().getUpdatedReferences(compound);
                 }
             }
             return compound.getCompound("Level").getCompound("Structures").getCompound("Starts");
@@ -86,12 +100,12 @@ class NBTStructureLoader {
 
         if (!loadedChunks.add(String.format("%s,%s", chunkX, chunkZ))) return;
 
-        CompoundNBT structureStarts = loadStructureStarts(chunkX, chunkZ);
-        if (structureStarts == null || structureStarts.size() == 0) return;
+        NbtCompound structureStarts = loadStructureStarts(chunkX, chunkZ);
+        if (structureStarts == null || structureStarts.getSize() == 0) return;
 
-        Map<String, StructureStart> structureStartMap = new HashMap<>();
-        for (String key : structureStarts.keySet()) {
-            CompoundNBT compound = structureStarts.getCompound(key);
+        Map<String, StructureStart<?>> structureStartMap = new HashMap<>();
+        for (String key : structureStarts.getKeys()) {
+            NbtCompound compound = structureStarts.getCompound(key);
             if (compound.contains("BB")) {
                 structureStartMap.put(key, new SimpleStructureStart(compound));
             }
@@ -100,58 +114,58 @@ class NBTStructureLoader {
         EventBus.publish(new StructuresLoaded(structureStartMap, dimensionId));
     }
 
-    private static class SimpleStructureStart extends StructureStart {
-        SimpleStructureStart(CompoundNBT compound) {
+    private static class SimpleStructureStart extends StructureStart<FeatureConfig> {
+        SimpleStructureStart(NbtCompound compound) {
             super(null,
-                    0,
-                    0,
                     null,
-                    new MutableBoundingBox(compound.getIntArray("BB")),
-                    0,
-                    0);
+                    0, 0);
 
-            ListNBT children = compound.getList("Children", 10);
+            NbtList children = compound.getList("Children", 10);
             for (int index = 0; index < children.size(); ++index) {
-                CompoundNBT child = children.getCompound(index);
-                if (child.contains("BB")) this.components.add(new SimpleStructurePiece(child));
+                NbtCompound child = children.getCompound(index);
+                if (child.contains("BB")) this.children.add(new SimpleStructurePiece(child));
             }
         }
 
         @Override
-        public void init(ChunkGenerator<?> chunkGenerator, TemplateManager templateManager, int i, int i1, Biome biome) {
+        public void init(DynamicRegistryManager registryManager, ChunkGenerator chunkGenerator, StructureManager manager, ChunkPos pos, Biome biome, FeatureConfig config, HeightLimitView world) {
 
         }
     }
 
     private static class SimpleStructurePiece extends StructurePiece {
-        SimpleStructurePiece(CompoundNBT compound) {
+        SimpleStructurePiece(NbtCompound compound) {
             super(null, compound);
         }
 
         @Override
-        protected void readAdditional(CompoundNBT compoundNBT) {
+        protected void writeNbt(ServerWorld world, NbtCompound nbt) {
 
         }
 
         @Override
-        public boolean addComponentParts(IWorld iWorld, Random random, MutableBoundingBox mutableBoundingBox, ChunkPos chunkPos) {
+        public boolean generate(StructureWorldAccess structureWorldAccess, StructureAccessor structureAccessor, ChunkGenerator chunkGenerator, Random random, BlockBox blockBox, ChunkPos chunkPos, BlockPos blockPos) {
             return false;
         }
     }
 
-    private static class ChunkLoader {
-        private final RegionFileCache regionFileCache;
+    private static class ChunkLoader implements AutoCloseable {
+        private static final BiFunction<File, Boolean, RegionBasedStorage> creator =
+                ReflectionHelper.getPrivateInstanceBuilder(RegionBasedStorage.class, File.class, boolean.class);
+
+        private final RegionBasedStorage regionFileCache;
 
         public ChunkLoader(File file) {
-            this.regionFileCache = new RegionFileCache(file) {
-            };
+            this.regionFileCache = creator.apply(file, false);
         }
 
-        public CompoundNBT readChunk(int chunkX, int chunkZ) throws IOException {
-            return regionFileCache.readChunk(new ChunkPos(chunkX, chunkZ));
+        public NbtCompound readChunk(int chunkX, int chunkZ) throws IOException {
+            if (regionFileCache == null) return null;
+            return regionFileCache.getTagAt(new ChunkPos(chunkX, chunkZ));
         }
 
         public void close() throws IOException {
+            if (regionFileCache == null) return;
             regionFileCache.close();
         }
     }