Skip to content

Commit

Permalink
fix: mysql user saving problem and performance optimize (#45)
Browse files Browse the repository at this point in the history
Merge pull request from ShallowAi/development (#45)
  • Loading branch information
Tigerpanzer02 authored Apr 23, 2024
2 parents 368f48b + 7e790dc commit fde43f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class User {
private Kit kit;
private final Map<StatisticType, Integer> stats = new HashMap<>();
private final Map<String, Double> cooldowns = new HashMap<>();
private boolean initialized;

@Deprecated
public User(Player player) {
Expand Down Expand Up @@ -170,4 +171,11 @@ public double getCooldown(String key) {
return cooldown <= cooldownCounter ? 0 : cooldown - cooldownCounter;
}

public boolean isInitialized() {
return initialized;
}

public void setInitialized(boolean initialized) {
this.initialized = initialized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import plugily.projects.minigamesbox.classic.user.data.UserDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
* @author Tigerpanzer_02
Expand All @@ -39,7 +41,7 @@
public class UserManager {

private final UserDatabase database;
private final List<User> users = new ArrayList<>();
private final HashMap<UUID, User> users = new HashMap<>();
private final PluginMain plugin;

public UserManager(PluginMain plugin) {
Expand All @@ -59,15 +61,13 @@ private void loadStatsForPlayersOnline() {
public User getUser(Player player) {
java.util.UUID playerId = player.getUniqueId();

for(User user : users) {
if(user.getUniqueId().equals(playerId)) {
return user;
}
if (users.containsKey(playerId)){
return users.get(playerId);
}

plugin.getDebugger().debug("Registering new user {0} ({1})", playerId, player.getName());
User user = new User(playerId);
users.add(user);
users.put(playerId, user);
return user;
}

Expand Down Expand Up @@ -129,7 +129,7 @@ public void loadStatistics(User user) {
}

public void removeUser(User user) {
users.remove(user);
users.remove(user.getUniqueId());
}

public UserDatabase getDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ public void saveStatistic(User user, StatisticType statisticType) {

@Override
public void saveAllStatistic(User user) {
try {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> database.executeUpdate(getUpdateQuery(user)));
} catch(IllegalPluginAccessException ignored) {
database.executeUpdate(getUpdateQuery(user));
if (!user.isInitialized()){
plugin.getDebugger().debug("User been saving while is not is not initialized.");
} else {
try {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> database.executeUpdate(getUpdateQuery(user)));
} catch (IllegalPluginAccessException ignored) {
database.executeUpdate(getUpdateQuery(user));
}
}
}

Expand All @@ -145,6 +149,7 @@ public void loadStatistics(User user) {
} else {
createUserStats(user, uuid, statement, playerName);
}
user.setInitialized(true);
} catch(SQLException exception) {
throwException(exception);
}
Expand Down

0 comments on commit fde43f8

Please sign in to comment.