fix for possible player data corruption

switched to using custom player data files instead of saving it with
vanilla playerdata
This commit is contained in:
Azanor committed 2014-04-21 20:19:42 +02:00
1 parent 27b140a578
commit 0cd68aedca
7 files changed
+123 -19

No files matched your search

+1 -1
View File
@@ -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;
@@ -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);
}
}
@@ -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);
@@ -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<String,InventoryBaubles> playerBaubles = new HashMap<String,InventoryBaubles>();
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");
}
}
}
@@ -20,13 +20,11 @@ public class EventHandlerNetwork {
}
}
HashMap<Integer,Long> lastPlayerUpdate = new HashMap<Integer,Long>();
@SubscribeEvent
public void playerUpdateEvent (PlayerEvent event) {
Side side = FMLCommonHandler.instance().getEffectiveSide();
if (side == Side.SERVER) {
PlayerHandler.updatePlayerBaubles(event.player);
PlayerHandler.savePlayerBaubles(event.player);
}
}