Cleaned up NBT Data Saving

This commit is contained in:
Ellpeck 2015-10-29 19:51:00 +01:00
parent a7c8a3c6e4
commit c282d6a8ef
4 changed files with 58 additions and 49 deletions

View file

@ -42,6 +42,7 @@ import ellpeck.actuallyadditions.tile.TileEntityBase;
import ellpeck.actuallyadditions.update.UpdateChecker; import ellpeck.actuallyadditions.update.UpdateChecker;
import ellpeck.actuallyadditions.util.ModUtil; import ellpeck.actuallyadditions.util.ModUtil;
import ellpeck.actuallyadditions.util.Util; import ellpeck.actuallyadditions.util.Util;
import ellpeck.actuallyadditions.util.playerdata.PersistentServerData;
import net.minecraft.init.Items; import net.minecraft.init.Items;
// So that BuildCraft Oil always gets used // So that BuildCraft Oil always gets used
@ -125,7 +126,8 @@ public class ActuallyAdditions{
@EventHandler @EventHandler
public void serverStopped(FMLServerStoppedEvent event){ 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(); LaserRelayConnectionHandler.getInstance().networks.clear();
PersistentServerData.playerSaveData.clear();
} }
} }

View file

@ -54,37 +54,35 @@ public class WorldData extends WorldSavedData{
@Override @Override
public void readFromNBT(NBTTagCompound compound){ public void readFromNBT(NBTTagCompound compound){
//Laser World Data //Laser World Data
NBTTagList list = compound.getTagList("Networks", 10); NBTTagList networkList = compound.getTagList("Networks", 10);
for(int i = 0; i < list.tagCount(); i++){ for(int i = 0; i < networkList.tagCount(); i++){
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(list.getCompoundTagAt(i)); LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().readNetworkFromNBT(networkList.getCompoundTagAt(i));
LaserRelayConnectionHandler.getInstance().networks.add(network); LaserRelayConnectionHandler.getInstance().networks.add(network);
} }
//Player Data //Player Data
int dataSize = compound.getInteger("PersistentDataSize"); NBTTagList playerList = compound.getTagList("PlayerData", 10);
PersistentServerData.playerSaveData.clear(); for(int i = 0; i < playerList.tagCount(); i++){
for(int i = 0; i < dataSize; i++){ PersistentServerData.PlayerSave aSave = PersistentServerData.PlayerSave.fromNBT(playerList.getCompoundTagAt(i));
PersistentServerData.PlayerSave aSave = PersistentServerData.PlayerSave.fromNBT(compound, "PlayerSaveData"+i); PersistentServerData.playerSaveData.add(aSave);
if(aSave != null){
PersistentServerData.playerSaveData.add(aSave);
}
} }
} }
@Override @Override
public void writeToNBT(NBTTagCompound compound){ public void writeToNBT(NBTTagCompound compound){
//Laser World Data //Laser World Data
NBTTagList list = new NBTTagList(); NBTTagList networkList = new NBTTagList();
for(LaserRelayConnectionHandler.Network network : LaserRelayConnectionHandler.getInstance().networks){ 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 //Player Data
compound.setInteger("PersistentDataSize", PersistentServerData.playerSaveData.size()); NBTTagList playerList = new NBTTagList();
for(int i = 0; i < PersistentServerData.playerSaveData.size(); i++){ for(int i = 0; i < PersistentServerData.playerSaveData.size(); i++){
PersistentServerData.PlayerSave theSave = PersistentServerData.playerSaveData.get(i); PersistentServerData.PlayerSave theSave = PersistentServerData.playerSaveData.get(i);
theSave.toNBT(compound, "PlayerSaveData"+i); playerList.appendTag(theSave.toNBT());
} }
compound.setTag("PlayerData", playerList);
} }
} }

View file

