From c282d6a8efc12e6d22c72172abb422f021557eed Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 29 Oct 2015 19:51:00 +0100 Subject: [PATCH] Cleaned up NBT Data Saving --- .../actuallyadditions/ActuallyAdditions.java | 4 +- .../actuallyadditions/misc/WorldData.java | 28 +++++---- .../util/playerdata/PersistentClientData.java | 58 ++++++++++--------- .../util/playerdata/PersistentServerData.java | 17 +++--- 4 files changed, 58 insertions(+), 49 deletions(-) diff --git a/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java b/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java index 11b502f6b..ee787c9ba 100644 --- a/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java +++ b/src/main/java/ellpeck/actuallyadditions/ActuallyAdditions.java @@ -42,6 +42,7 @@ import ellpeck.actuallyadditions.tile.TileEntityBase; import ellpeck.actuallyadditions.update.UpdateChecker; import ellpeck.actuallyadditions.util.ModUtil; import ellpeck.actuallyadditions.util.Util; +import ellpeck.actuallyadditions.util.playerdata.PersistentServerData; import net.minecraft.init.Items; // So that BuildCraft Oil always gets used @@ -125,7 +126,8 @@ public class ActuallyAdditions{ @EventHandler public void serverStopped(FMLServerStoppedEvent event){ - //Clear all Network Connections so that they won't be carried over into other worlds + //Clear Data so that it won't be carried over to other worlds LaserRelayConnectionHandler.getInstance().networks.clear(); + PersistentServerData.playerSaveData.clear(); } } diff --git a/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java b/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java index 968472cd9..4bc7347d8 100644 --- a/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java +++ b/src/main/java/ellpeck/actuallyadditions/misc/WorldData.java @@ -54,37 +54,35 @@ public class WorldData extends WorldSavedData{ @Override public void readFromNBT(NBTTagCompound compound){ //Laser World Data - NBTTagList list = compound.getTagList("Networks", 10); - for(int i = 0; i < list.tagCount(); i++){ - LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(list.getCompoundTagAt(i)); + NBTTagList networkList = compound.getTagList("Networks", 10); + for(int i = 0; i < networkList.tagCount(); i++){ + LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(networkList.getCompoundTagAt(i)); LaserRelayConnectionHandler.getInstance().networks.add(network); } //Player Data - int dataSize = compound.getInteger("PersistentDataSize"); - PersistentServerData.playerSaveData.clear(); - for(int i = 0; i < dataSize; i++){ - PersistentServerData.PlayerSave aSave = PersistentServerData.PlayerSave.fromNBT(compound, "PlayerSaveData"+i); - if(aSave != null){ - PersistentServerData.playerSaveData.add(aSave); - } + NBTTagList playerList = compound.getTagList("PlayerData", 10); + for(int i = 0; i < playerList.tagCount(); i++){ + PersistentServerData.PlayerSave aSave = PersistentServerData.PlayerSave.fromNBT(playerList.getCompoundTagAt(i)); + PersistentServerData.playerSaveData.add(aSave); } } @Override public void writeToNBT(NBTTagCompound compound){ //Laser World Data - NBTTagList list = new NBTTagList(); + NBTTagList networkList = new NBTTagList(); for(LaserRelayConnectionHandler.Network network : LaserRelayConnectionHandler.getInstance().networks){ - list.appendTag(LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(network)); + networkList.appendTag(LaserRelayConnectionHandler.getInstance().writeNetworkToNBT(network)); } - compound.setTag("Networks", list); + compound.setTag("Networks", networkList); //Player Data - compound.setInteger("PersistentDataSize", PersistentServerData.playerSaveData.size()); + NBTTagList playerList = new NBTTagList(); for(int i = 0; i < PersistentServerData.playerSaveData.size(); i++){ PersistentServerData.PlayerSave theSave = PersistentServerData.playerSaveData.get(i); - theSave.toNBT(compound, "PlayerSaveData"+i); + playerList.appendTag(theSave.toNBT()); } + compound.setTag("PlayerData", playerList); } } diff --git a/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentClientData.java b/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentClientData.java index bf3f6bea1..1a9ab2c27 100644 --- a/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentClientData.java +++ b/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentClientData.java @@ -32,17 +32,18 @@ public class PersistentClientData{ private static File theFile; public static void saveBookPage(BookletIndexEntry entry, BookletChapter chapter, BookletPage page, int pageInIndex){ - NBTTagCompound compound = readCompound(); - if(compound != null){ - compound.setInteger(getName("Entry"), entry == null ? -1 : InitBooklet.entries.indexOf(entry)); - compound.setInteger(getName("Chapter"), entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter)); - compound.setInteger(getName("Page"), page == null ? -1 : page.getID()); - compound.setInteger(getName("PageInIndex"), pageInIndex); - writeCompound(compound); + NBTTagCompound baseCompound = getBaseCompound(); + NBTTagCompound worldCompound = getCompoundForWorld(baseCompound); + if(worldCompound != null){ + worldCompound.setInteger("Entry", entry == null ? -1 : InitBooklet.entries.indexOf(entry)); + worldCompound.setInteger("Chapter", entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter)); + worldCompound.setInteger("Page", page == null ? -1 : page.getID()); + worldCompound.setInteger("PageInIndex", pageInIndex); + writeCompound(baseCompound, worldCompound); } } - private static NBTTagCompound readCompound(){ + private static NBTTagCompound getBaseCompound(){ try{ return CompressedStreamTools.readCompressed(new FileInputStream(getTheFile())); } @@ -51,14 +52,18 @@ public class PersistentClientData{ } } - private static String getName(String name){ - return (Minecraft.getMinecraft().isIntegratedServerRunning() ? Minecraft.getMinecraft().getIntegratedServer().getFolderName() : Minecraft.getMinecraft().func_147104_D().serverIP)+"-"+name; + private static String getName(){ + return Minecraft.getMinecraft().isIntegratedServerRunning() ? Minecraft.getMinecraft().getIntegratedServer().getFolderName() : Minecraft.getMinecraft().func_147104_D().serverIP; } - private static void writeCompound(NBTTagCompound compound){ - try{ + private static NBTTagCompound getCompoundForWorld(NBTTagCompound mainCompound){ + return mainCompound.getCompoundTag(getName()); + } - CompressedStreamTools.writeCompressed(compound, new FileOutputStream(getTheFile())); + private static void writeCompound(NBTTagCompound baseCompound, NBTTagCompound worldCompound){ + baseCompound.setTag(getName(), worldCompound); + try{ + CompressedStreamTools.writeCompressed(baseCompound, new FileOutputStream(getTheFile())); } catch(Exception e){ ModUtil.LOGGER.fatal("Couldn't write Persistent Variable!", e); @@ -82,17 +87,17 @@ public class PersistentClientData{ } public static void openLastBookPage(GuiBooklet gui){ - NBTTagCompound compound = readCompound(); - if(compound != null){ - if(compound.hasKey(getName("Entry"))){ - int entry = compound.getInteger(getName("Entry")); - int chapter = compound.getInteger(getName("Chapter")); - int page = compound.getInteger(getName("Page")); + NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound()); + if(worldCompound != null){ + if(worldCompound.hasKey("Entry")){ + int entry = worldCompound.getInteger("Entry"); + int chapter = worldCompound.getInteger("Chapter"); + int page = worldCompound.getInteger("Page"); BookletIndexEntry currentIndexEntry = entry == -1 ? null : InitBooklet.entries.get(entry); BookletChapter currentChapter = chapter == -1 || entry == -1 || currentIndexEntry.chapters.size() <= chapter ? null : currentIndexEntry.chapters.get(chapter); BookletPage currentPage = chapter == -1 || currentChapter == null || currentChapter.pages.length <= page-1 ? null : currentChapter.pages[page-1]; - int pageInIndex = compound.getInteger(getName("PageInIndex")); + int pageInIndex = worldCompound.getInteger("PageInIndex"); gui.openIndexEntry(currentIndexEntry, pageInIndex, true); if(currentChapter != null){ @@ -106,15 +111,16 @@ public class PersistentClientData{ } public static void setBoolean(String name, boolean bool){ - NBTTagCompound compound = readCompound(); - if(compound != null){ - compound.setBoolean(getName(name), bool); - writeCompound(compound); + NBTTagCompound baseCompound = getBaseCompound(); + NBTTagCompound worldCompound = getCompoundForWorld(baseCompound); + if(worldCompound != null){ + worldCompound.setBoolean(name, bool); + writeCompound(baseCompound, worldCompound); } } public static boolean getBoolean(String name){ - NBTTagCompound compound = readCompound(); - return compound != null && compound.getBoolean(getName(name)); + NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound()); + return worldCompound != null && worldCompound.getBoolean(name); } } diff --git a/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentServerData.java b/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentServerData.java index e58030b49..1eec655d9 100644 --- a/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentServerData.java +++ b/src/main/java/ellpeck/actuallyadditions/util/playerdata/PersistentServerData.java @@ -44,17 +44,20 @@ public class PersistentServerData{ this.theCompound = theCompound; } - public static PlayerSave fromNBT(NBTTagCompound compound, String name){ - UUID theID = new UUID(compound.getLong(name+"MostSignificant"), compound.getLong(name+"LeastSignificant")); - NBTTagCompound theCompound = compound.getCompoundTag(name+"Tag"); + public static PlayerSave fromNBT(NBTTagCompound compound){ + UUID theID = new UUID(compound.getLong("MostSignificant"), compound.getLong("LeastSignificant")); + NBTTagCompound theCompound = compound.getCompoundTag("Tag"); return new PlayerSave(theID, theCompound); } - public void toNBT(NBTTagCompound compound, String name){ - compound.setLong(name+"LeastSignificant", this.thePlayerUUID.getLeastSignificantBits()); - compound.setLong(name+"MostSignificant", this.thePlayerUUID.getMostSignificantBits()); + public NBTTagCompound toNBT(){ + NBTTagCompound compound = new NBTTagCompound(); + compound.setLong("LeastSignificant", this.thePlayerUUID.getLeastSignificantBits()); + compound.setLong("MostSignificant", this.thePlayerUUID.getMostSignificantBits()); - compound.setTag(name+"Tag", this.theCompound); + compound.setTag("Tag", this.theCompound); + + return compound; } }