Saving world data

This commit is contained in:
Connor Colenso committed 2024-01-05 15:40:11 +00:00
1 parent f693ca75b9
commit 06350f82ac
4 files changed
+151 -4

No files matched your search

+3 -1
View File
@@ -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(""));
}
}
@@ -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);
}
}
@@ -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<String> 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);
}
}
@@ -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<Pair<Integer, Integer>> 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<Pair<Integer, Integer>>) 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);
}
}
}