From 7949113f6ea6b50d356957c8a6b0cdaa5b22aae3 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 15 Jun 2017 15:14:51 +0300 Subject: [PATCH] Moved to use new utility package, attempts at #113 New Utility jar BukkitPluginDependency contains new classes that include methods & classes usually required for me to start creating a project, such as: - Version check - Abstract command classes (For subcommands & commands w/ subcommands) - UUIDFetcher - Log utility - Benchmarking It was tested with Plan to ensure it works. It will be used in the future to lower the threshold of starting a new project. --- Plan/dependency-reduced-pom.xml | 75 +++++++-- Plan/pom.xml | 33 ++-- .../main/java/com/djrapitops/plan/Log.java | 76 +-------- .../main/java/com/djrapitops/plan/Plan.java | 80 ++++------ .../djrapitops/plan/ServerVariableHolder.java | 8 + .../java/com/djrapitops/plan/api/API.java | 2 +- .../djrapitops/plan/command/CommandType.java | 16 -- .../djrapitops/plan/command/PlanCommand.java | 117 ++------------ .../djrapitops/plan/command/SubCommand.java | 105 ------------ .../plan/command/commands/AnalyzeCommand.java | 10 +- .../plan/command/commands/HelpCommand.java | 55 ------- .../plan/command/commands/InfoCommand.java | 12 +- .../plan/command/commands/InspectCommand.java | 14 +- .../plan/command/commands/ManageCommand.java | 105 ++---------- .../command/commands/QuickAnalyzeCommand.java | 10 +- .../command/commands/QuickInspectCommand.java | 10 +- .../plan/command/commands/ReloadCommand.java | 8 +- .../plan/command/commands/SearchCommand.java | 6 +- .../commands/manage/ManageBackupCommand.java | 6 +- .../commands/manage/ManageClearCommand.java | 6 +- .../commands/manage/ManageHelpCommand.java | 69 -------- .../commands/manage/ManageHotswapCommand.java | 8 +- .../commands/manage/ManageImportCommand.java | 6 +- .../commands/manage/ManageMoveCommand.java | 12 +- .../commands/manage/ManageRemoveCommand.java | 6 +- .../commands/manage/ManageRestoreCommand.java | 6 +- .../commands/manage/ManageStatusCommand.java | 6 +- .../djrapitops/plan/database/Container.java | 30 ++++ .../com/djrapitops/plan/database/DBUtils.java | 40 +++++ .../plan/database/databases/SQLDB.java | 41 +++++ .../plan/database/tables/SessionsTable.java | 55 ++++--- .../plan/database/tables/Table.java | 8 + .../plan/database/tables/UsersTable.java | 5 + .../djrapitops/plan/utilities/Benchmark.java | 18 +-- .../plan/utilities/FormatUtils.java | 66 ++------ .../djrapitops/plan/utilities/HtmlUtils.java | 8 +- .../plan/utilities/ManageUtils.java | 41 ++++- .../djrapitops/plan/utilities/MiscUtils.java | 48 ------ .../plan/utilities/uuid/UUIDFetcher.java | 149 ------------------ .../plan/utilities/uuid/UUIDUtility.java | 1 + Plan/src/main/resources/analysis.html | 2 +- .../plan/database/DatabaseTest.java | 2 +- .../plan/utilities/ManageUtilsTest.java | 142 +++++++++++++++++ .../plan/utilities/MiscUtilsTest.java | 38 ----- Plan/src/test/java/utils/TestInit.java | 6 + 45 files changed, 593 insertions(+), 974 deletions(-) delete mode 100644 Plan/src/main/java/com/djrapitops/plan/command/CommandType.java delete mode 100644 Plan/src/main/java/com/djrapitops/plan/command/SubCommand.java delete mode 100644 Plan/src/main/java/com/djrapitops/plan/command/commands/HelpCommand.java delete mode 100644 Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHelpCommand.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/database/Container.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/database/DBUtils.java delete mode 100644 Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDFetcher.java create mode 100644 Plan/src/test/java/main/java/com/djrapitops/plan/utilities/ManageUtilsTest.java diff --git a/Plan/dependency-reduced-pom.xml b/Plan/dependency-reduced-pom.xml index 4dbd50b76..d9186c961 100644 --- a/Plan/dependency-reduced-pom.xml +++ b/Plan/dependency-reduced-pom.xml @@ -39,19 +39,20 @@ - - - *:* - - org/** - javassist/** - mockito-extensions/** - net/** - junit/** - com/thoughtworks/** - - - + + + org.powermock:* + org.javassist:* + com.thoughtworks.xstream:* + xmlpull:* + xpp3:* + org.objenesis:* + cglib:* + org.*:* + org.easymock:* + junit:* + + @@ -111,6 +112,54 @@ pom compile + + org.powermock + powermock-api-mockito + 1.6.6 + compile + + + org.powermock + powermock-module-junit4 + 1.6.6 + compile + + + org.powermock + powermock-module-junit4-rule + 1.6.6 + compile + + + org.powermock + powermock-classloading-xstream + 1.6.6 + compile + + + org.mockito + mockito-core + 1.10.19 + compile + + + org.powermock + powermock-api-easymock + 1.6.6 + compile + + + org.easymock + easymock + 3.4 + compile + + + junit + junit + 4.12 + compile + org.hamcrest hamcrest-core diff --git a/Plan/pom.xml b/Plan/pom.xml index 2ac7bb6f9..281ed7543 100644 --- a/Plan/pom.xml +++ b/Plan/pom.xml @@ -13,6 +13,12 @@ provided + + com.djrapitops + BukkitPluginDependency + 1.0.0 + compile + com.djrapitops PlanPluginBridge @@ -113,19 +119,20 @@ - - - *:* - - org/** - javassist/** - mockito-extensions/** - net/** - junit/** - com/thoughtworks/** - - - + + + org.powermock:* + org.javassist:* + com.thoughtworks.xstream:* + xmlpull:* + xpp3:* + org.objenesis:* + cglib:* + org.*:* + org.easymock:* + junit:* + + diff --git a/Plan/src/main/java/com/djrapitops/plan/Log.java b/Plan/src/main/java/com/djrapitops/plan/Log.java index a25492c31..2ef65a7f2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Log.java +++ b/Plan/src/main/java/com/djrapitops/plan/Log.java @@ -1,13 +1,6 @@ package main.java.com.djrapitops.plan; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; import java.util.Collection; -import main.java.com.djrapitops.plan.utilities.FormatUtils; -import main.java.com.djrapitops.plan.utilities.MiscUtils; -import org.bukkit.command.ConsoleCommandSender; /** * This class manages the messages going to the Bukkit's Logger. @@ -17,27 +10,17 @@ import org.bukkit.command.ConsoleCommandSender; */ public class Log { - final private static String DEBUG = "DebugLog.txt"; - final private static String ERRORS = "Errors.txt"; - /** * Logs the message to the console as INFO. * * @param message "Message" will show up as [INFO][Plan]: Message */ public static void info(String message) { - Plan instance = Plan.getInstance(); - if (instance != null) { - instance.getLogger().info(message); - } - if (!message.contains("[DEBUG]")) { - debug(message); - } + Plan.getInstance().getPluginLogger().info(message); } public static void infoColor(String message) { - ConsoleCommandSender consoleSender = Plan.getInstance().getServer().getConsoleSender(); - consoleSender.sendMessage(Phrase.PREFIX + message); + Plan.getInstance().getPluginLogger().infoColor(message); } /** @@ -46,10 +29,7 @@ public class Log { * @param message "Message" will show up as [ERROR][Plan]: Message */ public static void error(String message) { - Plan instance = Plan.getInstance(); - if (instance != null) { - instance.getLogger().severe(message); - } + Plan.getInstance().getPluginLogger().error(message); } /** @@ -58,16 +38,7 @@ public class Log { * @param message "Message" will show up as [INFO][Plan]: [DEBUG] Message */ public static void debug(String message) { - String debugMode = Settings.DEBUG.toString().toLowerCase(); - boolean both = debugMode.equals("true") || debugMode.equals("both"); - boolean logConsole = Settings.DEBUG.isTrue() || both || debugMode.equals("console"); - boolean logFile = debugMode.equals("file") || both; - if (logConsole) { - info("[DEBUG] " + message); - } - if (logFile) { - toLog(message, DEBUG); - } + Plan.getInstance().getPluginLogger().debug(message); } /** @@ -77,12 +48,7 @@ public class Log { * @param e Throwable, eg NullPointerException */ public static void toLog(String source, Throwable e) { - error(Phrase.ERROR_LOGGED.parse(e.toString())); - toLog(source + " Caught " + e, ERRORS); - for (StackTraceElement x : e.getStackTrace()) { - toLog(" " + x, ERRORS); - } - toLog("", ERRORS); + Plan.getInstance().getPluginLogger().toLog(source, e); } /** @@ -92,9 +58,7 @@ public class Log { * @param e Collection of Throwables, eg NullPointerException */ public static void toLog(String source, Collection e) { - for (Throwable ex : e) { - toLog(source, ex); - } + Plan.getInstance().getPluginLogger().toLog(source, e); } /** @@ -104,34 +68,10 @@ public class Log { * @param filename Name of the file to write to. */ public static void toLog(String message, String filename) { - if (filename.equals(ERRORS)) { - Log.debug(message); - } - Plan plan = Plan.getInstance(); - if (plan == null) { - return; - } - File folder = plan.getDataFolder(); - if (!folder.exists()) { - folder.mkdir(); - } - File log = new File(folder, filename); - try { - if (!log.exists()) { - log.createNewFile(); - } - FileWriter fw = new FileWriter(log, true); - try (PrintWriter pw = new PrintWriter(fw)) { - String timestamp = FormatUtils.formatTimeStampSecond(MiscUtils.getTime()); - pw.println("[" + timestamp + "] " + message); - pw.flush(); - } - } catch (IOException e) { - Log.error("Failed to create" + filename + "file"); - } + Plan.getInstance().getPluginLogger().toLog(message, filename); } public static String getErrorsFilename() { - return ERRORS; + return Plan.getInstance().getPluginLogger().getErrorsFilename(); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 5cac94670..8240d6e69 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -19,6 +19,8 @@ */ package main.java.com.djrapitops.plan; +import com.djrapitops.javaplugin.ColorScheme; +import com.djrapitops.javaplugin.RslPlugin; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -42,7 +44,6 @@ import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; @@ -53,7 +54,7 @@ import org.bukkit.scheduler.BukkitTask; * @author Rsl1122 * @since 1.0.0 */ -public class Plan extends JavaPlugin { +public class Plan extends RslPlugin { private API api; private DataCacheHandler handler; @@ -79,19 +80,25 @@ public class Plan extends JavaPlugin { @Override public void onEnable() { setInstance(this); - getDataFolder().mkdirs(); + super.setDebugMode(Settings.DEBUG.toString()); + super.setColorScheme(new ColorScheme(Phrase.COLOR_MAIN.color(), Phrase.COLOR_SEC.color(), Phrase.COLOR_TER.color())); + super.setLogPrefix("[Plan]"); + super.setUpdateCheckUrl("https://raw.githubusercontent.com/Rsl1122/Plan-PlayerAnalytics/master/Plan/src/main/resources/plugin.yml"); + super.setUpdateUrl("https://www.spigotmc.org/resources/plan-player-analytics.32536/"); + super.onEnableDefaultTasks(); initLocale(); - - Server server = getServer(); + + Server server = getServer(); variable = new ServerVariableHolder(server); - + Log.debug("-------------------------------------"); - Log.debug("Debug log, Server Start: Plan v."+getDescription().getVersion()); - Log.debug("Server: "+server.getBukkitVersion()); - Log.debug("Version: "+server.getVersion()); + Log.debug("Debug log: Plan v." + getDescription().getVersion()); + Log.debug("Implements RslPlugin v." + getRslVersion()); + Log.debug("Server: " + server.getBukkitVersion()); + Log.debug("Version: " + server.getVersion()); Log.debug("-------------------------------------"); - + databases = new HashSet<>(); databases.add(new MySQLDB(this)); databases.add(new SQLiteDB(this)); @@ -100,8 +107,6 @@ public class Plan extends JavaPlugin { getConfig().options().header(Phrase.CONFIG_HEADER + ""); saveConfig(); - Log.info(MiscUtils.checkVersion()); - Log.info(Phrase.DB_INIT + ""); if (initDatabase()) { Log.info(Phrase.DB_ESTABLISHED.parse(db.getConfigName())); @@ -136,13 +141,13 @@ public class Plan extends JavaPlugin { || (Settings.USE_ALTERNATIVE_UI.isTrue())) { Log.infoColor(Phrase.ERROR_NO_DATA_VIEW + ""); } - if (!Settings.SHOW_ALTERNATIVE_IP.isTrue() && server.getIp().isEmpty()) { + if (!Settings.SHOW_ALTERNATIVE_IP.isTrue() && variable.getIp().isEmpty()) { Log.infoColor(Phrase.NOTIFY_EMPTY_IP + ""); } hookHandler = new HookHandler(); - - Log.debug("Verboose debug messages are enabled."); + + Log.debug("Verboose debug messages are enabled."); Log.info(Phrase.ENABLED + ""); } @@ -175,27 +180,27 @@ public class Plan extends JavaPlugin { pluginManager.registerEvents(new PlanPlayerListener(this), this); if (Settings.GATHERCHAT.isTrue()) { - pluginManager.registerEvents(new PlanChatListener(this), this); + registerListener(new PlanChatListener(this)); } else { Log.infoColor(Phrase.NOTIFY_DISABLED_CHATLISTENER + ""); } if (Settings.GATHERGMTIMES.isTrue()) { - pluginManager.registerEvents(new PlanGamemodeChangeListener(this), this); + registerListener(new PlanGamemodeChangeListener(this)); } else { Log.infoColor(Phrase.NOTIFY_DISABLED_GMLISTENER + ""); } if (Settings.GATHERCOMMANDS.isTrue()) { - pluginManager.registerEvents(new PlanCommandPreprocessListener(this), this); + registerListener(new PlanCommandPreprocessListener(this)); } else { Log.infoColor(Phrase.NOTIFY_DISABLED_COMMANDLISTENER + ""); } if (Settings.GATHERKILLS.isTrue()) { - pluginManager.registerEvents(new PlanDeathEventListener(this), this); + registerListener(new PlanDeathEventListener(this)); } else { Log.infoColor(Phrase.NOTIFY_DISABLED_DEATHLISTENER + ""); } if (Settings.GATHERLOCATIONS.isTrue()) { - pluginManager.registerEvents(new PlanPlayerMoveListener(this), this); + registerListener(new PlanPlayerMoveListener(this)); } } @@ -400,32 +405,6 @@ public class Plan extends JavaPlugin { public ServerVariableHolder getVariable() { return variable; } - - /** - * Used to get the current instance of Plan. - * - * Instance is set on the first line of onEnable method. - * - * @return current instance of Plan, Singleton. - * @throws IllegalStateException If onEnable method has not been called and - * the instance is null. - */ - public static Plan getInstance() { - Plan INSTANCE = PlanHolder.INSTANCE; - if (INSTANCE == null) { - throw new IllegalStateException("Plugin not enabled properly, Singleton instance is null."); - } - return INSTANCE; - } - - /** - * Used to set the current instance of Plan. - * - * @param plan The newly enabled Plan instance. - */ - public static void setInstance(Plan plan) { - PlanHolder.INSTANCE = plan; - } /** * Used to get the PlanAPI. @see API @@ -435,15 +414,14 @@ public class Plan extends JavaPlugin { * Plan and the instance is null. */ public static API getPlanAPI() throws IllegalStateException { - Plan INSTANCE = PlanHolder.INSTANCE; + Plan INSTANCE = getInstance(); if (INSTANCE == null) { throw new IllegalStateException("Plugin not enabled properly, Singleton instance is null."); } return INSTANCE.api; } - - private static class PlanHolder { - - private static Plan INSTANCE = null; + + public static Plan getInstance() { + return (Plan) getPluginInstance(); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/ServerVariableHolder.java b/Plan/src/main/java/com/djrapitops/plan/ServerVariableHolder.java index 2f0f79413..8affe7366 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ServerVariableHolder.java +++ b/Plan/src/main/java/com/djrapitops/plan/ServerVariableHolder.java @@ -3,19 +3,27 @@ package main.java.com.djrapitops.plan; import org.bukkit.Server; /** + * Class responsible for holding server variable values that do not change + * without a reload. * * @author Rsl1122 + * @since 3.4.1 */ public class ServerVariableHolder { private int maxPlayers; + private String ip; public ServerVariableHolder(Server server) { maxPlayers = server.getMaxPlayers(); + ip = server.getIp(); } public int getMaxPlayers() { return maxPlayers; } + public String getIp() { + return ip; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/api/API.java b/Plan/src/main/java/com/djrapitops/plan/api/API.java index ae465c0d0..ce14d431e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/api/API.java +++ b/Plan/src/main/java/com/djrapitops/plan/api/API.java @@ -1,5 +1,6 @@ package main.java.com.djrapitops.plan.api; +import com.djrapitops.javaplugin.utilities.UUIDFetcher; import java.sql.SQLException; import java.util.Collection; import java.util.List; @@ -14,7 +15,6 @@ import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo; import main.java.com.djrapitops.plan.ui.DataRequestHandler; import main.java.com.djrapitops.plan.ui.webserver.WebSocketServer; import main.java.com.djrapitops.plan.utilities.HtmlUtils; -import main.java.com.djrapitops.plan.utilities.uuid.UUIDFetcher; import static org.bukkit.Bukkit.getOfflinePlayer; import org.bukkit.OfflinePlayer; diff --git a/Plan/src/main/java/com/djrapitops/plan/command/CommandType.java b/Plan/src/main/java/com/djrapitops/plan/command/CommandType.java deleted file mode 100644 index 925a64178..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/command/CommandType.java +++ /dev/null @@ -1,16 +0,0 @@ -package main.java.com.djrapitops.plan.command; - -/** - * This enum contains different types of commands. - * - * CONSOLE can be used always, PLAYER can only be used as a player, - * CONSOLE_WITH_ARGUMENTS can be used always, except with arguments on console. - * - * @author Rsl1122 - * @since 1.0.0 - */ -public enum CommandType { - CONSOLE, - PLAYER, - CONSOLE_WITH_ARGUMENTS -} diff --git a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java index 5ad0a3aca..b27c1a458 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java @@ -1,17 +1,15 @@ package main.java.com.djrapitops.plan.command; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; +import com.djrapitops.javaplugin.command.TreeCommand; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import main.java.com.djrapitops.plan.Log; -import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.command.commands.*; -import main.java.com.djrapitops.plan.utilities.FormatUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; /** * CommandExecutor for the /plan command, and all subcommands. @@ -19,9 +17,7 @@ import org.bukkit.entity.Player; * @author Rsl1122 * @since 1.0.0 */ -public class PlanCommand implements CommandExecutor { - - private final List commands; +public class PlanCommand extends TreeCommand implements CommandExecutor { /** * CommandExecutor class Constructor. @@ -31,8 +27,19 @@ public class PlanCommand implements CommandExecutor { * @param plugin Current instance of Plan */ public PlanCommand(Plan plugin) { - commands = new ArrayList<>(); - commands.add(new HelpCommand(plugin, this)); + super(plugin, new SubCommand("plan", CommandType.CONSOLE, "") { + @Override + public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { + return true; + } + }, "plan"); + super.setDefaultCommand("inspect"); +// commands.add(new HelpCommand(plugin, this)); + + } + + @Override + public void addCommands() { commands.add(new InspectCommand(plugin)); commands.add(new QuickInspectCommand(plugin)); commands.add(new AnalyzeCommand(plugin)); @@ -42,94 +49,4 @@ public class PlanCommand implements CommandExecutor { commands.add(new ReloadCommand(plugin)); commands.add(new ManageCommand(plugin)); } - - /** - * Used to get the list of all subcommands. - * - * @return Initialized SubCommands - */ - public List getCommands() { - return this.commands; - } - - /** - * Checks SubCommands for matching aliases. - * - * @param name SubCommand in text form that might match alias. - * @return SubCommand, null if no match. - */ - public SubCommand getCommand(String name) { - for (SubCommand command : commands) { - String[] aliases = command.getName().split(","); - - for (String alias : aliases) { - if (alias.equalsIgnoreCase(name)) { - return command; - } - } - } - return null; - } - - private void sendDefaultCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { - String command = "inspect"; - if (args.length < 1) { - command = "help"; - } - onCommand(sender, cmd, commandLabel, FormatUtils.mergeArrays(new String[]{command}, args)); - } - - /** - * Checks if Sender has rights to run the command and executes matching - * subcommand. - * - * @param sender source of the command. - * @param cmd command. - * @param commandLabel label. - * @param args arguments of the command - * @return true - */ - @Override - public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { - Log.debug("Registered command with arguments: "+Arrays.toString(args)); - if (args.length < 1) { - sendDefaultCommand(sender, cmd, commandLabel, args); - return true; - } - - SubCommand command = getCommand(args[0]); - - if (command == null) { - sendDefaultCommand(sender, cmd, commandLabel, args); - return true; - } - - boolean console = !(sender instanceof Player); - - if (!command.getPermission().userHasThisPermission(sender)) { - sender.sendMessage("" + Phrase.COMMAND_NO_PERMISSION); - return true; - } - - if (console && args.length < 2 && command.getCommandType() == CommandType.CONSOLE_WITH_ARGUMENTS) { - sender.sendMessage("" + Phrase.COMMAND_REQUIRES_ARGUMENTS.parse(Phrase.USE_PLAN + "")); - return true; - } - - if (console && command.getCommandType() == CommandType.PLAYER) { - sender.sendMessage("" + Phrase.COMMAND_SENDER_NOT_PLAYER); - - return true; - } - - String[] realArgs = new String[args.length - 1]; - - for (int i = 1; i < args.length; i++) { - realArgs[i - 1] = args[i]; - } - - command.onCommand(sender, cmd, commandLabel, realArgs); - return true; - } - } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/SubCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/SubCommand.java deleted file mode 100644 index 3aabdacbc..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/command/SubCommand.java +++ /dev/null @@ -1,105 +0,0 @@ -package main.java.com.djrapitops.plan.command; - -import main.java.com.djrapitops.plan.Permissions; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -/** - * Abstract subcommand class that stores all the required information of a - * command. - * - * @author Rsl1122 - * @since 1.0.0 - */ -public abstract class SubCommand { - - private final String name; - private final Permissions permission; - private final String usage; - private final CommandType commandType; - private final String arguments; - - /** - * Class constructor, called with super(...) in subcommands. - * - * @param name Name(s) (aliases) of the command - * @param permission Required permission - * @param usage Usage information - * @param commandType Type Enum - * @param arguments Additional possible arguments the command requires - */ - public SubCommand(String name, Permissions permission, String usage, CommandType commandType, String arguments) { - this.name = name; - this.permission = permission; - this.usage = usage; - this.commandType = commandType; - this.arguments = arguments; - } - - /** - * Used to get a string format of required arguments. - * - * @return Additional possible arguments the command requires - */ - public String getArguments() { - return arguments; - } - - /** - * Used to get the first alias. - * - * @return First alias of the command - */ - public String getFirstName() { - return name.split(",")[0]; - } - - /** - * Used to get all aliases. - * - * @return All aliases separated with ',' - */ - public String getName() { - return name; - } - - /** - * Used to get the permission required by the command. - * - * @return Required permission - */ - public Permissions getPermission() { - return permission; - } - - /** - * Used to get the info about usage of the command. - * - * @return Usage information - */ - public String getUsage() { - return usage; - } - - /** - * Used to get the command type. - * - * @return CommandType Enum. - */ - public CommandType getCommandType() { - return commandType; - } - - /** - * The Command Execution method. - * - * @param sender Parameter of onCommand in CommandExecutor. - * @param cmd Parameter of onCommand in CommandExecutor. - * @param commandLabel Parameter of onCommand in CommandExecutor. - * @param args Parameter of onCommand in CommandExecutor. - * @return Was the execution successful? - * @see CommandExecutor - */ - public abstract boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args); -} diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java index c9b50b8eb..c8d70c513 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java @@ -1,13 +1,13 @@ package main.java.com.djrapitops.plan.command.commands; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Settings; -import main.java.com.djrapitops.plan.command.CommandType; import main.java.com.djrapitops.plan.command.CommandUtils; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.data.cache.AnalysisCacheHandler; import main.java.com.djrapitops.plan.ui.TextUI; import main.java.com.djrapitops.plan.utilities.HtmlUtils; @@ -27,8 +27,8 @@ import org.bukkit.scheduler.BukkitTask; */ public class AnalyzeCommand extends SubCommand { - private Plan plugin; - private AnalysisCacheHandler analysisCache; + private final Plan plugin; + private final AnalysisCacheHandler analysisCache; /** * Subcommand Constructor. @@ -36,7 +36,7 @@ public class AnalyzeCommand extends SubCommand { * @param plugin Current instance of Plan */ public AnalyzeCommand(Plan plugin) { - super("analyze, analyse, analysis", Permissions.ANALYZE, Phrase.CMD_USG_ANALYZE.parse(), CommandType.CONSOLE, ""); + super("analyze, analyse, analysis", CommandType.CONSOLE, Permissions.ANALYZE.getPermission(), Phrase.CMD_USG_ANALYZE.parse()); this.plugin = plugin; analysisCache = plugin.getAnalysisCache(); } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/HelpCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/HelpCommand.java deleted file mode 100644 index 787f7d941..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/HelpCommand.java +++ /dev/null @@ -1,55 +0,0 @@ -package main.java.com.djrapitops.plan.command.commands; - -import main.java.com.djrapitops.plan.Permissions; -import main.java.com.djrapitops.plan.Phrase; -import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.PlanCommand; -import main.java.com.djrapitops.plan.command.SubCommand; -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -/** - * This subcommand is used to view the subcommands. - * - * @author Rsl1122 - * @since 1.0.0 - */ -public class HelpCommand extends SubCommand { - - private final Plan plugin; - private final PlanCommand command; - - /** - * Subcommand Constructor. - * - * @param plugin Current instance of Plan - * @param command Current instance of PlanCommand - */ - public HelpCommand(Plan plugin, PlanCommand command) { - super("help,?", Permissions.HELP, Phrase.CMD_USG_HELP + "", CommandType.CONSOLE, ""); - - this.plugin = plugin; - this.command = command; - } - - @Override - public boolean onCommand(CommandSender sender, Command comd, String commandLabel, String[] args) { - boolean isConsole = !(sender instanceof Player); - ChatColor oColor = Phrase.COLOR_MAIN.color(); - ChatColor tColor = Phrase.COLOR_SEC.color(); - - sender.sendMessage(Phrase.CMD_HELP_HEADER + ""); - - this.command.getCommands().stream() - .filter(cmd -> !cmd.getName().equalsIgnoreCase(getName())) - .filter(cmd -> cmd.getPermission().userHasThisPermission(sender)) - .filter(cmd -> !(isConsole && cmd.getCommandType() == CommandType.PLAYER)) - .map(cmd -> tColor + " " + Phrase.BALL.toString() + oColor + " /plan " + cmd.getFirstName() + " " + cmd.getArguments() + tColor + " - " + cmd.getUsage()) - .forEach(msg -> sender.sendMessage(msg)); - sender.sendMessage(Phrase.CMD_FOOTER + ""); - return true; - } -} diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/InfoCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/InfoCommand.java index 5791de27f..bbcaaf19d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/InfoCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/InfoCommand.java @@ -1,11 +1,11 @@ package main.java.com.djrapitops.plan.command.commands; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; +import com.djrapitops.javaplugin.utilities.VersionUtils; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; -import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -18,7 +18,7 @@ import org.bukkit.command.CommandSender; */ public class InfoCommand extends SubCommand { - private Plan plugin; + private final Plan plugin; /** * Subcommand Constructor. @@ -26,7 +26,7 @@ public class InfoCommand extends SubCommand { * @param plugin Current instance of Plan */ public InfoCommand(Plan plugin) { - super("info", Permissions.INFO, Phrase.CMD_USG_INFO + "", CommandType.CONSOLE, ""); + super("info", CommandType.CONSOLE,Permissions.INFO.getPermission(), Phrase.CMD_USG_INFO + ""); this.plugin = plugin; } @@ -38,7 +38,7 @@ public class InfoCommand extends SubCommand { String[] messages = { Phrase.CMD_INFO_HEADER + "", Phrase.CMD_INFO_VERSION.parse(plugin.getDescription().getVersion()), - Phrase.CMD_BALL.toString() + tColor + " " + MiscUtils.checkVersion(), + Phrase.CMD_BALL.toString() + tColor + " " + VersionUtils.checkVersion(plugin), Phrase.CMD_MANAGE_STATUS_ACTIVE_DB.parse(plugin.getDB().getConfigName()), Phrase.CMD_FOOTER + "" }; diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java index c5e87fbb6..57465bc5b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.command.commands; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import main.java.com.djrapitops.plan.command.CommandUtils; import java.util.UUID; import main.java.com.djrapitops.plan.Log; @@ -7,9 +9,7 @@ import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Settings; -import main.java.com.djrapitops.plan.command.CommandType; import main.java.com.djrapitops.plan.command.Condition; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler; import main.java.com.djrapitops.plan.ui.TextUI; import main.java.com.djrapitops.plan.utilities.HtmlUtils; @@ -30,8 +30,8 @@ import org.bukkit.scheduler.BukkitTask; */ public class InspectCommand extends SubCommand { - private Plan plugin; - private InspectCacheHandler inspectCache; + private final Plan plugin; + private final InspectCacheHandler inspectCache; /** * Class Constructor. @@ -39,7 +39,7 @@ public class InspectCommand extends SubCommand { * @param plugin Current instance of Plan */ public InspectCommand(Plan plugin) { - super("inspect", Permissions.INSPECT, Phrase.CMD_USG_INSPECT + "", CommandType.CONSOLE_WITH_ARGUMENTS, Phrase.ARG_PLAYER + ""); + super("inspect", CommandType.CONSOLE_WITH_ARGUMENTS, Permissions.INSPECT.getPermission(), Phrase.CMD_USG_INSPECT + "", Phrase.ARG_PLAYER + ""); this.plugin = plugin; inspectCache = plugin.getInspectCache(); @@ -75,7 +75,7 @@ public class InspectCommand extends SubCommand { private int timesrun = 0; @Override - public void run() { + public void run() { timesrun++; if (inspectCache.isCached(uuid)) { sendInspectMsg(sender, playerName, uuid); @@ -85,7 +85,7 @@ public class InspectCommand extends SubCommand { if (timesrun > 10) { Log.debug("Command Timeout Message, Inspect."); sender.sendMessage(Phrase.COMMAND_TIMEOUT.parse("Inspect")); - this.cancel(); + this.cancel(); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java index b19c650e0..787636334 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java @@ -1,17 +1,14 @@ package main.java.com.djrapitops.plan.command.commands; -import java.util.ArrayList; -import java.util.List; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; +import com.djrapitops.javaplugin.command.TreeCommand; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.command.commands.manage.*; -import main.java.com.djrapitops.plan.utilities.FormatUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; /** * This command is used to manage the database of the plugin. @@ -21,10 +18,7 @@ import org.bukkit.entity.Player; * @author Rsl1122 * @since 2.3.0 */ -public class ManageCommand extends SubCommand { - - private final List commands; - private Plan plugin; +public class ManageCommand extends TreeCommand { /** * Subcommand Constructor. @@ -32,10 +26,16 @@ public class ManageCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageCommand(Plan plugin) { - super("manage, m", Permissions.MANAGE, Phrase.CMD_USG_MANAGE + "", CommandType.CONSOLE, ""); - this.plugin = plugin; - commands = new ArrayList<>(); - commands.add(new ManageHelpCommand(plugin, this)); + super(plugin, new SubCommand("manage,m", CommandType.CONSOLE, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE + "") { + @Override + public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { + return true; + } + }, "plan manage"); + } + + @Override + public void addCommands() { commands.add(new ManageMoveCommand(plugin)); commands.add(new ManageHotswapCommand(plugin)); commands.add(new ManageBackupCommand(plugin)); @@ -45,81 +45,4 @@ public class ManageCommand extends SubCommand { commands.add(new ManageRemoveCommand(plugin)); commands.add(new ManageClearCommand(plugin)); } - - /** - * Used to get the list of manage subcommands. - * - * @return Initialized SubCommands - */ - public List getCommands() { - return this.commands; - } - - /** - * Checks SubCommands for matching aliases. - * - * @param name SubCommand in text form that might match alias. - * @return SubCommand, null if no match. - */ - public SubCommand getCommand(String name) { - for (SubCommand command : commands) { - String[] aliases = command.getName().split(","); - - for (String alias : aliases) { - if (alias.equalsIgnoreCase(name)) { - return command; - } - } - } - return null; - } - - private void sendDefaultCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { - String command = "help"; - onCommand(sender, cmd, commandLabel, FormatUtils.mergeArrays(new String[]{command}, args)); - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { - if (args.length < 1) { - sendDefaultCommand(sender, cmd, commandLabel, args); - return true; - } - - SubCommand command = getCommand(args[0]); - - if (command == null) { - sendDefaultCommand(sender, cmd, commandLabel, args); - return true; - } - - boolean console = !(sender instanceof Player); - - if (!command.getPermission().userHasThisPermission(sender)) { - sender.sendMessage("" + Phrase.COMMAND_NO_PERMISSION); - return true; - } - - if (console && args.length < 2 && command.getCommandType() == CommandType.CONSOLE_WITH_ARGUMENTS) { - sender.sendMessage("" + Phrase.COMMAND_REQUIRES_ARGUMENTS.parse(Phrase.USE_MANAGE + "")); - - return true; - } - - if (console && command.getCommandType() == CommandType.PLAYER) { - sender.sendMessage("" + Phrase.COMMAND_SENDER_NOT_PLAYER); - - return true; - } - - String[] realArgs = new String[args.length - 1]; - - for (int i = 1; i < args.length; i++) { - realArgs[i - 1] = args[i]; - } - - command.onCommand(sender, cmd, commandLabel, realArgs); - return true; - } - } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java index 7fede306c..734f2e567 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java @@ -1,11 +1,11 @@ package main.java.com.djrapitops.plan.command.commands; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.data.cache.AnalysisCacheHandler; import main.java.com.djrapitops.plan.ui.TextUI; import main.java.com.djrapitops.plan.utilities.MiscUtils; @@ -23,8 +23,8 @@ import org.bukkit.scheduler.BukkitTask; */ public class QuickAnalyzeCommand extends SubCommand { - private Plan plugin; - private AnalysisCacheHandler analysisCache; + private final Plan plugin; + private final AnalysisCacheHandler analysisCache; /** * Subcommand Constructor. @@ -32,7 +32,7 @@ public class QuickAnalyzeCommand extends SubCommand { * @param plugin Current instance of Plan */ public QuickAnalyzeCommand(Plan plugin) { - super("qanalyze, qanalyse, qanalysis", Permissions.QUICK_ANALYZE, Phrase.CMD_USG_QANALYZE.parse(), CommandType.CONSOLE, ""); + super("qanalyze, qanalyse, qanalysis", CommandType.CONSOLE, Permissions.QUICK_ANALYZE.getPermission(), Phrase.CMD_USG_QANALYZE.parse()); this.plugin = plugin; analysisCache = plugin.getAnalysisCache(); } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java index a68d2fde6..6f4299a3e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java @@ -1,14 +1,14 @@ package main.java.com.djrapitops.plan.command.commands; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.util.UUID; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; import main.java.com.djrapitops.plan.command.CommandUtils; import main.java.com.djrapitops.plan.command.Condition; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler; import main.java.com.djrapitops.plan.ui.TextUI; import main.java.com.djrapitops.plan.utilities.MiscUtils; @@ -26,8 +26,8 @@ import org.bukkit.scheduler.BukkitTask; */ public class QuickInspectCommand extends SubCommand { - private Plan plugin; - private InspectCacheHandler inspectCache; + private final Plan plugin; + private final InspectCacheHandler inspectCache; /** * Class Constructor. @@ -35,7 +35,7 @@ public class QuickInspectCommand extends SubCommand { * @param plugin Current instance of Plan */ public QuickInspectCommand(Plan plugin) { - super("qinspect", Permissions.QUICK_INSPECT, Phrase.CMD_USG_QINSPECT + "", CommandType.CONSOLE_WITH_ARGUMENTS, Phrase.ARG_PLAYER + ""); + super("qinspect", CommandType.CONSOLE_WITH_ARGUMENTS, Permissions.QUICK_INSPECT.getPermission(), Phrase.CMD_USG_QINSPECT + "", Phrase.ARG_PLAYER + ""); this.plugin = plugin; inspectCache = plugin.getInspectCache(); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/ReloadCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/ReloadCommand.java index f98416f7d..ecf950e03 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/ReloadCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/ReloadCommand.java @@ -1,10 +1,10 @@ package main.java.com.djrapitops.plan.command.commands; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -16,7 +16,7 @@ import org.bukkit.command.CommandSender; */ public class ReloadCommand extends SubCommand { - private Plan plugin; + private final Plan plugin; /** * Subcommand constructor. @@ -24,7 +24,7 @@ public class ReloadCommand extends SubCommand { * @param plugin Current instance of Plan */ public ReloadCommand(Plan plugin) { - super("reload", Permissions.MANAGE, Phrase.CMD_USG_RELOAD + "", CommandType.CONSOLE, ""); + super("reload", CommandType.CONSOLE, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_RELOAD + ""); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java index ee3ab0483..aead7ff87 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.command.commands; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -7,10 +9,8 @@ import java.util.UUID; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; import main.java.com.djrapitops.plan.command.CommandUtils; import main.java.com.djrapitops.plan.command.Condition; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler; import main.java.com.djrapitops.plan.utilities.HtmlUtils; import main.java.com.djrapitops.plan.utilities.MiscUtils; @@ -39,7 +39,7 @@ public class SearchCommand extends SubCommand { * @param plugin Current instance of Plan */ public SearchCommand(Plan plugin) { - super("search", Permissions.SEARCH, Phrase.CMD_USG_SEARCH + "", CommandType.CONSOLE_WITH_ARGUMENTS, Phrase.ARG_SEARCH + ""); + super("search", CommandType.CONSOLE_WITH_ARGUMENTS, Permissions.SEARCH.getPermission(), Phrase.CMD_USG_SEARCH + "", Phrase.ARG_SEARCH + ""); this.plugin = plugin; inspectCache = plugin.getInspectCache(); } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java index 43d06639a..c6a317ce4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java @@ -1,11 +1,11 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.utilities.ManageUtils; import org.bukkit.command.Command; @@ -28,7 +28,7 @@ public class ManageBackupCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageBackupCommand(Plan plugin) { - super("backup", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_BACKUP + "", CommandType.CONSOLE, ""); + super("backup", CommandType.CONSOLE, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_BACKUP + "", ""); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java index fcf8e13c4..6f6cca4c6 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java @@ -1,13 +1,13 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.sql.SQLException; import java.util.Arrays; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.database.Database; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -29,7 +29,7 @@ public class ManageClearCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageClearCommand(Plan plugin) { - super("clear", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_CLEAR + "", CommandType.CONSOLE_WITH_ARGUMENTS, " [-a]"); + super("clear", CommandType.CONSOLE_WITH_ARGUMENTS, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_CLEAR + "", " [-a]"); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHelpCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHelpCommand.java deleted file mode 100644 index ef7a3ef97..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHelpCommand.java +++ /dev/null @@ -1,69 +0,0 @@ -package main.java.com.djrapitops.plan.command.commands.manage; - -import main.java.com.djrapitops.plan.Permissions; -import main.java.com.djrapitops.plan.Phrase; -import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; -import main.java.com.djrapitops.plan.command.commands.ManageCommand; -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -/** - * This manage subcommand is used to view all other manage subcommands. - * - * @author Rsl1122 - * @since 2.3.0 - */ -public class ManageHelpCommand extends SubCommand { - - private final Plan plugin; - private final ManageCommand command; - - /** - * Subcommand Constructor. - * - * @param plugin Current instance of Plan - * @param command Current instance of PlanCommand - */ - public ManageHelpCommand(Plan plugin, ManageCommand command) { - super("help,?", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_HELP + "", CommandType.CONSOLE, ""); - - this.plugin = plugin; - this.command = command; - } - - @Override - public boolean onCommand(CommandSender sender, Command c, String commandLabel, String[] args) { - - ChatColor oColor = Phrase.COLOR_MAIN.color(); - ChatColor tColor = Phrase.COLOR_SEC.color(); - ChatColor hColor = Phrase.COLOR_TER.color(); - - // Header - sender.sendMessage(Phrase.CMD_MANAGE_HELP_HEADER + ""); - // Help results - for (SubCommand cmd : this.command.getCommands()) { - if (cmd.getName().equalsIgnoreCase(getName())) { - continue; - } - - if (!cmd.getPermission().userHasThisPermission(sender)) { - continue; - } - - if (!(sender instanceof Player) && cmd.getCommandType() == CommandType.PLAYER) { - continue; - } - - sender.sendMessage(tColor + " " + Phrase.BALL.toString() + oColor - + " /plan manage " + cmd.getFirstName() + " " + cmd.getArguments() + tColor + " - " + cmd.getUsage()); - } - // Footer - sender.sendMessage(hColor + Phrase.ARROWS_RIGHT.toString()); - return true; - } - -} diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java index d9306673e..f5fc972c1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java @@ -1,12 +1,12 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.sql.SQLException; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.database.Database; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -20,7 +20,7 @@ import org.bukkit.command.CommandSender; */ public class ManageHotswapCommand extends SubCommand { - private Plan plugin; + private final Plan plugin; /** * Class Constructor. @@ -28,7 +28,7 @@ public class ManageHotswapCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageHotswapCommand(Plan plugin) { - super("hotswap", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_HOTSWAP + "", CommandType.CONSOLE_WITH_ARGUMENTS, ""); + super("hotswap", CommandType.CONSOLE_WITH_ARGUMENTS, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_HOTSWAP + "", ""); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java index 503e11b3a..d41edf1a7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -8,8 +10,6 @@ import java.util.stream.Collectors; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.data.handling.importing.ImportUtils; import main.java.com.djrapitops.plan.data.handling.importing.Importer; import static org.bukkit.Bukkit.getOfflinePlayers; @@ -36,7 +36,7 @@ public class ManageImportCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageImportCommand(Plan plugin) { - super("import", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_IMPORT + "", CommandType.CONSOLE, Phrase.ARG_IMPORT + ""); + super("import", CommandType.CONSOLE, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_IMPORT + "", Phrase.ARG_IMPORT + ""); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java index 1d165286a..53c516b89 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.util.Arrays; import java.util.Collection; import java.util.UUID; @@ -7,8 +9,6 @@ import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.utilities.ManageUtils; import org.bukkit.command.Command; @@ -17,9 +17,9 @@ import org.bukkit.scheduler.BukkitRunnable; /** * This manage subcommand is used to move all data from one database to another. - * + * * Destination database will be cleared. - * + * * @author Rsl1122 * @since 2.3.0 */ @@ -33,11 +33,11 @@ public class ManageMoveCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageMoveCommand(Plan plugin) { - super("move", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_MOVE + "", CommandType.CONSOLE_WITH_ARGUMENTS, Phrase.ARG_MOVE + ""); + super("move", CommandType.CONSOLE_WITH_ARGUMENTS, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_MOVE + "", Phrase.ARG_MOVE + ""); this.plugin = plugin; } - + @Override public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (args.length < 2) { diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java index 220995a77..329d46a27 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.sql.SQLException; import java.util.Arrays; import java.util.UUID; @@ -7,8 +9,6 @@ import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility; import org.bukkit.command.Command; @@ -31,7 +31,7 @@ public class ManageRemoveCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageRemoveCommand(Plan plugin) { - super("remove", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_REMOVE + "", CommandType.CONSOLE_WITH_ARGUMENTS, Phrase.ARG_PLAYER + " [-a]"); + super("remove", CommandType.CONSOLE_WITH_ARGUMENTS, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_REMOVE + "", Phrase.ARG_PLAYER + " [-a]"); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java index aa7dad922..67eb155d3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java @@ -1,5 +1,7 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import java.io.File; import java.util.Arrays; import java.util.Collection; @@ -8,8 +10,6 @@ import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.utilities.ManageUtils; @@ -34,7 +34,7 @@ public class ManageRestoreCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageRestoreCommand(Plan plugin) { - super("restore", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_RESTORE + "", CommandType.CONSOLE, Phrase.ARG_RESTORE + ""); + super("restore", CommandType.CONSOLE, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_RESTORE + "", Phrase.ARG_RESTORE + ""); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageStatusCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageStatusCommand.java index 1e304be3f..9b1b31ed9 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageStatusCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageStatusCommand.java @@ -1,10 +1,10 @@ package main.java.com.djrapitops.plan.command.commands.manage; +import com.djrapitops.javaplugin.command.CommandType; +import com.djrapitops.javaplugin.command.SubCommand; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; -import main.java.com.djrapitops.plan.command.CommandType; -import main.java.com.djrapitops.plan.command.SubCommand; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -23,7 +23,7 @@ public class ManageStatusCommand extends SubCommand { * @param plugin Current instance of Plan */ public ManageStatusCommand(Plan plugin) { - super("status", Permissions.MANAGE, Phrase.CMD_USG_MANAGE_STATUS + "", CommandType.CONSOLE, ""); + super("status", CommandType.CONSOLE, Permissions.MANAGE.getPermission(), Phrase.CMD_USG_MANAGE_STATUS + ""); this.plugin = plugin; } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/Container.java b/Plan/src/main/java/com/djrapitops/plan/database/Container.java new file mode 100644 index 000000000..c4bda27cf --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/database/Container.java @@ -0,0 +1,30 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package main.java.com.djrapitops.plan.database; + +/** + * + * @author Rsl1122 + * @since 3.4.3 + * @param + */ +public class Container { + private T object; + private int id; + + public Container(T object, int id) { + this.object = object; + this.id = id; + } + + public T getObject() { + return object; + } + + public int getId() { + return id; + } +} diff --git a/Plan/src/main/java/com/djrapitops/plan/database/DBUtils.java b/Plan/src/main/java/com/djrapitops/plan/database/DBUtils.java new file mode 100644 index 000000000..4f6cad67c --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/database/DBUtils.java @@ -0,0 +1,40 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package main.java.com.djrapitops.plan.database; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +/** + * + * @author Rsl1122 + * @since 3.4.3 + */ +public class DBUtils { + + public static List>> splitIntoBatches(Map> objects) { + List>> wrappedBatches = new ArrayList<>(); + + int i = 0; + int j = 0; + + for (Entry> entry : objects.entrySet()) { + for (T object : entry.getValue()) { + if (wrappedBatches.size() - 1 <= j) { + wrappedBatches.add(new ArrayList<>()); + } + wrappedBatches.get(j).add(new Container<>(object, entry.getKey())); + i++; + if (i % 1024 == 0) { + j++; + } + } + } + return wrappedBatches; + } +} diff --git a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java index 61cf46be9..58c4bf99e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -21,6 +22,8 @@ import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.tables.*; import main.java.com.djrapitops.plan.utilities.Benchmark; import main.java.com.djrapitops.plan.utilities.FormatUtils; +import main.java.com.djrapitops.plan.utilities.ManageUtils; +import main.java.com.djrapitops.plan.utilities.analysis.MathUtils; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.scheduler.BukkitRunnable; @@ -151,6 +154,7 @@ public abstract class SQLDB extends Database { @Override public void run() { try { + clean(); Benchmark.start("Convert Bukkitdata to DB data"); Set uuids = usersTable.getSavedUUIDs(); uuids.removeAll(usersTable.getContainsBukkitData(uuids)); @@ -465,8 +469,45 @@ public abstract class SQLDB extends Database { */ @Override public void clean() { + Log.info("Cleaning the database."); try { checkConnection(); + Map> allSessions = sessionsTable.getSessionData(usersTable.getAllUserIds().values()); + Benchmark.start("Combine Sessions"); + int before = MathUtils.sumInt(allSessions.values().stream().map(l -> l.size())); + Log.debug("Sessions before: " + before); + Map beforeM = new HashMap<>(); + Map afterM = new HashMap<>(); + for (Integer id : allSessions.keySet()) { + List sessions = allSessions.get(id); + beforeM.put(id, sessions.size()); + if (sessions.isEmpty()) { + afterM.put(id, 0); + continue; + } + List combined = ManageUtils.combineSessions(sessions); + afterM.put(id, combined.size()); + allSessions.put(id, combined); + } + int after = MathUtils.sumInt(allSessions.values().stream().map(l -> l.size())); + Log.debug("Sessions after: " + after); + if (before - after > 50) { + Benchmark.start("Save combined sessions"); + Iterator iterator = new HashSet<>(allSessions.keySet()).iterator(); + while (iterator.hasNext()) { + int id = iterator.next(); + if (afterM.get(id) < beforeM.get(id)) { + sessionsTable.removeUserSessions(id); + } else { + allSessions.remove(id); + } + } + sessionsTable.saveSessionData(allSessions); + Benchmark.stop("Save combined sessions"); + } + Benchmark.stop("Combine Sessions"); + Log.info("Combined " + (before - after) + " sessions."); + Log.info("Clean complete."); } catch (SQLException e) { Log.toLog(this.getClass().getName(), e); } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java index 10e9ddc23..918bd52c4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/SessionsTable.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.data.SessionData; +import main.java.com.djrapitops.plan.database.Container; import main.java.com.djrapitops.plan.database.databases.SQLDB; import main.java.com.djrapitops.plan.utilities.Benchmark; @@ -200,6 +201,25 @@ public class SessionsTable extends Table { } Benchmark.start("Save Sessions multiple " + sessions.size()); Map> saved = getSessionData(sessions.keySet()); + for (Integer id : sessions.keySet()) { + List sessionList = sessions.get(id); + List s = saved.get(id); + if (s != null) { + sessionList.removeAll(s); + } + if (sessionList.isEmpty()) { + continue; + } + saved.put(id, sessionList); + } + List>> batches = splitIntoBatches(sessions); + for (List> batch : batches) { + saveSessionBatch(batch); + } + Benchmark.stop("Save Sessions multiple " + sessions.size()); + } + + private void saveSessionBatch(List> batch) throws SQLException { PreparedStatement statement = null; try { statement = prepareStatement("INSERT INTO " + tableName + " (" @@ -210,34 +230,23 @@ public class SessionsTable extends Table { boolean commitRequired = false; int i = 0; - for (Integer id : sessions.keySet()) { - List sessionList = sessions.get(id); - List s = saved.get(id); - if (s != null) { - sessionList.removeAll(s); - } - if (sessionList.isEmpty()) { + for (Container data : batch) { + SessionData session = data.getObject(); + int id = data.getId(); + if (!session.isValid()) { continue; } - for (SessionData session : sessionList) { - long end = session.getSessionEnd(); - long start = session.getSessionStart(); - if (end < start) { - continue; - } - statement.setInt(1, id); - statement.setLong(2, start); - statement.setLong(3, end); - statement.addBatch(); - commitRequired = true; - i++; - } + statement.setInt(1, id); + statement.setLong(2, session.getSessionStart()); + statement.setLong(3, session.getSessionEnd()); + statement.addBatch(); + commitRequired = true; + i++; } if (commitRequired) { - Log.debug("Executing session batch: "+i); - statement.executeBatch(); + Log.debug("Executing session batch: " + i); + statement.executeBatch(); } - Benchmark.stop("Save Sessions multiple " + sessions.size()); } finally { close(statement); } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/Table.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/Table.java index 7f89063e6..1d040a11c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/Table.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/Table.java @@ -3,7 +3,11 @@ package main.java.com.djrapitops.plan.database.tables; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.List; +import java.util.Map; import main.java.com.djrapitops.plan.Log; +import main.java.com.djrapitops.plan.database.Container; +import main.java.com.djrapitops.plan.database.DBUtils; import main.java.com.djrapitops.plan.database.databases.SQLDB; /** @@ -123,4 +127,8 @@ public abstract class Table { return false; } } + + protected List>> splitIntoBatches(Map> objects) { + return DBUtils.splitIntoBatches(objects); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java index 4a708472c..24890a74b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/UsersTable.java @@ -874,4 +874,9 @@ public class UsersTable extends Table { close(statement); } } + + public Map getLoginTimes(Collection uuids) { + //TODO + return new HashMap<>(); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/Benchmark.java b/Plan/src/main/java/com/djrapitops/plan/utilities/Benchmark.java index a766efa96..4b9bb2b17 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/Benchmark.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/Benchmark.java @@ -1,23 +1,20 @@ package main.java.com.djrapitops.plan.utilities; -import java.util.HashMap; -import java.util.Map; +import com.djrapitops.javaplugin.utilities.BenchmarkUtil; import main.java.com.djrapitops.plan.Log; /** * - * @author Risto + * @author Rsl1122 */ public class Benchmark { - private static Map starts = new HashMap<>(); - /** * * @param source */ public static void start(String source) { - starts.put(source, System.nanoTime()); + BenchmarkUtil.start(source); Log.debug(source); } @@ -27,13 +24,10 @@ public class Benchmark { * @return */ public static long stop(String source) { - Long s = starts.get(source); - if (s != null) { - long ms = (System.nanoTime() - s) / 1000000; + long ms = BenchmarkUtil.stop(source); + if (ms != -1) { Log.debug(source + " took " + ms+" ms"); - starts.remove(source); - return ms; } - return -1; + return ms; } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java index 4a8e70258..ba9a96256 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/FormatUtils.java @@ -1,7 +1,7 @@ package main.java.com.djrapitops.plan.utilities; +import com.djrapitops.javaplugin.utilities.FormattingUtils; import java.text.DecimalFormat; -import java.util.Date; import main.java.com.djrapitops.plan.Settings; import org.bukkit.Location; @@ -36,30 +36,15 @@ public class FormatUtils { * @return */ public static String formatTimeStamp(long epochMs) { - Date date = new Date(epochMs); - String timeStamp = date.toString(); - // "EEE MMM dd HH:mm:ss zzz yyyy" - // "0123456789012345678901234567" - String day = timeStamp.substring(4, 10); - String clock = timeStamp.substring(11, 16); - return day + ", " + clock; + return FormattingUtils.formatTimeStamp(epochMs); } public static String formatTimeStampSecond(long epochMs) { - Date date = new Date(epochMs); - String timeStamp = date.toString(); - String day = timeStamp.substring(4, 10); - String clock = timeStamp.substring(11, 19); - return day + ", " + clock; + return FormattingUtils.formatTimeStampSecond(epochMs); } public static String formatTimeStampYear(long epochMs) { - Date date = new Date(epochMs); - String timeStamp = date.toString(); - String year = timeStamp.substring(24); - String day = timeStamp.substring(4, 10); - String clock = timeStamp.substring(11, 16); - return day + " " + year + ", " + clock; + return FormattingUtils.formatTimeStampYear(epochMs); } /** @@ -69,7 +54,7 @@ public class FormatUtils { * @return */ public static String removeLetters(String dataPoint) { - return dataPoint.replaceAll("[^\\d.]", ""); + return FormattingUtils.removeLetters(dataPoint); } /** @@ -78,11 +63,7 @@ public class FormatUtils { * @return */ public static String removeNumbers(String dataPoint) { - for (char c : removeLetters(dataPoint).toCharArray()) { - dataPoint = dataPoint.replace(c + "", ""); - } - dataPoint = dataPoint.replace(" ", ""); - return dataPoint; + return FormattingUtils.removeNumbers(dataPoint); } // Formats long in milliseconds into d:h:m:s string @@ -114,21 +95,21 @@ public class FormatUtils { } if (hours != 0) { String h = Settings.FORMAT_HOURS.toString().replace("%hours%", "" + hours); - if (h.contains("%zero%") && (hours+"").length() == 1) { + if (h.contains("%zero%") && (hours + "").length() == 1) { builder.append('0'); } builder.append(h); } if (minutes != 0) { String m = Settings.FORMAT_MINUTES.toString().replace("%minutes%", "" + minutes); - if (m.contains("%zero%") && (minutes+"").length() == 1) { + if (m.contains("%zero%") && (minutes + "").length() == 1) { builder.append('0'); } - builder.append(m); + builder.append(m); } - if (seconds != 0) { + if (seconds != 0) { String s = Settings.FORMAT_SECONDS.toString().replace("%seconds%", "" + seconds); - if (s.contains("%zero%") && (seconds+"").length() == 1) { + if (s.contains("%zero%") && (seconds + "").length() == 1) { builder.append('0'); } builder.append(s); @@ -148,15 +129,7 @@ public class FormatUtils { * @throws NumberFormatException When wrong format */ public static int parseVersionNumber(String versionString) throws NumberFormatException { - String[] versionArray = versionString.split("\\."); - if (versionArray.length != 3) { - throw new NumberFormatException("Wrong format used"); - } - int main = Integer.parseInt(versionArray[0]) * 10000; - int major = Integer.parseInt(versionArray[1]) * 100; - int minor = Integer.parseInt(versionArray[2]); - int versionNumber = main + major + minor; - return versionNumber; + return FormattingUtils.parseVersionNumber(versionString); } /** @@ -166,18 +139,7 @@ public class FormatUtils { * @return One array with contents of the multiple */ public static String[] mergeArrays(String[]... arrays) { - int arraySize = 0; - for (String[] array : arrays) { - arraySize += array.length; - } - String[] result = new String[arraySize]; - int j = 0; - for (String[] array : arrays) { - for (String string : array) { - result[j++] = string; - } - } - return result; + return FormattingUtils.mergeArrays(arrays); } /** @@ -187,7 +149,7 @@ public class FormatUtils { * @return Readable location format. */ public static String formatLocation(Location loc) { - return "x " + loc.getBlockX() + " z " + loc.getBlockZ() + " in " + loc.getWorld(); + return FormattingUtils.formatLocation(loc); } /** diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java index 8c3c0d9f0..b4a47587f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java @@ -50,7 +50,7 @@ public class HtmlUtils { } public static String getServerAnalysisUrlWithProtocol() { - return Settings.LINK_PROTOCOL.toString()+getServerAnalysisUrl(); + return Settings.LINK_PROTOCOL.toString()+":"+getServerAnalysisUrl(); } /** @@ -59,7 +59,7 @@ public class HtmlUtils { */ public static String getServerAnalysisUrl() { int port = Settings.WEBSERVER_PORT.getNumber(); - String ip = Plan.getInstance().getServer().getIp() + ":" + port; + String ip = Plan.getInstance().getVariable().getIp() + ":" + port; String securityCode = Settings.SECURITY_CODE.toString(); boolean useAlternativeIP = Settings.SHOW_ALTERNATIVE_IP.isTrue(); if (useAlternativeIP) { @@ -70,7 +70,7 @@ public class HtmlUtils { } public static String getInspectUrlWithProtocol(String playerName) { - return Settings.LINK_PROTOCOL.toString()+getInspectUrl(playerName); + return Settings.LINK_PROTOCOL.toString()+":"+getInspectUrl(playerName); } /** @@ -80,7 +80,7 @@ public class HtmlUtils { */ public static String getInspectUrl(String playerName) { int port = Settings.WEBSERVER_PORT.getNumber(); - String ip = Plan.getInstance().getServer().getIp() + ":" + port; + String ip = Plan.getInstance().getVariable().getIp() + ":" + port; String securityCode = Settings.SECURITY_CODE.toString(); boolean useAlternativeIP = Settings.SHOW_ALTERNATIVE_IP.isTrue(); if (useAlternativeIP) { diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java index 3b6913932..20212cb16 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java @@ -1,17 +1,22 @@ package main.java.com.djrapitops.plan.utilities; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.data.SessionData; import main.java.com.djrapitops.plan.data.UserData; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.databases.SQLiteDB; +import main.java.com.djrapitops.plan.utilities.analysis.MathUtils; /** * @@ -69,11 +74,45 @@ public class ManageUtils { clearAndCopyToDB.removeAllData(); List allUserData = copyFromDB.getUserDataForUUIDS(copyFromDB.getSavedUUIDs()); clearAndCopyToDB.saveMultipleUserData(allUserData); - clearAndCopyToDB.getCommandUseTable().saveCommandUse(copyFromDB.getCommandUseTable().getCommandUse()); + clearAndCopyToDB.getCommandUseTable().saveCommandUse(copyFromDB.getCommandUseTable().getCommandUse()); } catch (SQLException | NullPointerException e) { Log.toLog("ManageUtils.move", e); return false; } return true; } + + public static boolean containsCombinable(List sessions) { + // Checks if there are starts & ends that are the same, or less than 5000 ms away from each other. + return sessions.stream() + .anyMatch(s -> sessions.stream() + .filter(ses -> !ses.equals(s)) + .map(ses -> ses.getSessionStart()) + .anyMatch((Long start) -> (Math.abs(s.getSessionEnd() - start) < 5000))); + } + + public static List combineSessions(List sessions) { + List newSessions = new ArrayList<>(); + List removed = new ArrayList<>(); + Iterator iterator = sessions.iterator(); + while (iterator.hasNext()) { + SessionData session = iterator.next(); + if (removed.contains(session)) { + continue; + } + List close = sessions.stream().filter(ses -> Math.abs(session.getSessionEnd() - ses.getSessionStart()) < 5000).collect(Collectors.toList()); + if (!close.isEmpty()) { + long big = MathUtils.getBiggestLong(close.stream().map((SessionData ses) -> ses.getSessionEnd()).collect(Collectors.toList())); + session.endSession(big); + removed.addAll(close); + } + newSessions.add(session); + } + boolean containsCombinable = containsCombinable(newSessions); + if (containsCombinable) { + return combineSessions(newSessions); + } else { + return newSessions; + } + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java index 486586fd7..56bc86fff 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java @@ -34,54 +34,6 @@ public class MiscUtils { return System.currentTimeMillis(); } - /** - * Checks the version and returns response String. - * - * @return String informing about status of plugins version. - */ - public static String checkVersion() { - try { - Plan plugin = Plan.getInstance(); - String cVersion = plugin.getDescription().getVersion(); - String gitVersion = getGitVersion(); - if (checkVersion(cVersion, gitVersion)) { - return Phrase.VERSION_NEW_AVAILABLE.parse(gitVersion); - } else { - return Phrase.VERSION_LATEST + ""; - } - } catch (IOException | NumberFormatException e) { - Log.error(Phrase.VERSION_CHECK_ERROR + ""); - } - return Phrase.VERSION_FAIL + ""; - } - - private static String getGitVersion() throws IOException { - URL githubUrl = new URL("https://raw.githubusercontent.com/Rsl1122/Plan-PlayerAnalytics/master/Plan/src/main/resources/plugin.yml"); - String lineWithVersion = ""; - Scanner websiteScanner = new Scanner(githubUrl.openStream()); - while (websiteScanner.hasNextLine()) { - String line = websiteScanner.nextLine(); - if (line.toLowerCase().contains("version")) { - lineWithVersion = line; - break; - } - } - return lineWithVersion.split(": ")[1]; - } - - /** - * - * @param currentVersion - * @param gitVersion - * @return - * @throws NumberFormatException - */ - public static boolean checkVersion(String currentVersion, String gitVersion) throws NumberFormatException { - int newestVersionNumber = FormatUtils.parseVersionNumber(gitVersion); - int currentVersionNumber = FormatUtils.parseVersionNumber(currentVersion); - return newestVersionNumber > currentVersionNumber; - } - /** * * @param args diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDFetcher.java b/Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDFetcher.java deleted file mode 100644 index 065121303..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDFetcher.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (c) 2015 Nate Mortensen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package main.java.com.djrapitops.plan.utilities.uuid; - -import com.google.common.collect.ImmutableList; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.ByteBuffer; -import java.util.*; -import java.util.concurrent.Callable; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; - -/** - * - * @author Rsl1122 - */ -public class UUIDFetcher implements Callable> { - - private static final double PROFILES_PER_REQUEST = 100; - private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; - private final JSONParser jsonParser = new JSONParser(); - private final List names; - private final boolean rateLimiting; - - /** - * - * @param names - * @param rateLimiting - */ - public UUIDFetcher(List names, boolean rateLimiting) { - this.names = ImmutableList.copyOf(names); - this.rateLimiting = rateLimiting; - } - - /** - * - * @param names - */ - public UUIDFetcher(List names) { - this(names, true); - } - - @Override - public Map call() throws Exception { - Map uuidMap = new HashMap<>(); - - int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); - - for (int i = 0; i < requests; i++) { - HttpURLConnection connection = createConnection(); - String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); - writeBody(connection, body); - JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); - for (Object profile : array) { - JSONObject jsonProfile = (JSONObject) profile; - String id = (String) jsonProfile.get("id"); - String name = (String) jsonProfile.get("name"); - UUID uuid = UUIDFetcher.getUUID(id); - uuidMap.put(name, uuid); - } - if (rateLimiting && i != requests - 1) { - Thread.sleep(100L); - } - } - return uuidMap; - } - - private static void writeBody(HttpURLConnection connection, String body) throws Exception { - OutputStream stream = connection.getOutputStream(); - stream.write(body.getBytes()); - stream.flush(); - stream.close(); - } - - private static HttpURLConnection createConnection() throws Exception { - URL url = new URL(PROFILE_URL); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setUseCaches(false); - connection.setDoInput(true); - connection.setDoOutput(true); - return connection; - } - - private static UUID getUUID(String id) { - return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32)); - } - - /** - * - * @param uuid - * @return - */ - public static byte[] toBytes(UUID uuid) { - ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]); - byteBuffer.putLong(uuid.getMostSignificantBits()); - byteBuffer.putLong(uuid.getLeastSignificantBits()); - return byteBuffer.array(); - } - - /** - * - * @param array - * @return - */ - public static UUID fromBytes(byte[] array) { - if (array.length != 16) { - throw new IllegalArgumentException("Illegal byte array length: " + array.length); - } - ByteBuffer byteBuffer = ByteBuffer.wrap(array); - long mostSignificant = byteBuffer.getLong(); - long leastSignificant = byteBuffer.getLong(); - return new UUID(mostSignificant, leastSignificant); - } - - /** - * - * @param name - * @return - * @throws Exception - */ - public static UUID getUUIDOf(String name) throws Exception { - return new UUIDFetcher(Arrays.asList(name)).call().get(name); - } -} diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java b/Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java index 148d4a362..5fcdfedfa 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/uuid/UUIDUtility.java @@ -5,6 +5,7 @@ */ package main.java.com.djrapitops.plan.utilities.uuid; +import com.djrapitops.javaplugin.utilities.UUIDFetcher; import java.sql.SQLException; import java.util.UUID; import main.java.com.djrapitops.plan.Log; diff --git a/Plan/src/main/resources/analysis.html b/Plan/src/main/resources/analysis.html index 290a2b165..1a48540d7 100644 --- a/Plan/src/main/resources/analysis.html +++ b/Plan/src/main/resources/analysis.html @@ -498,7 +498,7 @@ header p { -

