3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 16:12:42 +01:00

Add tab-complete possibility and help subcommand

Dieser Commit ist enthalten in:
Matsv 2016-03-31 20:50:22 +02:00
Ursprung c48922eb3e
Commit cb8ec2ce20
4 geänderte Dateien mit 99 neuen und 24 gelöschten Zeilen

Datei anzeigen

@ -5,7 +5,6 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -81,6 +80,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe
Bukkit.getPluginManager().registerEvents(new UpdateListener(this), this); Bukkit.getPluginManager().registerEvents(new UpdateListener(this), this);
getCommand("viaversion").setExecutor(commandHandler = new ViaCommandHandler()); getCommand("viaversion").setExecutor(commandHandler = new ViaCommandHandler());
getCommand("viaversion").setTabCompleter(new ViaCommandHandler());
} }
public void gatherProtocolVersion() { public void gatherProtocolVersion() {
@ -286,15 +286,15 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe
return this.debug; return this.debug;
} }
public void setDebug(boolean value) {
this.debug = value;
}
@Override @Override
public ViaVersionCommand getCommandHandler() { public ViaVersionCommand getCommandHandler() {
return commandHandler; return commandHandler;
} }
public void setDebug(boolean value) {
this.debug = value;
}
public boolean isCheckForUpdates() { public boolean isCheckForUpdates() {
return getConfig().getBoolean("checkforupdates", true); return getConfig().getBoolean("checkforupdates", true);
} }

Datei anzeigen

