Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-03 14:50:30 +01:00
Add tab-complete possibility and help subcommand
Dieser Commit ist enthalten in:
Ursprung
c48922eb3e
Commit
cb8ec2ce20
@ -5,7 +5,6 @@ import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -81,6 +80,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe
|
||||
Bukkit.getPluginManager().registerEvents(new UpdateListener(this), this);
|
||||
|
||||
getCommand("viaversion").setExecutor(commandHandler = new ViaCommandHandler());
|
||||
getCommand("viaversion").setTabCompleter(new ViaCommandHandler());
|
||||
}
|
||||
|
||||
public void gatherProtocolVersion() {
|
||||
@ -286,15 +286,15 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe
|
||||
return this.debug;
|
||||
}
|
||||
|
||||
public void setDebug(boolean value) {
|
||||
this.debug = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViaVersionCommand getCommandHandler() {
|
||||
return commandHandler;
|
||||
}
|
||||
|
||||
public void setDebug(boolean value) {
|
||||
this.debug = value;
|
||||
}
|
||||
|
||||
public boolean isCheckForUpdates() {
|
||||
return getConfig().getBoolean("checkforupdates", true);
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package us.myles.ViaVersion.api.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.myles.ViaVersion.commands.ViaCommandHandler;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ViaSubCommand {
|
||||
/**
|
||||
* Subcommand name
|
||||
@ -25,15 +27,16 @@ public abstract class ViaSubCommand {
|
||||
*
|
||||
* @return your input
|
||||
*/
|
||||
public String usage(){
|
||||
public String usage() {
|
||||
return name();
|
||||
}
|
||||
|
||||
/**
|
||||
* Permission, null for everyone
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String permission(){
|
||||
public String permission() {
|
||||
return "viaversion.admin";
|
||||
}
|
||||
|
||||
@ -46,7 +49,18 @@ public abstract class ViaSubCommand {
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import us.myles.ViaVersion.api.ViaVersion;
|
||||
import us.myles.ViaVersion.api.command.ViaSubCommand;
|
||||
import us.myles.ViaVersion.api.command.ViaVersionCommand;
|
||||
@ -13,7 +14,7 @@ import us.myles.ViaVersion.commands.defaultsubs.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
|
||||
public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor, TabCompleter {
|
||||
private Map<String, ViaSubCommand> commandMap;
|
||||
|
||||
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
|
||||
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");
|
||||
@ -50,14 +59,14 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hasSubCommand(args[0])){
|
||||
if (!hasSubCommand(args[0])) {
|
||||
sender.sendMessage(color("&cThis command is not found"));
|
||||
showHelp(sender);
|
||||
return false;
|
||||
}
|
||||
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!"));
|
||||
return false;
|
||||
}
|
||||
@ -69,9 +78,42 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
|
||||
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) {
|
||||
Set<ViaSubCommand> allowed = calculateAllowedCommands(sender);
|
||||
if (allowed.size() == 0){
|
||||
if (allowed.size() == 0) {
|
||||
sender.sendMessage(color("&cYou are not allowed to use this command!"));
|
||||
return;
|
||||
}
|
||||
@ -90,24 +132,16 @@ public class ViaCommandHandler implements ViaVersionCommand, CommandExecutor {
|
||||
return cmds;
|
||||
}
|
||||
|
||||
private boolean hasPermission(CommandSender sender, String permission){
|
||||
private boolean hasPermission(CommandSender sender, String 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 {
|
||||
registerSubCommand(new ListSubCmd());
|
||||
registerSubCommand(new DebugSubCmd());
|
||||
registerSubCommand(new DisplayLeaksSubCmd());
|
||||
registerSubCommand(new DontBugMeSubCmd());
|
||||
registerSubCommand(new AutoTeamSubCmd());
|
||||
registerSubCommand(new HelpSubCmd());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren