mirror of
https://github.com/essentials/Essentials.git
synced 2025-09-02 11:13:55 +02:00
Add tabComplete support to Essentials commands
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
package net.ess3.api;
|
package net.ess3.api;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.bukkit.command.Command;
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
|
||||||
public interface ICommandHandler extends IReload
|
public interface ICommandHandler extends IReload, TabExecutor
|
||||||
{
|
{
|
||||||
Map<String, String> disabledCommands();
|
Map<String, String> disabledCommands();
|
||||||
|
|
||||||
@@ -14,7 +14,5 @@ public interface ICommandHandler extends IReload
|
|||||||
|
|
||||||
public void addPlugin(Plugin plugin);
|
public void addPlugin(Plugin plugin);
|
||||||
|
|
||||||
boolean handleCommand(CommandSender sender, Command command, String commandLabel, String[] args);
|
|
||||||
|
|
||||||
void showCommandError(CommandSender sender, String commandLabel, Throwable exception);
|
void showCommandError(CommandSender sender, String commandLabel, Throwable exception);
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package net.ess3.bukkit;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import net.ess3.Essentials;
|
import net.ess3.Essentials;
|
||||||
@@ -17,6 +18,7 @@ import net.ess3.metrics.MetricsListener;
|
|||||||
import net.ess3.metrics.MetricsStarter;
|
import net.ess3.metrics.MetricsStarter;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
@@ -119,7 +121,13 @@ public class BukkitPlugin extends JavaPlugin implements IPlugin
|
|||||||
@Override
|
@Override
|
||||||
public boolean onCommand(final org.bukkit.command.CommandSender sender, final Command command, final String label, final String[] args)
|
public boolean onCommand(final org.bukkit.command.CommandSender sender, final Command command, final String label, final String[] args)
|
||||||
{
|
{
|
||||||
return ess.getCommandHandler().handleCommand(sender, command, label, args);
|
return ess.getCommandHandler().onCommand(sender, command, label, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args)
|
||||||
|
{
|
||||||
|
return ess.getCommandHandler().onTabComplete(sender, command, alias, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package net.ess3.commands;
|
package net.ess3.commands;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import static net.ess3.I18n._;
|
import static net.ess3.I18n._;
|
||||||
import net.ess3.api.IEssentials;
|
import net.ess3.api.IEssentials;
|
||||||
@@ -37,51 +38,6 @@ public abstract class EssentialsCommand extends AbstractSuperpermsPermission imp
|
|||||||
this.module = module;
|
this.module = module;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*protected IUser getPlayer(final String[] args, final int pos) throws NoSuchFieldException, NotEnoughArgumentsException
|
|
||||||
{
|
|
||||||
return getPlayer(args, pos, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected IUser getPlayer(final String[] args, final int pos, final boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException
|
|
||||||
{
|
|
||||||
if (args.length <= pos)
|
|
||||||
{
|
|
||||||
throw new NotEnoughArgumentsException();
|
|
||||||
}
|
|
||||||
if (args[pos].isEmpty())
|
|
||||||
{
|
|
||||||
throw new NoSuchFieldException(_("playerNotFound"));
|
|
||||||
}
|
|
||||||
final IUser user = ess.getUserMap().getUser(args[pos]);
|
|
||||||
if (user != null)
|
|
||||||
{
|
|
||||||
if (!getOffline && (!user.isOnline() || user.isHidden()))
|
|
||||||
{
|
|
||||||
throw new NoSuchFieldException(_("playerNotFound"));
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
final List<Player> matches = server.matchPlayer(args[pos]);
|
|
||||||
|
|
||||||
if (!matches.isEmpty())
|
|
||||||
{
|
|
||||||
for (Player player : matches)
|
|
||||||
{
|
|
||||||
final IUser userMatch = player.getUser();
|
|
||||||
if (userMatch.getDisplayName().startsWith(args[pos]) && (getOffline || !userMatch.isHidden()))
|
|
||||||
{
|
|
||||||
return userMatch;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
final IUser userMatch = matches.get(0).getUser();
|
|
||||||
if (getOffline || !userMatch.isHidden())
|
|
||||||
{
|
|
||||||
return userMatch;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new NoSuchFieldException(_("playerNotFound"));
|
|
||||||
}*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void run(final IUser user, final Command cmd, final String commandLabel, final String[] args) throws Exception
|
public final void run(final IUser user, final Command cmd, final String commandLabel, final String[] args) throws Exception
|
||||||
{
|
{
|
||||||
@@ -107,6 +63,28 @@ public abstract class EssentialsCommand extends AbstractSuperpermsPermission imp
|
|||||||
throw new Exception(_("onlyPlayers", commandName));
|
throw new Exception(_("onlyPlayers", commandName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final List<String> tabComplete(final IUser user, final Command cmd, final String commandLabel, final String[] args)
|
||||||
|
{
|
||||||
|
return tabComplete(user, commandLabel, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<String> tabComplete(final IUser user, final String commandLabel, final String[] args)
|
||||||
|
{
|
||||||
|
return tabComplete((CommandSender)user, commandLabel, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final List<String> tabComplete(final CommandSender sender, final Command cmd, final String commandLabel, final String[] args)
|
||||||
|
{
|
||||||
|
return tabComplete(sender, commandLabel, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<String> tabComplete(final CommandSender sender, final String commandLabel, final String[] args)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static String getFinalArg(final String[] args, final int start)
|
public static String getFinalArg(final String[] args, final int start)
|
||||||
{
|
{
|
||||||
final StringBuilder bldr = new StringBuilder();
|
final StringBuilder bldr = new StringBuilder();
|
||||||
|
@@ -11,11 +11,12 @@ import org.bukkit.command.Command;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.PluginCommand;
|
import org.bukkit.command.PluginCommand;
|
||||||
import org.bukkit.command.PluginCommandYamlParser;
|
import org.bukkit.command.PluginCommandYamlParser;
|
||||||
|
import org.bukkit.command.TabExecutor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
|
||||||
public class EssentialsCommandHandler implements ICommandHandler
|
public class EssentialsCommandHandler implements ICommandHandler, TabExecutor
|
||||||
{
|
{
|
||||||
private final transient ClassLoader classLoader;
|
private final transient ClassLoader classLoader;
|
||||||
private final transient String commandPath;
|
private final transient String commandPath;
|
||||||
@@ -49,7 +50,7 @@ public class EssentialsCommandHandler implements ICommandHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handleCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args)
|
public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args)
|
||||||
{
|
{
|
||||||
ISettings settings = ess.getSettings();
|
ISettings settings = ess.getSettings();
|
||||||
|
|
||||||
@@ -109,6 +110,9 @@ public class EssentialsCommandHandler implements ICommandHandler
|
|||||||
cmd.init(ess, commandName);
|
cmd.init(ess, commandName);
|
||||||
cmd.setEssentialsModule(module);
|
cmd.setEssentialsModule(module);
|
||||||
commands.put(commandName, cmd);
|
commands.put(commandName, cmd);
|
||||||
|
if (command instanceof PluginCommand) {
|
||||||
|
((PluginCommand)command).setExecutor(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -175,6 +179,122 @@ public class EssentialsCommandHandler implements ICommandHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: Clean this up, since both methods have a lot in common.
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args)
|
||||||
|
{
|
||||||
|
ISettings settings = ess.getSettings();
|
||||||
|
|
||||||
|
boolean disabled = settings.getData().getCommands().isDisabled(command.getName());
|
||||||
|
boolean overridden = !disabled || settings.getData().getCommands().isOverridden(command.getName());
|
||||||
|
|
||||||
|
// TODO: Move this stuff to bukkit workarounds
|
||||||
|
// Allow plugins to override the command via onCommand
|
||||||
|
if (!overridden && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName())))
|
||||||
|
{
|
||||||
|
final PluginCommand pc = getAlternative(commandLabel);
|
||||||
|
if (pc != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
executed(commandLabel, pc.getLabel());
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return pc.tabComplete(sender, commandLabel, args);
|
||||||
|
}
|
||||||
|
catch (final Exception ex)
|
||||||
|
{
|
||||||
|
final ArrayList<StackTraceElement> elements = new ArrayList<StackTraceElement>(Arrays.asList(ex.getStackTrace()));
|
||||||
|
elements.remove(0);
|
||||||
|
final ArrayList<StackTraceElement> toRemove = new ArrayList<StackTraceElement>();
|
||||||
|
for (final StackTraceElement e : elements)
|
||||||
|
{
|
||||||
|
if (e.getClassName().equals("net.ess3.Essentials"))
|
||||||
|
{
|
||||||
|
toRemove.add(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elements.removeAll(toRemove);
|
||||||
|
final StackTraceElement[] trace = elements.toArray(new StackTraceElement[elements.size()]);
|
||||||
|
ex.setStackTrace(trace);
|
||||||
|
ex.printStackTrace();
|
||||||
|
sender.sendMessage(ChatColor.RED + "An internal error occurred while attempting to perform this command");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Check for disabled commands
|
||||||
|
if (disabled)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String commandName = command.getName().toLowerCase(Locale.ENGLISH);
|
||||||
|
IEssentialsCommand cmd = commands.get(commandName);
|
||||||
|
if (cmd == null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
cmd = (IEssentialsCommand)classLoader.loadClass(commandPath + commandName).newInstance();
|
||||||
|
cmd.init(ess, commandName);
|
||||||
|
cmd.setEssentialsModule(module);
|
||||||
|
commands.put(commandName, cmd);
|
||||||
|
if (command instanceof PluginCommand) {
|
||||||
|
((PluginCommand)command).setExecutor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
sender.sendMessage(_("commandNotLoaded", commandName));
|
||||||
|
LOGGER.log(Level.SEVERE, _("commandNotLoaded", commandName), ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check authorization
|
||||||
|
if (sender != null && !cmd.isAuthorized(sender))
|
||||||
|
{
|
||||||
|
LOGGER.log(Level.WARNING, _("deniedAccessCommand", sender.getName()));
|
||||||
|
sender.sendMessage(_("noAccessCommand"));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
IUser user = (sender instanceof Player) ? ess.getUserMap().getUser((Player)sender) : null;
|
||||||
|
// Run the command
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
return cmd.tabComplete(sender, command, commandLabel, args);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
user.setPlayerCache((Player)sender);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return cmd.tabComplete(user, command, commandLabel, args);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
user.setPlayerCache(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable ex)
|
||||||
|
{
|
||||||
|
showCommandError(sender, commandLabel, ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable ex)
|
||||||
|
{
|
||||||
|
LOGGER.log(Level.SEVERE, _("commandFailed", commandLabel), ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showCommandError(final CommandSender sender, final String commandLabel, final Throwable exception)
|
public void showCommandError(final CommandSender sender, final String commandLabel, final Throwable exception)
|
||||||
{
|
{
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package net.ess3.commands;
|
package net.ess3.commands;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import net.ess3.api.IEssentials;
|
import net.ess3.api.IEssentials;
|
||||||
import net.ess3.api.IEssentialsModule;
|
import net.ess3.api.IEssentialsModule;
|
||||||
import net.ess3.api.IPermission;
|
import net.ess3.api.IPermission;
|
||||||
@@ -16,6 +17,10 @@ public interface IEssentialsCommand extends IPermission
|
|||||||
void run(CommandSender sender, Command cmd, String commandLabel, String[] args)
|
void run(CommandSender sender, Command cmd, String commandLabel, String[] args)
|
||||||
throws Exception;
|
throws Exception;
|
||||||
|
|
||||||
|
List<String> tabComplete(IUser user, Command cmd, String commandLabel, String[] args);
|
||||||
|
|
||||||
|
List<String> tabComplete(CommandSender sender, Command cmd, String commandLabel, String[] args);
|
||||||
|
|
||||||
void init(IEssentials ess, String commandLabel);
|
void init(IEssentials ess, String commandLabel);
|
||||||
|
|
||||||
void setEssentialsModule(IEssentialsModule module);
|
void setEssentialsModule(IEssentialsModule module);
|
||||||
|
2
pom.xml
2
pom.xml
@@ -76,7 +76,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<bukkit.version>1.3.2-R1.1-SNAPSHOT</bukkit.version>
|
<bukkit.version>1.3.2-R2.1-SNAPSHOT</bukkit.version>
|
||||||
<build.number>Unknown</build.number>
|
<build.number>Unknown</build.number>
|
||||||
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
|
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
|
||||||
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
|
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
|
||||||
|
Reference in New Issue
Block a user