Feat: Change way of handling voids

Only store to database when fetching scoreboard or on disconnect.
else just use the persistentStorage from players
This commit is contained in:
Debucquoy Anthony 2024-02-04 21:38:28 +01:00
parent a7a88bb619
commit c9dd268c14
Signed by: tonitch
GPG Key ID: A78D6421F083D42E

View File

@ -5,17 +5,18 @@ import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scoreboard.Criteria; import org.bukkit.scoreboard.Criteria;
import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.RenderType;
import org.bukkit.scoreboard.Score; import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -25,26 +26,30 @@ import net.kyori.adventure.title.Title;
public final class Voids implements Listener{ public final class Voids implements Listener{
private static HashMap<Player, BossBar> bossbars = new HashMap<>(); private static HashMap<Player, BossBar> bossbars = new HashMap<>();
private static final NamespacedKey voids = new NamespacedKey(TheVoidRoad.instance, "voids");
public static int get(Player ply){ public static int get(Player ply){
if(ply.getPersistentDataContainer().has(voids)){
return ply.getPersistentDataContainer().get(voids, PersistentDataType.INTEGER);
}
return TheVoidRoad.db.getVoids(ply.getUniqueId()); return TheVoidRoad.db.getVoids(ply.getUniqueId());
} }
public static void set(Player ply, int amount){ public static void set(Player ply, int amount){
TheVoidRoad.db.setVoids(ply.getUniqueId(), amount); ply.getPersistentDataContainer().set(voids, PersistentDataType.INTEGER, amount);
UpdateVoids(ply); UpdateVoids(ply);
} }
public static void add(Player ply, int amount){ public static void add(Player ply, int amount){
int curr = DatabaseManager.getInstance().getVoids(ply.getUniqueId()); int curr = get(ply);
DatabaseManager.getInstance().setVoids(ply.getUniqueId(), curr + amount); set(ply, curr + amount);
UpdateVoids(ply); UpdateVoids(ply);
} }
public static boolean subtract(Player ply, int amount, boolean force){ public static boolean subtract(Player ply, int amount, boolean force){
int curr = TheVoidRoad.db.getVoids(ply.getUniqueId()); int curr = get(ply);
if(curr - amount < 0 && !force) return false; if(curr - amount < 0 && !force) return false;
TheVoidRoad.db.setVoids(ply.getUniqueId(), curr - amount); set(ply, curr - amount);
UpdateVoids(ply); UpdateVoids(ply);
return true; return true;
} }
@ -59,7 +64,15 @@ public final class Voids implements Listener{
return true; return true;
} }
public static void save(Player ply){
int curr = get(ply);
TheVoidRoad.db.setVoids(ply.getUniqueId(), curr);
}
public static HashMap<Player, Integer> scoreboard(){ public static HashMap<Player, Integer> scoreboard(){
Bukkit.getServer().forEachAudience(a -> {
if(a instanceof Player ply) save(ply);
});
HashMap<Player, Integer> ret = new HashMap<>(); HashMap<Player, Integer> ret = new HashMap<>();
try { try {
HashMap<UUID, Integer> scores_uuid = TheVoidRoad.db.getVoidsScoreboard(); HashMap<UUID, Integer> scores_uuid = TheVoidRoad.db.getVoidsScoreboard();
@ -95,4 +108,9 @@ public final class Voids implements Listener{
public void UpdateVoids(PlayerJoinEvent e){ public void UpdateVoids(PlayerJoinEvent e){
UpdateVoids(e.getPlayer()); UpdateVoids(e.getPlayer());
} }
@EventHandler
public void SaveToDbBeforeQuit(PlayerQuitEvent e){
save(e.getPlayer());
}
} }