Implemented nukkit command adapter

This commit is contained in:
Rsl1122 2020-07-14 15:56:45 +03:00 committed by Risto Lahtela
parent f85fff7f93
commit 4699fd8af2
5 changed files with 151 additions and 10 deletions

View File

@ -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<String, Subcommand> 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

View File

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

View File

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

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 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<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

@ -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) {