fix for baubles getting duped between worlds

- properly clear player baubles inventory before he enters the world and
reset file pointers
- only attempt to load or save baubles server-side
This commit is contained in:
Azanor committed 2014-05-16 08:48:53 +02:00
1 parent d852710516
commit 15e3d5b761
6 files changed
+129 -124

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.12";
public static final String VERSION = "1.0.0.13";
@SidedProxy(clientSide = "baubles.client.ClientProxy", serverSide = "baubles.common.CommonProxy")
public static CommonProxy proxy;
@@ -148,6 +148,7 @@ public class InventoryBaubles implements IInventory {
*/
@Override
public void markDirty() {
try {
PlayerHandler.savePlayerBaubles(player.get());
} catch (Exception e) { }
@@ -12,10 +12,13 @@ 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 void clearPlayerBaubles(EntityPlayer player) {
playerBaubles.remove(player.getCommandSenderName());
}
public static InventoryBaubles getPlayerBaubles(EntityPlayer player) {
if (!playerBaubles.containsKey(player.getCommandSenderName())) {
InventoryBaubles inventory = new InventoryBaubles(player);
@@ -28,140 +31,136 @@ public class PlayerHandler {
playerBaubles.put(player.getCommandSenderName(), inventory);
}
// 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");
try {
File playersDirectory = new File(player.worldObj.getSaveHandler().getWorldDirectory(), "players");
return new File(playersDirectory, player.getCommandSenderName() + ".baub");
} catch (Exception e) { e.printStackTrace(); }
return null;
}
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");
try {
File playersDirectory = new File(player.worldObj.getSaveHandler().getWorldDirectory(), "players");
return new File(playersDirectory, player.getCommandSenderName() + ".baubback");
} catch (Exception e) { e.printStackTrace(); }
return null;
}
public static void loadPlayerBaubles(EntityPlayer player) {
try
{
NBTTagCompound data = null;
boolean save = false;
File file = getFileFromPlayer(player);
if (file != null && file.exists())
{
try {
FileInputStream fileinputstream = new FileInputStream(file);
data = CompressedStreamTools.readCompressed(fileinputstream);
fileinputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (file == null || !file.exists() || data == null || data.hasNoTags())
{
Baubles.log.warn("Data not found for "+player.getCommandSenderName()+". Trying to load backup data.");
file = getBackupFileFromPlayer(player);
if (file != null && file.exists())
{
try {
FileInputStream fileinputstream = new FileInputStream(file);
data = CompressedStreamTools.readCompressed(fileinputstream);
fileinputstream.close();
save = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (file == null || !file.exists() || data == null || data.hasNoTags())
{ //legacy data
Baubles.log.warn("Data not found for "+player.getCommandSenderName()+". Trying to load legacy data.");
data = player.getEntityData();
save = true;
}
if (data!=null) {
InventoryBaubles inventory = new InventoryBaubles(player);
inventory.readNBT(data);
playerBaubles.put(player.getCommandSenderName(), inventory);
if (save) savePlayerBaubles(player);
}
}
catch (Exception exception1)
{
Baubles.log.fatal("Error loading baubles inventory");
exception1.printStackTrace();
}
if (player!=null && !player.worldObj.isRemote) {
try
{
NBTTagCompound data = null;
boolean save = false;
File file = getFileFromPlayer(player);
if (file != null && file.exists())
{
try {
FileInputStream fileinputstream = new FileInputStream(file);
data = CompressedStreamTools.readCompressed(fileinputstream);
fileinputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (file == null || !file.exists() || data == null || data.hasNoTags())
{
Baubles.log.warn("Data not found for "+player.getCommandSenderName()+". Trying to load backup data.");
file = getBackupFileFromPlayer(player);
if (file != null && file.exists())
{
try {
FileInputStream fileinputstream = new FileInputStream(file);
data = CompressedStreamTools.readCompressed(fileinputstream);
fileinputstream.close();
save = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (file == null || !file.exists() || data == null || data.hasNoTags())
{ //legacy data
Baubles.log.warn("Data not found for "+player.getCommandSenderName()+". Trying to load legacy data.");
data = player.getEntityData();
save = true;
}
if (data!=null) {
InventoryBaubles inventory = new InventoryBaubles(player);
inventory.readNBT(data);
playerBaubles.put(player.getCommandSenderName(), inventory);
if (save) savePlayerBaubles(player);
}
}
catch (Exception exception1)
{
Baubles.log.fatal("Error loading baubles inventory");
exception1.printStackTrace();
}
}
}
public static void savePlayerBaubles(EntityPlayer player) {
try
{
File file2 = getBackupFileFromPlayer(player);
if (file2 != null && file2.exists())
{
try {
file2.delete();
}
catch (Exception e) {
Baubles.log.error("Could not delete backup file for player "+player.getCommandSenderName());
}
}
File file1 = getFileFromPlayer(player);
file2 = getBackupFileFromPlayer(player);
if (file1!=null && file1.exists()) {
try {
file1.renameTo(file2);
}
catch (Exception e) {
Baubles.log.error("Could not backup old baubles file for player "+player.getCommandSenderName());
}
}
file1 = getFileFromPlayer(player);
try {
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();
}
} catch (Exception e) {
Baubles.log.error("Could not save baubles file for player "+player.getCommandSenderName());
if (file1.exists()) {
try {
file1.delete();
if (player!=null && !player.worldObj.isRemote) {
try
{
File file2 = getBackupFileFromPlayer(player);
if (file2 != null && file2.exists())
{
try {
file2.delete();
}
catch (Exception e2) {}
catch (Exception e) {
Baubles.log.error("Could not delete backup file for player "+player.getCommandSenderName());
}
}
File file1 = getFileFromPlayer(player);
file2 = getBackupFileFromPlayer(player);
if (file1!=null && file1.exists()) {
try {
file1.renameTo(file2);
}
catch (Exception e) {
Baubles.log.error("Could not backup old baubles file for player "+player.getCommandSenderName());
}
}
file1 = getFileFromPlayer(player);
try {
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();
}
} catch (Exception e) {
Baubles.log.error("Could not save baubles file for player "+player.getCommandSenderName());
e.printStackTrace();
if (file1.exists()) {
try {
file1.delete();
}
catch (Exception e2) {}
}
}
}
}
catch (Exception exception1)
{
Baubles.log.fatal("Error saving baubles inventory");
exception1.printStackTrace();
}
}
catch (Exception exception1)
{
Baubles.log.fatal("Error saving baubles inventory");
exception1.printStackTrace();
}
}
}
}
@@ -12,6 +12,7 @@ public class EventHandlerNetwork {
public void playerLoggedInEvent (PlayerEvent.PlayerLoggedInEvent event) {
Side side = FMLCommonHandler.instance().getEffectiveSide();
if (side == Side.SERVER) {
PlayerHandler.clearPlayerBaubles(event.player);
PlayerHandler.loadPlayerBaubles(event.player);
for (int a=0;a<4;a++)
PlayerHandler.getPlayerBaubles(event.player).syncSlotToClients(a);