import com.irtimaled.bbor.common.ReflectionHelper;
import com.irtimaled.bbor.common.events.StructuresLoaded;
import com.irtimaled.bbor.common.models.DimensionId;
+import net.minecraft.client.MinecraftClient;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
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.registry.DynamicRegistryManager;
import net.minecraft.world.FeatureUpdater;
-import net.minecraft.world.IWorld;
import net.minecraft.world.PersistentStateManager;
-import net.minecraft.world.WorldSaveHandler;
+import net.minecraft.world.StructureWorldAccess;
+import net.minecraft.world.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;
}
CompoundTag 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);
if (compound.contains("BB")) {
EventBus.publish(new StructuresLoaded(structureStartMap, dimensionId));
}
- private static class SimpleStructureStart extends StructureStart {
+ private static class SimpleStructureStart extends StructureStart<FeatureConfig> {
SimpleStructureStart(CompoundTag compound) {
super(null,
0,
}
@Override
- public void initialize(ChunkGenerator<?> chunkGenerator, StructureManager structureManager, int i, int i1, Biome biome) {
-
+ public void init(DynamicRegistryManager dynamicRegistryManager, ChunkGenerator chunkGenerator, StructureManager structureManager, int i, int j, Biome biome, FeatureConfig featureConfig) {
}
}
}
@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 {