diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java index 7d0675d..72a7726 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 GeorgH93 + * Copyright (C) 2016-2017 GeorgH93 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -179,7 +179,12 @@ public int getBackpackMaxSize() public boolean getAutoUpdate() { - return config.getBoolean("auto-update", true); + return config.getBoolean("Misc.AutoUpdate", true); + } + + public boolean isBungeeCordModeEnabled() + { + return config.getBoolean("Misc.UseBungeeCord", false); } public long getCommandCooldown() diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java index 1e3b9ca..66bf9f2 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 GeorgH93 + * Copyright (C) 2016-2017 GeorgH93 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,6 +19,7 @@ import at.pcgamingfreaks.Minepacks.Bukkit.API.Callback; import at.pcgamingfreaks.Minepacks.Bukkit.Backpack; +import at.pcgamingfreaks.Minepacks.Bukkit.Database.UnCacheStrategies.OnDisconnect; import at.pcgamingfreaks.Minepacks.Bukkit.Database.UnCacheStrategies.UnCacheStrategie; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; @@ -41,7 +42,7 @@ public abstract class Database implements Listener protected final Minepacks plugin; protected final InventorySerializer itsSerializer = new InventorySerializer(); - protected final boolean useUUIDs; + protected final boolean useUUIDs, bungeeCordMode; protected boolean useUUIDSeparators; protected long maxAge; private final Map backpacks = new ConcurrentHashMap<>(); @@ -52,8 +53,9 @@ public Database(Minepacks mp) plugin = mp; useUUIDSeparators = plugin.config.getUseUUIDSeparators(); useUUIDs = plugin.config.getUseUUIDs(); + bungeeCordMode = plugin.config.isBungeeCordModeEnabled(); maxAge = plugin.config.getAutoCleanupMaxInactiveDays(); - unCacheStrategie = UnCacheStrategie.getUnCacheStrategie(this); + unCacheStrategie = bungeeCordMode ? new OnDisconnect(this) : UnCacheStrategie.getUnCacheStrategie(this); } public void init() @@ -112,30 +114,17 @@ protected String getPlayerFormattedUUID(OfflinePlayer player) return backpacks.values(); } + /** + * Gets a backpack for a player. This only includes backpacks that are cached! Do not use it unless you are sure that you only want to use cached data! + * + * @param player The player who's backpack should be retrieved. + * @return The backpack for the player. null if the backpack is not in the cache. + */ public @Nullable Backpack getBackpack(@Nullable OfflinePlayer player) { return (player == null) ? null : backpacks.get(player); } - public Backpack getBackpack(OfflinePlayer player, boolean loadedOnly) - { - if(player == null) - { - return null; - } - Backpack lbp = backpacks.get(player); - if(lbp == null && !loadedOnly) - { - lbp = loadBackpack(player); - if(lbp == null) - { - lbp = new Backpack(player); - } - backpacks.put(player, lbp); - } - return lbp; - } - public void getBackpack(final OfflinePlayer player, final Callback callback) { if(player == null) @@ -205,7 +194,7 @@ public void onPlayerLoginEvent(PlayerJoinEvent event) public void updatePlayerAndLoadBackpack(Player player) { updatePlayer(player); - asyncLoadBackpack(player); + if(!bungeeCordMode) asyncLoadBackpack(player); } public abstract void updatePlayer(Player player); @@ -214,18 +203,5 @@ public void updatePlayerAndLoadBackpack(Player player) public abstract void syncCooldown(Player player, long time); - protected abstract Backpack loadBackpack(OfflinePlayer player); - - protected void loadBackpack(final OfflinePlayer player, final Callback callback) - { - Backpack loadedBackpack = loadBackpack(player); - if(loadedBackpack == null) - { - callback.onFail(); - } - else - { - callback.onResult(loadedBackpack); - } - } + protected abstract void loadBackpack(final OfflinePlayer player, final Callback callback); } \ No newline at end of file diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java index 37ef6ab..8695d99 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 GeorgH93 + * Copyright (C) 2016-2017 GeorgH93 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +24,7 @@ import javax.swing.filechooser.FileFilter; +import at.pcgamingfreaks.Minepacks.Bukkit.API.Callback; import at.pcgamingfreaks.Minepacks.Bukkit.Backpack; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.UUIDConverter; @@ -149,7 +150,7 @@ public void syncCooldown(Player player, long time) } @Override - public Backpack loadBackpack(OfflinePlayer player) + protected void loadBackpack(final OfflinePlayer player, final Callback callback) { File save = new File(saveFolder, getFileName(player)); try @@ -165,7 +166,7 @@ public Backpack loadBackpack(OfflinePlayer player) { plugin.getLogger().warning("Problem reading file, only read " + c + " of " + v + " bytes."); } - return new Backpack(player, itsSerializer.deserialize(out, v), -1); + callback.onResult(new Backpack(player, itsSerializer.deserialize(out, v), -1)); } } } @@ -173,7 +174,7 @@ public Backpack loadBackpack(OfflinePlayer player) { e.printStackTrace(); } - return null; + callback.onFail(); } private static class BackpackFileFilter extends FileFilter implements FilenameFilter diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java index 0d3c986..de49d08 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java @@ -379,27 +379,6 @@ protected void loadBackpack(final OfflinePlayer player, final Callback } }); } - - @Override - public Backpack loadBackpack(OfflinePlayer player) // The sync function shouldn't be called at all - { - try(Connection connection = getConnection(); PreparedStatement ps = connection.prepareStatement(queryGetBP)) - { - ps.setString(1, getPlayerNameOrUUID(player)); - try(ResultSet rs = ps.executeQuery()) - { - if(rs.next()) - { - return new Backpack(player, itsSerializer.deserialize(rs.getBytes(2), rs.getInt(3)), rs.getInt(1)); - } - } - } - catch(Exception e) - { - e.printStackTrace(); - } - return null; - } @Override public void syncCooldown(Player player, long cooldownTime) diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java index 754cde4..e178d61 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java @@ -89,5 +89,7 @@ public void onPlayerLeaveEvent(PlayerQuitEvent event) { plugin.cooldowns.remove(event.getPlayer().getUniqueId()); } + Backpack backpack = plugin.getDb().getBackpack(event.getPlayer()); + if(backpack != null) backpack.save(); } } \ No newline at end of file