Added fallback to handle prefixed commands

The Bukkit executor will join the arguments again and passes the command line to the command system
Also added support for tab completion
#96
This commit is contained in:
Norbi Peti 2020-03-31 02:51:54 +02:00
parent 7b505bb8e9
commit 82858b0a41
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 56 additions and 30 deletions

View file

@ -33,14 +33,15 @@
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration> <configuration>
<compilerArgument>-proc:none</compilerArgument> <compilerArgument>-proc:none</compilerArgument>
<source>8</source> <source>8</source>
<target>8</target> <target>8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>

View file

@ -84,14 +84,12 @@ public class MainPlugin extends ButtonPlugin {
return getIConfig().getData("test", false); return getIConfig().getData("test", false);
} }
/* /**
* By default, the plugin uses Vault for all command permission checks, but this can have issues (with PEX for example) where default permissions aren't granted. * If a Chroma command clashes with another plugin's command, this setting determines whether the Chroma command should be executed or the other plugin's.
* When this setting is off, the plugin uses Bukkit's built-in way of handling permissions, which usually works fine for players.
* You can also grant chroma.command.* to each player (mod-only commands need another permission, chroma.mod).
*/ */
/*public ConfigData<Boolean> useVaultForCommands() { public ConfigData<Boolean> prioritizeCustomCommands() {
return getIConfig().getData("useVaultForCommands", true); return getIConfig().getData("prioritizeCustomCommands", false);
}*/ }
@Override @Override
public void pluginEnable() { public void pluginEnable() {

View file

@ -32,7 +32,7 @@ public class TownyComponent extends Component<MainPlugin> {
public static void renameInTowny(String oldName, String newName) { public static void renameInTowny(String oldName, String newName) {
if (!ComponentManager.isEnabled(TownyComponent.class)) if (!ComponentManager.isEnabled(TownyComponent.class))
return; return;
Bukkit.getLogger().info("Renaming" + oldName + " in Towny to " + newName); Bukkit.getLogger().info("Renaming " + oldName + " in Towny to " + newName);
TownyUniverse tu = TownyUniverse.getInstance(); TownyUniverse tu = TownyUniverse.getInstance();
try { try {
Resident resident = tu.getDataSource().getResident(oldName); Resident resident = tu.getDataSource().getResident(oldName);

View file

@ -165,7 +165,7 @@ public abstract class Command2<TC extends ICommand2<TP>, TP extends Command2Send
* @param sync Whether the command was originally sync * @param sync Whether the command was originally sync
* @throws Exception If something's not right * @throws Exception If something's not right
*/ */
public void handleCommandAsync(TP sender, String commandline, SubcommandData<TC> sd, String subcommand, boolean sync) throws Exception { private void handleCommandAsync(TP sender, String commandline, SubcommandData<TC> sd, String subcommand, boolean sync) throws Exception {
if (sd.method == null || sd.command == null) { //Main command not registered, but we have subcommands if (sd.method == null || sd.command == null) { //Main command not registered, but we have subcommands
sender.sendMessage(sd.helpText); sender.sendMessage(sd.helpText);
return; return;
@ -330,15 +330,12 @@ public abstract class Command2<TC extends ICommand2<TP>, TP extends Command2Send
subcommands.put(commandChar + path, sd); subcommands.put(commandChar + path, sd);
addedSubcommands.add(sd); addedSubcommands.add(sd);
} }
if (mainMethod != null && !mainPath.equals(commandChar + path)) { //Main command, typically the same as the above if (isSubcommand) { //The class itself is a subcommand
if (isSubcommand) { //The class itself is a subcommand val scmd = subcommands.computeIfAbsent(mainPath, p -> new SubcommandData<>(null, null, new String[0], new String[]{"§6---- Subcommands ----"}));
val scmd = subcommands.computeIfAbsent(mainPath, p -> new SubcommandData<>(null, null, new String[0], new String[]{"§6---- Subcommands ----"})); val scmdHelp = Arrays.copyOf(scmd.helpText, scmd.helpText.length + scmdHelpList.size());
val scmdHelp = Arrays.copyOf(scmd.helpText, scmd.helpText.length + scmdHelpList.size()); for (int i = 0; i < scmdHelpList.size(); i++)
for (int i = 0; i < scmdHelpList.size(); i++) scmdHelp[scmd.helpText.length + i] = scmdHelpList.get(i);
scmdHelp[scmd.helpText.length + i] = scmdHelpList.get(i); scmd.helpText = scmdHelp;
scmd.helpText = scmdHelp;
} else if (!subcommands.containsKey(mainPath))
subcommands.put(mainPath, new SubcommandData<>(null, null, new String[0], scmdHelpList.toArray(new String[0])));
} }
return addedSubcommands; return addedSubcommands;
} }

View file

@ -4,6 +4,7 @@ import buttondevteam.core.MainPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.ButtonPlugin; import buttondevteam.lib.architecture.ButtonPlugin;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.player.ChromaGamerBase;
import com.mojang.brigadier.arguments.*; import com.mojang.brigadier.arguments.*;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder;
@ -16,10 +17,7 @@ import me.lucko.commodore.CommodoreProvider;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.*;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -48,7 +46,15 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
*/ */
@Override @Override
public void registerCommand(ICommand2MC command) { public void registerCommand(ICommand2MC command) {
/*String mainpath;
var plugin = command.getPlugin();
{
String cpath = command.getCommandPath();
int i = cpath.indexOf(' ');
mainpath = cpath.substring(0, i == -1 ? cpath.length() : i);
}*/
var subcmds = super.registerCommand(command, '/'); var subcmds = super.registerCommand(command, '/');
var perm = "chroma.command." + command.getCommandPath().replace(' ', '.'); var perm = "chroma.command." + command.getCommandPath().replace(' ', '.');
if (Bukkit.getPluginManager().getPermission(perm) == null) //Check needed for plugin reset if (Bukkit.getPluginManager().getPermission(perm) == null) //Check needed for plugin reset
Bukkit.getPluginManager().addPermission(new Permission(perm, Bukkit.getPluginManager().addPermission(new Permission(perm,
@ -173,6 +179,19 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
event.getCompletions().clear(); event.getCompletions().clear();
} }
@Override
public boolean handleCommand(Command2MCSender sender, String commandline) {
int i = commandline.indexOf(' ');
String mainpath = commandline.substring(1, i == -1 ? commandline.length() : i); //Without the slash
PluginCommand pcmd;
if (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);
else
return false;
}
private boolean shouldRegisterOfficially = true; private boolean shouldRegisterOfficially = true;
private void registerOfficially(ICommand2MC command, List<SubcommandData<ICommand2MC>> subcmds) { private void registerOfficially(ICommand2MC command, List<SubcommandData<ICommand2MC>> subcmds) {
@ -199,7 +218,14 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
@Override @Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) { public boolean execute(CommandSender sender, String commandLabel, String[] args) {
sender.sendMessage("§cThe command wasn't executed for some reason... (command processing failed)"); 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 true;
} }
@ -378,6 +404,10 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
} }
} }
commodore.register(maincmd); commodore.register(maincmd);
var prefixedcmd = new LiteralCommandNode<>(command2MC.getPlugin().getName().toLowerCase() + ":" + path[0], maincmd.getCommand(), maincmd.getRequirement(), maincmd.getRedirect(), maincmd.getRedirectModifier(), maincmd.isFork());
for (var child : maincmd.getChildren())
prefixedcmd.addChild(child);
commodore.register(prefixedcmd);
} }
} }
} }

