Some more minor code improvements, made reload effective for ShareManager settings and added some logging

This commit is contained in:
Artemis-the-gr8 2022-07-12 01:07:15 +02:00
parent 0356bc6df8
commit ec13ec51b8
5 changed files with 45 additions and 35 deletions

View File

@ -7,7 +7,6 @@ import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener;
import com.gmail.artemis.the.gr8.playerstats.msg.MessageWriter;
import com.gmail.artemis.the.gr8.playerstats.statistic.ShareManager;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;

View File

@ -1,7 +1,9 @@
package com.gmail.artemis.the.gr8.playerstats.statistic;
package com.gmail.artemis.the.gr8.playerstats;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
import com.gmail.artemis.the.gr8.playerstats.models.StatResult;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import net.kyori.adventure.text.TextComponent;
import javax.annotation.Nullable;
@ -13,12 +15,12 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class ShareManager {
public final class ShareManager {
private static volatile ShareManager instance;
private boolean isEnabled;
private int waitingTime;
private static boolean isEnabled;
private static int waitingTime;
private volatile AtomicInteger resultID; //always starts with value 0
private ConcurrentHashMap<UUID, StatResult> statResults = null;
@ -65,7 +67,7 @@ public class ShareManager {
}
public boolean isEnabled() {
return this.isEnabled;
return isEnabled;
}
public UUID saveStatResult(String playerName, TextComponent statResult) {
@ -109,6 +111,7 @@ public class ShareManager {
.parallelStream()
.min(Comparator.comparing(StatResult::ID))
.orElseThrow().uuid();
MyLogger.logMsg("Removing old stat no. " + statResults.get(uuid) + " for player " + playerName, DebugLevel.MEDIUM);
statResults.remove(uuid);
}
}

View File

@ -1,7 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.commands;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.statistic.ShareManager;
import com.gmail.artemis.the.gr8.playerstats.ShareManager;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.reload;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.ShareManager;
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
@ -23,27 +24,30 @@ import java.util.function.Predicate;
public class ReloadThread extends Thread {
private final int reloadThreadID;
private final StatThread statThread;
private static BukkitAudiences adventure;
private static ConfigHandler config;
private final MessageWriter messageWriter;
private final OfflinePlayerHandler offlinePlayerHandler;
private static BukkitAudiences adventure;
private static ShareManager shareManager;
private final int reloadThreadID;
private final StatThread statThread;
private final CommandSender sender;
public ReloadThread(ConfigHandler c, MessageWriter m, OfflinePlayerHandler o, int ID, @Nullable StatThread s, @Nullable CommandSender se) {
reloadThreadID = ID;
statThread = s;
config = c;
messageWriter = m;
sender = se;
offlinePlayerHandler = o;
adventure = Main.adventure();
offlinePlayerHandler = o;
shareManager = ShareManager.getInstance(c);
reloadThreadID = ID;
statThread = s;
sender = se;
this.setName("ReloadThread-" + reloadThreadID);
MyLogger.threadCreated(this.getName());
@ -66,9 +70,7 @@ public class ReloadThread extends Thread {
if (reloadThreadID != 1 && config.reloadConfig()) { //during a reload
MyLogger.logMsg("Reloading!", false);
MyLogger.setDebugLevel(config.getDebugLevel());
MessageWriter.updateComponentFactory();
loadOfflinePlayers();
reloadEverything();
boolean isBukkitConsole = sender instanceof ConsoleCommandSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit");
if (sender != null) {
@ -78,12 +80,19 @@ public class ReloadThread extends Thread {
}
else { //during first start-up
MyLogger.setDebugLevel(config.getDebugLevel());
loadOfflinePlayers();
offlinePlayerHandler.updateOfflinePlayerList(loadOfflinePlayers());
ThreadManager.recordCalcTime(System.currentTimeMillis() - time);
}
}
private void loadOfflinePlayers() {
private void reloadEverything() {
MyLogger.setDebugLevel(config.getDebugLevel());
MessageWriter.updateComponentFactory();
offlinePlayerHandler.updateOfflinePlayerList(loadOfflinePlayers());
shareManager.updateSettings(config);
}
private ConcurrentHashMap<String, UUID> loadOfflinePlayers() {
long time = System.currentTimeMillis();
OfflinePlayer[] offlinePlayers;
@ -121,8 +130,8 @@ public class ReloadThread extends Thread {
ForkJoinPool.commonPool().invoke(task);
MyLogger.actionFinished(1);
offlinePlayerHandler.updateOfflinePlayerList(playerMap);
MyLogger.logTimeTaken("ReloadThread",
("loaded " + offlinePlayerHandler.getOfflinePlayerCount() + " offline players"), time);
return playerMap;
}
}

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.statistic;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.ShareManager;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.msg.MessageWriter;
@ -49,7 +50,6 @@ public class StatThread extends Thread {
MyLogger.threadCreated(this.getName());
}
//what the thread will do once started
@Override
public void run() throws IllegalStateException, NullPointerException {
MyLogger.threadStart(this.getName());
@ -60,9 +60,8 @@ public class StatThread extends Thread {
if (reloadThread != null && reloadThread.isAlive()) {
try {
MyLogger.waitingForOtherThread(this.getName(), reloadThread.getName());
adventure.sender(request.getCommandSender())
.sendMessage(messageWriter
.stillReloading(request.isBukkitConsoleSender()));
adventure.sender(request.getCommandSender()).sendMessage(
messageWriter.stillReloading(request.isBukkitConsoleSender()));
reloadThread.join();
} catch (InterruptedException e) {
@ -72,20 +71,20 @@ public class StatThread extends Thread {
}
Target selection = request.getSelection();
if (ThreadManager.getLastRecordedCalcTime() > 2000) {
long lastCalc = ThreadManager.getLastRecordedCalcTime();
if (lastCalc > 2000) {
adventure.sender(request.getCommandSender()).sendMessage(
messageWriter.waitAMoment(ThreadManager.getLastRecordedCalcTime() > 20000, request.isBukkitConsoleSender()));
messageWriter.waitAMoment(lastCalc > 20000, request.isBukkitConsoleSender()));
}
TextComponent statResult;
try {
if (selection == Target.PLAYER) {
statResult = messageWriter.formatPlayerStat(getIndividualStat(), request);
} else if (selection == Target.TOP) {
statResult = messageWriter.formatTopStats(getTopStats(), request);
} else {
statResult = messageWriter.formatServerStat(getServerTotal(), request);
}
statResult = switch (selection) {
case PLAYER -> messageWriter.formatPlayerStat(getIndividualStat(), request);
case TOP -> messageWriter.formatTopStats(getTopStats(), request);
case SERVER -> messageWriter.formatServerStat(getServerTotal(), request);
};
if (shareManager.isEnabled()) {
UUID shareCode = shareManager.saveStatResult(request.getCommandSender().getName(), statResult);
statResult = messageWriter.addShareButton(statResult, shareCode);