From b764fcb5fa07487ba520c7ec3d6d89408ebbd1b7 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 4 Aug 2018 15:55:21 -0700 Subject: [PATCH] Fixed bug where levels were not being saved correctly. Added a join/leave listener to pre-load players to the cache and remove and save them when they logout. --- src/main/java/bentobox/addon/level/Level.java | 32 ++++++++++----- .../level/listeners/JoinLeaveListener.java | 39 +++++++++++++++++++ 2 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 src/main/java/bentobox/addon/level/listeners/JoinLeaveListener.java diff --git a/src/main/java/bentobox/addon/level/Level.java b/src/main/java/bentobox/addon/level/Level.java index bc7ce12..0ef4a43 100644 --- a/src/main/java/bentobox/addon/level/Level.java +++ b/src/main/java/bentobox/addon/level/Level.java @@ -12,6 +12,7 @@ import bentobox.addon.level.commands.IslandLevel; import bentobox.addon.level.commands.IslandTop; import bentobox.addon.level.config.Settings; import bentobox.addon.level.database.object.LevelsData; +import bentobox.addon.level.listeners.JoinLeaveListener; import bentobox.addon.level.listeners.NewIslandListener; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.commands.CompositeCommand; @@ -62,9 +63,18 @@ public class Level extends Addon { return ld == null ? 0L : ld.getLevel(world); } - private LevelsData getLevelsData(UUID targetPlayer) { - // Load player - return levelsCache.getOrDefault(targetPlayer, handler.loadObject(targetPlayer.toString())); + /** + * Load a player from the cache or database + * @param targetPlayer - UUID of target player + * @return LevelsData object or null if not found + */ + public LevelsData getLevelsData(UUID targetPlayer) { + // Get from database if not in cache + if (!levelsCache.containsKey(targetPlayer) && handler.objectExists(targetPlayer.toString())) { + levelsCache.put(targetPlayer, handler.loadObject(targetPlayer.toString())); + } + // Return cached value or null + return levelsCache.get(targetPlayer); } /** @@ -138,6 +148,7 @@ public class Level extends Addon { // Register new island listener registerListener(new NewIslandListener(this)); + registerListener(new JoinLeaveListener(this)); // Done } @@ -147,12 +158,8 @@ public class Level extends Addon { * @param async - if true, saving will be done async */ public void save(boolean async){ - Runnable save = () -> levelsCache.values().forEach(handler::saveObject); - if(async){ - getServer().getScheduler().runTaskAsynchronously(getPlugin(), save); - } else { - save.run(); - } + // No async for now + levelsCache.values().forEach(handler::saveObject); } /** @@ -188,4 +195,11 @@ public class Level extends Addon { return handler; } + public void uncachePlayer(UUID uniqueId) { + if (levelsCache.containsKey(uniqueId)) { + handler.saveObject(levelsCache.get(uniqueId)); + } + levelsCache.remove(uniqueId); + } + } diff --git a/src/main/java/bentobox/addon/level/listeners/JoinLeaveListener.java b/src/main/java/bentobox/addon/level/listeners/JoinLeaveListener.java new file mode 100644 index 0000000..542231a --- /dev/null +++ b/src/main/java/bentobox/addon/level/listeners/JoinLeaveListener.java @@ -0,0 +1,39 @@ +package bentobox.addon.level.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import bentobox.addon.level.Level; + +/** + * Listens for when players join and leave + * @author tastybento + * + */ +public class JoinLeaveListener implements Listener { + + private Level addon; + + /** + * @param addon + */ + public JoinLeaveListener(Level addon) { + this.addon = addon; + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onPlayerJoin(PlayerJoinEvent e) { + // Load player into cache + addon.getLevelsData(e.getPlayer().getUniqueId()); + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onPlayerQuit(PlayerQuitEvent e) { + addon.uncachePlayer(e.getPlayer().getUniqueId()); + } + + +}