@ -32,17 +32,18 @@ public class PersistentClientData{
private static File theFile; private static File theFile;
public static void saveBookPage(BookletIndexEntry entry, BookletChapter chapter, BookletPage page, int pageInIndex){ public static void saveBookPage(BookletIndexEntry entry, BookletChapter chapter, BookletPage page, int pageInIndex){
NBTTagCompound compound = readCompound(); NBTTagCompound baseCompound = getBaseCompound();
if(compound != null){ NBTTagCompound worldCompound = getCompoundForWorld(baseCompound);
compound.setInteger(getName("Entry"), entry == null ? -1 : InitBooklet.entries.indexOf(entry)); if(worldCompound != null){
compound.setInteger(getName("Chapter"), entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter)); worldCompound.setInteger("Entry", entry == null ? -1 : InitBooklet.entries.indexOf(entry));
compound.setInteger(getName("Page"), page == null ? -1 : page.getID()); worldCompound.setInteger("Chapter", entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter));
compound.setInteger(getName("PageInIndex"), pageInIndex); worldCompound.setInteger("Page", page == null ? -1 : page.getID());
writeCompound(compound); worldCompound.setInteger("PageInIndex", pageInIndex);
writeCompound(baseCompound, worldCompound);
} }
} }
private static NBTTagCompound readCompound(){ private static NBTTagCompound getBaseCompound(){
try{ try{
return CompressedStreamTools.readCompressed(new FileInputStream(getTheFile())); return CompressedStreamTools.readCompressed(new FileInputStream(getTheFile()));
} }
@ -51,14 +52,18 @@ public class PersistentClientData{
} }
} }
private static String getName(String name){ private static String getName(){
return (Minecraft.getMinecraft().isIntegratedServerRunning() ? Minecraft.getMinecraft().getIntegratedServer().getFolderName() : Minecraft.getMinecraft().func_147104_D().serverIP)+"-"+name; return Minecraft.getMinecraft().isIntegratedServerRunning() ? Minecraft.getMinecraft().getIntegratedServer().getFolderName() : Minecraft.getMinecraft().func_147104_D().serverIP;
} }
private static void writeCompound(NBTTagCompound compound){ private static NBTTagCompound getCompoundForWorld(NBTTagCompound mainCompound){
try{ 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){ catch(Exception e){
ModUtil.LOGGER.fatal("Couldn't write Persistent Variable!", e); ModUtil.LOGGER.fatal("Couldn't write Persistent Variable!", e);
@ -82,17 +87,17 @@ public class PersistentClientData{
} }
public static void openLastBookPage(GuiBooklet gui){ public static void openLastBookPage(GuiBooklet gui){
NBTTagCompound compound = readCompound(); NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound());
if(compound != null){ if(worldCompound != null){
if(compound.hasKey(getName("Entry"))){ if(worldCompound.hasKey("Entry")){
int entry = compound.getInteger(getName("Entry")); int entry = worldCompound.getInteger("Entry");
int chapter = compound.getInteger(getName("Chapter")); int chapter = worldCompound.getInteger("Chapter");
int page = compound.getInteger(getName("Page")); int page = worldCompound.getInteger("Page");
BookletIndexEntry currentIndexEntry = entry == -1 ? null : InitBooklet.entries.get(entry); BookletIndexEntry currentIndexEntry = entry == -1 ? null : InitBooklet.entries.get(entry);
BookletChapter currentChapter = chapter == -1 || entry == -1 || currentIndexEntry.chapters.size() <= chapter ? null : currentIndexEntry.chapters.get(chapter); 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]; 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); gui.openIndexEntry(currentIndexEntry, pageInIndex, true);
if(currentChapter != null){ if(currentChapter != null){
@ -106,15 +111,16 @@ public class PersistentClientData{
} }
public static void setBoolean(String name, boolean bool){ public static void setBoolean(String name, boolean bool){
NBTTagCompound compound = readCompound(); NBTTagCompound baseCompound = getBaseCompound();
if(compound != null){ NBTTagCompound worldCompound = getCompoundForWorld(baseCompound);
compound.setBoolean(getName(name), bool); if(worldCompound != null){
writeCompound(compound); worldCompound.setBoolean(name, bool);
writeCompound(baseCompound, worldCompound);
} }
} }
public static boolean getBoolean(String name){ public static boolean getBoolean(String name){
NBTTagCompound compound = readCompound(); NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound());
return compound != null && compound.getBoolean(getName(name)); return worldCompound != null && worldCompound.getBoolean(name);
} }
} }

View file

@ -44,17 +44,20 @@ public class PersistentServerData{
this.theCompound = theCompound; this.theCompound = theCompound;
} }
public static PlayerSave fromNBT(NBTTagCompound compound, String name){ public static PlayerSave fromNBT(NBTTagCompound compound){
UUID theID = new UUID(compound.getLong(name+"MostSignificant"), compound.getLong(name+"LeastSignificant")); UUID theID = new UUID(compound.getLong("MostSignificant"), compound.getLong("LeastSignificant"));
NBTTagCompound theCompound = compound.getCompoundTag(name+"Tag"); NBTTagCompound theCompound = compound.getCompoundTag("Tag");
return new PlayerSave(theID, theCompound); return new PlayerSave(theID, theCompound);
} }
public void toNBT(NBTTagCompound compound, String name){ public NBTTagCompound toNBT(){
compound.setLong(name+"LeastSignificant", this.thePlayerUUID.getLeastSignificantBits()); NBTTagCompound compound = new NBTTagCompound();
compound.setLong(name+"MostSignificant", this.thePlayerUUID.getMostSignificantBits()); 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;
} }
} }