mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2024-11-22 18:45:24 +01:00
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:
parent
51a9fed184
commit
b764fcb5fa
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user