From ec13ec51b82bb4e1bba337830e38e6c149f45e74 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Tue, 12 Jul 2022 01:07:15 +0200 Subject: [PATCH] Some more minor code improvements, made reload effective for ShareManager settings and added some logging --- .../artemis/the/gr8/playerstats/Main.java | 1 - .../{statistic => }/ShareManager.java | 13 ++++--- .../playerstats/commands/ShareCommand.java | 2 +- .../gr8/playerstats/reload/ReloadThread.java | 39 ++++++++++++------- .../gr8/playerstats/statistic/StatThread.java | 25 ++++++------ 5 files changed, 45 insertions(+), 35 deletions(-) rename src/main/java/com/gmail/artemis/the/gr8/playerstats/{statistic => }/ShareManager.java (89%) diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java index 6802e3e..0c43767 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java @@ -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; diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/ShareManager.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/ShareManager.java similarity index 89% rename from src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/ShareManager.java rename to src/main/java/com/gmail/artemis/the/gr8/playerstats/ShareManager.java index 85985f5..bea6690 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/ShareManager.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/ShareManager.java @@ -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 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); } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ShareCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ShareCommand.java index 6723587..9028510 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ShareCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/ShareCommand.java @@ -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; diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java index 6b69530..6084421 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java @@ -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 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; } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java index 8598b3a..afecbe1 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java @@ -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);