unfinished work commit

This commit is contained in:
Risto Lahtela 2020-05-12 19:25:16 +03:00
parent a3015db57c
commit 03bfe28762
25 changed files with 798 additions and 167 deletions

View File

@ -17,7 +17,8 @@
package com.djrapitops.plan;
import com.djrapitops.plan.addons.placeholderapi.BukkitPlaceholderRegistrar;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.use.BukkitCommand;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.settings.locale.Locale;
@ -28,6 +29,7 @@ import com.djrapitops.plugin.benchmarking.Benchmark;
import com.djrapitops.plugin.command.ColorScheme;
import com.djrapitops.plugin.task.AbsRunnable;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.logging.Level;
@ -74,9 +76,7 @@ public class Plan extends BukkitPlugin implements PlanPlugin {
logger.error("This error should be reported at https://github.com/Rsl1122/Plan-PlayerAnalytics/issues");
onDisable();
}
PlanCommand command = component.planCommand();
command.registerCommands();
registerCommand("plan", command);
registerCommand(component.planCommand().build());
if (system != null) {
system.getProcessing().submitNonCritical(() -> system.getListenerSystem().callEnableEvent(this));
}
@ -145,6 +145,22 @@ public class Plan extends BukkitPlugin implements PlanPlugin {
return reloading;
}
@Override
public void registerCommand(Subcommand command) {
if (command == null) {
logger.warn("Attempted to register a null command!");
return;
}
for (String name : command.getAliases()) {
PluginCommand registering = getCommand(name);
if (registering == null) {
logger.warn("Attempted to register '" + name + "'-command, but it is not in plugin.yml!");
continue;
}
registering.setExecutor(new BukkitCommand(command));
}
}
/**
* @deprecated Deprecated due to use of APF Config
*/

View File

@ -0,0 +1,56 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.use;
import org.bukkit.command.CommandSender;
import java.util.Optional;
import java.util.UUID;
public class BukkitCMDSender implements CMDSender {
final CommandSender sender;
public BukkitCMDSender(CommandSender sender) {
this.sender = sender;
}
@Override
public MessageBuilder buildMessage() {
return new BukkitPartBuilder(this);
}
@Override
public Optional<String> getPlayerName() {
return Optional.empty();
}
@Override
public boolean hasPermission(String permission) {
return sender.hasPermission(permission);
}
@Override
public Optional<UUID> getUUID() {
return Optional.empty();
}
@Override
public void send(String msg) {
sender.sendMessage(msg);
}
}

View File

@ -0,0 +1,42 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.use;
import com.djrapitops.plan.commands.Arguments;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class BukkitCommand implements CommandExecutor {
private final Subcommand command;
public BukkitCommand(Subcommand command) {
this.command = command;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
command.getExecutor().accept(new BukkitPlayerCMDSender((Player) sender), new Arguments(args));
} else {
command.getExecutor().accept(new BukkitCMDSender(sender), new Arguments(args));
}
return true;
}
}

View File

@ -0,0 +1,107 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.use;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import java.util.Arrays;
import java.util.Collection;
class BukkitPartBuilder implements MessageBuilder {
private final BukkitPartBuilder previous;
private final ComponentBuilder part;
private BukkitCMDSender sender;
public BukkitPartBuilder(BukkitCMDSender sender) {
this((BukkitPartBuilder) null);
this.sender = sender;
}
public BukkitPartBuilder(BukkitPartBuilder previous) {
this.part = new ComponentBuilder("");
this.previous = previous;
}
@Override
public MessageBuilder addPart(String text) {
BukkitPartBuilder nextPart = new BukkitPartBuilder(this);
nextPart.part.appendLegacy(text);
return nextPart;
}
@Override
public MessageBuilder newLine() {
part.append("\n");
return new BukkitPartBuilder(this);
}
@Override
public MessageBuilder link(String url) {
part.event(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
return this;
}
@Override
public MessageBuilder command(String command) {
part.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command));
return this;
}
@Override
public MessageBuilder hover(String text) {
part.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(text).create()));
return this;
}
@Override
public MessageBuilder hover(String... lines) {
return hover(Arrays.asList(lines));
}
@Override
public MessageBuilder hover(Collection<String> lines) {
ComponentBuilder hoverMsg = new ComponentBuilder("");
for (String line : lines) {
hoverMsg.append(line + "\n");
}
part.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverMsg.create()));
return this;
}
@Override
public MessageBuilder indent(int i) {
return this;
}
@Override
public MessageBuilder tabular(CharSequence charSequence) {
return this;
}
@Override
public void send() {
if (sender != null) {
sender.sender.sendMessage(part.create());
} else if (previous != null) {
previous.part.append(part.create());
previous.send();
}
}
}

