From 06350f82ac9e51410597369a002143ec67ddc3ab Mon Sep 17 00:00:00 2001 From: Connor Colenso Date: Fri, 5 Jan 2024 15:40:11 +0000 Subject: [PATCH] Saving world data --- src/main/java/com/colen/postea/Postea.java | 4 +- .../postea/Utility/ChunkFixerUtility.java | 16 ++- .../colen/postea/Utility/PosteaUtilities.java | 31 ++++++ .../PosteaProcessedChunksWorldSavedData.java | 104 ++++++++++++++++++ 4 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/colen/postea/checker/PosteaProcessedChunksWorldSavedData.java diff --git a/src/main/java/com/colen/postea/Postea.java b/src/main/java/com/colen/postea/Postea.java index 36c0d6f..40db775 100644 --- a/src/main/java/com/colen/postea/Postea.java +++ b/src/main/java/com/colen/postea/Postea.java @@ -1,7 +1,9 @@ package com.colen.postea; +import com.colen.postea.checker.PosteaProcessedChunksWorldSavedData; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.common.MinecraftForge; @Mod(modid = Tags.MODID, version = Tags.VERSION, name = Tags.MODNAME, acceptedMinecraftVersions = "[1.7.10]") @SuppressWarnings("unused") @@ -9,7 +11,7 @@ public class Postea { @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { - + MinecraftForge.EVENT_BUS.register(new PosteaProcessedChunksWorldSavedData("")); } } diff --git a/src/main/java/com/colen/postea/Utility/ChunkFixerUtility.java b/src/main/java/com/colen/postea/Utility/ChunkFixerUtility.java index 18ee057..8a9f94f 100644 --- a/src/main/java/com/colen/postea/Utility/ChunkFixerUtility.java +++ b/src/main/java/com/colen/postea/Utility/ChunkFixerUtility.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.function.BiFunction; import java.util.stream.Collectors; +import com.colen.postea.checker.PosteaProcessedChunksWorldSavedData; import net.minecraft.block.Block; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -17,12 +18,19 @@ import akka.japi.Pair; import cpw.mods.fml.common.registry.GameRegistry; public class ChunkFixerUtility { - public static void processChunkNBT(NBTTagCompound compound, World world) { - transformTileEntities(compound, world); - transformNormalBlocks(compound, world); + NBTTagCompound level = compound.getCompoundTag("Level"); + int x = level.getInteger("xPos"); + int z = level.getInteger("zPos"); + + if (PosteaProcessedChunksWorldSavedData.addChunk(x, z)) { + transformTileEntities(compound, world); + transformNormalBlocks(compound, world); + } + } + private static void transformNormalBlocks(NBTTagCompound compound, World world) { NBTTagCompound level = compound.getCompoundTag("Level"); NBTTagList sections = level.getTagList("Sections", 10); @@ -171,6 +179,8 @@ public class ChunkFixerUtility { } // Otherwise they are removed, therefore not appended. conversionInfo.add(new ConversionInfo(x, y, z, blockInfo)); + } else { + tileEntitiesCopy.appendTag(tileEntity); } } diff --git a/src/main/java/com/colen/postea/Utility/PosteaUtilities.java b/src/main/java/com/colen/postea/Utility/PosteaUtilities.java index 79968f9..77c86b2 100644 --- a/src/main/java/com/colen/postea/Utility/PosteaUtilities.java +++ b/src/main/java/com/colen/postea/Utility/PosteaUtilities.java @@ -1,7 +1,13 @@ package com.colen.postea.Utility; +import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.ModContainer; import net.minecraft.nbt.NBTTagCompound; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Objects; + public abstract class PosteaUtilities { @SuppressWarnings("unused") @@ -21,4 +27,29 @@ public abstract class PosteaUtilities { return tagCompound; } + + public static int getModListHash() { + + // We iterate over every mod, get its version and put it into a giant list. + // This is done so we can see if our mods have actually changed, if so, we + // will recheck every chunk for Postea adjustments. + + ArrayList modList = new ArrayList<>(); + + for (ModContainer mod : Loader.instance().getActiveModList()) { + modList.add(mod.getModId() + ":" + mod.getVersion()); + } + + // Sort this first. + Collections.sort(modList); + + // Step 2: Append all elements into one string + StringBuilder combinedModStringSorted = new StringBuilder(); + for (String element : modList) { + combinedModStringSorted.append(element); + } + + return Objects.hash(combinedModStringSorted); + } + } diff --git a/src/main/java/com/colen/postea/checker/PosteaProcessedChunksWorldSavedData.java b/src/main/java/com/colen/postea/checker/PosteaProcessedChunksWorldSavedData.java new file mode 100644 index 0000000..f6ac7f5 --- /dev/null +++ b/src/main/java/com/colen/postea/checker/PosteaProcessedChunksWorldSavedData.java @@ -0,0 +1,104 @@ +package com.colen.postea.checker; + +import akka.japi.Pair; +import cpw.mods.fml.common.eventhandler.EventPriority; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import net.minecraft.world.WorldSavedData; +import net.minecraft.world.storage.MapStorage; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.WorldEvent; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.HashSet; + +public class PosteaProcessedChunksWorldSavedData extends WorldSavedData { + + private static final String DATA_NAME = "GregTech_GlobalIntValue"; + public static HashSet> chunkCoordinates = new HashSet<>(); + public static PosteaProcessedChunksWorldSavedData INSTANCE; + + // True if chunk has not been processed, else false. + public static boolean addChunk(int x, int z) { + boolean updateChunk = chunkCoordinates.add(new Pair<>(x, z)); + + if (updateChunk) { + // Only mark the world NBT as dirty if we actually process a new chunk, since we will need to save that info. + INSTANCE.markDirty(); + } + + return updateChunk; + } + + public PosteaProcessedChunksWorldSavedData() { + super(DATA_NAME); + } + + public PosteaProcessedChunksWorldSavedData(String name) { + super(name); + } + + @Override + public void readFromNBT(NBTTagCompound nbtTagCompound) { + try { + byte[] ba = nbtTagCompound.getByteArray(DATA_NAME); + InputStream byteArrayInputStream = new ByteArrayInputStream(ba); + ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); + Object data = objectInputStream.readObject(); + chunkCoordinates = (HashSet>) data; + } catch (IOException | ClassNotFoundException exception) { + System.out.println(DATA_NAME + " FAILED"); + exception.printStackTrace(); + } + + } + + @Override + public void writeToNBT(NBTTagCompound nbtTagCompound) { + // Write the integer value to NBT + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); + objectOutputStream.writeObject(chunkCoordinates); + objectOutputStream.flush(); + byte[] data = byteArrayOutputStream.toByteArray(); + nbtTagCompound.setByteArray(DATA_NAME, data); + } catch (IOException exception) { + System.out.println(DATA_NAME + " SAVE FAILED"); + exception.printStackTrace(); + } + } + + + public static void get(World world) { + + chunkCoordinates.clear(); + + MapStorage storage = world.mapStorage; + INSTANCE = (PosteaProcessedChunksWorldSavedData) storage.loadData(PosteaProcessedChunksWorldSavedData.class, DATA_NAME); + + if (INSTANCE == null) { + INSTANCE = new PosteaProcessedChunksWorldSavedData(); + storage.setData(DATA_NAME, INSTANCE); + } + + } + + // Register event in a static block to ensure it's done as early as possible + static { + MinecraftForge.EVENT_BUS.register(new PosteaProcessedChunksWorldSavedData()); + } + + @SubscribeEvent(priority = EventPriority.HIGHEST) + public void onWorldLoad(WorldEvent.Load event) { + if (!event.world.isRemote && event.world.provider.dimensionId == 0) { + get(event.world); + } + } +}