From 0cd68aedca19b4ed72574f4880c847aac5e87ff7 Mon Sep 17 00:00:00 2001 From: Azanor Date: Mon, 21 Apr 2014 20:19:36 +0200 Subject: [PATCH] fix for possible player data corruption switched to using custom player data files instead of saving it with vanilla playerdata --- build.gradle | 2 +- src/main/java/baubles/common/Baubles.java | 2 +- .../container/ContainerPlayerExpanded.java | 2 +- .../common/container/InventoryBaubles.java | 10 +- .../baubles/common/lib/PlayerHandler.java | 117 ++++++++++++++++-- .../common/network/EventHandlerNetwork.java | 4 +- src/main/resources/changelog.txt | 5 + 7 files changed, 123 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index 94e8338..f9f8f95 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { apply plugin: 'forge' -version = "1.7.2-1.0.0.9" +version = "1.7.2-1.0.0.10" group= "com.azanor.baubles" archivesBaseName = "Baubles" diff --git a/src/main/java/baubles/common/Baubles.java b/src/main/java/baubles/common/Baubles.java index 302c4ea..c1fa9e6 100644 --- a/src/main/java/baubles/common/Baubles.java +++ b/src/main/java/baubles/common/Baubles.java @@ -26,7 +26,7 @@ public class Baubles { public static final String MODID = "Baubles"; public static final String MODNAME = "Baubles"; - public static final String VERSION = "1.0.0.9"; + public static final String VERSION = "1.0.0.10"; @SidedProxy(clientSide = "baubles.client.ClientProxy", serverSide = "baubles.common.CommonProxy") public static CommonProxy proxy; diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index e154dfe..e3dab06 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -118,7 +118,7 @@ public class ContainerPlayerExpanded extends Container this.craftResult.setInventorySlotContents(0, (ItemStack)null); if (!player.worldObj.isRemote) { PlayerHandler.setPlayerBaubles(player, baubles); - PlayerHandler.updatePlayerBaubles(player); + PlayerHandler.savePlayerBaubles(player); } } diff --git a/src/main/java/baubles/common/container/InventoryBaubles.java b/src/main/java/baubles/common/container/InventoryBaubles.java index 4235c9e..f55f00a 100644 --- a/src/main/java/baubles/common/container/InventoryBaubles.java +++ b/src/main/java/baubles/common/container/InventoryBaubles.java @@ -149,7 +149,7 @@ public class InventoryBaubles implements IInventory { @Override public void markDirty() { try { - PlayerHandler.updatePlayerBaubles(player.get()); + PlayerHandler.savePlayerBaubles(player.get()); } catch (Exception e) { } } @@ -195,6 +195,10 @@ public class InventoryBaubles implements IInventory { public void saveNBT(EntityPlayer player) { NBTTagCompound tags = player.getEntityData(); + saveNBT(tags); + } + + public void saveNBT(NBTTagCompound tags) { NBTTagList tagList = new NBTTagList(); NBTTagCompound invSlot; for (int i = 0; i < this.stackList.length; ++i) { @@ -210,6 +214,10 @@ public class InventoryBaubles implements IInventory { public void readNBT(EntityPlayer player) { NBTTagCompound tags = player.getEntityData(); + readNBT(tags); + } + + public void readNBT(NBTTagCompound tags) { NBTTagList tagList = tags.getTagList("Baubles.Inventory", 10); for (int i = 0; i < tagList.tagCount(); ++i) { NBTTagCompound nbttagcompound = (NBTTagCompound) tagList.getCompoundTagAt(i); diff --git a/src/main/java/baubles/common/lib/PlayerHandler.java b/src/main/java/baubles/common/lib/PlayerHandler.java index 5948c80..e937cfc 100644 --- a/src/main/java/baubles/common/lib/PlayerHandler.java +++ b/src/main/java/baubles/common/lib/PlayerHandler.java @@ -1,12 +1,19 @@ package baubles.common.lib; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.util.HashMap; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; +import baubles.common.Baubles; import baubles.common.container.InventoryBaubles; public class PlayerHandler { + private static File playersDirectory; + private static HashMap playerBaubles = new HashMap(); public static InventoryBaubles getPlayerBaubles(EntityPlayer player) { @@ -21,19 +28,105 @@ public class PlayerHandler { playerBaubles.put(player.getCommandSenderName(), inventory); } - public static void updatePlayerBaubles(EntityPlayer player) { - InventoryBaubles inventory = getPlayerBaubles(player); - inventory.saveNBT(player); - } +// public static void updatePlayerBaubles(EntityPlayer player) { +// InventoryBaubles inventory = getPlayerBaubles(player); +// inventory.saveNBT(player); +// } + + public static File getFileFromPlayer(EntityPlayer player) + { + if (playersDirectory == null) { + try { + playersDirectory = new File(player.worldObj.getSaveHandler().getWorldDirectory(), "players"); + } catch (Exception e) { e.printStackTrace(); } + } + return new File(playersDirectory, player.getCommandSenderName() + ".baub"); + } + + public static File getBackupFileFromPlayer(EntityPlayer player) + { + if (playersDirectory == null) { + try { + playersDirectory = new File(player.worldObj.getSaveHandler().getWorldDirectory(), "players"); + } catch (Exception e) { e.printStackTrace(); } + } + return new File(playersDirectory, player.getCommandSenderName() + ".baubback"); + } public static void loadPlayerBaubles(EntityPlayer player) { - NBTTagCompound data = player.getEntityData(); - if (!data.hasKey("Baubles")) { - data.setTag("Baubles", new NBTTagCompound()); - } - InventoryBaubles inventory = new InventoryBaubles(player); - inventory.readNBT(player); - playerBaubles.put(player.getCommandSenderName(), inventory); - updatePlayerBaubles(player); + try + { + File file1 = getFileFromPlayer(player); + + if (file1 == null || !file1.exists()) + { + Baubles.logger.warn("Baubles inventory not found for "+player.getCommandSenderName()+". Trying to load backup baubles data."); + file1 = getBackupFileFromPlayer(player); + } + if (file1 != null && file1.exists()) + { + FileInputStream fileinputstream = new FileInputStream(file1); + NBTTagCompound data = CompressedStreamTools.readCompressed(fileinputstream); + fileinputstream.close(); + + InventoryBaubles inventory = new InventoryBaubles(player); + inventory.readNBT(data); + playerBaubles.put(player.getCommandSenderName(), inventory); + + } else { //legacy data + Baubles.logger.warn("Baubles inventory not found for "+player.getCommandSenderName()+". Trying to load legacy baubles data."); + NBTTagCompound data = player.getEntityData(); + if (!data.hasKey("Baubles")) { + data.setTag("Baubles", new NBTTagCompound()); + } + InventoryBaubles inventory = new InventoryBaubles(player); + inventory.readNBT(player); + playerBaubles.put(player.getCommandSenderName(), inventory); + savePlayerBaubles(player); + } + } + catch (Exception exception1) + { + exception1.printStackTrace(); + Baubles.logger.fatal("Error loading baubles inventory"); + } + + } + + public static void savePlayerBaubles(EntityPlayer player) { + try + { + File file1 = getFileFromPlayer(player); + if (file1 != null ) + { + InventoryBaubles inventory = getPlayerBaubles(player); + NBTTagCompound data = new NBTTagCompound(); + inventory.saveNBT(data); + + FileOutputStream fileoutputstream = new FileOutputStream(file1); + CompressedStreamTools.writeCompressed(data, fileoutputstream); + fileoutputstream.close(); + + } + + File file2 = getBackupFileFromPlayer(player); + if (file2 != null ) + { + InventoryBaubles inventory = getPlayerBaubles(player); + NBTTagCompound data = new NBTTagCompound(); + inventory.saveNBT(data); + + FileOutputStream fileoutputstream = new FileOutputStream(file2); + CompressedStreamTools.writeCompressed(data, fileoutputstream); + fileoutputstream.close(); + + } + } + catch (Exception exception1) + { + exception1.printStackTrace(); + Baubles.logger.fatal("Error saving baubles inventory"); + } + } } diff --git a/src/main/java/baubles/common/network/EventHandlerNetwork.java b/src/main/java/baubles/common/network/EventHandlerNetwork.java index f00636f..b6799eb 100644 --- a/src/main/java/baubles/common/network/EventHandlerNetwork.java +++ b/src/main/java/baubles/common/network/EventHandlerNetwork.java @@ -20,13 +20,11 @@ public class EventHandlerNetwork { } } - HashMap lastPlayerUpdate = new HashMap(); - @SubscribeEvent public void playerUpdateEvent (PlayerEvent event) { Side side = FMLCommonHandler.instance().getEffectiveSide(); if (side == Side.SERVER) { - PlayerHandler.updatePlayerBaubles(event.player); + PlayerHandler.savePlayerBaubles(event.player); } } diff --git a/src/main/resources/changelog.txt b/src/main/resources/changelog.txt index e013f09..a6ff012 100644 --- a/src/main/resources/changelog.txt +++ b/src/main/resources/changelog.txt @@ -1,3 +1,8 @@ +1.0.0.10 +- moved baubles data from player data to custom savefiles (*.baub) +- a backup file is also saved each time in case the main one becomes corrupt. +- automatically revert to backup file if main one cannot be loaded. If both cannot be loaded revert to old player data format. This will also insure that data from previous baubles versions will be loaded the first time. + 1.0.0.9 - undoing the "baubles hotkey closes all inventories" change. It was messing with anything that allows text input