Fixed prime restart and already registered cmds

Commands that are registered in plugin.yml
Commands conflicting with Essentials commands have to be registered in plugin.yml
This commit is contained in:
Norbi Peti 2020-09-04 03:37:32 +02:00
parent 28e44d5179
commit 5b27af8925
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 39 additions and 13 deletions

View file

@ -21,6 +21,7 @@ import org.bukkit.command.CommandSender;
public class PrimeRestartCommand extends ICommand2MC {
private final RestartComponent component;
@Command2.Subcommand
public void def(CommandSender sender, @Command2.TextArg @Command2.OptionalArg String somethingrandom) {
loud = somethingrandom != null;
if (Bukkit.getOnlinePlayers().size() > 0) {

View file

@ -184,10 +184,20 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
@Override
public boolean handleCommand(Command2MCSender sender, String commandline) {
return handleCommand(sender, commandline, true);
}
private boolean handleCommand(Command2MCSender sender, String commandline, boolean checkPlugin) {
int i = commandline.indexOf(' ');
String mainpath = commandline.substring(1, i == -1 ? commandline.length() : i); //Without the slash
PluginCommand pcmd;
if (MainPlugin.Instance.prioritizeCustomCommands().get()
/*System.out.println("Command line: " + commandline);
System.out.println("Prioritize: " + MainPlugin.Instance.prioritizeCustomCommands().get());
System.out.println("PCMD: " + (pcmd = Bukkit.getPluginCommand(mainpath)));
if (pcmd != null)
System.out.println("ButtonPlugin: " + (pcmd.getPlugin() instanceof ButtonPlugin));*/
if (!checkPlugin
|| MainPlugin.Instance.prioritizeCustomCommands().get()
|| (pcmd = Bukkit.getPluginCommand(mainpath)) == null //Our commands aren't PluginCommands
|| pcmd.getPlugin() instanceof ButtonPlugin) //Unless it's specified in the plugin.yml
return super.handleCommand(sender, commandline);
@ -205,11 +215,21 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
int x = path.indexOf(' ');
var mainPath = path.substring(0, x == -1 ? path.length() : x);
Command bukkitCommand;
{
var oldcmd = cmdmap.getCommand(mainPath);
bukkitCommand = oldcmd == null ? new BukkitCommand(mainPath) : oldcmd;
}
{ //TODO: Commands conflicting with Essentials have to be registered in plugin.yml
var oldcmd = cmdmap.getCommand(command.getPlugin().getName() + ":" + mainPath); //The label with the fallback prefix is always registered
if (oldcmd == null) {
bukkitCommand = new BukkitCommand(mainPath);
cmdmap.register(command.getPlugin().getName(), bukkitCommand);
} else {
bukkitCommand = oldcmd;
if (bukkitCommand instanceof PluginCommand)
((PluginCommand) bukkitCommand).setExecutor(this::executeCommand);
}
bukkitCommand = oldcmd == null ? new BukkitCommand(mainPath) : oldcmd;
/*System.out.println("oldcmd: " + oldcmd);
System.out.println("bukkitCommand: " + bukkitCommand);*/
}
//System.out.println("Registering to " + command.getPlugin().getName());
if (CommodoreProvider.isSupported())
TabcompleteHelper.registerTabcomplete(command, subcmds, bukkitCommand);
} catch (Exception e) {
@ -218,6 +238,19 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
}
}
private boolean executeCommand(CommandSender sender, Command command, String label, String[] args) {
var user = ChromaGamerBase.getFromSender(sender);
if (user == null) {
TBMCCoreAPI.SendException("Failed to run Bukkit command for user!", new Throwable("No Chroma user found"));
sender.sendMessage("§cAn internal error occurred.");
return true;
}
//System.out.println("Executing " + label + " which is actually " + command.getName());
handleCommand(new Command2MCSender(sender, user.channel().get(), sender),
("/" + command.getName() + " " + String.join(" ", args)).trim(), false); ///trim(): remove space if there are no args
return true;
}
private static class BukkitCommand extends Command {
protected BukkitCommand(String name) {
super(name);
@ -225,15 +258,7 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
var user = ChromaGamerBase.getFromSender(sender);
if (user == null) {
TBMCCoreAPI.SendException("Failed to run Bukkit command for user!", new Throwable("No Chroma user found"));
sender.sendMessage("§cAn internal error occurred.");
return true;
}
ButtonPlugin.getCommand2MC().handleCommand(new Command2MCSender(sender, user.channel().get(), sender),
"/" + getName() + " " + String.join(" ", args));
return true;
return ButtonPlugin.getCommand2MC().executeCommand(sender, this, commandLabel, args);
}
@Override