diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java index 2c1fe58..5a6da43 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java @@ -121,14 +121,14 @@ public void close() catch(IllegalStateException ignored) {} catch(Exception e) { - e.printStackTrace(); + plugin.getLogger().log(Level.SEVERE, "Failed to initialize database.", e); } return null; } public void backup(@NotNull Backpack backpack) { - writeBackup(backpack.getOwner().getName(), getPlayerFormattedUUID(backpack.getOwner()), itsSerializer.getUsedSerializer(), itsSerializer.serialize(backpack.getInventory())); + writeBackup(backpack.getOwner().getName(), getPlayerFormattedUUID(backpack.getOwnerId()), itsSerializer.getUsedSerializer(), itsSerializer.serialize(backpack.getInventory())); } protected void writeBackup(@Nullable String userName, @NotNull String userIdentifier, final int usedSerializer, final byte[] data) @@ -172,7 +172,12 @@ public ArrayList getBackups() protected String getPlayerFormattedUUID(OfflinePlayer player) { - return (useUUIDSeparators) ? player.getUniqueId().toString() : player.getUniqueId().toString().replace("-", ""); + return getPlayerFormattedUUID(player.getUniqueId()); + } + + protected String getPlayerFormattedUUID(UUID uuid) + { + return (useUUIDSeparators) ? uuid.toString() : uuid.toString().replace("-", ""); } public @NotNull Collection getLoadedBackpacks() @@ -238,7 +243,6 @@ public void getBackpack(final OfflinePlayer player, final Callback 0 && System.currentTimeMillis() - file.lastModified() > maxAge) // Check if the file is older then x days + if(maxAge > 0 && System.currentTimeMillis() - file.lastModified() > maxAge) // Check if the file is older than x days { if(!file.delete()) { plugin.getLogger().warning("Failed to delete file (" + file.getAbsolutePath() + ")."); } - continue; // We don't have to check if the file name is correct cause we have the deleted the file + continue; // We don't have to check if the file name is correct because we have the deleted the file } int len = file.getName().length() - EXT.length(); if(len <= 16) // It's a player name @@ -110,16 +112,16 @@ private void checkFiles() } } - private String getFileName(OfflinePlayer player) + private String getFileName(UUID uuid) { - return getPlayerFormattedUUID(player) + EXT; + return getPlayerFormattedUUID(uuid) + EXT; } // DB Functions @Override public void saveBackpack(Backpack backpack) { - File save = new File(saveFolder, getFileName(backpack.getOwner())); + File save = new File(saveFolder, getFileName(backpack.getOwnerId())); try(FileOutputStream fos = new FileOutputStream(save)) { fos.write(itsSerializer.getUsedSerializer()); @@ -128,7 +130,7 @@ public void saveBackpack(Backpack backpack) } catch(Exception e) { - e.printStackTrace(); + plugin.getLogger().log(Level.SEVERE, "Failed to save backpack.", e); } } @@ -161,7 +163,7 @@ protected void loadBackpack(final OfflinePlayer player, final Callback } catch(Exception e) { - e.printStackTrace(); + logger.log(Level.WARNING, "Failed to read backpack.", e); } } return null; diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java index 747eaac..3b7863c 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java @@ -203,7 +203,7 @@ public void saveBackpack(final Backpack backpack) { final byte[] data = itsSerializer.serialize(backpack.getInventory()); final int id = backpack.getOwnerDatabaseId(), usedSerializer = itsSerializer.getUsedSerializer(); - final String nameOrUUID = getPlayerFormattedUUID(backpack.getOwner()), name = backpack.getOwner().getName(); + final String nameOrUUID = getPlayerFormattedUUID(backpack.getOwnerId()), name = backpack.getOwner().getName(); Runnable runnable = () -> { try(Connection connection = getConnection()) diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/Interval.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/Interval.java index 17107f6..c7d9772 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/Interval.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/Interval.java @@ -37,7 +37,7 @@ public void run() { for(Backpack backpack : cache.getLoadedBackpacks()) { - if(!backpack.getOwner().isOnline() && !backpack.isOpen()) + if(backpack.getOwnerPlayer() == null && !backpack.isOpen()) { this.cache.unloadBackpack(backpack); } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/IntervalChecked.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/IntervalChecked.java index 4334aaa..06903b9 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/IntervalChecked.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/UnCacheStrategies/IntervalChecked.java @@ -22,6 +22,7 @@ import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; public class IntervalChecked extends UnCacheStrategy implements Runnable { @@ -42,7 +43,8 @@ public void run() long currentTime = System.currentTimeMillis() - delay; for(Backpack backpack : cache.getLoadedBackpacks()) { - if(!backpack.getOwner().isOnline() && backpack.getOwner().getLastPlayed() < currentTime && !backpack.isOpen()) + OfflinePlayer owner = backpack.getOwner(); + if(!owner.isOnline() && owner.getLastPlayed() < currentTime && !backpack.isOpen()) { this.cache.unloadBackpack(backpack); } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java index 796e8ec..24b5fc3 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/BackpackEventListener.java @@ -55,14 +55,13 @@ public void onClose(InventoryCloseEvent event) backpack.save(); } backpack.close(closer); - if(event.getPlayer().getUniqueId().equals(backpack.getOwner().getUniqueId())) + if(event.getPlayer().getUniqueId().equals(backpack.getOwnerId())) { messageOwnBackpackClose.send(closer); } else { - OfflinePlayer owner = backpack.getOwner(); - messageOtherBackpackClose.send(closer, owner); + messageOtherBackpackClose.send(closer, backpack.getOwner()); } if(closeSound != null) {