@ -1,9 +1,11 @@
package us.myles.ViaVersion.api.command; package us.myles.ViaVersion.api.command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import us.myles.ViaVersion.commands.ViaCommandHandler; import us.myles.ViaVersion.commands.ViaCommandHandler;
import java.util.Collections;
import java.util.List;
public abstract class ViaSubCommand { public abstract class ViaSubCommand {
/** /**
* Subcommand name * Subcommand name
@ -25,15 +27,16 @@ public abstract class ViaSubCommand {
* *
* @return your input * @return your input
*/ */
public String usage(){ public String usage() {
return name(); return name();
} }
/** /**
* Permission, null for everyone * Permission, null for everyone
*
* @return * @return
*/ */
public String permission(){ public String permission() {
return "viaversion.admin"; return "viaversion.admin";
} }
@ -41,12 +44,23 @@ public abstract class ViaSubCommand {
* Gets triggered on execution * Gets triggered on execution
* *
* @param sender Command sender * @param sender Command sender
* @param args Arguments * @param args Arguments
* @return command executed succesfully if false, show usage * @return command executed succesfully if false, show usage
*/ */
public abstract boolean execute(CommandSender sender, String[] args); public abstract boolean execute(CommandSender sender, String[] args);
public String color(String s){ /**
* Yay, possibility to implement tab-completion
*
* @param sender Command sender
* @param args args
* @return tab complete possibilities
*/
public List<String> onTabComplete(CommandSender sender, String[] args) {
return Collections.emptyList();
}
public String color(String s) {
return ViaCommandHandler.color(s); return ViaCommandHandler.color(s);
} }
} }

Datei anzeigen

@ -6,6 +6,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.command.ViaSubCommand; import us.myles.ViaVersion.api.command.ViaSubCommand;
import us.myles.ViaVersion.api.command.ViaVersionCommand; import us.myles.ViaVersion.api.command.ViaVersionCommand;
@ -13,7 +14,7 @@ import us.myles.ViaVersion.commands.defaultsubs.*;
import java.util.*; import java.util.*;
public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor { public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor, TabCompleter {
private Map<String, ViaSubCommand> commandMap; private Map<String, ViaSubCommand> commandMap;
public ViaCommandHandler() { public ViaCommandHandler() {
@ -25,6 +26,14 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
} }
} }
public static String color(String string) {
try {
string = ChatColor.translateAlternateColorCodes('&', string); //Dont replace all & with $ like we did before.
} catch (Exception ignored) {
}
return string;
}
@Override @Override
public void registerSubCommand(@NonNull ViaSubCommand command) throws Exception { public void registerSubCommand(@NonNull ViaSubCommand command) throws Exception {
Validate.isTrue(command.name().matches("^[a-z0-9_-]{3,15}$"), command.name() + " is not a valid subcommand name"); Validate.isTrue(command.name().matches("^[a-z0-9_-]{3,15}$"), command.name() + " is not a valid subcommand name");
@ -50,14 +59,14 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
return false; return false;
} }
if (!hasSubCommand(args[0])){ if (!hasSubCommand(args[0])) {
sender.sendMessage(color("&cThis command is not found")); sender.sendMessage(color("&cThis command is not found"));
showHelp(sender); showHelp(sender);
return false; return false;
} }
ViaSubCommand handler = getSubCommand(args[0]); ViaSubCommand handler = getSubCommand(args[0]);
if (!hasPermission(sender, handler.permission())){ if (!hasPermission(sender, handler.permission())) {
sender.sendMessage(color("&cYou are not allowed to use this command!")); sender.sendMessage(color("&cYou are not allowed to use this command!"));
return false; return false;
} }
@ -69,9 +78,42 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
return result; return result;
} }
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String arg, String[] args) {
Set<ViaSubCommand> allowed = calculateAllowedCommands(sender);
List<String> output = new ArrayList<>();
//SubCommands tabcomplete
if (args.length == 1) {
if (!args[0].equals("")) {
for (ViaSubCommand sub : allowed)
if (sub.name().toLowerCase().startsWith(args[0].toLowerCase()))
output.add(sub.name());
} else {
for (ViaSubCommand sub : allowed)
output.add(sub.name());
}
}
//Let the SubCommand handle it
else if (args.length >= 2) {
if (getSubCommand(args[0]) != null) {
ViaSubCommand sub = getSubCommand(args[0]);
if (!allowed.contains(sub))
return output;
String[] subArgs = Arrays.copyOfRange(args, 1, args.length);
List<String> tab = sub.onTabComplete(sender, subArgs);
Collections.sort(tab);
return tab;
}
}
return output;
}
public void showHelp(CommandSender sender) { public void showHelp(CommandSender sender) {
Set<ViaSubCommand> allowed = calculateAllowedCommands(sender); Set<ViaSubCommand> allowed = calculateAllowedCommands(sender);
if (allowed.size() == 0){ if (allowed.size() == 0) {
sender.sendMessage(color("&cYou are not allowed to use this command!")); sender.sendMessage(color("&cYou are not allowed to use this command!"));
return; return;
} }
@ -90,24 +132,16 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
return cmds; return cmds;
} }
private boolean hasPermission(CommandSender sender, String permission){ private boolean hasPermission(CommandSender sender, String permission) {
return permission == null || sender.hasPermission(permission); return permission == null || sender.hasPermission(permission);
} }
public static String color(String string) {
try {
string = ChatColor.translateAlternateColorCodes('&', string); //Dont replace all & with $ like we did before.
} catch (Exception ignored) {
}
return string;
}
private void registerDefaults() throws Exception { private void registerDefaults() throws Exception {
registerSubCommand(new ListSubCmd()); registerSubCommand(new ListSubCmd());
registerSubCommand(new DebugSubCmd()); registerSubCommand(new DebugSubCmd());
registerSubCommand(new DisplayLeaksSubCmd()); registerSubCommand(new DisplayLeaksSubCmd());
registerSubCommand(new DontBugMeSubCmd()); registerSubCommand(new DontBugMeSubCmd());
registerSubCommand(new AutoTeamSubCmd()); registerSubCommand(new AutoTeamSubCmd());
registerSubCommand(new HelpSubCmd());
} }
} }

Datei anzeigen

@ -0,0 +1,27 @@
package us.myles.ViaVersion.commands.defaultsubs;
import org.bukkit.command.CommandSender;
import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.command.ViaSubCommand;
import us.myles.ViaVersion.commands.ViaCommandHandler;
public class HelpSubCmd extends ViaSubCommand {
@Override
public String name() {
return "help";
}
@Override
public String description() {
return "You are looking at it right now!";
}
@Override
public boolean execute(CommandSender sender, String[] args) {
ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance();
((ViaCommandHandler) plugin.getCommandHandler()).showHelp(sender);
return true;
}
}