From 4699fd8af2dea373a650c26ccefb7dc0c8b335e3 Mon Sep 17 00:00:00 2001 From: Rsl1122 <24460436+Rsl1122@users.noreply.github.com> Date: Tue, 14 Jul 2020 15:56:45 +0300 Subject: [PATCH] Implemented nukkit command adapter --- .../java/com/djrapitops/plan/PlanNukkit.java | 47 +++++++++++--- .../djrapitops/plan/PlanNukkitComponent.java | 4 +- .../plan/commands/use/NukkitCMDSender.java | 61 +++++++++++++++++++ .../commands/use/NukkitPlayerCMDSender.java | 47 ++++++++++++++ .../plan/commands/use/VelocityCommand.java | 2 +- 5 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitCMDSender.java create mode 100644 Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitPlayerCMDSender.java diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java index 35a162fe4..e64cb2374 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java @@ -16,9 +16,11 @@ */ package com.djrapitops.plan; +import cn.nukkit.Player; +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; import com.djrapitops.plan.addons.placeholderapi.NukkitPlaceholderRegistrar; -import com.djrapitops.plan.commands.OldPlanCommand; -import com.djrapitops.plan.commands.use.Subcommand; +import com.djrapitops.plan.commands.use.*; import com.djrapitops.plan.exceptions.EnableException; import com.djrapitops.plan.gathering.ServerShutdownSave; import com.djrapitops.plan.settings.locale.Locale; @@ -29,6 +31,8 @@ import com.djrapitops.plugin.benchmarking.Benchmark; import com.djrapitops.plugin.command.ColorScheme; import com.djrapitops.plugin.task.AbsRunnable; +import java.util.HashMap; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -43,6 +47,8 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { private Locale locale; private ServerShutdownSave serverShutdownSave; + private final Map commands = new HashMap<>(); + @Override public void onEnable() { PlanNukkitComponent component = DaggerPlanNukkitComponent.builder().plan(this).build(); @@ -72,9 +78,8 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { logger.error("This error should be reported at https://github.com/Rsl1122/Plan-PlayerAnalytics/issues"); onDisable(); } - OldPlanCommand command = component.planCommand(); - command.registerCommands(); - registerCommand("plan", command); + + registerCommand(component.planCommand().build()); if (system != null) { system.getProcessing().submitNonCritical(() -> system.getListenerSystem().callEnableEvent(this)); } @@ -100,6 +105,28 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { logger.info(locale != null ? locale.getString(PluginLang.DISABLED) : PluginLang.DISABLED.getDefault()); } + @Override + public boolean onCommand(CommandSender actualSender, Command actualCommand, String label, String[] args) { + String name = actualCommand.getName(); + Subcommand command = commands.get(name); + if (command == null) return false; + + CMDSender sender; + if (actualSender instanceof Player) { + sender = new NukkitPlayerCMDSender((Player) actualSender); + } else { + sender = new NukkitCMDSender(actualSender); + } + + runnableFactory.create("", new AbsRunnable() { + @Override + public void run() { + command.getExecutor().accept(sender, new Arguments(args)); + } + }).runTaskAsynchronously(); + return true; + } + @Override public String getVersion() { return getDescription().getVersion(); @@ -116,8 +143,14 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { } @Override - public void registerCommand(Subcommand subcommand) { - throw new UnsupportedOperationException(); + public void registerCommand(Subcommand command) { + if (command == null) { + logger.warn("Attempted to register a null command!"); + return; + } + for (String name : command.getAliases()) { + commands.put(name, command); + } } @Override diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java index 9014b85ff..c41c7ad14 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java @@ -17,7 +17,7 @@ package com.djrapitops.plan; import com.djrapitops.plan.addons.placeholderapi.NukkitPlaceholderRegistrar; -import com.djrapitops.plan.commands.OldPlanCommand; +import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.gathering.ServerShutdownSave; import com.djrapitops.plan.modules.APFModule; import com.djrapitops.plan.modules.PlaceholderModule; @@ -49,7 +49,7 @@ import javax.inject.Singleton; }) public interface PlanNukkitComponent { - OldPlanCommand planCommand(); + PlanCommand planCommand(); PlanSystem system(); diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitCMDSender.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitCMDSender.java new file mode 100644 index 000000000..f8cab52e3 --- /dev/null +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitCMDSender.java @@ -0,0 +1,61 @@ +/* + * 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.use; + +import cn.nukkit.command.CommandSender; + +import java.util.Optional; +import java.util.UUID; + +public class NukkitCMDSender implements CMDSender { + + final CommandSender sender; + + public NukkitCMDSender(CommandSender sender) { + this.sender = sender; + } + + @Override + public MessageBuilder buildMessage() { + return null; /*TODO*/ + } + + @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 void send(String message) { + sender.sendMessage(message); + } + + @Override + public ChatFormatter getFormatter() { + return new ConsoleChatFormatter(); + } +} diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitPlayerCMDSender.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitPlayerCMDSender.java new file mode 100644 index 000000000..6b4f46edc --- /dev/null +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/commands/use/NukkitPlayerCMDSender.java @@ -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 . + */ +package com.djrapitops.plan.commands.use; + +import cn.nukkit.Player; + +import java.util.Optional; +import java.util.UUID; + +public class NukkitPlayerCMDSender extends NukkitCMDSender { + + private final Player player; + + public NukkitPlayerCMDSender(Player 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/velocity/src/main/java/com/djrapitops/plan/commands/use/VelocityCommand.java b/Plan/velocity/src/main/java/com/djrapitops/plan/commands/use/VelocityCommand.java index 2e9b7aa6a..eed150f04 100644 --- a/Plan/velocity/src/main/java/com/djrapitops/plan/commands/use/VelocityCommand.java +++ b/Plan/velocity/src/main/java/com/djrapitops/plan/commands/use/VelocityCommand.java @@ -41,7 +41,7 @@ public class VelocityCommand implements Command { public void run() { command.getExecutor().accept(getSender(source), new Arguments(args)); } - }); + }).runTaskAsynchronously(); } private CMDSender getSender(CommandSource source) {