mirror of
https://github.com/itHotL/PlayerStats.git
synced 2025-02-12 01:01:21 +01:00
Even more API experimentation
This commit is contained in:
parent
8136bce894
commit
ca7a1d3e67
@ -6,6 +6,9 @@
|
||||
</list>
|
||||
</component>
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="JavadocGenerationManager">
|
||||
<option name="OUTPUT_DIRECTORY" value="$PROJECT_DIR$/javadoc" />
|
||||
</component>
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
|
@ -2,6 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.api.PlayerStats;
|
||||
import com.gmail.artemis.the.gr8.playerstats.api.PlayerStatsAPI;
|
||||
import com.gmail.artemis.the.gr8.playerstats.api.StatFormatter;
|
||||
import com.gmail.artemis.the.gr8.playerstats.commands.ReloadCommand;
|
||||
import com.gmail.artemis.the.gr8.playerstats.commands.ShareCommand;
|
||||
import com.gmail.artemis.the.gr8.playerstats.commands.StatCommand;
|
||||
@ -9,6 +10,7 @@ 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.OutputManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.statistic.StatManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -23,35 +25,37 @@ public class Main extends JavaPlugin {
|
||||
|
||||
public static @NotNull BukkitAudiences adventure() {
|
||||
if (adventure == null) {
|
||||
throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!");
|
||||
throw new IllegalStateException("Tried to access Adventure without PlayerStats being enabled!");
|
||||
}
|
||||
return adventure;
|
||||
}
|
||||
|
||||
public static @NotNull PlayerStats getPlayerStatsAPI() {
|
||||
if (playerStatsAPI == null) {
|
||||
playerStatsAPI = new PlayerStatsAPI();
|
||||
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
|
||||
}
|
||||
return playerStatsAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
//initialize the Adventure library
|
||||
adventure = BukkitAudiences.create(this);
|
||||
|
||||
//first get an instance of all the classes that need to be passed along to different classes
|
||||
ConfigHandler config = new ConfigHandler(this);
|
||||
OfflinePlayerHandler offlinePlayerHandler = new OfflinePlayerHandler();
|
||||
|
||||
OutputManager outputManager = OutputManager.getInstance(config);
|
||||
ThreadManager threadManager = ThreadManager.getInstance(config, outputManager, offlinePlayerHandler);
|
||||
StatManager statManager = StatManager.getInstance(outputManager, offlinePlayerHandler);
|
||||
ThreadManager threadManager = ThreadManager.getInstance(config, outputManager, statManager, offlinePlayerHandler);
|
||||
ShareManager shareManager = ShareManager.getInstance(config);
|
||||
|
||||
//initialize the Adventure library and the API
|
||||
adventure = BukkitAudiences.create(this);
|
||||
playerStatsAPI = PlayerStatsAPI.load(this, threadManager, outputManager, statManager);
|
||||
|
||||
//register all commands and the tabCompleter
|
||||
PluginCommand statcmd = this.getCommand("statistic");
|
||||
if (statcmd != null) {
|
||||
statcmd.setExecutor(new StatCommand(outputManager, threadManager, offlinePlayerHandler));
|
||||
statcmd.setExecutor(new StatCommand(outputManager, threadManager, statManager));
|
||||
statcmd.setTabCompleter(new TabCompleter(offlinePlayerHandler));
|
||||
}
|
||||
PluginCommand reloadcmd = this.getCommand("statisticreload");
|
||||
|
@ -5,6 +5,7 @@ import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.reload.ReloadThread;
|
||||
import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
|
||||
import com.gmail.artemis.the.gr8.playerstats.statistic.StatManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.statistic.StatThread;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
@ -22,6 +23,7 @@ public final class ThreadManager {
|
||||
|
||||
private static ConfigHandler config;
|
||||
private static OutputManager outputManager;
|
||||
private static StatManager statManager;
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
|
||||
private ReloadThread lastActiveReloadThread;
|
||||
@ -29,9 +31,10 @@ public final class ThreadManager {
|
||||
private final HashMap<String, Thread> statThreads;
|
||||
private static long lastRecordedCalcTime;
|
||||
|
||||
private ThreadManager(ConfigHandler c, OutputManager m, OfflinePlayerHandler o) {
|
||||
private ThreadManager(ConfigHandler c, OutputManager m, StatManager s, OfflinePlayerHandler o) {
|
||||
config = c;
|
||||
outputManager = m;
|
||||
statManager = s;
|
||||
offlinePlayerHandler = o;
|
||||
|
||||
statThreads = new HashMap<>();
|
||||
@ -42,14 +45,14 @@ public final class ThreadManager {
|
||||
startReloadThread(null);
|
||||
}
|
||||
|
||||
public static ThreadManager getInstance(ConfigHandler config, OutputManager messageSender, OfflinePlayerHandler offlinePlayerHandler) {
|
||||
public static ThreadManager getInstance(ConfigHandler config, OutputManager output, StatManager statManager, OfflinePlayerHandler offlinePlayerHandler) {
|
||||
ThreadManager threadManager = instance;
|
||||
if (threadManager != null) {
|
||||
return threadManager;
|
||||
}
|
||||
synchronized (ThreadManager.class) {
|
||||
if (instance == null) {
|
||||
instance = new ThreadManager(config, messageSender, offlinePlayerHandler);
|
||||
instance = new ThreadManager(config, output, statManager, offlinePlayerHandler);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
@ -100,7 +103,7 @@ public final class ThreadManager {
|
||||
}
|
||||
|
||||
private void startNewStatThread(StatRequest request) {
|
||||
lastActiveStatThread = new StatThread(config, outputManager, offlinePlayerHandler, statThreadID, request, lastActiveReloadThread);
|
||||
lastActiveStatThread = new StatThread(config, outputManager, statManager, offlinePlayerHandler, statThreadID, request, lastActiveReloadThread);
|
||||
statThreads.put(request.getCommandSender().getName(), lastActiveStatThread);
|
||||
lastActiveStatThread.start();
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.gmail.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.Main;
|
||||
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public interface PlayerStats extends RequestManager, StatManager, StatFormatter {
|
||||
public interface PlayerStats {
|
||||
|
||||
static PlayerStats getAPI() {
|
||||
return Main.getPlayerStatsAPI();
|
||||
}
|
||||
|
||||
TextComponent getFancyStat(Target selection, CommandSender sender, String[] args) throws IllegalArgumentException;
|
||||
}
|
@ -1,56 +1,57 @@
|
||||
package com.gmail.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.Main;
|
||||
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
|
||||
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.statistic.StatManager;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class PlayerStatsAPI implements PlayerStats {
|
||||
import static org.jetbrains.annotations.ApiStatus.Internal;
|
||||
|
||||
/** This class implements the API*/
|
||||
public final class PlayerStatsAPI extends JavaPlugin implements PlayerStats {
|
||||
|
||||
private final Main plugin;
|
||||
private static ThreadManager threadManager;
|
||||
private static StatFormatter statFormatter;
|
||||
private static StatManager statManager;
|
||||
|
||||
@Internal
|
||||
private PlayerStatsAPI(Main plugin, ThreadManager thread, StatFormatter format, StatManager stat) {
|
||||
this.plugin = plugin;
|
||||
threadManager = thread;
|
||||
statFormatter = format;
|
||||
statManager = stat;
|
||||
}
|
||||
|
||||
@Internal
|
||||
public static PlayerStatsAPI load(Main plugin, ThreadManager threadManager, StatFormatter formatter, StatManager statManager) {
|
||||
return new PlayerStatsAPI(plugin, threadManager, formatter, statManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest generateRequest(CommandSender sender, String[] args) {
|
||||
public TextComponent getFancyStat(Target selection, CommandSender sender, String[] args) throws IllegalArgumentException {
|
||||
StatRequest request = statManager.generateRequest(sender, args);
|
||||
if (statManager.requestIsValid(request)) {
|
||||
switch (selection) {
|
||||
case PLAYER -> {
|
||||
int stat = statManager.getPlayerStat(request);
|
||||
return statFormatter.formatPlayerStat(request, stat);
|
||||
}
|
||||
case SERVER -> {
|
||||
//do something async
|
||||
}
|
||||
case TOP -> {
|
||||
//also do something async
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("This is not a valid stat-request!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requestIsValid(StatRequest request) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(TextComponent component) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextComponent formatPlayerStat(StatRequest request, int playerStat) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextComponent formatServerStat(StatRequest request, long serverStat) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextComponent formatTopStat(StatRequest request, LinkedHashMap<String, Integer> topStats) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashMap<String, Integer> getTopStats(StatRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getServerStat(StatRequest request) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPlayerStat(StatRequest request) {
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.gmail.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
/** The RequestHandler will help you turn a String (such as "stat animals_bred") into a specific StatRequest
|
||||
with all the information PlayerStatsAPI needs to work with. You'll need this StatRequest Object to get the Statistic
|
||||
data that you want, and to format this data into a fancy Component or String, so you can output it somewhere.*/
|
||||
public interface RequestHandler {
|
||||
|
||||
/** This will create a StatRequest from the provided args, with the requesting Player (or Console)
|
||||
as CommandSender. This CommandSender will receive feedback messages if the StatRequest could not be created.
|
||||
@param args an Array of args corresponding to a Statistic, a potential Sub-Statistic, and a Target
|
||||
(exactly as they are typed in Minecraft chat when using PlayerStatsAPI' /stat command -
|
||||
for example "/stat kill_entity bee top")
|
||||
@param sender the CommandSender that requested this specific statistic*/
|
||||
StatRequest generateRequest(CommandSender sender, String[] args);
|
||||
|
||||
/** This method validates the StatRequest and returns feedback to the player if it returns false.
|
||||
It checks the following:
|
||||
<p>1. Is a Statistic set?</p>
|
||||
<p>2. Is a subStat needed, and is a subStat Enum Constant present? (block/entity/item)</p>
|
||||
<p>3. If the target is PLAYER, is a valid PlayerName provided? </p>
|
||||
@return true if the StatRequest is valid, and false + an explanation message otherwise. */
|
||||
boolean requestIsValid(StatRequest request);
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.gmail.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
/** The RequestManager will help you turn a String (such as "stat animals_bred") into a specific StatRequest
|
||||
with all the information PlayerStats needs to work with. You'll need this StatRequest Object to get the Statistic
|
||||
data that you want, and to format it into a fancy Component or String, so you can output it somewhere.*/
|
||||
public interface RequestManager {
|
||||
|
||||
/** This will create a StatRequest from the provided args, with the requesting Player (or Console)
|
||||
as CommandSender. This CommandSender will receive feedback messages if the StatRequest could not be created.
|
||||
@param args an Array of args corresponding to a Statistic, a potential Sub-Statistic, and a Target
|
||||
(exactly as one would type them in Minecraft chat when using PlayerStats' /stat command)*/
|
||||
StatRequest generateRequest(CommandSender sender, String[] args);
|
||||
|
||||
boolean requestIsValid(StatRequest request);
|
||||
}
|
@ -3,6 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.api;
|
||||
import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
|
||||
import static org.jetbrains.annotations.ApiStatus.Internal;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
@ -10,7 +11,15 @@ import java.util.LinkedHashMap;
|
||||
This is meant for an outgoing API - for internal use, more output functionality may exist. */
|
||||
public interface StatFormatter {
|
||||
|
||||
String toString(TextComponent component);
|
||||
default String toString(TextComponent component) {
|
||||
return component.content();
|
||||
}
|
||||
|
||||
/** Returns the setting for whether TextComponents should be saved internally for later stat-sharing by players.
|
||||
Make this method return "false" if you only want to get a fancy stat-result, and don't want to send it
|
||||
to players in chat with a clickable "share"-button. */
|
||||
@Internal
|
||||
boolean saveOutputForSharing();
|
||||
|
||||
TextComponent formatPlayerStat(StatRequest request, int playerStat);
|
||||
|
||||
|
@ -4,13 +4,11 @@ import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public interface StatManager extends RequestManager {
|
||||
|
||||
//use ThreadManager.startStatThread
|
||||
LinkedHashMap<String, Integer> getTopStats(StatRequest request);
|
||||
|
||||
long getServerStat(StatRequest request);
|
||||
public interface StatGetter {
|
||||
|
||||
int getPlayerStat(StatRequest request);
|
||||
|
||||
long getServerStat(StatRequest request);
|
||||
|
||||
LinkedHashMap<String, Integer> getTopStats(StatRequest request);
|
||||
}
|
@ -1,34 +1,25 @@
|
||||
package com.gmail.artemis.the.gr8.playerstats.commands;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.api.RequestManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
|
||||
import com.gmail.artemis.the.gr8.playerstats.statistic.StatManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.models.StatRequest;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
public class StatCommand implements CommandExecutor, RequestManager {
|
||||
public class StatCommand implements CommandExecutor {
|
||||
|
||||
private static ThreadManager threadManager;
|
||||
private static OutputManager outputManager;
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
private final StatManager statManager;
|
||||
|
||||
public StatCommand(OutputManager m, ThreadManager t, OfflinePlayerHandler o) {
|
||||
public StatCommand(OutputManager m, ThreadManager t, StatManager s) {
|
||||
threadManager = t;
|
||||
outputManager = m;
|
||||
offlinePlayerHandler = o;
|
||||
statManager = s;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,8 +32,8 @@ public class StatCommand implements CommandExecutor, RequestManager {
|
||||
outputManager.sendExamples(sender);
|
||||
}
|
||||
else {
|
||||
StatRequest request = generateRequest(sender, args);
|
||||
if (requestIsValid(request)) {
|
||||
StatRequest request = statManager.generateRequest(sender, args);
|
||||
if (statManager.requestIsValid(request)) {
|
||||
threadManager.startStatThread(request);
|
||||
} else {
|
||||
return false;
|
||||
@ -50,132 +41,4 @@ public class StatCommand implements CommandExecutor, RequestManager {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Create a StatRequest Object with all the relevant information from the args[]. */
|
||||
@Override
|
||||
public StatRequest generateRequest(CommandSender sender, String[] args) {
|
||||
StatRequest request = new StatRequest(sender);
|
||||
for (String arg : args) {
|
||||
//check for statName
|
||||
if (EnumHandler.isStatistic(arg) && request.getStatistic() == null) {
|
||||
request.setStatistic(EnumHandler.getStatEnum(arg));
|
||||
}
|
||||
//check for subStatEntry and playerFlag
|
||||
else if (EnumHandler.isSubStatEntry(arg)) {
|
||||
if (arg.equalsIgnoreCase("player") && !request.playerFlag()) {
|
||||
request.setPlayerFlag(true);
|
||||
}
|
||||
else {
|
||||
if (request.getSubStatEntry() == null) request.setSubStatEntry(arg);
|
||||
}
|
||||
}
|
||||
//check for selection
|
||||
else if (arg.equalsIgnoreCase("top")) {
|
||||
request.setSelection(Target.TOP);
|
||||
}
|
||||
else if (arg.equalsIgnoreCase("server")) {
|
||||
request.setSelection(Target.SERVER);
|
||||
}
|
||||
else if (arg.equalsIgnoreCase("me")) {
|
||||
if (sender instanceof Player) {
|
||||
request.setPlayerName(sender.getName());
|
||||
request.setSelection(Target.PLAYER);
|
||||
}
|
||||
else if (sender instanceof ConsoleCommandSender) {
|
||||
request.setSelection(Target.SERVER);
|
||||
}
|
||||
}
|
||||
else if (offlinePlayerHandler.isRelevantPlayer(arg) && request.getPlayerName() == null) {
|
||||
request.setPlayerName(arg);
|
||||
request.setSelection(Target.PLAYER);
|
||||
}
|
||||
}
|
||||
patchRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/** Adjust the StatRequest object if needed: unpack the playerFlag into a subStatEntry,
|
||||
try to retrieve the corresponding Enum Constant for any relevant block/entity/item,
|
||||
and remove any unnecessary subStatEntries.*/
|
||||
private void patchRequest(StatRequest request) {
|
||||
if (request.getStatistic() != null) {
|
||||
Statistic.Type type = request.getStatistic().getType();
|
||||
|
||||
if (request.playerFlag()) { //unpack the playerFlag
|
||||
if (type == Statistic.Type.ENTITY && request.getSubStatEntry() == null) {
|
||||
request.setSubStatEntry("player");
|
||||
}
|
||||
else {
|
||||
request.setSelection(Target.PLAYER);
|
||||
}
|
||||
}
|
||||
|
||||
String subStatEntry = request.getSubStatEntry();
|
||||
switch (type) { //attempt to convert relevant subStatEntries into their corresponding Enum Constant
|
||||
case BLOCK -> {
|
||||
Material block = EnumHandler.getBlockEnum(subStatEntry);
|
||||
if (block != null) request.setBlock(block);
|
||||
}
|
||||
case ENTITY -> {
|
||||
EntityType entity = EnumHandler.getEntityEnum(subStatEntry);
|
||||
if (entity != null) request.setEntity(entity);
|
||||
}
|
||||
case ITEM -> {
|
||||
Material item = EnumHandler.getItemEnum(subStatEntry);
|
||||
if (item != null) request.setItem(item);
|
||||
}
|
||||
case UNTYPED -> { //remove unnecessary subStatEntries
|
||||
if (subStatEntry != null) request.setSubStatEntry(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** This method validates the StatRequest and returns feedback to the player if it returns false.
|
||||
It checks the following:
|
||||
<p>1. Is a Statistic set?</p>
|
||||
<p>2. Is a subStat needed, and is a subStat Enum Constant present? (block/entity/item)</p>
|
||||
<p>3. If the target is PLAYER, is a valid PlayerName provided? </p>
|
||||
@return true if the RequestManager is valid, and false + an explanation message otherwise. */
|
||||
@Override
|
||||
public boolean requestIsValid(StatRequest request) {
|
||||
if (request.getStatistic() == null) {
|
||||
outputManager.sendFeedbackMsg(request.getCommandSender(), StandardMessage.MISSING_STAT_NAME);
|
||||
return false;
|
||||
}
|
||||
Statistic.Type type = request.getStatistic().getType();
|
||||
if (request.getSubStatEntry() == null && type != Statistic.Type.UNTYPED) {
|
||||
outputManager.sendFeedbackMsgMissingSubStat(request.getCommandSender(), type);
|
||||
return false;
|
||||
}
|
||||
else if (!matchingSubStat(request)) {
|
||||
outputManager.sendFeedbackMsgWrongSubStat(request.getCommandSender(), type, request.getSubStatEntry());
|
||||
return false;
|
||||
}
|
||||
else if (request.getSelection() == Target.PLAYER && request.getPlayerName() == null) {
|
||||
outputManager.sendFeedbackMsg(request.getCommandSender(), StandardMessage.MISSING_PLAYER_NAME);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean matchingSubStat(StatRequest request) {
|
||||
Statistic.Type type = request.getStatistic().getType();
|
||||
switch (type) {
|
||||
case BLOCK -> {
|
||||
return request.getBlock() != null;
|
||||
}
|
||||
case ENTITY -> {
|
||||
return request.getEntity() != null;
|
||||
}
|
||||
case ITEM -> {
|
||||
return request.getItem() != null;
|
||||
}
|
||||
default -> {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -25,6 +25,7 @@ import java.util.UUID;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.jetbrains.annotations.ApiStatus.Internal;
|
||||
import static com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage.*;
|
||||
|
||||
public final class OutputManager implements StatFormatter {
|
||||
@ -63,16 +64,12 @@ public final class OutputManager implements StatFormatter {
|
||||
getMessageWriters(config);
|
||||
}
|
||||
|
||||
@Internal
|
||||
@Override
|
||||
public boolean saveOutputForSharing() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(@NotNull TextComponent component) {
|
||||
return component.content();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextComponent formatPlayerStat(@NotNull StatRequest request, int playerStat) {
|
||||
CommandSender sender = request.getCommandSender();
|
||||
|
@ -0,0 +1,177 @@
|
||||
package com.gmail.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.api.RequestHandler;
|
||||
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
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.OutputManager;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public final class StatManager implements RequestHandler {
|
||||
|
||||
private static volatile StatManager instance;
|
||||
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
private static OutputManager outputManager;
|
||||
|
||||
private StatManager(OutputManager output, OfflinePlayerHandler offlinePlayerHandler) {
|
||||
this.offlinePlayerHandler = offlinePlayerHandler;
|
||||
outputManager = output;
|
||||
}
|
||||
|
||||
public static StatManager getInstance(OutputManager outputManager, OfflinePlayerHandler offlinePlayerHandler) {
|
||||
StatManager statManager = instance;
|
||||
if (statManager != null) {
|
||||
return statManager;
|
||||
}
|
||||
synchronized (StatManager.class) {
|
||||
if (instance == null) {
|
||||
instance = new StatManager(outputManager, offlinePlayerHandler);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
public StatRequest generateRequest(CommandSender sender, String[] args) {
|
||||
StatRequest request = new StatRequest(sender);
|
||||
for (String arg : args) {
|
||||
//check for statName
|
||||
if (EnumHandler.isStatistic(arg) && request.getStatistic() == null) {
|
||||
request.setStatistic(EnumHandler.getStatEnum(arg));
|
||||
}
|
||||
//check for subStatEntry and playerFlag
|
||||
else if (EnumHandler.isSubStatEntry(arg)) {
|
||||
if (arg.equalsIgnoreCase("player") && !request.playerFlag()) {
|
||||
request.setPlayerFlag(true);
|
||||
}
|
||||
else {
|
||||
if (request.getSubStatEntry() == null) request.setSubStatEntry(arg);
|
||||
}
|
||||
}
|
||||
//check for selection
|
||||
else if (arg.equalsIgnoreCase("top")) {
|
||||
request.setSelection(Target.TOP);
|
||||
}
|
||||
else if (arg.equalsIgnoreCase("server")) {
|
||||
request.setSelection(Target.SERVER);
|
||||
}
|
||||
else if (arg.equalsIgnoreCase("me")) {
|
||||
if (sender instanceof Player) {
|
||||
request.setPlayerName(sender.getName());
|
||||
request.setSelection(Target.PLAYER);
|
||||
}
|
||||
else if (sender instanceof ConsoleCommandSender) {
|
||||
request.setSelection(Target.SERVER);
|
||||
}
|
||||
}
|
||||
else if (offlinePlayerHandler.isRelevantPlayer(arg) && request.getPlayerName() == null) {
|
||||
request.setPlayerName(arg);
|
||||
request.setSelection(Target.PLAYER);
|
||||
}
|
||||
}
|
||||
patchRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
/** Adjust the StatRequest object if needed: unpack the playerFlag into a subStatEntry,
|
||||
try to retrieve the corresponding Enum Constant for any relevant block/entity/item,
|
||||
and remove any unnecessary subStatEntries.*/
|
||||
private void patchRequest(StatRequest request) {
|
||||
if (request.getStatistic() != null) {
|
||||
Statistic.Type type = request.getStatistic().getType();
|
||||
|
||||
if (request.playerFlag()) { //unpack the playerFlag
|
||||
if (type == Statistic.Type.ENTITY && request.getSubStatEntry() == null) {
|
||||
request.setSubStatEntry("player");
|
||||
}
|
||||
else {
|
||||
request.setSelection(Target.PLAYER);
|
||||
}
|
||||
}
|
||||
|
||||
String subStatEntry = request.getSubStatEntry();
|
||||
switch (type) { //attempt to convert relevant subStatEntries into their corresponding Enum Constant
|
||||
case BLOCK -> {
|
||||
Material block = EnumHandler.getBlockEnum(subStatEntry);
|
||||
if (block != null) request.setBlock(block);
|
||||
}
|
||||
case ENTITY -> {
|
||||
EntityType entity = EnumHandler.getEntityEnum(subStatEntry);
|
||||
if (entity != null) request.setEntity(entity);
|
||||
}
|
||||
case ITEM -> {
|
||||
Material item = EnumHandler.getItemEnum(subStatEntry);
|
||||
if (item != null) request.setItem(item);
|
||||
}
|
||||
case UNTYPED -> { //remove unnecessary subStatEntries
|
||||
if (subStatEntry != null) request.setSubStatEntry(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean requestIsValid(StatRequest request) {
|
||||
if (request.getStatistic() == null) {
|
||||
outputManager.sendFeedbackMsg(request.getCommandSender(), StandardMessage.MISSING_STAT_NAME);
|
||||
return false;
|
||||
}
|
||||
Statistic.Type type = request.getStatistic().getType();
|
||||
if (request.getSubStatEntry() == null && type != Statistic.Type.UNTYPED) {
|
||||
outputManager.sendFeedbackMsgMissingSubStat(request.getCommandSender(), type);
|
||||
return false;
|
||||
}
|
||||
else if (!hasMatchingSubStat(request)) {
|
||||
outputManager.sendFeedbackMsgWrongSubStat(request.getCommandSender(), type, request.getSubStatEntry());
|
||||
return false;
|
||||
}
|
||||
else if (request.getSelection() == Target.PLAYER && request.getPlayerName() == null) {
|
||||
outputManager.sendFeedbackMsg(request.getCommandSender(), StandardMessage.MISSING_PLAYER_NAME);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasMatchingSubStat(StatRequest request) {
|
||||
Statistic.Type type = request.getStatistic().getType();
|
||||
switch (type) {
|
||||
case BLOCK -> {
|
||||
return request.getBlock() != null;
|
||||
}
|
||||
case ENTITY -> {
|
||||
return request.getEntity() != null;
|
||||
}
|
||||
case ITEM -> {
|
||||
return request.getItem() != null;
|
||||
}
|
||||
default -> {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Gets the statistic data for an individual player. If somehow the player
|
||||
cannot be found, this returns 0.*/
|
||||
public int getPlayerStat(StatRequest request) {
|
||||
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(request.getPlayerName());
|
||||
if (player != null) {
|
||||
return switch (request.getStatistic().getType()) {
|
||||
case UNTYPED -> player.getStatistic(request.getStatistic());
|
||||
case ENTITY -> player.getStatistic(request.getStatistic(), request.getEntity());
|
||||
case BLOCK -> player.getStatistic(request.getStatistic(), request.getBlock());
|
||||
case ITEM -> player.getStatistic(request.getStatistic(), request.getItem());
|
||||
};
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -23,15 +23,18 @@ import java.util.stream.Collectors;
|
||||
public class StatThread extends Thread {
|
||||
|
||||
private static ConfigHandler config;
|
||||
private final OutputManager outputManager;
|
||||
private static OutputManager outputManager;
|
||||
private static StatManager statManager;
|
||||
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
|
||||
private final ReloadThread reloadThread;
|
||||
private final StatRequest request;
|
||||
|
||||
public StatThread(ConfigHandler c, OutputManager m, OfflinePlayerHandler o, int ID, StatRequest s, @Nullable ReloadThread r) {
|
||||
public StatThread(ConfigHandler c, OutputManager m, StatManager t, OfflinePlayerHandler o, int ID, StatRequest s, @Nullable ReloadThread r) {
|
||||
config = c;
|
||||
outputManager = m;
|
||||
statManager = t;
|
||||
offlinePlayerHandler = o;
|
||||
|
||||
reloadThread = r;
|
||||
@ -68,7 +71,7 @@ public class StatThread extends Thread {
|
||||
Target selection = request.getSelection();
|
||||
try {
|
||||
TextComponent statResult = switch (selection) {
|
||||
case PLAYER -> outputManager.formatPlayerStat(request, getIndividualStat());
|
||||
case PLAYER -> outputManager.formatPlayerStat(request, statManager.getPlayerStat(request));
|
||||
case TOP -> outputManager.formatTopStat(request, getTopStats());
|
||||
case SERVER -> outputManager.formatServerStat(request, getServerStat());
|
||||
};
|
||||
@ -119,19 +122,4 @@ public class StatThread extends Thread {
|
||||
|
||||
return playerStats;
|
||||
}
|
||||
|
||||
/** Gets the statistic data for an individual player. If somehow the player
|
||||
cannot be found, this returns 0.*/
|
||||
private int getIndividualStat() {
|
||||
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(request.getPlayerName());
|
||||
if (player != null) {
|
||||
return switch (request.getStatistic().getType()) {
|
||||
case UNTYPED -> player.getStatistic(request.getStatistic());
|
||||
case ENTITY -> player.getStatistic(request.getStatistic(), request.getEntity());
|
||||
case BLOCK -> player.getStatistic(request.getStatistic(), request.getBlock());
|
||||
case ITEM -> player.getStatistic(request.getStatistic(), request.getItem());
|
||||
};
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
package com.gmail.artemis.the.gr8.playerstats.utils;
|
||||
|
||||
import com.gmail.artemis.the.gr8.playerstats.api.PlayerStats;
|
||||
import com.gmail.artemis.the.gr8.playerstats.api.PlayerStatsAPI;
|
||||
import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
Loading…
Reference in New Issue
Block a user