fix: maybe some more concurrent fard fix

This commit is contained in:
ChomeNS
2024-12-05 10:20:07 +07:00
parent 3782c97d21
commit 808e29c0af
4 changed files with 23 additions and 3 deletions

View File

@@ -1 +1 @@
1174
1177

View File

@@ -52,6 +52,8 @@ public class FindAltsCommand extends Command {
}
private Component handle (Bot bot, String targetIP, boolean argumentIsIP, String player) {
PlayersPersistentDataPlugin.lock.lock();
final Stream<String> matches = PlayersPersistentDataPlugin.playersObject.deepCopy().properties()
.stream()
.filter(
@@ -70,6 +72,8 @@ public class FindAltsCommand extends Command {
)
.map(Map.Entry::getKey);
PlayersPersistentDataPlugin.lock.unlock();
Component component = Component
.translatable("Possible alts for the %s %s:")
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))

View File

@@ -10,10 +10,13 @@ import me.chayapak1.chomens_bot.util.PersistentDataUtilities;
import java.time.Instant;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
public static ObjectNode playersObject = (ObjectNode) PersistentDataUtilities.getOrDefault("players", JsonNodeFactory.instance.objectNode());
public static final ReentrantLock lock = new ReentrantLock();
private final Bot bot;
public PlayersPersistentDataPlugin (Bot bot) {
@@ -24,6 +27,8 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
@Override
public synchronized void playerJoined(PlayerEntry target) {
lock.lock();
final JsonNode originalElement = playersObject.get(target.profile.getName());
ObjectNode object;
@@ -35,9 +40,12 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
} else if (originalElement instanceof ObjectNode) {
object = (ObjectNode) originalElement;
} else {
lock.unlock();
return;
}
lock.unlock();
bot.executorService.submit(() -> {
final CompletableFuture<String> future = bot.players.getPlayerIP(target);
@@ -62,9 +70,13 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
// is this bad?
private synchronized void setPersistentEntry (PlayerEntry target, ObjectNode object) {
lock.lock();
playersObject.set(getName(target), object);
PersistentDataUtilities.put("players", playersObject);
lock.unlock();
}
private String getName(PlayerEntry target) {
@@ -73,6 +85,8 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
@Override
public synchronized void playerLeft(PlayerEntry target) {
lock.lock();
if (!playersObject.has(getName(target))) return;
final ObjectNode player = (ObjectNode) playersObject.get(getName(target));
@@ -84,5 +98,7 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
player.set("lastSeen", object);
PersistentDataUtilities.put("players", playersObject);
lock.unlock();
}
}

View File

@@ -73,10 +73,10 @@ public class PersistentDataUtilities {
public static synchronized void stop () {
stopping = true;
lock.lock();
writeFuture.cancel(false);
lock.lock();
try {
writeToFile(true);
} finally {