]> 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 e3cf56dc91f5f72e40094a891010ed97dd741117..b0c76ec3582e7760650a5fc097fb5e9b17c03423 100644 (file)
@@ -4,75 +4,84 @@ import com.irtimaled.bbor.common.EventBus;
 import com.irtimaled.bbor.common.ReflectionHelper;
 import com.irtimaled.bbor.common.events.StructuresLoaded;
 import com.irtimaled.bbor.common.models.DimensionId;
-import net.minecraft.nbt.CompoundTag;
-import net.minecraft.nbt.ListTag;
+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.world.FeatureUpdater;
-import net.minecraft.world.IWorld;
-import net.minecraft.world.PersistentStateManager;
-import net.minecraft.world.WorldSaveHandler;
+import net.minecraft.util.registry.DynamicRegistryManager;
+import net.minecraft.world.*;
 import net.minecraft.world.biome.Biome;
 import net.minecraft.world.dimension.DimensionType;
+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.Function;
+import java.util.function.BiFunction;
 
 class NBTStructureLoader {
     private final DimensionId dimensionId;
     private final Set<String> loadedChunks = new HashSet<>();
 
     private FeatureUpdater legacyStructureDataUtil = null;
-    private WorldSaveHandler saveHandler = null;
+    private LevelStorage.Session saveHandler = null;
     private File chunkSaveLocation = null;
     private ChunkLoader chunkLoader;
 
-    NBTStructureLoader(DimensionId dimensionId, WorldSaveHandler saveHandler, File worldDirectory) {
+    NBTStructureLoader(DimensionId dimensionId, LevelStorage.Session saveHandler, File worldDirectory) {
         this.dimensionId = dimensionId;
         this.configure(saveHandler, worldDirectory);
     }
 
     void clear() {
         this.legacyStructureDataUtil = null;
-        this.saveHandler = 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) {
+            }
         }
-        this.chunkLoader = null;
     }
 
-    void configure(WorldSaveHandler saveHandler, File worldDirectory) {
+    void configure(LevelStorage.Session saveHandler, File worldDirectory) {
         this.saveHandler = saveHandler;
         if (worldDirectory != null) {
-            this.chunkSaveLocation = new File(dimensionId.getDimensionType().getSaveDirectory(worldDirectory), "region");
+            this.chunkSaveLocation = new File(DimensionType.getSaveDirectory(this.dimensionId.getDimensionType(), worldDirectory), "region");
             this.chunkLoader = new ChunkLoader(this.chunkSaveLocation);
         }
     }
 
     private FeatureUpdater getLegacyStructureDataUtil() {
         if (this.legacyStructureDataUtil == null) {
-            File dataFolder = new File(DimensionType.OVERWORLD.getSaveDirectory(this.saveHandler.getWorldDir()), "data");
+            File dataFolder = new File(this.saveHandler.getWorldDirectory(World.OVERWORLD), "data");
             this.legacyStructureDataUtil = FeatureUpdater.create(dimensionId.getDimensionType(),
-                    new PersistentStateManager(dataFolder, this.saveHandler.getDataFixer()));
+                    new PersistentStateManager(dataFolder, MinecraftClient.getInstance().getDataFixer()));
         }
         return this.legacyStructureDataUtil;
     }
 
-    private CompoundTag loadStructureStarts(int chunkX, int chunkZ) {
+    private NbtCompound loadStructureStarts(int chunkX, int chunkZ) {
         try {
-            CompoundTag 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) {
@@ -91,12 +100,12 @@ class NBTStructureLoader {
 
         if (!loadedChunks.add(String.format("%s,%s", chunkX, chunkZ))) return;
 
-        CompoundTag structureStarts = loadStructureStarts(chunkX, chunkZ);
+        NbtCompound structureStarts = loadStructureStarts(chunkX, chunkZ);
         if (structureStarts == null || structureStarts.getSize() == 0) return;
 
-        Map<String, StructureStart> structureStartMap = new HashMap<>();
+        Map<String, StructureStart<?>> structureStartMap = new HashMap<>();
         for (String key : structureStarts.getKeys()) {
-            CompoundTag compound = structureStarts.getCompound(key);
+            NbtCompound compound = structureStarts.getCompound(key);
             if (compound.contains("BB")) {
                 structureStartMap.put(key, new SimpleStructureStart(compound));
             }
@@ -105,55 +114,52 @@ class NBTStructureLoader {
         EventBus.publish(new StructuresLoaded(structureStartMap, dimensionId));
     }
 
-    private static class SimpleStructureStart extends StructureStart {
-        SimpleStructureStart(CompoundTag compound) {
+    private static class SimpleStructureStart extends StructureStart<FeatureConfig> {
+        SimpleStructureStart(NbtCompound compound) {
             super(null,
-                    0,
-                    0,
-                    new BlockBox(compound.getIntArray("BB")),
-                    0,
-                    0);
+                    null,
+                    0, 0);
 
-            ListTag children = compound.getList("Children", 10);
+            NbtList children = compound.getList("Children", 10);
             for (int index = 0; index < children.size(); ++index) {
-                CompoundTag child = children.getCompound(index);
+                NbtCompound child = children.getCompound(index);
                 if (child.contains("BB")) this.children.add(new SimpleStructurePiece(child));
             }
         }
 
         @Override
-        public void initialize(ChunkGenerator<?> chunkGenerator, StructureManager structureManager, 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(CompoundTag compound) {
+        SimpleStructurePiece(NbtCompound compound) {
             super(null, compound);
         }
 
         @Override
-        protected void toNbt(CompoundTag compoundTag) {
+        protected void writeNbt(ServerWorld world, NbtCompound nbt) {
 
         }
 
         @Override
-        public boolean generate(IWorld iWorld, ChunkGenerator<?> chunkGenerator, Random random, BlockBox blockBox, 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 static final Function<File, RegionBasedStorage> creator =
-                ReflectionHelper.getPrivateInstanceBuilder(RegionBasedStorage.class, File.class);
+    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 = creator.apply(file);
+            this.regionFileCache = creator.apply(file, false);
         }
 
-        public CompoundTag readChunk(int chunkX, int chunkZ) throws IOException {
+        public NbtCompound readChunk(int chunkX, int chunkZ) throws IOException {
             if (regionFileCache == null) return null;
             return regionFileCache.getTagAt(new ChunkPos(chunkX, chunkZ));
         }