diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitCommand.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitCommand.java index daaaab1e7..b98fdf21d 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitCommand.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/commands/use/BukkitCommand.java @@ -16,7 +16,6 @@ */ package com.djrapitops.plan.commands.use; -import com.djrapitops.plan.commands.Arguments; import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.RunnableFactory; import org.bukkit.command.Command; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/OldPlanCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/OldPlanCommand.java index b53de1185..5cc20f7c4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/OldPlanCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/OldPlanCommand.java @@ -42,13 +42,8 @@ import javax.inject.Singleton; public class OldPlanCommand extends TreeCmdNode { private final PlanConfig config; - private final InspectCommand inspectCommand; private final QInspectCommand qInspectCommand; private final SearchCommand searchCommand; - private final ListPlayersCommand listPlayersCommand; - private final AnalyzeCommand analyzeCommand; - private final NetworkCommand networkCommand; - private final ListServersCommand listServersCommand; private final Lazy webUserCommand; private final RegisterCommand registerCommand; private final UnregisterCommand unregisterCommand; @@ -65,13 +60,8 @@ public class OldPlanCommand extends TreeCmdNode { Locale locale, PlanConfig config, // Group 1 - InspectCommand inspectCommand, QInspectCommand qInspectCommand, SearchCommand searchCommand, - ListPlayersCommand listPlayersCommand, - AnalyzeCommand analyzeCommand, - NetworkCommand networkCommand, - ListServersCommand listServersCommand, // Group 2 Lazy webUserCommand, RegisterCommand registerCommand, @@ -88,13 +78,8 @@ public class OldPlanCommand extends TreeCmdNode { commandsRegistered = false; this.config = config; - this.inspectCommand = inspectCommand; this.qInspectCommand = qInspectCommand; this.searchCommand = searchCommand; - this.listPlayersCommand = listPlayersCommand; - this.analyzeCommand = analyzeCommand; - this.networkCommand = networkCommand; - this.listServersCommand = listServersCommand; this.webUserCommand = webUserCommand; this.registerCommand = registerCommand; this.infoCommand = infoCommand; @@ -114,13 +99,8 @@ public class OldPlanCommand extends TreeCmdNode { } CommandNode[] analyticsGroup = { - inspectCommand, qInspectCommand, - searchCommand, - listPlayersCommand, - analyzeCommand, - networkCommand, - listServersCommand + searchCommand }; CommandNode[] webGroup = { webUserCommand.get(), diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java index c3a4e6f5e..7874024e6 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java @@ -17,11 +17,15 @@ package com.djrapitops.plan.commands; import com.djrapitops.plan.commands.subcommands.LinkCommands; +import com.djrapitops.plan.commands.use.Arguments; import com.djrapitops.plan.commands.use.CMDSender; import com.djrapitops.plan.commands.use.CommandWithSubcommands; import com.djrapitops.plan.commands.use.Subcommand; import com.djrapitops.plan.settings.locale.Locale; +import com.djrapitops.plan.utilities.logging.ErrorContext; +import com.djrapitops.plan.utilities.logging.ErrorLogger; import com.djrapitops.plugin.command.ColorScheme; +import com.djrapitops.plugin.logging.L; import javax.inject.Inject; import javax.inject.Singleton; @@ -34,23 +38,26 @@ public class PlanCommand { private final Locale locale; private final ColorScheme colors; private final LinkCommands linkCommands; + private final ErrorLogger errorLogger; @Inject public PlanCommand( Locale locale, ColorScheme colors, - LinkCommands linkCommands + LinkCommands linkCommands, + ErrorLogger errorLogger ) { this.locale = locale; this.colors = colors; this.linkCommands = linkCommands; + this.errorLogger = errorLogger; } - private void handleException(RuntimeException error, CMDSender sender) { + private void handleException(RuntimeException error, CMDSender sender, Arguments arguments) { if (error instanceof IllegalArgumentException) { sender.send("§c" + error.getMessage()); } else { - throw error; + errorLogger.log(L.WARN, error, ErrorContext.builder().related(sender, arguments).build()); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanProxyCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanProxyCommand.java index e60089f80..de6c29d75 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanProxyCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanProxyCommand.java @@ -42,9 +42,6 @@ import javax.inject.Singleton; @Singleton public class PlanProxyCommand extends TreeCmdNode { - private final NetworkCommand networkCommand; - private final ListServersCommand listServersCommand; - private final ListPlayersCommand listPlayersCommand; private final RegisterCommand registerCommand; private final UnregisterCommand unregisterCommand; private final Lazy webUserCommand; @@ -61,9 +58,6 @@ public class PlanProxyCommand extends TreeCmdNode { ColorScheme colorScheme, Locale locale, // Group 1 - NetworkCommand networkCommand, - ListServersCommand listServersCommand, - ListPlayersCommand listPlayersCommand, // Group 2 RegisterCommand registerCommand, UnregisterCommand unregisterCommand, @@ -80,9 +74,6 @@ public class PlanProxyCommand extends TreeCmdNode { commandsRegistered = false; - this.networkCommand = networkCommand; - this.listServersCommand = listServersCommand; - this.listPlayersCommand = listPlayersCommand; this.registerCommand = registerCommand; this.webUserCommand = webUserCommand; this.rawDataCommand = rawDataCommand; @@ -100,9 +91,9 @@ public class PlanProxyCommand extends TreeCmdNode { } CommandNode[] analyticsGroup = { - networkCommand, - listServersCommand, - listPlayersCommand + // networkCommand, + // listPlayersCommand, + // list servers }; CommandNode[] webGroup = { registerCommand, diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/AnalyzeCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/AnalyzeCommand.java deleted file mode 100644 index 5d46f08b3..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/AnalyzeCommand.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.commands.subcommands; - -import com.djrapitops.plan.delivery.export.Exporter; -import com.djrapitops.plan.delivery.rendering.html.Html; -import com.djrapitops.plan.delivery.webserver.Addresses; -import com.djrapitops.plan.delivery.webserver.WebServer; -import com.djrapitops.plan.exceptions.ExportException; -import com.djrapitops.plan.exceptions.database.DBOpException; -import com.djrapitops.plan.identification.Server; -import com.djrapitops.plan.identification.ServerInfo; -import com.djrapitops.plan.processing.Processing; -import com.djrapitops.plan.settings.Permissions; -import com.djrapitops.plan.settings.locale.Locale; -import com.djrapitops.plan.settings.locale.lang.CmdHelpLang; -import com.djrapitops.plan.settings.locale.lang.CommandLang; -import com.djrapitops.plan.settings.locale.lang.DeepHelpLang; -import com.djrapitops.plan.storage.database.DBSystem; -import com.djrapitops.plan.storage.database.Database; -import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; -import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries; -import com.djrapitops.plan.utilities.logging.ErrorLogger; -import com.djrapitops.plugin.command.CommandNode; -import com.djrapitops.plugin.command.CommandType; -import com.djrapitops.plugin.command.CommandUtils; -import com.djrapitops.plugin.command.Sender; -import com.djrapitops.plugin.logging.L; - -import javax.inject.Inject; -import javax.inject.Singleton; -import java.util.Optional; - -/** - * This SubCommand is used to run the analysis and access the /server link. - * - * @author Rsl1122 - */ -@Singleton -public class AnalyzeCommand extends CommandNode { - - private final Locale locale; - private final Processing processing; - private final Exporter exporter; - private final Addresses addresses; - private final ServerInfo serverInfo; - private final WebServer webServer; - private final DBSystem dbSystem; - private final ErrorLogger errorLogger; - - @Inject - public AnalyzeCommand( - Locale locale, - Processing processing, - Exporter exporter, - Addresses addresses, - ServerInfo serverInfo, - WebServer webServer, - DBSystem dbSystem, - ErrorLogger errorLogger - ) { - super("analyze|analyse|analysis|a", Permissions.ANALYZE.getPermission(), CommandType.CONSOLE); - - this.locale = locale; - this.processing = processing; - this.exporter = exporter; - this.addresses = addresses; - this.serverInfo = serverInfo; - this.webServer = webServer; - this.dbSystem = dbSystem; - this.errorLogger = errorLogger; - - setShortHelp(locale.getString(CmdHelpLang.ANALYZE)); - setInDepthHelp(locale.getArray(DeepHelpLang.ANALYZE)); - setArguments("[server/id]"); - } - - @Override - public void onCommand(Sender sender, String commandLabel, String[] args) { - Database database = dbSystem.getDatabase(); - Database.State dbState = database.getState(); - if (dbState != Database.State.OPEN) { - sender.sendMessage(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name())); - return; - } - - processing.submitNonCritical(() -> { - try { - Server server = getServer(args); - sendWebUserNotificationIfNecessary(sender); - exporter.exportServerPage(server); - sendLink(server, sender); - } catch (DBOpException | ExportException e) { - sender.sendMessage("§cError occurred: " + e.toString()); - errorLogger.log(L.ERROR, this.getClass(), e); - } - }); - } - - private void sendLink(Server server, Sender sender) { - String target = "/server/" + Html.encodeToURL(server.getName()); - String address = addresses.getMainAddress().orElseGet(() -> { - sender.sendMessage(locale.getString(CommandLang.NO_ADDRESS_NOTIFY)); - return addresses.getFallbackLocalhostAddress(); - }); - String url = address + target; - String linkPrefix = locale.getString(CommandLang.LINK_PREFIX); - sender.sendMessage(locale.getString(CommandLang.HEADER_ANALYSIS)); - // Link - boolean console = !CommandUtils.isPlayer(sender); - if (console) { - sender.sendMessage(linkPrefix + url); - } else { - sender.sendMessage(linkPrefix); - sender.sendLink(" ", locale.getString(CommandLang.LINK_CLICK_ME), url); - } - sender.sendMessage(">"); - } - - private void sendWebUserNotificationIfNecessary(Sender sender) { - if (webServer.isAuthRequired() && - CommandUtils.isPlayer(sender) && - !dbSystem.getDatabase().query(WebUserQueries.fetchUserLinkedTo(sender.getName())).isPresent()) { - sender.sendMessage("§e" + locale.getString(CommandLang.NO_WEB_USER_NOTIFY)); - } - } - - private Server getServer(String[] args) { - return getGivenIdentifier(args) - .flatMap(serverIdentifier -> dbSystem.getDatabase() - .query(ServerQueries.fetchServerMatchingIdentifier(serverIdentifier)) - ).filter(server -> !server.isProxy()) - .orElseGet(serverInfo::getServer); - } - - private Optional getGivenIdentifier(String[] args) { - if (args.length < 1) { - return Optional.empty(); - } - StringBuilder idBuilder = new StringBuilder(args[0]); - if (args.length > 1) { - for (int i = 1; i < args.length; i++) { - idBuilder.append(" ").append(args[i]); - } - } - return Optional.of(idBuilder.toString()); - } -} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/InspectCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/InspectCommand.java deleted file mode 100644 index cb7d60679..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/InspectCommand.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.commands.subcommands; - -import com.djrapitops.plan.delivery.rendering.html.Html; -import com.djrapitops.plan.delivery.webserver.Addresses; -import com.djrapitops.plan.delivery.webserver.WebServer; -import com.djrapitops.plan.exceptions.database.DBOpException; -import com.djrapitops.plan.identification.UUIDUtility; -import com.djrapitops.plan.processing.Processing; -import com.djrapitops.plan.settings.Permissions; -import com.djrapitops.plan.settings.locale.Locale; -import com.djrapitops.plan.settings.locale.lang.CmdHelpLang; -import com.djrapitops.plan.settings.locale.lang.CommandLang; -import com.djrapitops.plan.settings.locale.lang.DeepHelpLang; -import com.djrapitops.plan.storage.database.DBSystem; -import com.djrapitops.plan.storage.database.Database; -import com.djrapitops.plan.storage.database.queries.PlayerFetchQueries; -import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries; -import com.djrapitops.plan.utilities.MiscUtils; -import com.djrapitops.plan.utilities.logging.ErrorLogger; -import com.djrapitops.plugin.command.CommandNode; -import com.djrapitops.plugin.command.CommandType; -import com.djrapitops.plugin.command.CommandUtils; -import com.djrapitops.plugin.command.Sender; -import com.djrapitops.plugin.logging.L; - -import javax.inject.Inject; -import java.util.UUID; - -/** - * This command is used to refresh Inspect page and display link. - * - * @author Rsl1122 - */ -public class InspectCommand extends CommandNode { - - private final Locale locale; - private final DBSystem dbSystem; - private final WebServer webServer; - private final Addresses addresses; - private final Processing processing; - private final UUIDUtility uuidUtility; - private final ErrorLogger errorLogger; - - @Inject - public InspectCommand( - Locale locale, - Addresses addresses, - Processing processing, - DBSystem dbSystem, - WebServer webServer, - UUIDUtility uuidUtility, - ErrorLogger errorLogger - ) { - super("inspect", Permissions.INSPECT.getPermission(), CommandType.PLAYER_OR_ARGS); - this.addresses = addresses; - this.processing = processing; - setArguments(""); - - this.locale = locale; - this.dbSystem = dbSystem; - this.webServer = webServer; - this.uuidUtility = uuidUtility; - this.errorLogger = errorLogger; - - setShortHelp(locale.getString(CmdHelpLang.INSPECT)); - setInDepthHelp(locale.getArray(DeepHelpLang.INSPECT)); - } - - @Override - public void onCommand(Sender sender, String commandLabel, String[] args) { - String playerName = MiscUtils.getPlayerName(args, sender); - - if (playerName == null) { - sender.sendMessage(locale.getString(CommandLang.FAIL_NO_PERMISSION)); - return; - } - - Database.State dbState = dbSystem.getDatabase().getState(); - if (dbState != Database.State.OPEN) { - sender.sendMessage(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name())); - return; - } - - runInspectTask(playerName, sender); - } - - private void runInspectTask(String playerName, Sender sender) { - processing.submitNonCritical(() -> { - try { - UUID playerUUID = uuidUtility.getUUIDOf(playerName); - if (playerUUID == null) { - sender.sendMessage(locale.getString(CommandLang.FAIL_USERNAME_NOT_VALID)); - return; - } - - if (!dbSystem.getDatabase().query(PlayerFetchQueries.isPlayerRegistered(playerUUID))) { - sender.sendMessage(locale.getString(CommandLang.FAIL_USERNAME_NOT_KNOWN)); - return; - } - - checkWebUserAndNotify(sender); - this.sendInspectMsg(sender, playerName); - } catch (DBOpException e) { - sender.sendMessage("§eDatabase exception occurred: " + e.getMessage()); - errorLogger.log(L.ERROR, this.getClass(), e); - } - }); - } - - private void checkWebUserAndNotify(Sender sender) { - if (CommandUtils.isPlayer(sender) && webServer.isAuthRequired()) { - boolean senderHasWebUser = dbSystem.getDatabase().query(WebUserQueries.fetchUserLinkedTo(sender.getName())).isPresent(); - - if (!senderHasWebUser) { - sender.sendMessage("§e" + locale.getString(CommandLang.NO_WEB_USER_NOTIFY)); - } - } - } - - private void sendInspectMsg(Sender sender, String playerName) { - sender.sendMessage(locale.getString(CommandLang.HEADER_INSPECT, playerName)); - - String address = addresses.getMainAddress().orElseGet(() -> { - sender.sendMessage(locale.getString(CommandLang.NO_ADDRESS_NOTIFY)); - return addresses.getFallbackLocalhostAddress(); - }); - String url = address + "/player/" + Html.encodeToURL(playerName); - String linkPrefix = locale.getString(CommandLang.LINK_PREFIX); - - boolean console = !CommandUtils.isPlayer(sender); - if (console) { - sender.sendMessage(linkPrefix + url); - } else { - sender.sendMessage(linkPrefix); - sender.sendLink(" ", locale.getString(CommandLang.LINK_CLICK_ME), url); - } - - sender.sendMessage(">"); - } -} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/LinkCommands.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/LinkCommands.java index dc6ef0206..5d7c5a283 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/LinkCommands.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/LinkCommands.java @@ -16,8 +16,9 @@ */ package com.djrapitops.plan.commands.subcommands; -import com.djrapitops.plan.commands.Arguments; +import com.djrapitops.plan.commands.use.Arguments; import com.djrapitops.plan.commands.use.CMDSender; +import com.djrapitops.plan.commands.use.MessageBuilder; import com.djrapitops.plan.delivery.rendering.html.Html; import com.djrapitops.plan.delivery.webserver.Addresses; import com.djrapitops.plan.identification.Identifiers; @@ -26,6 +27,7 @@ import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.lang.CommandLang; import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries; import com.djrapitops.plugin.command.ColorScheme; @@ -34,6 +36,11 @@ import javax.inject.Inject; import javax.inject.Singleton; import java.util.UUID; +/** + * Implementation of commands that send a link to the command sender. + * + * @author Rsl1122 + */ @Singleton public class LinkCommands { @@ -68,6 +75,21 @@ public class LinkCommands { }); } + private MessageBuilder linkTo(MessageBuilder builder, CMDSender sender, String address) { + if (sender.isPlayer()) { + builder.addPart(colors.getTertiaryColor() + "§l[Link]").link(address).hover(address); + } else { + builder.addPart(colors.getTertiaryColor() + address); + } + return builder; + } + + /** + * Implementation of server subcommand, used to get link to server page. + * + * @param sender Sender of command. + * @param arguments Given arguments. + */ public void onServerCommand(CMDSender sender, Arguments arguments) { Server server; String identifier = arguments.concatenate(" "); @@ -80,59 +102,21 @@ public class LinkCommands { .orElseThrow(() -> new IllegalArgumentException("Server '" + identifier + "' was not found from the database.")); } - String address = getAddress(sender); - String target = "/server/" + Html.encodeToURL(server.getName()); + String address = getAddress(sender) + "/server/" + Html.encodeToURL(server.getName()); sender.buildMessage() .addPart(colors.getMainColor() + "View server page: ") - .addPart(colors.getTertiaryColor() + "§l[Link]").link(address + target).hover(address + target) + .apply(builder -> linkTo(builder, sender, address)) .send(); } - public void onPlayerCommand(CMDSender sender, Arguments arguments) { - String identifier = arguments.concatenate(" "); - UUID playerUUID = identifiers.getPlayerUUID(identifier); - UUID senderUUID = sender.getUUID().orElse(null); - if (playerUUID == null) playerUUID = senderUUID; - if (playerUUID == null) { - throw new IllegalArgumentException("Player '" + identifier + "' was not found, they have no UUID."); - } - - String playerName = dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID)) - .orElseThrow(() -> new IllegalArgumentException("Player '" + identifier + "' was not found in the database.")); - - if (sender.hasPermission("plan.player.other") || playerUUID.equals(senderUUID)) { - String address = getAddress(sender); - String target = "/player/" + Html.encodeToURL(playerName); - sender.buildMessage() - .addPart(colors.getMainColor() + "View player page: ") - .addPart(colors.getTertiaryColor() + "§l[Link]").link(address + target).hover(address + target) - .send(); - } else { - throw new IllegalArgumentException("Insufficient permissions: You can not view other player's pages."); - } - } - - public void onPlayersCommand(CMDSender sender, Arguments arguments) { - String address = getAddress(sender); - String target = "/players"; - sender.buildMessage() - .addPart(colors.getMainColor() + "View players page: ") - .addPart(colors.getTertiaryColor() + "§l[Link]").link(address + target).hover(address + target) - .send(); - } - - public void onNetworkCommand(CMDSender sender, Arguments arguments) { - String address = getAddress(sender); - String target = "/network"; - sender.buildMessage() - .addPart(colors.getMainColor() + "View network page: ") - .addPart(colors.getTertiaryColor() + "§l[Link]").link(address + target).hover(address + target) - .send(); - dbSystem.getDatabase().query(ServerQueries.fetchProxyServerInformation()) - .orElseThrow(() -> new IllegalArgumentException("Server is not connected to a network. The link redirects to server page.")); - } - + /** + * Implementation of servers subcommand, used to list servers. + * + * @param sender Sender of command. + * @param arguments Given arguments. + */ public void onServersCommand(CMDSender sender, Arguments arguments) { + ensureDatabaseIsOpen(); String m = colors.getMainColor(); String s = colors.getSecondaryColor(); String t = colors.getTertiaryColor(); @@ -148,4 +132,71 @@ public class LinkCommands { t + "id:name:uuid\n" + serversListed, ":")) .send(); } + + private void ensureDatabaseIsOpen() { + Database.State dbState = dbSystem.getDatabase().getState(); + if (dbState != Database.State.OPEN) { + throw new IllegalArgumentException(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name())); + } + } + + /** + * Implementation of player command, used to get link to player page. + * + * @param sender Sender of command. + * @param arguments Given arguments. + */ + public void onPlayerCommand(CMDSender sender, Arguments arguments) { + String identifier = arguments.concatenate(" "); + UUID playerUUID = identifiers.getPlayerUUID(identifier); + UUID senderUUID = sender.getUUID().orElse(null); + if (playerUUID == null) playerUUID = senderUUID; + if (playerUUID == null) { + throw new IllegalArgumentException("Player '" + identifier + "' was not found, they have no UUID."); + } + + String playerName = dbSystem.getDatabase().query(UserIdentifierQueries.fetchPlayerNameOf(playerUUID)) + .orElseThrow(() -> new IllegalArgumentException("Player '" + identifier + "' was not found in the database.")); + + if (sender.hasPermission("plan.player.other") || playerUUID.equals(senderUUID)) { + String address = getAddress(sender) + "/player/" + Html.encodeToURL(playerName); + sender.buildMessage() + .addPart(colors.getMainColor() + "View player page: ") + .apply(builder -> linkTo(builder, sender, address)) + .send(); + } else { + throw new IllegalArgumentException("Insufficient permissions: You can not view other player's pages."); + } + } + + /** + * Implementation of players subcommand, used to get link to players page. + * + * @param sender Sender of command + * @param arguments Only present to fulfill Subcommand#onCommand requirements. + */ + public void onPlayersCommand(CMDSender sender, Arguments arguments) { + String address = getAddress(sender) + "/players"; + sender.buildMessage() + .addPart(colors.getMainColor() + "View players page: ") + .apply(builder -> linkTo(builder, sender, address)) + .send(); + } + + /** + * Implementation of network subcommand, used to get link to network page. + * + * @param sender Sender of command + * @param arguments Only present to fulfill Subcommand#onCommand requirements. + */ + public void onNetworkCommand(CMDSender sender, Arguments arguments) { + String address = getAddress(sender) + "/network"; + sender.buildMessage() + .addPart(colors.getMainColor() + "View network page: ") + .apply(builder -> linkTo(builder, sender, address)) + .send(); + dbSystem.getDatabase().query(ServerQueries.fetchProxyServerInformation()) + .orElseThrow(() -> new IllegalArgumentException("Server is not connected to a network. The link redirects to server page.")); + } + } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/ListPlayersCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/ListPlayersCommand.java deleted file mode 100644 index 41eebed13..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/ListPlayersCommand.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.commands.subcommands; - -import com.djrapitops.plan.delivery.webserver.Addresses; -import com.djrapitops.plan.processing.Processing; -import com.djrapitops.plan.settings.Permissions; -import com.djrapitops.plan.settings.locale.Locale; -import com.djrapitops.plan.settings.locale.lang.CmdHelpLang; -import com.djrapitops.plan.settings.locale.lang.CommandLang; -import com.djrapitops.plan.settings.locale.lang.DeepHelpLang; -import com.djrapitops.plugin.command.CommandNode; -import com.djrapitops.plugin.command.CommandType; -import com.djrapitops.plugin.command.CommandUtils; -import com.djrapitops.plugin.command.Sender; - -import javax.inject.Inject; - -/** - * Command used to display url to the player list page. - * - * @author Rsl1122 - */ -public class ListPlayersCommand extends CommandNode { - - private final Locale locale; - private final Addresses addresses; - private final Processing processing; - - @Inject - public ListPlayersCommand( - Locale locale, - Addresses addresses, - Processing processing - ) { - super("players|pl|playerlist|list", Permissions.INSPECT_OTHER.getPermission(), CommandType.CONSOLE); - - this.locale = locale; - this.addresses = addresses; - this.processing = processing; - - setShortHelp(locale.getString(CmdHelpLang.PLAYERS)); - setInDepthHelp(locale.getArray(DeepHelpLang.PLAYERS)); - } - - @Override - public void onCommand(Sender sender, String commandLabel, String[] args) { - processing.submitNonCritical(() -> sendListMsg(sender)); - } - - private void sendListMsg(Sender sender) { - sender.sendMessage(locale.getString(CommandLang.HEADER_PLAYERS)); - - // Link - String address = addresses.getMainAddress().orElseGet(() -> { - sender.sendMessage(locale.getString(CommandLang.NO_ADDRESS_NOTIFY)); - return addresses.getFallbackLocalhostAddress(); - }); - String url = address + "/players"; - String linkPrefix = locale.getString(CommandLang.LINK_PREFIX); - boolean console = !CommandUtils.isPlayer(sender); - if (console) { - sender.sendMessage(linkPrefix + url); - } else { - sender.sendMessage(linkPrefix); - sender.sendLink(" ", locale.getString(CommandLang.LINK_CLICK_ME), url); - } - sender.sendMessage(">"); - } -} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/ListServersCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/ListServersCommand.java deleted file mode 100644 index 3b36fdc7d..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/ListServersCommand.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.commands.subcommands; - -import com.djrapitops.plan.delivery.formatting.Formatter; -import com.djrapitops.plan.exceptions.database.DBOpException; -import com.djrapitops.plan.identification.Server; -import com.djrapitops.plan.settings.Permissions; -import com.djrapitops.plan.settings.locale.Locale; -import com.djrapitops.plan.settings.locale.lang.CmdHelpLang; -import com.djrapitops.plan.settings.locale.lang.CommandLang; -import com.djrapitops.plan.settings.locale.lang.DeepHelpLang; -import com.djrapitops.plan.storage.database.DBSystem; -import com.djrapitops.plan.storage.database.Database; -import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; -import com.djrapitops.plan.utilities.logging.ErrorLogger; -import com.djrapitops.plugin.command.ColorScheme; -import com.djrapitops.plugin.command.CommandNode; -import com.djrapitops.plugin.command.CommandType; -import com.djrapitops.plugin.command.Sender; -import com.djrapitops.plugin.logging.L; - -import javax.inject.Inject; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * This SubCommand is used to list all servers found in the database. - * - * @author Rsl1122 - */ -public class ListServersCommand extends CommandNode { - - private final Locale locale; - private final ColorScheme colorScheme; - private final DBSystem dbSystem; - private final ErrorLogger errorLogger; - - @Inject - public ListServersCommand( - Locale locale, - ColorScheme colorScheme, - DBSystem dbSystem, - ErrorLogger errorLogger - ) { - super("servers|serverlist|listservers|sl|ls", Permissions.MANAGE.getPermission(), CommandType.CONSOLE); - - this.locale = locale; - this.colorScheme = colorScheme; - this.dbSystem = dbSystem; - this.errorLogger = errorLogger; - - setShortHelp(locale.getString(CmdHelpLang.SERVERS)); - setInDepthHelp(locale.getArray(DeepHelpLang.SERVERS)); - } - - @Override - public void onCommand(Sender sender, String commandLabel, String[] args) { - Database.State dbState = dbSystem.getDatabase().getState(); - if (dbState != Database.State.OPEN) { - sender.sendMessage(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name())); - return; - } - - String sCol = colorScheme.getSecondaryColor(); - String tCol = colorScheme.getTertiaryColor(); - Formatter serverFormatter = serverLister(sCol, tCol); - try { - sender.sendMessage(locale.getString(CommandLang.HEADER_SERVERS)); - sendServers(sender, serverFormatter); - sender.sendMessage(">"); - } catch (DBOpException e) { - sender.sendMessage("§cDatabase Exception occurred."); - errorLogger.log(L.WARN, this.getClass(), e); - } - } - - private void sendServers(Sender sender, Formatter serverFormatter) { - List servers = new ArrayList<>(dbSystem.getDatabase().query(ServerQueries.fetchPlanServerInformation()).values()); - Collections.sort(servers); - for (Server server : servers) { - sender.sendMessage(serverFormatter.apply(server)); - } - } - - private Formatter serverLister(String tertiaryColor, String secondaryColor) { - return server -> " " + tertiaryColor + server.getId() + secondaryColor + " : " + server.getName() + " : " + server.getWebAddress(); - } - -} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/NetworkCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/NetworkCommand.java deleted file mode 100644 index 0cde2a67a..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/NetworkCommand.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.commands.subcommands; - -import com.djrapitops.plan.delivery.webserver.Addresses; -import com.djrapitops.plan.processing.Processing; -import com.djrapitops.plan.settings.Permissions; -import com.djrapitops.plan.settings.locale.Locale; -import com.djrapitops.plan.settings.locale.lang.CmdHelpLang; -import com.djrapitops.plan.settings.locale.lang.CommandLang; -import com.djrapitops.plan.settings.locale.lang.DeepHelpLang; -import com.djrapitops.plugin.command.CommandNode; -import com.djrapitops.plugin.command.CommandType; -import com.djrapitops.plugin.command.CommandUtils; -import com.djrapitops.plugin.command.Sender; - -import javax.inject.Inject; - -/** - * Command used to display url to the network page. - * - * @author Rsl1122 - */ -public class NetworkCommand extends CommandNode { - - private final Locale locale; - private final Addresses addresses; - private final Processing processing; - - @Inject - public NetworkCommand( - Locale locale, - Addresses addresses, - Processing processing - ) { - super("network|n|netw", Permissions.ANALYZE.getPermission(), CommandType.CONSOLE); - - this.locale = locale; - this.addresses = addresses; - this.processing = processing; - - setShortHelp(locale.getString(CmdHelpLang.NETWORK)); - setInDepthHelp(locale.getArray(DeepHelpLang.NETWORK)); - } - - @Override - public void onCommand(Sender sender, String commandLabel, String[] args) { - processing.submitNonCritical(() -> sendNetworkMsg(sender)); - } - - private void sendNetworkMsg(Sender sender) { - sender.sendMessage(locale.getString(CommandLang.HEADER_NETWORK)); - - // Link - String address = addresses.getMainAddress().orElseGet(() -> { - sender.sendMessage(locale.getString(CommandLang.NO_ADDRESS_NOTIFY)); - return addresses.getFallbackLocalhostAddress(); - }); - String url = address + "/network"; - String linkPrefix = locale.getString(CommandLang.LINK_PREFIX); - boolean console = !CommandUtils.isPlayer(sender); - if (console) { - sender.sendMessage(linkPrefix + url); - } else { - sender.sendMessage(linkPrefix); - sender.sendLink(" ", locale.getString(CommandLang.LINK_CLICK_ME), url); - } - sender.sendMessage(">"); - } -} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/RegisterCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/RegisterCommand.java index fb78f2f94..20f75b7fd 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/RegisterCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/RegisterCommand.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan.commands.subcommands; -import com.djrapitops.plan.commands.Arguments; +import com.djrapitops.plan.commands.use.Arguments; import com.djrapitops.plan.delivery.domain.auth.User; import com.djrapitops.plan.delivery.webserver.Addresses; import com.djrapitops.plan.delivery.webserver.auth.FailReason; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/UnregisterCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/UnregisterCommand.java index c276ced2e..83750944d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/UnregisterCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/UnregisterCommand.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan.commands.subcommands; -import com.djrapitops.plan.commands.Arguments; +import com.djrapitops.plan.commands.use.Arguments; import com.djrapitops.plan.delivery.domain.auth.User; import com.djrapitops.plan.delivery.webserver.auth.FailReason; import com.djrapitops.plan.identification.UUIDUtility; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/Arguments.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Arguments.java similarity index 92% rename from Plan/common/src/main/java/com/djrapitops/plan/commands/Arguments.java rename to Plan/common/src/main/java/com/djrapitops/plan/commands/use/Arguments.java index bc2601686..6515904ea 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/Arguments.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Arguments.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU Lesser General Public License * along with Plan. If not, see . */ -package com.djrapitops.plan.commands; +package com.djrapitops.plan.commands.use; import org.apache.commons.text.TextStringBuilder; @@ -79,4 +79,11 @@ public class Arguments { public boolean isEmpty() { return args.isEmpty(); } + + @Override + public String toString() { + return "Arguments{" + + "args=" + args + + '}'; + } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java index bc1d702b8..9a554a576 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java @@ -40,6 +40,10 @@ public interface CMDSender { return false; } + default boolean isPlayer() { + return getPlayerName().isPresent(); + } + Optional getUUID(); void send(String message); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CommandWithSubcommands.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CommandWithSubcommands.java index 62a48aada..29635abeb 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CommandWithSubcommands.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CommandWithSubcommands.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan.commands.use; -import com.djrapitops.plan.commands.Arguments; +import com.djrapitops.plan.utilities.java.TriConsumer; import com.djrapitops.plugin.command.ColorScheme; import java.util.ArrayList; @@ -31,7 +31,7 @@ public class CommandWithSubcommands extends Subcommand { private final List subcommands; private BiConsumer fallback; - private BiConsumer exceptionHandler; + private TriConsumer exceptionHandler; private ColorScheme colors; private CommandWithSubcommands() { @@ -61,7 +61,7 @@ public class CommandWithSubcommands extends Subcommand { try { executeCommand(sender, arguments); } catch (RuntimeException e) { - exceptionHandler.accept(e, sender); + exceptionHandler.accept(e, sender, arguments); } } @@ -151,7 +151,7 @@ public class CommandWithSubcommands extends Subcommand { }); } - public Builder exceptionHandler(BiConsumer exceptionHandler) { + public Builder exceptionHandler(TriConsumer exceptionHandler) { command.exceptionHandler = exceptionHandler; return this; } @@ -166,7 +166,7 @@ public class CommandWithSubcommands extends Subcommand { onTabComplete(command::onTabComplete); super.build(); if (command.fallback == null) fallback(command::onHelp); - if (command.exceptionHandler == null) exceptionHandler((error, sender) -> {throw error;}); + if (command.exceptionHandler == null) exceptionHandler((error, sender, arguments) -> {throw error;}); if (command.colors == null) colorScheme(new ColorScheme("§2", "§7", "§f")); return command; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java index 8b2041c57..d01d10203 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java @@ -16,8 +16,6 @@ */ package com.djrapitops.plan.commands.use; -import com.djrapitops.plan.commands.Arguments; - import java.util.*; import java.util.function.BiConsumer; import java.util.function.BiFunction; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java index d4c2360e3..9d7689c10 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java @@ -16,7 +16,6 @@ */ package com.djrapitops.plan.commands.use; -import com.djrapitops.plan.commands.Arguments; import org.apache.commons.lang3.StringUtils; import java.util.List; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/TriConsumer.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/TriConsumer.java new file mode 100644 index 000000000..e86ee4e40 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/TriConsumer.java @@ -0,0 +1,31 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.utilities.java; + +/** + * Functional interface for three arguments, similar to BiConsumer. + * + * @param Type of first argument + * @param Type of second argument + * @param Type of third argument. + * @author Rsl1122 + */ +public interface TriConsumer { + + void accept(T t, V v, K k); + +}