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.
This commit is contained in:
tastybento 2018-08-04 15:55:21 -07:00
parent 51a9fed184
commit b764fcb5fa
2 changed files with 62 additions and 9 deletions

View File

@ -12,6 +12,7 @@ import bentobox.addon.level.commands.IslandLevel;
import bentobox.addon.level.commands.IslandTop; import bentobox.addon.level.commands.IslandTop;
import bentobox.addon.level.config.Settings; import bentobox.addon.level.config.Settings;
import bentobox.addon.level.database.object.LevelsData; import bentobox.addon.level.database.object.LevelsData;
import bentobox.addon.level.listeners.JoinLeaveListener;
import bentobox.addon.level.listeners.NewIslandListener; import bentobox.addon.level.listeners.NewIslandListener;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -62,9 +63,18 @@ public class Level extends Addon {
return ld == null ? 0L : ld.getLevel(world); return ld == null ? 0L : ld.getLevel(world);
} }
private LevelsData getLevelsData(UUID targetPlayer) { /**
// Load player * Load a player from the cache or database
return levelsCache.getOrDefault(targetPlayer, handler.loadObject(targetPlayer.toString())); * @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 // Register new island listener
registerListener(new NewIslandListener(this)); registerListener(new NewIslandListener(this));
registerListener(new JoinLeaveListener(this));
// Done // Done
} }
@ -147,12 +158,8 @@ public class Level extends Addon {
* @param async - if true, saving will be done async * @param async - if true, saving will be done async
*/ */
public void save(boolean async){ public void save(boolean async){
Runnable save = () -> levelsCache.values().forEach(handler::saveObject); // No async for now
if(async){ levelsCache.values().forEach(handler::saveObject);
getServer().getScheduler().runTaskAsynchronously(getPlugin(), save);
} else {
save.run();
}
} }
/** /**
@ -188,4 +195,11 @@ public class Level extends Addon {
return handler; return handler;
} }
public void uncachePlayer(UUID uniqueId) {
if (levelsCache.containsKey(uniqueId)) {
handler.saveObject(levelsCache.get(uniqueId));
}
levelsCache.remove(uniqueId);
}
} }

View File

@ -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());
}
}