Implemented Bungee command adapters

- Fixed checkstyle errors
- planbungee & planvelocity now aliases to new
  'planproxy' command
This commit is contained in:
Rsl1122 2020-07-13 21:36:57 +03:00 committed by Risto Lahtela
parent b47fde8e97
commit e95e84a89b
11 changed files with 348 additions and 22 deletions

View File

@ -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));
}
}

View File

@ -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();

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<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 MessageBuilder buildMessage() {
return new BungeePartBuilder(this);
}
@Override
public void send(String message) {
sender.sendMessage(new TextComponent(message));
}
@Override
public ChatFormatter getFormatter() {
return new ConsoleChatFormatter();
}
}

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.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();
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<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,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 <https://www.gnu.org/licenses/>.
*/
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<String> getPlayerName() {
return Optional.of(player.getName());
}
@Override
public Optional<UUID> getUUID() {
return Optional.of(player.getUniqueId());
}
@Override
public ChatFormatter getFormatter() {
return new PlayerChatFormatter();
}
}

View File

@ -27,6 +27,6 @@ public class BungeeCommandModule {
@Provides
@Named("mainCommandName")
String provideMainCommandName() {
return "planbungee";
return "planproxy";
}
}

View File

@ -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<String> serverNames(CMDSender sender, Arguments arguments) {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String[]> 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 "";

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;

View File

@ -27,7 +27,7 @@ public class VelocityCommandModule {
@Provides
@Named("mainCommandName")
String provideMainCommandName() {
return "planvelocity";
return "planproxy";
}
}