Skip to content

Commit

Permalink
Added is_updating flag to ZPlayer attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
zerrium committed Dec 9, 2020
1 parent 821e51f commit d70744a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/zerrium/SpigotListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/zerrium/ZPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Long> x; //convert those stupid many attributes into a hashmap
LinkedHashMap<Material, Long> craft;
LinkedHashMap<Material, Long> place;
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/zerrium/ZUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d70744a

Please sign in to comment.