View File

@ -0,0 +1,47 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.use;
import org.bukkit.entity.Player;
import java.util.Optional;
import java.util.UUID;
public class BukkitPlayerCMDSender extends BukkitCMDSender {
final Player player;
public BukkitPlayerCMDSender(Player player) {
super(player);
this.player = player;
}
@Override
public Optional<String> getPlayerName() {
return Optional.of(player.getName());
}
@Override
public boolean hasPermission(String permission) {
return player.hasPermission(permission);
}
@Override
public Optional<UUID> getUUID() {
return Optional.of(player.getUniqueId());
}
}

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.modules.bukkit;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plugin.command.CommandNode;
import dagger.Binds;
import dagger.Module;
@ -38,5 +38,5 @@ public interface BukkitPlanModule {
@Binds
@Named("mainCommand")
CommandNode bindMainCommand(PlanCommand command);
CommandNode bindMainCommand(OldPlanCommand command);
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanProxyCommand;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.PluginLang;
@ -90,6 +91,18 @@ public class PlanBungee extends BungeePlugin implements PlanPlugin {
// Nothing to be done, systems are disabled
}
@Override
public void registerCommand(Subcommand command) {
if (command == null) {
logger.warn("Attempted to register a null command!");
return;
}
for (String name : command.getAliases()) {
throw new UnsupportedOperationException();
// getProxy().getPluginManager().registerCommand(this, new BungeeCommand(command, name));
}
}
@Override
public InputStream getResource(String resource) {
return getResourceAsStream(resource);

View File

@ -16,6 +16,7 @@
*/
package com.djrapitops.plan;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plugin.IPlugin;
import com.djrapitops.plugin.command.ColorScheme;
@ -44,4 +45,6 @@ public interface PlanPlugin extends IPlugin {
default boolean isSystemEnabled() {
return getSystem().isEnabled();
}
void registerCommand(Subcommand command);
}

View File

@ -16,6 +16,8 @@
*/
package com.djrapitops.plan.commands;
import org.apache.commons.text.TextStringBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -69,4 +71,12 @@ public class Arguments {
if (!copy.isEmpty()) copy.remove(0);
return new Arguments(copy);
}
public String concatenate(String separator) {
return new TextStringBuilder().appendWithSeparators(args, separator).build();
}
public boolean isEmpty() {
return args.isEmpty();
}
}

View File

@ -0,0 +1,139 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands;
import com.djrapitops.plan.commands.subcommands.*;
import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.PluginSettings;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.DeepHelpLang;
import com.djrapitops.plugin.command.ColorScheme;
import com.djrapitops.plugin.command.CommandNode;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.TreeCmdNode;
import dagger.Lazy;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* TreeCommand for the /plan command, and all SubCommands.
* <p>
* Uses the Abstract Plugin Framework for easier command management.
*
* @author Rsl1122
*/
@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> webUserCommand;
private final RegisterCommand registerCommand;
private final UnregisterCommand unregisterCommand;
private final InfoCommand infoCommand;
private final ReloadCommand reloadCommand;
private final Lazy<ManageCommand> manageCommand;
private final DevCommand devCommand;
private boolean commandsRegistered;
@Inject
public OldPlanCommand(
ColorScheme colorScheme,
Locale locale,
PlanConfig config,
// Group 1
InspectCommand inspectCommand,
QInspectCommand qInspectCommand,
SearchCommand searchCommand,
ListPlayersCommand listPlayersCommand,
AnalyzeCommand analyzeCommand,
NetworkCommand networkCommand,
ListServersCommand listServersCommand,
// Group 2
Lazy<WebUserCommand> webUserCommand,
RegisterCommand registerCommand,
UnregisterCommand unregisterCommand,
// Group 3
InfoCommand infoCommand,
ReloadCommand reloadCommand,
Lazy<ManageCommand> manageCommand,
DevCommand devCommand
) {
super("plan", "", CommandType.CONSOLE, null);
this.unregisterCommand = unregisterCommand;
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;
this.reloadCommand = reloadCommand;
this.manageCommand = manageCommand;
this.devCommand = devCommand;
getHelpCommand().setPermission(Permissions.HELP.getPermission());
setDefaultCommand("inspect");
setColorScheme(colorScheme);
setInDepthHelp(locale.getArray(DeepHelpLang.PLAN));
}
public void registerCommands() {
if (commandsRegistered) {
return;
}
CommandNode[] analyticsGroup = {
inspectCommand,
qInspectCommand,
searchCommand,
listPlayersCommand,
analyzeCommand,
networkCommand,
listServersCommand
};
CommandNode[] webGroup = {
webUserCommand.get(),
registerCommand,
unregisterCommand
};
CommandNode[] manageGroup = {
infoCommand,
reloadCommand,
manageCommand.get(),
config.isTrue(PluginSettings.DEV_MODE) ? devCommand : null
};
setNodeGroups(analyticsGroup, webGroup, manageGroup);
commandsRegistered = true;
}
}