Unique Playes: %uniquejoinsmonth% | Unique/Day: %avguniquejoinsmonth%

+

Unique Players: %uniquejoinsmonth% | Unique/Day: %avguniquejoinsmonth%

diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java index c43d9c702..333e07af6 100644 --- a/Plan/src/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/database/DatabaseTest.java @@ -415,5 +415,5 @@ public class DatabaseTest { assertTrue("Doesn't contain /tp", commandUse.containsKey("/tp")); assertTrue("Doesn't contain /pla", commandUse.containsKey("/pla")); assertTrue("Doesn't contain /help", commandUse.containsKey("/help")); - } + } } diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/ManageUtilsTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/ManageUtilsTest.java new file mode 100644 index 000000000..95df8c2ca --- /dev/null +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/ManageUtilsTest.java @@ -0,0 +1,142 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package test.java.main.java.com.djrapitops.plan.utilities; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import main.java.com.djrapitops.plan.data.SessionData; +import main.java.com.djrapitops.plan.utilities.ManageUtils; +import org.bukkit.plugin.java.JavaPlugin; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import test.java.utils.TestInit; + +/** + * + * @author Risto + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({JavaPlugin.class}) +public class ManageUtilsTest { + + private int threshold = 5000; + + public ManageUtilsTest() { + } + + @Before + public void setUp() throws IOException, Exception { + TestInit t = new TestInit(); + assertTrue("Not set up", t.setUp()); + } + + @Test + public void testContainsCombinable() { + List data = new ArrayList<>(); + data.add(new SessionData(0, 100)); + data.add(new SessionData(threshold - 100, threshold * 2)); + data.add(new SessionData(threshold * 2 + 100, threshold * 3)); + assertTrue(ManageUtils.containsCombinable(data)); + } + + @Test + public void testContainsCombinableFalse() { + List data = new ArrayList<>(); + data.add(new SessionData(0, threshold)); + data.add(new SessionData(threshold * 3, threshold * 4)); + assertTrue(!ManageUtils.containsCombinable(data)); + } + + @Test + public void testContainsCombinableFalse2() { + List data = new ArrayList<>(); + data.add(new SessionData(0, threshold * 2)); + data.add(new SessionData(threshold * 3, threshold * 4)); + assertTrue(!ManageUtils.containsCombinable(data)); + } + + @Test + public void testContainsCombinableFalse3() { + List data = new ArrayList<>(); + data.add(new SessionData(0, threshold * 2)); + data.add(new SessionData(threshold * 3 + 200, threshold * 4)); + assertTrue(!ManageUtils.containsCombinable(data)); + } + + @Test + public void testCombineSessions() { + List data = new ArrayList<>(); + data.add(new SessionData(0, 100)); + data.add(new SessionData(threshold, threshold * 2)); + data.add(new SessionData(threshold * 2 + 100, threshold * 3)); + SessionData get = ManageUtils.combineSessions(data).get(0); + SessionData exp = new SessionData(0, threshold * 3); + assertEquals(exp, get); + } + + @Test + public void testCombineSessions2() { + List data = new ArrayList<>(); + data.add(new SessionData(0, 100)); + data.add(new SessionData(threshold, threshold * 2)); + data.add(new SessionData(threshold * 2 + 100, threshold * 3)); + data.add(new SessionData(threshold * 3 + 200, threshold * 4)); + SessionData get = ManageUtils.combineSessions(data).get(0); + SessionData exp = new SessionData(0, threshold * 4); + assertEquals(exp, get); + } + + @Test + public void testCombineSessions3() { + List data = new ArrayList<>(); + data.add(new SessionData(0, 100)); + data.add(new SessionData(threshold, threshold * 2)); + data.add(new SessionData(threshold * 3 + 200, threshold * 4)); + List result = ManageUtils.combineSessions(data); + SessionData exp = new SessionData(0, threshold * 2); + assertEquals(exp, result.get(0)); + SessionData exp2 = new SessionData(threshold * 3 + 200, threshold * 4); + assertEquals(exp2, result.get(1)); + } + + @Test + public void testCombineSessions4() { + List data = new ArrayList<>(); + data.add(new SessionData(0, 100)); + data.add(new SessionData(threshold, threshold * 2)); + data.add(new SessionData(threshold * 3 + 200, threshold * 4)); + data.add(new SessionData(threshold * 5 - 200, threshold * 5)); + List result = ManageUtils.combineSessions(data); + SessionData exp = new SessionData(0, threshold * 2); + assertEquals(exp, result.get(0)); + SessionData exp2 = new SessionData(threshold * 3 + 200, threshold * 5); + assertEquals(exp2, result.get(1)); + } + + @Test + public void testCombineSessions5() { + List data = new ArrayList<>(); + data.add(new SessionData(0, 100)); + data.add(new SessionData(threshold, threshold * 2)); + data.add(new SessionData(threshold * 5, threshold * 5 + 100)); + data.add(new SessionData(threshold * 8, threshold * 8 + 200)); + data.add(new SessionData(threshold * 9 - 200, threshold * 10)); + List result = ManageUtils.combineSessions(data); + SessionData exp = new SessionData(0, threshold * 2); + assertEquals(exp, result.get(0)); + SessionData exp2 = new SessionData(threshold * 5, threshold * 5 + 100); + assertEquals(exp2, result.get(1)); + SessionData exp3 = new SessionData(threshold * 8, threshold * 10); + assertEquals(exp3, result.get(2)); + } + +} diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java index e88d6c4ff..f27d31795 100644 --- a/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/utilities/MiscUtilsTest.java @@ -52,44 +52,6 @@ public class MiscUtilsTest { PowerMock.replay(Bukkit.class); } - /** - * - */ - @Test - public void testCheckVersion() { - String versionG = "2.10.9"; - assertTrue(MiscUtils.checkVersion("2.0.0", versionG)); - } - - /** - * - */ - @Test - public void testCheckVersion2() { - assertTrue(!MiscUtils.checkVersion("3.0.0", "2.10.9")); - } - - @Test - public void testCheckVersion5() { - assertTrue(MiscUtils.checkVersion("2.10.9", "3.0.0")); - } - - /** - * - */ - @Test - public void testCheckVersion3() { - assertTrue(!MiscUtils.checkVersion("2.11.0", "2.10.9")); - } - - /** - * - */ - @Test - public void testCheckVersion4() { - assertTrue(!MiscUtils.checkVersion("2.11.0", "2.11.0")); - } - /** * */ diff --git a/Plan/src/test/java/utils/TestInit.java b/Plan/src/test/java/utils/TestInit.java index 41e21bde3..5e9037c91 100644 --- a/Plan/src/test/java/utils/TestInit.java +++ b/Plan/src/test/java/utils/TestInit.java @@ -5,11 +5,13 @@ */ package test.java.utils; +import com.djrapitops.javaplugin.utilities.PluginLog; import java.io.File; import java.io.FileInputStream; import java.nio.file.Files; import java.util.logging.Logger; import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.ServerVariableHolder; import main.java.com.djrapitops.plan.Settings; import org.bukkit.Server; import org.bukkit.configuration.file.YamlConfiguration; @@ -63,6 +65,10 @@ public class TestInit { // Mockito.doReturn("0.0.0.0").when(mockServer).getIp(); when(planMock.getServer()).thenReturn(mockServer); when(planMock.getLogger()).thenReturn(Logger.getGlobal()); + ServerVariableHolder serverVariableHolder = new ServerVariableHolder(mockServer); + when(planMock.getVariable()).thenReturn(serverVariableHolder); + PluginLog log = new PluginLog(planMock, "console", ""); + when(planMock.getPluginLogger()).thenReturn(log); Plan.setInstance(planMock); // Mockito.doReturn("0.0.0.0").when(planMock).getServer().getIp(); Settings.DEBUG.setValue(true);