From d70744ae20e01d9a86c917c4e55d23c40c1dc3b1 Mon Sep 17 00:00:00 2001 From: zerrium Date: Wed, 9 Dec 2020 14:59:16 +0700 Subject: [PATCH] Added is_updating flag to ZPlayer attribute --- src/main/java/zerrium/SpigotListener.java | 1 + src/main/java/zerrium/ZPlayer.java | 5 +++++ src/main/java/zerrium/ZUpdater.java | 2 ++ 3 files changed, 8 insertions(+) diff --git a/src/main/java/zerrium/SpigotListener.java b/src/main/java/zerrium/SpigotListener.java index 9ec91ce..af775b0 100644 --- a/src/main/java/zerrium/SpigotListener.java +++ b/src/main/java/zerrium/SpigotListener.java @@ -64,6 +64,7 @@ public void onPlayerQuit(PlayerQuitEvent event){ if(Zstats.debug) System.out.println(Zstats.online_player); System.out.println(ChatColor.YELLOW + "[Zstats] " + ChatColor.RESET + name + " left the game. Updating stats..."); ZPlayer zp = Zstats.zplayer.get(Zstats.zplayer.indexOf(new ZPlayer(uuid))); + if(zp.is_updating) return; zp.last_played = System.currentTimeMillis()/1000; BukkitRunnable r = new BukkitRunnable() { @Override diff --git a/src/main/java/zerrium/ZPlayer.java b/src/main/java/zerrium/ZPlayer.java index 19d1ccd..82e5e72 100644 --- a/src/main/java/zerrium/ZPlayer.java +++ b/src/main/java/zerrium/ZPlayer.java @@ -14,6 +14,7 @@ public class ZPlayer { String name; UUID uuid; long afk_time, last_played; + boolean is_updating; //flag to prevent double update to the same Object simultaneously HashMap x; //convert those stupid many attributes into a hashmap LinkedHashMap craft; LinkedHashMap place; @@ -56,6 +57,7 @@ public ZPlayer(UUID uuid, String name) throws SQLException { pss.close(); rs.close(); connection.close(); + this.is_updating = false; } public ZPlayer(UUID uuid){ @@ -130,6 +132,7 @@ private void clearStat(){ } public void updateStat(Connection connection) throws SQLException { //Should be called Asynchronously + this.is_updating = true; //Clear existing Stats this.clearStat(); @@ -222,10 +225,12 @@ public void updateStat(Connection connection) throws SQLException { //Should be } System.out.println(ChatColor.YELLOW + "[Zstats]" + ChatColor.RESET + " Update stats of " + uuid.toString() + " associates with " + name + " done."); + this.is_updating = false; } public void deleteStat(Connection connection) throws SQLException { //Should be called Asynchronously //delete from SQL + if(this.is_updating) return; System.out.println(ChatColor.YELLOW + "[Zstats]" + ChatColor.RESET + " Deleting stats of " + uuid.toString() + " associates with " + name + " from database..."); PreparedStatement pss = connection.prepareStatement("delete from stats where uuid=?"); pss.setString(1, uuid.toString()); diff --git a/src/main/java/zerrium/ZUpdater.java b/src/main/java/zerrium/ZUpdater.java index 70cc8d0..cae1876 100644 --- a/src/main/java/zerrium/ZUpdater.java +++ b/src/main/java/zerrium/ZUpdater.java @@ -33,6 +33,7 @@ public void run() { try { connection = SqlCon.openConnection(); for(ZPlayer p : Zstats.zplayer){ + if(p.is_updating) continue; p.updateStat(connection); } } catch (SQLException throwables) { @@ -64,6 +65,7 @@ public void run() { public void run() { for(ZPlayer z : Zstats.zplayer){ if(args[1].equalsIgnoreCase(z.name)){ + if(z.is_updating) return; Connection connection = null; try { connection = SqlCon.openConnection();