View File

@ -16,124 +16,116 @@
*/
package com.djrapitops.plan.commands;
import com.djrapitops.plan.commands.subcommands.*;
import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.PluginSettings;
import com.djrapitops.plan.commands.subcommands.LinkCommands;
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.settings.locale.lang.DeepHelpLang;
import com.djrapitops.plugin.command.ColorScheme;
import com.djrapitops.plugin.command.CommandNode;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.TreeCmdNode;
import dagger.Lazy;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Collections;
import java.util.List;
/**
* TreeCommand for the /plan command, and all SubCommands.
* <p>
* Uses the Abstract Plugin Framework for easier command management.
*
* @author Rsl1122
*/
@Singleton
public class PlanCommand extends TreeCmdNode {
public class PlanCommand {
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> webUserCommand;
private final RegisterCommand registerCommand;
private final UnregisterCommand unregisterCommand;
private final InfoCommand infoCommand;
private final ReloadCommand reloadCommand;
private final Lazy<ManageCommand> manageCommand;
private final DevCommand devCommand;
private boolean commandsRegistered;
private final Locale locale;
private final ColorScheme colors;
private final LinkCommands linkCommands;
@Inject
public PlanCommand(
ColorScheme colorScheme,
Locale locale,
PlanConfig config,
// Group 1
InspectCommand inspectCommand,
QInspectCommand qInspectCommand,
SearchCommand searchCommand,
ListPlayersCommand listPlayersCommand,
AnalyzeCommand analyzeCommand,
NetworkCommand networkCommand,
ListServersCommand listServersCommand,
// Group 2
Lazy<WebUserCommand> webUserCommand,
RegisterCommand registerCommand,
UnregisterCommand unregisterCommand,
// Group 3
InfoCommand infoCommand,
ReloadCommand reloadCommand,
Lazy<ManageCommand> manageCommand,
DevCommand devCommand
ColorScheme colors,
LinkCommands linkCommands
) {
super("plan", "", CommandType.CONSOLE, null);
this.unregisterCommand = unregisterCommand;
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;
this.reloadCommand = reloadCommand;
this.manageCommand = manageCommand;
this.devCommand = devCommand;
getHelpCommand().setPermission(Permissions.HELP.getPermission());
setDefaultCommand("inspect");
setColorScheme(colorScheme);
setInDepthHelp(locale.getArray(DeepHelpLang.PLAN));
this.locale = locale;
this.colors = colors;
this.linkCommands = linkCommands;
}
public void registerCommands() {
if (commandsRegistered) {
return;
private void handleException(RuntimeException error, CMDSender sender) {
if (error instanceof IllegalArgumentException) {
sender.send("§c" + error.getMessage());
} else {
throw error;
}
CommandNode[] analyticsGroup = {
inspectCommand,
qInspectCommand,
searchCommand,
listPlayersCommand,
analyzeCommand,
networkCommand,
listServersCommand
};
CommandNode[] webGroup = {
webUserCommand.get(),
registerCommand,
unregisterCommand
};
CommandNode[] manageGroup = {
infoCommand,
reloadCommand,
manageCommand.get(),
config.isTrue(PluginSettings.DEV_MODE) ? devCommand : null
};
setNodeGroups(analyticsGroup, webGroup, manageGroup);
commandsRegistered = true;
}
public CommandWithSubcommands build() {
return CommandWithSubcommands.builder()
.alias("plan")
.colorScheme(colors)
.subcommand(serverCommand())
.subcommand(playerCommand())
.subcommand(playersCommand())
.subcommand(networkCommand())
.exceptionHandler(this::handleException)
.build();
}
public List<String> serverNames(CMDSender sender, Arguments arguments) {
return Collections.emptyList(); // TODO
}
private List<String> playerNames(CMDSender sender, Arguments arguments) {
return Collections.emptyList(); // TODO
}
private Subcommand serverCommand() {
return Subcommand.builder()
.aliases("server", "analyze", "a", "analyse", "analysis")
.optionalArgument("server", "Name, ID or UUID of a server")
.requirePermission("plan.server")
.description("View a server page")
.inDepthDescription("Obtain a link to the /server page of a specific server, or the current server if no arguments are given.")
.onTabComplete(this::serverNames)
.onCommand(linkCommands::onServerCommand)
.build();
}
private Subcommand serversCommand() {
return Subcommand.builder()
.aliases("servers", "serverlist", "listservers", "sl", "ls")
.requirePermission("plan.servers")
.description("List servers")
.inDepthDescription("List ids, names and uuids of servers in the database.")
.onCommand(linkCommands::onServersCommand)
.build();
}
private Subcommand playerCommand() {
return Subcommand.builder()
.aliases("player", "inspect")
.optionalArgument("name/uuid", "Name or UUID of a player")
.requirePermission("plan.player.self")
.description("View a player page")
.inDepthDescription("Obtain a link to the /player page of a specific player, or the current player.")
.onTabComplete(this::playerNames)
.onCommand(linkCommands::onPlayerCommand)
.build();
}
private Subcommand playersCommand() {
return Subcommand.builder()
.aliases("players", "pl", "playerlist", "list")
.requirePermission("plan.player.other")
.description("View players page")
.inDepthDescription("Obtain a link to the /players page to see a list of players.")
.onCommand(linkCommands::onPlayersCommand)
.build();
}
private Subcommand networkCommand() {
return Subcommand.builder()
.aliases("network", "n", "netw")
.requirePermission("plan.network")
.description("View network page")
.inDepthDescription("Obtain a link to the /network page, only does so on networks.")
.onCommand(linkCommands::onNetworkCommand)
.build();
}
}

View File

@ -0,0 +1,148 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.subcommands;
import com.djrapitops.plan.commands.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;
import com.djrapitops.plan.identification.Server;
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.queries.objects.ServerQueries;
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
import com.djrapitops.plugin.command.ColorScheme;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.UUID;
@Singleton
public class LinkCommands {
private final Locale locale;
private final ColorScheme colors;
private final Addresses addresses;
private final Identifiers identifiers;
private final DBSystem dbSystem;
private final ServerInfo serverInfo;
@Inject
public LinkCommands(
Locale locale,
ColorScheme colorScheme,
Addresses addresses,
Identifiers identifiers,
DBSystem dbSystem,
ServerInfo serverInfo
) {
this.locale = locale;
this.colors = colorScheme;
this.addresses = addresses;
this.identifiers = identifiers;
this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
}
private String getAddress(CMDSender sender) {
return addresses.getMainAddress().orElseGet(() -> {
sender.send(locale.getString(CommandLang.NO_ADDRESS_NOTIFY));
return addresses.getFallbackLocalhostAddress();
});
}
public void onServerCommand(CMDSender sender, Arguments arguments) {
Server server;
String identifier = arguments.concatenate(" ");
if (arguments.isEmpty()) {
server = serverInfo.getServer();
} else {
server = dbSystem.getDatabase()
.query(ServerQueries.fetchServerMatchingIdentifier(identifier))
.filter(s -> !s.isProxy())
.orElseThrow(() -> new IllegalArgumentException("Server '" + identifier + "' was not found from the database."));
}
String address = getAddress(sender);
String target = "/server/" + Html.encodeToURL(server.getName());
sender.buildMessage()
.addPart(colors.getMainColor() + "View server page: ")
.addPart(colors.getTertiaryColor() + "§l[Link]").link(address + target).hover(address + target)
.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."));
}
public void onServersCommand(CMDSender sender, Arguments arguments) {
String m = colors.getMainColor();
String t = colors.getTertiaryColor();
String serversListed = dbSystem.getDatabase()
.query(ServerQueries.fetchPlanServerInformationCollection())
.stream().sorted()
.map(server -> server.getId() + ":" + server.getName() + ":" + server.getUuid() + "\n")
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();
String table = ChatFormatter
MessageBuilder message = sender.buildMessage().addPart(t + '>' + m + " Servers; id : name : uuid").newLine();
}
}

View File

@ -31,6 +31,8 @@ public class CommandWithSubcommands extends Subcommand {
private final List<Subcommand> subcommands;
private BiConsumer<CMDSender, Arguments> fallback;
private BiConsumer<RuntimeException, CMDSender> exceptionHandler;
private ColorScheme colors;
private CommandWithSubcommands() {
subcommands = new ArrayList<>();
@ -41,13 +43,11 @@ public class CommandWithSubcommands extends Subcommand {
}
public void onHelp(CMDSender sender, Arguments arguments) {
MessageBuilder message = sender.buildMessage()
.addPart("Header").newLine().newLine();
new HelpFormatter(/* TODO */new ColorScheme("§2", "§7", "§f"), getPrimaryAlias(),
subcommands.stream().filter(sender::hasAllPermissionsFor).collect(Collectors.toList()))
.addSubcommands(message);
message.newLine().addPart("Footer")
List<Subcommand> hasPermissionFor = subcommands.stream().filter(sender::hasAllPermissionsFor).collect(Collectors.toList());
sender.buildMessage()
.addPart("Header").newLine().newLine()
.apply(new HelpFormatter(colors, getPrimaryAlias(), hasPermissionFor)::addSubcommands)
.newLine().addPart("Footer")
.send();
}
@ -56,6 +56,14 @@ public class CommandWithSubcommands extends Subcommand {
sender.send(/* TODO */"NO PERMISSION");
return;
}
try {
executeCommand(sender, arguments);
} catch (RuntimeException e) {
exceptionHandler.accept(e, sender);
}
}
public void executeCommand(CMDSender sender, Arguments arguments) {
Optional<String> gotAlias = arguments.get(0);
if (gotAlias.isPresent()) {
String alias = gotAlias.get();
@ -69,7 +77,7 @@ public class CommandWithSubcommands extends Subcommand {
continue;
}
subcommand.getExecutor().accept(sender, arguments.removeFirst());
break;
return;
}
}
}
@ -101,7 +109,7 @@ public class CommandWithSubcommands extends Subcommand {
return options;
}
public static class Builder extends Subcommand.Builder {
public static class Builder extends Subcommand.Builder<Builder> {
private final CommandWithSubcommands command;
private Builder() {
@ -118,6 +126,11 @@ public class CommandWithSubcommands extends Subcommand {
return this;
}
public Builder subcommandOnCondition(boolean condition, Subcommand subcommand) {
if (condition) command.subcommands.add(subcommand);
return this;
}
public Builder fallback(BiConsumer<CMDSender, Arguments> executor) {
command.fallback = executor;
return this;
@ -136,11 +149,23 @@ public class CommandWithSubcommands extends Subcommand {
});
}
public Builder exceptionHandler(BiConsumer<RuntimeException, CMDSender> exceptionHandler) {
command.exceptionHandler = exceptionHandler;
return this;
}
public Builder colorScheme(ColorScheme colors) {
command.colors = colors;
return this;
}
public CommandWithSubcommands build() {
super.build();
if (command.fallback == null) fallback(command::onHelp);
onCommand(command::onCommand);
onTabComplete(command::onTabComplete);
super.build();
if (command.fallback == null) fallback(command::onHelp);
if (command.exceptionHandler == null) exceptionHandler((error, sender) -> {throw error;});
if (command.colors == null) colorScheme(new ColorScheme("§2", "§7", "§f"));
return command;
}
}

View File

@ -17,20 +17,17 @@
package com.djrapitops.plan.commands.use;
import java.util.Collection;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
public interface MessageBuilder {
MessageBuilder defineColorCharacter(char colorChar);
MessageBuilder addPart(String msg);
MessageBuilder newLine();
MessageBuilder link(String address);
MessageBuilder click(Supplier<UUID> clicker);
MessageBuilder command(String command);
MessageBuilder hover(String text);
@ -42,6 +39,10 @@ public interface MessageBuilder {
MessageBuilder tabular(CharSequence columnSeparator);
default MessageBuilder apply(UnaryOperator<MessageBuilder> operation) {
return operation.apply(this);
}
void send();
}

View File

@ -41,7 +41,7 @@ public class Subcommand {
}
public static SubcommandBuilder builder() {
return new Builder();
return new Builder<>();
}
public String getPrimaryAlias() {
@ -76,7 +76,7 @@ public class Subcommand {
return argumentResolver;
}
public static class Builder implements SubcommandBuilder {
public static class Builder<T extends SubcommandBuilder> implements SubcommandBuilder {
private final Subcommand subcommand;
private Builder() {
@ -88,60 +88,60 @@ public class Subcommand {
}
@Override
public SubcommandBuilder alias(String alias) {
public T alias(String alias) {
subcommand.aliases.add(alias);
if (subcommand.primaryAlias == null) subcommand.primaryAlias = alias;
return this;
return (T) this;
}
@Override
public SubcommandBuilder aliases(String... aliases) {
public T aliases(String... aliases) {
for (String alias : aliases) {
alias(alias);
}
return this;
return (T) this;
}
@Override
public SubcommandBuilder requirePermission(String permission) {
public T requirePermission(String permission) {
subcommand.requiredPermissions.add(permission);
return this;
return (T) this;
}
@Override
public SubcommandBuilder description(String description) {
public T description(String description) {
subcommand.description = description;
return this;
return (T) this;
}
@Override
public SubcommandBuilder inDepthDescription(String... lines) {
public T inDepthDescription(String... lines) {
subcommand.inDepthDescription.addAll(Arrays.asList(lines));
return this;
return (T) this;
}
@Override
public SubcommandBuilder requiredArgument(String name, String description) {
public T requiredArgument(String name, String description) {
subcommand.arguments.add(new ArgumentDescriptor(name, description, true));
return this;
return (T) this;
}
@Override
public SubcommandBuilder optionalArgument(String name, String description) {
public T optionalArgument(String name, String description) {
subcommand.arguments.add(new ArgumentDescriptor(name, description, false));
return this;
return (T) this;
}
@Override
public SubcommandBuilder onCommand(BiConsumer<CMDSender, Arguments> executor) {
public T onCommand(BiConsumer<CMDSender, Arguments> executor) {
subcommand.executor = executor;
return this;
return (T) this;
}
@Override
public SubcommandBuilder onTabComplete(BiFunction<CMDSender, Arguments, List<String>> resolver) {
public T onTabComplete(BiFunction<CMDSender, Arguments, List<String>> resolver) {
subcommand.argumentResolver = resolver;
return this;
return (T) this;
}
@Override

View File

@ -52,17 +52,31 @@ public class Identifiers {
* @throws BadRequestException If server parameter is not defined or the server is not in the database.
*/
public UUID getServerUUID(Request request) {
String serverIndentifier = request.getQuery().get("server")
String identifier = request.getQuery().get("server")
.orElseThrow(() -> new BadRequestException("'server' parameter was not defined."));
Optional<UUID> parsed = UUIDUtility.parseFromString(serverIndentifier);
return parsed.orElseGet(() -> getServerUUIDFromName(serverIndentifier));
Optional<UUID> parsed = UUIDUtility.parseFromString(identifier);
return parsed.orElseGet(() -> getServerUUIDFromName(identifier).orElseThrow(
() -> new BadRequestException("Given 'server' was not found in the database: '" + identifier + "'")
));
}
private UUID getServerUUIDFromName(String serverName) {
/**
* Obtain UUID of the server.
*
* @param identifier Identifier (name or uuid string) of the server
* @return UUID of the server.
* @throws BadRequestException If the server is not in the database.
*/
public Optional<UUID> getServerUUID(String identifier) {
Optional<UUID> parsed = UUIDUtility.parseFromString(identifier);
if (parsed.isPresent()) return parsed;
return getServerUUIDFromName(identifier);
}
private Optional<UUID> getServerUUIDFromName(String serverName) {
return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverName))
.map(Server::getUuid)
.orElseThrow(() -> new BadRequestException("Given 'server' was not found in the database: '" + serverName + "'"));
.map(Server::getUuid);
}
/**

View File

@ -18,7 +18,7 @@ package utilities.dagger;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.PlaceholderModule;
import com.djrapitops.plan.modules.SystemObjectProvidingModule;
@ -43,7 +43,7 @@ import javax.inject.Singleton;
PluginSuperClassBindingModule.class
})
public interface PlanPluginComponent {
PlanCommand planCommand();
OldPlanCommand planCommand();
PlanSystem system();

View File

@ -16,7 +16,7 @@
*/
package utilities.dagger;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plan.gathering.importing.EmptyImportSystem;
import com.djrapitops.plan.gathering.importing.ImportSystem;
import com.djrapitops.plan.identification.ServerInfo;
@ -39,7 +39,7 @@ public interface PlanPluginModule {
@Binds
@Named("mainCommand")
CommandNode bindMainCommand(PlanCommand command);
CommandNode bindMainCommand(OldPlanCommand command);
@Binds
ImportSystem bindImportSystem(EmptyImportSystem emptyImportSystem);

View File

@ -17,7 +17,8 @@
package com.djrapitops.plan;
import com.djrapitops.plan.addons.placeholderapi.NukkitPlaceholderRegistrar;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.settings.locale.Locale;
@ -71,7 +72,7 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin {
logger.error("This error should be reported at https://github.com/Rsl1122/Plan-PlayerAnalytics/issues");
onDisable();
}
PlanCommand command = component.planCommand();
OldPlanCommand command = component.planCommand();
command.registerCommands();
registerCommand("plan", command);
if (system != null) {
@ -114,6 +115,11 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin {
return reloading;
}
@Override
public void registerCommand(Subcommand subcommand) {
throw new UnsupportedOperationException();
}
@Override
public PlanSystem getSystem() {
return system;

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan;
import com.djrapitops.plan.addons.placeholderapi.NukkitPlaceholderRegistrar;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.PlaceholderModule;
@ -47,7 +47,7 @@ import javax.inject.Singleton;
})
public interface PlanNukkitComponent {
PlanCommand planCommand();
OldPlanCommand planCommand();
PlanSystem system();

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.modules.nukkit;
import com.djrapitops.plan.PlanNukkit;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plugin.command.CommandNode;
import dagger.Binds;
import dagger.Module;
@ -38,5 +38,5 @@ public interface NukkitPlanModule {
@Binds
@Named("mainCommand")
CommandNode bindMainCommand(PlanCommand command);
CommandNode bindMainCommand(OldPlanCommand command);
}

View File

@ -16,7 +16,8 @@
*/
package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.settings.locale.Locale;
@ -107,7 +108,7 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
logger.error("This error should be reported at https://github.com/Rsl1122/Plan-PlayerAnalytics/issues");
onDisable();
}
PlanCommand command = component.planCommand();
OldPlanCommand command = component.planCommand();
command.registerCommands();
registerCommand("plan", command);
if (system != null) {
@ -147,6 +148,11 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
return false;
}
@Override
public void registerCommand(Subcommand subcommand) {
throw new UnsupportedOperationException();
}
@Override
public Logger getLogger() {
return slf4jLogger;

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.modules.APFModule;
import com.djrapitops.plan.modules.PlaceholderModule;
@ -46,7 +46,7 @@ import javax.inject.Singleton;
})
public interface PlanSpongeComponent {
PlanCommand planCommand();
OldPlanCommand planCommand();
PlanSystem system();

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.modules.sponge;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.PlanSponge;
import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.commands.OldPlanCommand;
import com.djrapitops.plugin.command.CommandNode;
import dagger.Binds;
import dagger.Module;
@ -38,5 +38,5 @@ public interface SpongePlanModule {
@Binds
@Named("mainCommand")
CommandNode bindMainCommand(PlanCommand command);
CommandNode bindMainCommand(OldPlanCommand command);
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan;
import com.djrapitops.plan.commands.PlanProxyCommand;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.PluginLang;
@ -112,6 +113,11 @@ public class PlanVelocity extends VelocityPlugin implements PlanPlugin {
// Nothing to be done, systems are disabled
}
@Override
public void registerCommand(Subcommand subcommand) {
throw new UnsupportedOperationException();
}
@Override
public InputStream getResource(String resource) {
return getClass().getResourceAsStream("/" + resource);