From e95e84a89b7d5f394b2bc9b7381707ef7ae50f1c Mon Sep 17 00:00:00 2001 From: Rsl1122 <24460436+Rsl1122@users.noreply.github.com> Date: Mon, 13 Jul 2020 21:36:57 +0300 Subject: [PATCH] Implemented Bungee command adapters - Fixed checkstyle errors - planbungee & planvelocity now aliases to new 'planproxy' command --- .../java/com/djrapitops/plan/PlanBungee.java | 9 +- .../djrapitops/plan/PlanBungeeComponent.java | 4 +- .../plan/command/use/BungeeCMDSender.java | 66 +++++++++++ .../plan/command/use/BungeeCommand.java | 56 +++++++++ .../plan/command/use/BungeePartBuilder.java | 108 ++++++++++++++++++ .../command/use/BungeePlayerCMDSender.java | 52 +++++++++ .../modules/bungee/BungeeCommandModule.java | 2 +- .../djrapitops/plan/commands/PlanCommand.java | 8 +- .../plan/utilities/chat/ChatFormatter.java | 43 +++++-- .../plan/utilities/chat/DefaultFontInfo.java | 20 +++- .../velocity/VelocityCommandModule.java | 2 +- 11 files changed, 348 insertions(+), 22 deletions(-) create mode 100644 Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCMDSender.java create mode 100644 Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCommand.java create mode 100644 Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePartBuilder.java create mode 100644 Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePlayerCMDSender.java diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungee.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungee.java index e29ea7195..dcca56d3e 100644 --- a/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungee.java +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungee.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan; -import com.djrapitops.plan.commands.PlanProxyCommand; +import com.djrapitops.plan.command.use.BungeeCommand; import com.djrapitops.plan.commands.use.Subcommand; import com.djrapitops.plan.exceptions.EnableException; import com.djrapitops.plan.settings.locale.Locale; @@ -66,9 +66,7 @@ public class PlanBungee extends BungeePlugin implements PlanPlugin { logger.error("This error should be reported at https://github.com/Rsl1122/Plan-PlayerAnalytics/issues"); onDisable(); } - PlanProxyCommand command = component.planCommand(); - command.registerCommands(); - registerCommand("planbungee", command); + registerCommand(component.planCommand().build()); if (system != null) { system.getProcessing().submitNonCritical(() -> system.getListenerSystem().callEnableEvent(this)); } @@ -98,8 +96,7 @@ public class PlanBungee extends BungeePlugin implements PlanPlugin { return; } for (String name : command.getAliases()) { - throw new UnsupportedOperationException(); -// getProxy().getPluginManager().registerCommand(this, new BungeeCommand(command, name)); + getProxy().getPluginManager().registerCommand(this, new BungeeCommand(runnableFactory, command, name)); } } diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java index 8ca225841..81223dffb 100644 --- a/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan; -import com.djrapitops.plan.commands.PlanProxyCommand; +import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.modules.APFModule; import com.djrapitops.plan.modules.PlaceholderModule; import com.djrapitops.plan.modules.ProxySuperClassBindingModule; @@ -49,7 +49,7 @@ import javax.inject.Singleton; }) public interface PlanBungeeComponent { - PlanProxyCommand planCommand(); + PlanCommand planCommand(); PlanSystem system(); diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCMDSender.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCMDSender.java new file mode 100644 index 000000000..380637e89 --- /dev/null +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCMDSender.java @@ -0,0 +1,66 @@ +/* + * 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.command.use; + +import com.djrapitops.plan.commands.use.CMDSender; +import com.djrapitops.plan.commands.use.ChatFormatter; +import com.djrapitops.plan.commands.use.ConsoleChatFormatter; +import com.djrapitops.plan.commands.use.MessageBuilder; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.chat.TextComponent; + +import java.util.Optional; +import java.util.UUID; + +public class BungeeCMDSender implements CMDSender { + + CommandSender sender; + + public BungeeCMDSender(CommandSender sender) { + this.sender = sender; + } + + @Override + public Optional getPlayerName() { + return Optional.empty(); + } + + @Override + public boolean hasPermission(String permission) { + return sender.hasPermission(permission); + } + + @Override + public Optional getUUID() { + return Optional.empty(); + } + + @Override + public MessageBuilder buildMessage() { + return new BungeePartBuilder(this); + } + + @Override + public void send(String message) { + sender.sendMessage(new TextComponent(message)); + } + + @Override + public ChatFormatter getFormatter() { + return new ConsoleChatFormatter(); + } +} diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCommand.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCommand.java new file mode 100644 index 000000000..c9d70a14f --- /dev/null +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeeCommand.java @@ -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 . + */ +package com.djrapitops.plan.command.use; + +import com.djrapitops.plan.commands.use.Arguments; +import com.djrapitops.plan.commands.use.Subcommand; +import com.djrapitops.plugin.task.AbsRunnable; +import com.djrapitops.plugin.task.RunnableFactory; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.plugin.Command; + +public class BungeeCommand extends Command { + + private final RunnableFactory runnableFactory; + private final Subcommand command; + + public BungeeCommand( + RunnableFactory runnableFactory, + Subcommand command, + String name + ) { + super(name); + this.runnableFactory = runnableFactory; + + this.command = command; + } + + @Override + public void execute(CommandSender sender, String[] args) { + runnableFactory.create("", new AbsRunnable() { + @Override + public void run() { + if (sender instanceof ProxiedPlayer) { + command.getExecutor().accept(new BungeePlayerCMDSender((ProxiedPlayer) sender), new Arguments(args)); + } else { + command.getExecutor().accept(new BungeeCMDSender(sender), new Arguments(args)); + } + } + }).runTaskAsynchronously(); + } +} diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePartBuilder.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePartBuilder.java new file mode 100644 index 000000000..674a32e76 --- /dev/null +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePartBuilder.java @@ -0,0 +1,108 @@ +/* + * 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.command.use; + +import com.djrapitops.plan.commands.use.MessageBuilder; +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 BungeePartBuilder implements MessageBuilder { + + private final BungeePartBuilder previous; + private final ComponentBuilder part; + private BungeeCMDSender sender; + + public BungeePartBuilder(BungeeCMDSender sender) { + this((BungeePartBuilder) null); + this.sender = sender; + } + + public BungeePartBuilder(BungeePartBuilder previous) { + this.part = new ComponentBuilder(""); + this.previous = previous; + } + + @Override + public MessageBuilder addPart(String text) { + BungeePartBuilder nextPart = new BungeePartBuilder(this); + nextPart.part.appendLegacy(text); + return nextPart; + } + + @Override + public MessageBuilder newLine() { + part.append("\n"); + return new BungeePartBuilder(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 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(); + } + } +} diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePlayerCMDSender.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePlayerCMDSender.java new file mode 100644 index 000000000..1d90f289e --- /dev/null +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/command/use/BungeePlayerCMDSender.java @@ -0,0 +1,52 @@ +/* + * 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.command.use; + +import com.djrapitops.plan.commands.use.ChatFormatter; +import com.djrapitops.plan.commands.use.PlayerChatFormatter; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.Optional; +import java.util.UUID; + +public class BungeePlayerCMDSender extends BungeeCMDSender { + + private final ProxiedPlayer player; + + public BungeePlayerCMDSender(ProxiedPlayer player) { + super(player); + + this.player = player; + } + + @Override + public Optional getPlayerName() { + return Optional.of(player.getName()); + } + + @Override + public Optional getUUID() { + return Optional.of(player.getUniqueId()); + } + + @Override + public ChatFormatter getFormatter() { + return new PlayerChatFormatter(); + } + + +} diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeCommandModule.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeCommandModule.java index 9c5952c9b..c38c3b056 100644 --- a/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeCommandModule.java +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeCommandModule.java @@ -27,6 +27,6 @@ public class BungeeCommandModule { @Provides @Named("mainCommandName") String provideMainCommandName() { - return "planbungee"; + return "planproxy"; } } 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 4cee2a203..6eecda5cf 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 @@ -93,7 +93,7 @@ public class PlanCommand { } public CommandWithSubcommands build() { - return CommandWithSubcommands.builder() + CommandWithSubcommands command = CommandWithSubcommands.builder() .alias(commandName) .colorScheme(colors) .subcommand(serverCommand()) @@ -119,6 +119,12 @@ public class PlanCommand { .subcommand(importCommand()) .exceptionHandler(this::handleException) .build(); + if (!"plan".equalsIgnoreCase(commandName)) { + command.getAliases().add("planbungee"); + command.getAliases().add("planvelocity"); + command.getAliases().add("planproxy"); + } + return command; } public List serverNames(CMDSender sender, Arguments arguments) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java index 9248356c4..85bf8bec2 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java @@ -1,3 +1,19 @@ +/* + * 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.chat; import org.apache.commons.lang3.StringUtils; @@ -45,15 +61,7 @@ public class ChatFormatter { for (String line : lines) { table.add(StringUtils.split(line, separator, columns)); } - int[] biggestWidth = new int[columns]; - for (String[] line : table) { - for (int i = 0; i < line.length; i++) { - int width = getPxMessageWidth(line[i]); - if (biggestWidth[i] < width) { - biggestWidth[i] = width; - } - } - } + int[] biggestWidth = getBiggestWidthsForColumns(columns, table); for (String[] line : table) { StringBuilder lineBuilder = new StringBuilder(); @@ -71,6 +79,21 @@ public class ChatFormatter { return returnMessage.toString(); } + private static int[] getBiggestWidthsForColumns(int columns, List table) { + int[] biggestWidth = new int[columns]; + for (String[] line : table) { + for (int i = 0; i < line.length; i++) { + int width = getPxMessageWidth(line[i]); + if (biggestWidth[i] < width) { + biggestWidth[i] = width; + } + } + } + return biggestWidth; + } + + // Checkstyle.OFF: CyclomaticComplexity + public static String getLastStyle(String message) { boolean wasColorChar = false; char color = ' '; @@ -133,6 +156,8 @@ public class ChatFormatter { return (color == ' ' ? "§" + color : "") + (k ? "§k" : "") + (l ? "§l" : "") + (m ? "§m" : "") + (n ? "§n" : "") + (o ? "§o" : ""); } + // Checkstyle.ON: CyclomaticComplexity + public static String center(String message) { if (message == null) return null; if (message.isEmpty()) return ""; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java index 64e77ee71..f03c8a6e1 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java @@ -1,3 +1,19 @@ +/* + * 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.chat; /** @@ -104,8 +120,8 @@ public enum DefaultFontInfo { SPACE(' ', 3), DEFAULT('a', 4); - private char character; - private int length; + private final char character; + private final int length; DefaultFontInfo(char character, int length) { this.character = character; diff --git a/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityCommandModule.java b/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityCommandModule.java index 32bb6f429..37878b8d4 100644 --- a/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityCommandModule.java +++ b/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityCommandModule.java @@ -27,7 +27,7 @@ public class VelocityCommandModule { @Provides @Named("mainCommandName") String provideMainCommandName() { - return "planvelocity"; + return "planproxy"; } }