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

View File

@ -1,7 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.commands; package com.gmail.artemis.the.gr8.playerstats.commands;
import com.gmail.artemis.the.gr8.playerstats.Main; 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 net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;

View File

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

View File

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