View file

@ -1,5 +1,6 @@
package buttondevteam.lib.player; package buttondevteam.lib.player;
import buttondevteam.core.MainPlugin;
import buttondevteam.core.component.towny.TownyComponent; import buttondevteam.core.component.towny.TownyComponent;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -114,14 +115,13 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
*/ */
public static void joinPlayer(Player p) { public static void joinPlayer(Player p) {
TBMCPlayer player = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class); TBMCPlayer player = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class);
Bukkit.getLogger().info("Loaded player: " + player.PlayerName().get());
if (player.PlayerName().get() == null) { if (player.PlayerName().get() == null) {
player.PlayerName().set(p.getName()); player.PlayerName().set(p.getName());
Bukkit.getLogger().info("Player name saved: " + player.PlayerName().get()); MainPlugin.Instance.getLogger().info("Player name saved: " + player.PlayerName().get());
} else if (!p.getName().equals(player.PlayerName().get())) { } else if (!p.getName().equals(player.PlayerName().get())) {
TownyComponent.renameInTowny(player.PlayerName().get(), p.getName()); TownyComponent.renameInTowny(player.PlayerName().get(), p.getName());
MainPlugin.Instance.getLogger().info(player.PlayerName().get() + " renamed to " + p.getName());
player.PlayerName().set(p.getName()); player.PlayerName().set(p.getName());
Bukkit.getLogger().info("Renamed to " + p.getName());
} }
playermap.put(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName(), player); playermap.put(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName(), player);