Skip to content

Commit

Permalink
fix: properly shutting down its async tasks (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock authored Sep 5, 2023
1 parent db504dd commit b0b5636
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
* @author RoinujNosde
Expand All @@ -69,25 +83,25 @@ public void setup() {
try {
Statement statement = getConnection().createStatement();
statement.execute("CREATE TABLE IF NOT EXISTS tb_warriors "
+ "(displayname varchar(30) NOT NULL,"
+ " uuid varchar(255) NOT NULL,"
+ " kills int NOT NULL,"
+ " deaths int NOT NULL,"
+ " victories int NOT NULL,"
+ " game varchar(20) NOT NULL);");
+ "(displayname varchar(30) NOT NULL,"
+ " uuid varchar(255) NOT NULL,"
+ " kills int NOT NULL,"
+ " deaths int NOT NULL,"
+ " victories int NOT NULL,"
+ " game varchar(20) NOT NULL);");
statement.execute("CREATE TABLE IF NOT EXISTS tb_groups"
+ "(identification varchar(255) NOT NULL,"
+ " kills int NOT NULL,"
+ " deaths int NOT NULL,"
+ " victories int NOT NULL,"
+ " defeats int NOT NULL,"
+ " game varchar(20) NOT NULL);");
+ "(identification varchar(255) NOT NULL,"
+ " kills int NOT NULL,"
+ " deaths int NOT NULL,"
+ " victories int NOT NULL,"
+ " defeats int NOT NULL,"
+ " game varchar(20) NOT NULL);");
statement.execute("CREATE TABLE IF NOT EXISTS tb_winners"
+ "(date varchar(10) NOT NULL,"
+ " killer varchar(255),"
+ " player_winners text,"
+ " winner_group varchar(255),"
+ " game varchar(20) NOT NULL);");
+ "(date varchar(10) NOT NULL,"
+ " killer varchar(255),"
+ " player_winners text,"
+ " winner_group varchar(255),"
+ " game varchar(20) NOT NULL);");
} catch (SQLException ex) {
plugin.debug("Error while creating the tables: " + ex.getMessage(), false);
}
Expand All @@ -104,7 +118,7 @@ private void start() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database +
"?useSSL=false", username, password);
"?useSSL=false", username, password);
} catch (ClassNotFoundException ex) {
plugin.debug("MySQL driver not found!", false);
}
Expand Down Expand Up @@ -201,7 +215,7 @@ private void update(@NotNull String id, @NotNull GroupData data) {
HashSet<GameConfiguration> updated = new HashSet<>();

String update = "UPDATE tb_groups SET kills=?, deaths=?, victories=?,"
+ " defeats=? WHERE identification=? AND game=?;";
+ " defeats=? WHERE identification=? AND game=?;";
try (PreparedStatement statement = getConnection().prepareStatement(update)) {
for (GameConfiguration game : getGames()) {
String gameName = game.getName();
Expand Down Expand Up @@ -488,14 +502,31 @@ public void loadDataToMemory() {

public void saveAll() {
// Copying collections for async use, avoiding ConcurrentModificationException
final Map<String, GroupData> groups = new HashMap<>(getGroups());
final Set<Warrior> warriors = new HashSet<>(getWarriors());
final List<Winners> winners = new ArrayList<>(getWinners());
final Map<String, GroupData> groupMap = new HashMap<>(getGroups());
final Set<Warrior> warriorsSet = new HashSet<>(getWarriors());
final List<Winners> winnersList = new ArrayList<>(getWinners());

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
groups.forEach(this::update);
warriors.forEach(this::update);
winners.forEach(this::update);
for (Map.Entry<String, GroupData> data : groupMap.entrySet()) {
if (data.getValue().isModified()) {
update(data.getKey(), data.getValue());
data.getValue().setModified(false);
}
}

for (Warrior warrior : warriorsSet) {
if (warrior.isModified()) {
update(warrior);
warrior.setModified(false);
}
}

for (Winners winner : winnersList) {
if (winner.isModified()) {
update(winner);
winner.setModified(false);
}
}
});
}

Expand Down
16 changes: 15 additions & 1 deletion src/main/java/me/roinujnosde/titansbattle/types/GroupData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public class GroupData {
private final Map<String, Integer> defeats;
private final Map<String, Integer> kills;
private final Map<String, Integer> deaths;
private boolean isModified;

public GroupData() {
this(null, null, null, null);
isModified = true;
}

public GroupData(
@Nullable Map<String, Integer> victories,
@Nullable Map<String, Integer> defeats,
Expand Down Expand Up @@ -51,10 +53,12 @@ public int getDefeats(String game) {

public void increaseDefeats(@NotNull String game) {
defeats.compute(game, (g, i) -> i == null ? 1 : i + 1);
isModified = true;
}

public void increaseVictories(@NotNull String game) {
victories.compute(game, (g, i) -> i == null ? 1 : i + 1);
isModified = true;
}

public void setVictories(String game, int newVictories) {
Expand All @@ -63,13 +67,23 @@ public void setVictories(String game, int newVictories) {

public void setKills(String game, int newKills) {
kills.put(game, newKills);
isModified = true;
}

public void setDeaths(String game, int newDeaths) {
deaths.put(game, newDeaths);
isModified = true;
}

public void setDefeats(String game, int newDefeats) {
defeats.put(game, newDefeats);
}

public boolean isModified() {
return isModified;
}

public void setModified(boolean isModified) {
this.isModified = isModified;
}
}
25 changes: 19 additions & 6 deletions src/main/java/me/roinujnosde/titansbattle/types/Warrior.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import static me.roinujnosde.titansbattle.utils.Helper.caseInsensitiveMap;

/**
*
* @author RoinujNosde
*/
public class Warrior {
Expand All @@ -48,9 +47,11 @@ public class Warrior {
private final Map<String, Integer> kills;
private final Map<String, Integer> deaths;
private final Map<String, Integer> victories;
private boolean isModified;

public Warrior(@NotNull OfflinePlayer offlinePlayer, @NotNull Supplier<GroupManager> groupManager) {
this(offlinePlayer, groupManager, null, null, null);
isModified = true;
}

public Warrior(@NotNull OfflinePlayer offlinePlayer,
Expand Down Expand Up @@ -100,9 +101,9 @@ public void sendMessage(@Nullable String message) {
@Override
public String toString() {
return "Warrior{" +
"name=" + offlinePlayer.getName() +
", uuid=" + offlinePlayer.getUniqueId() +
'}';
"name=" + offlinePlayer.getName() +
", uuid=" + offlinePlayer.getUniqueId() +
'}';
}

@Nullable
Expand Down Expand Up @@ -181,24 +182,36 @@ public int getTotalVictories() {
public int getVictories(@NotNull String game) {
return victories.getOrDefault(game, 0);
}

public void setKills(@NotNull String game, int newKills) {
kills.put(game, newKills);
isModified = true;
}

public void setDeaths(@NotNull String game, int newDeaths) {
deaths.put(game, newDeaths);
isModified = true;
}

public void setVictories(@NotNull String game, int newVictories) {
victories.put(game, newVictories);
isModified = true;
}

public void increaseVictories(@NotNull String game) {
setVictories(game, getVictories(game) + 1);
isModified = true;
}

private <T> int getSum(Map<T, Integer> map) {
return map.values().stream().mapToInt(i -> i).sum();
}

public boolean isModified() {
return isModified;
}

public void setModified(boolean isModified) {
this.isModified = isModified;
}
}
14 changes: 13 additions & 1 deletion src/main/java/me/roinujnosde/titansbattle/types/Winners.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import static me.roinujnosde.titansbattle.utils.Helper.caseInsensitiveMap;

/**
*
* @author RoinujNosde
*/
public class Winners implements Comparable<Winners> {
Expand All @@ -43,9 +42,11 @@ public class Winners implements Comparable<Winners> {
private final Map<String, UUID> killer;
private final Map<String, List<UUID>> playerWinners;
private final Map<String, String> winnerGroup;
private boolean isModified;

public Winners(@NotNull Date date) {
this(date, null, null, null);
isModified = true;
}

public Winners(@NotNull Date date,
Expand Down Expand Up @@ -76,20 +77,31 @@ public String getWinnerGroup(String game) {

public void setKiller(String game, UUID uuid) {
killer.put(game, uuid);
isModified = true;
}

public void setWinnerGroup(String game, String group) {
winnerGroup.put(game, group);
isModified = true;
}

public void setWinners(String game, List<UUID> winners) {
playerWinners.put(game, winners);
isModified = true;
}

public boolean isEmpty(String game) {
return killer.get(game) == null && playerWinners.get(game) == null && winnerGroup.get(game) == null;
}

public boolean isModified() {
return isModified;
}

public void setModified(boolean isModified) {
this.isModified = isModified;
}

@Override
public int compareTo(@NotNull Winners o) {
return this.date.compareTo(o.date);
Expand Down

0 comments on commit b0b5636

Please sign in to comment.