Skip to content

Commit

Permalink
Cleaned up some classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jss2a98aj committed Feb 2, 2023
1 parent ce4a210 commit cc16879
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 72 deletions.
38 changes: 15 additions & 23 deletions src/main/java/baubles/common/event/EventHandlerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,35 @@ public void playerLoad(PlayerEvent.LoadFromFile event) {
private void playerLoadDo(EntityPlayer player, File directory, Boolean gamemode) {
PlayerHandler.clearPlayerBaubles(player);

File file1, file2;
String fileName, fileNameBackup;
fileName = "baub";
fileNameBackup = "baubback";
File mainFile, backupFile;
final String fileExtension = "baub";
final String fileExtensionBackup = "baubback";

// look for normal files first
file1 = getPlayerFile(fileName, directory, player.getCommandSenderName());
file2 = getPlayerFile(fileNameBackup, directory, player.getCommandSenderName());
mainFile = getPlayerFile(fileExtension, directory, player.getCommandSenderName());
backupFile = getPlayerFile(fileExtensionBackup, directory, player.getCommandSenderName());

// look for uuid files when normal file missing
if (!file1.exists()) {
File filep = getPlayerFileUUID(fileName, directory, player.getGameProfile().getId().toString());
if (!mainFile.exists()) {
File filep = getPlayerFile(fileExtension, directory, player.getGameProfile().getId().toString());
if (filep.exists()) {
try {
Files.copy(filep, file1);
Baubles.log.info("Using and converting UUID Baubles savefile for "+player.getCommandSenderName());
Files.copy(filep, mainFile);
Baubles.log.info("Using and converting UUID Baubles savefile for " + player.getCommandSenderName());
filep.delete();
File fb = getPlayerFileUUID(fileNameBackup, directory, player.getGameProfile().getId().toString());
File fb = getPlayerFile(fileExtensionBackup, directory, player.getGameProfile().getId().toString());
if (fb.exists()) fb.delete();
} catch (IOException e) {}
}
}
PlayerHandler.loadPlayerBaubles(player, file1, file2);

PlayerHandler.loadPlayerBaubles(player, mainFile, backupFile);
EventHandlerNetwork.syncBaubles(player);
}

public File getPlayerFile(String suffix, File playerDirectory, String playername)
{
if ("dat".equals(suffix)) throw new IllegalArgumentException("The suffix 'dat' is reserved");
return new File(playerDirectory, playername+"."+suffix);
}

public File getPlayerFileUUID(String suffix, File playerDirectory, String playerUUID)
{
if ("dat".equals(suffix)) throw new IllegalArgumentException("The suffix 'dat' is reserved");
return new File(playerDirectory, playerUUID+"."+suffix);
public File getPlayerFile(String extension, File playerDirectory, String playerName) {
if("dat".equals(extension)) throw new IllegalArgumentException("The extension 'dat' is reserved");
return new File(playerDirectory, playerName + "." + extension);
}

@SubscribeEvent
Expand Down
89 changes: 40 additions & 49 deletions src/main/java/baubles/common/lib/PlayerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,102 +29,93 @@ public static InventoryBaubles getPlayerBaubles(EntityPlayer player) {
return playerBaubles.get(player.getCommandSenderName());
}

public static void setPlayerBaubles(EntityPlayer player,
InventoryBaubles inventory) {
public static void setPlayerBaubles(EntityPlayer player, InventoryBaubles inventory) {
playerBaubles.put(player.getCommandSenderName(), inventory);
}

public static void loadPlayerBaubles(EntityPlayer player, File file1, File file2) {
if (player != null && !player.worldObj.isRemote) {
public static void loadPlayerBaubles(EntityPlayer player, File mainFile, File backupFile) {
if(player != null && !player.worldObj.isRemote) {
try {
NBTTagCompound data = null;
boolean save = false;
if (file1 != null && file1.exists()) {
if(mainFile != null && mainFile.exists()) {
try {
FileInputStream fileinputstream = new FileInputStream(
file1);
data = CompressedStreamTools
.readCompressed(fileinputstream);
FileInputStream fileinputstream = new FileInputStream(mainFile);
data = CompressedStreamTools.readCompressed(fileinputstream);
fileinputstream.close();
} catch (Exception e) {
e.printStackTrace();
} catch (Exception loadMainException) {
loadMainException.printStackTrace();
}
}

if (file1 == null || !file1.exists() || data == null
|| data.hasNoTags()) {
if(mainFile == null || !mainFile.exists() || data == null || data.hasNoTags()) {
Baubles.log.warn("Data not found for "
+ player.getCommandSenderName()
+ ". Trying to load backup data.");
if (file2 != null && file2.exists()) {
+ ". Trying to load backup data."
);
if(backupFile != null && backupFile.exists()) {
try {
FileInputStream fileinputstream = new FileInputStream(
file2);
data = CompressedStreamTools
.readCompressed(fileinputstream);
FileInputStream fileinputstream = new FileInputStream(backupFile);
data = CompressedStreamTools.readCompressed(fileinputstream);
fileinputstream.close();
save = true;
} catch (Exception e) {
e.printStackTrace();
} catch(Exception loadBackupException) {
loadBackupException.printStackTrace();
}
}
}

if (data != null) {
if(data != null) {
InventoryBaubles inventory = new InventoryBaubles(player);
inventory.readNBT(data);
playerBaubles.put(player.getCommandSenderName(), inventory);
if (save)
savePlayerBaubles(player, file1, file2);
if(save) {
savePlayerBaubles(player, mainFile, backupFile);
}
}
} catch (Exception exception1) {
} catch(Exception loadException) {
Baubles.log.fatal("Error loading baubles inventory");
exception1.printStackTrace();
loadException.printStackTrace();
}
}
}

public static void savePlayerBaubles(EntityPlayer player, File file1, File file2) {
if (player != null && !player.worldObj.isRemote) {
public static void savePlayerBaubles(EntityPlayer player, File mainFile, File backupFile) {
if(player != null && !player.worldObj.isRemote) {
try {
if (file1 != null && file1.exists()) {
if (mainFile != null && mainFile.exists()) {
try {
Files.copy(file1, file2);
} catch (Exception e) {
Baubles.log
.error("Could not backup old baubles file for player "
+ player.getCommandSenderName());
Files.copy(mainFile, backupFile);
} catch (Exception saveBackupException) {
Baubles.log.error("Could not backup old baubles file for player " + player.getCommandSenderName());
}
}

try {
if (file1 != null) {
if(mainFile != null) {
InventoryBaubles inventory = getPlayerBaubles(player);
NBTTagCompound data = new NBTTagCompound();
inventory.saveNBT(data);

FileOutputStream fileoutputstream = new FileOutputStream(
file1);
CompressedStreamTools.writeCompressed(data,
fileoutputstream);
FileOutputStream fileoutputstream = new FileOutputStream(mainFile);
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()) {
} catch(Exception saveMainException) {
Baubles.log.error("Could not save baubles file for player " + player.getCommandSenderName());
saveMainException.printStackTrace();
if (mainFile.exists()) {
try {
file1.delete();
} catch (Exception e2) {
mainFile.delete();
} catch (Exception deleteException) {
}
}
}
} catch (Exception exception1) {
} catch(Exception saveException) {
Baubles.log.fatal("Error saving baubles inventory");
exception1.printStackTrace();
saveException.printStackTrace();
}
}
}

}

0 comments on commit cc16879

Please sign in to comment.