Bukkit 1.13.2 TabCompleter

Discussion in 'Plugin Development' started by MrMxffin, Feb 10, 2019.

  1. Offline

    MrMxffin

    Hello, some weeks ago, I started writing a plugin in Java Eclipse. Today I wanted to make the Arguments tabbable so that I can write eg. /info and tab through a list of arguments. I hope you can help me troubleshooting.

    Main (open)
    Code:
    package me.themx.info;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Info extends JavaPlugin {
       @Override
       public void onEnable() {
          System.out.println("[Info] Plugin erfolgreich aktiviert!");// Start
       }
       @Override
       public void onDisable() {
          System.out.println("[Info] Plugin erfolgreich deaktiviert!");// Stop
       }
     
     
     
       public void registerCommands() {
          getCommand("info").setTabCompleter(new InfoCompleter(this));
        
        
        
       }
       public boolean onCommand(CommandSender sender, Command cmd, String cmdlabel, String[] args) {
          Player p = (Player) sender;
          if (cmd.getName().equalsIgnoreCase("info")) {
             if (args.length == 0) {
                p.sendMessage("      §3§n§lBefehle");
                p.sendMessage("");
                p.sendMessage("§7[§3Info§7] §3/info verkaufen §7Zeigt dir alle Befehle an die relevant zum Verkaufen sind.");
                p.sendMessage("§7[§3Info§7] §3/info kits §7Zeigt dir alle Kits an.");
                p.sendMessage("§7[§3Info§7] §3/info warps §7Zeigt dir alle Warps an.");
                p.sendMessage("§7[§3Info§7] §3/info farbcodes §7Zeigt alle Farbcodes an.");
                p.sendMessage("§7[§3Info§7] §3/info team §7Zeigt das Serverteam an.");
                p.sendMessage("§7[§3Info§7] §3/info plugin §7Zeigt Informationen zum Plugin.");
                p.sendMessage("§7-----------------------------------------------------");
             } else {
                if (args[0].equalsIgnoreCase("verkaufen")) {
                   p.sendMessage("      §3§n§lVerkaufen");
                   p.sendMessage("");
                   p.sendMessage("§7[§3Info§7] §3/sell hand §7Verkauft das Item in deiner Haupthand.");
                   p.sendMessage("§7[§3Info§7] §3/sell blocks §7Verkauft alle Blöcke.");
                   p.sendMessage("§7[§3Info§7] §3/sell inventory §7Verkauft das ganze Inventar.");
                   p.sendMessage("§7-----------------------------------------------------");
                   return true;
                }
                if (args[0].equalsIgnoreCase("kits") | args[0].equalsIgnoreCase("kit")) {
                   p.sendMessage("      §3§n§lKits");
                   p.sendMessage("");
                   p.sendMessage("§7[§3Info§7] §3/kit food §7Gibt dir 12 Steak.");
                   p.sendMessage("§7[§3Info§7] §3/kit claim §7Gibt dir einen Stick, eine Goldschaufel und die zugehörige Anleitung.");
                   p.sendMessage("§7-----------------------------------------------------");
                   return true;
                }
                if (args[0].equalsIgnoreCase("warps") | args[0].equalsIgnoreCase("warp")) {
                   p.sendMessage("      §3§n§lWarps");
                   p.sendMessage("");
                   p.sendMessage("§7[§3Info§7] §3/spawn §7Teleportiert dich zum Spawn.");
                   p.sendMessage("§7[§3Info§7] §3/end §7Teleportiert dich zum Ende.");
                   p.sendMessage("§7[§3Info§7] §3/nether §7Teleportiert dich in das Nether.");
                   p.sendMessage("§7[§3Info§7] §3/farmwelt §7Teleportiert dich in die Farmwelt.");
                   p.sendMessage("§7-----------------------------------------------------");
                   return true;
                }
                if (args[0].equalsIgnoreCase("team")) {
                   p.sendMessage("      §3§n§lTeam");
                   p.sendMessage("");
                   p.sendMessage("§7[§3Info§7] §3Lord_Schne §7Owner.");
                   p.sendMessage("§7[§3Info§7] §3DennisSpielt2002 §7Admin.");
                   p.sendMessage("§7[§3Info§7] §3MrMxffin §7Developer.");
                   p.sendMessage("§7[§3Info§7] §3Zukoto §7Builder.");
                   p.sendMessage("§7-----------------------------------------------------");
                   return true;
                }
                if (args[0].equalsIgnoreCase("plugin")) {
                   p.sendMessage("      §3§n§lPlugin");
                   p.sendMessage("");
                   p.sendMessage("§7[§3Info§7] §3Autor: §7MrMxffin.");
                   p.sendMessage("§7[§3Info§7] §3Version: §71.2.");
                   p.sendMessage("§7[§3Info§7] §3System: §7CraftBukkit.");
                   p.sendMessage("§7-----------------------------------------------------");
                   return true;
                }
                if (args[0].equalsIgnoreCase("farbcodes")| args[0].equalsIgnoreCase("farbcode")) {
                   p.sendMessage("      §3§n§lFarbcodes");
                   p.sendMessage("");
                   p.sendMessage("§7[§3Info§7] &0 = §0Schwarz");
                   p.sendMessage("§7[§3Info§7] &1 = §1Dunkelblau");
                   p.sendMessage("§7[§3Info§7] &2 = §2Dunkelgrün");
                   p.sendMessage("§7[§3Info§7] &3 = §3Dunkelcyan");
                   p.sendMessage("§7[§3Info§7] &4 = §4Dunkelrot");
                   p.sendMessage("§7[§3Info§7] &5 = §5Dunkelviolett");
                   p.sendMessage("§7[§3Info§7] &6 = §6Gold");
                   p.sendMessage("§7[§3Info§7] &7 = §7Grau");
                   p.sendMessage("§7[§3Info§7] &8 = §8Dunkelgrau");
                   p.sendMessage("§7[§3Info§7] &9 = §9Blau");
                   p.sendMessage("§7[§3Info§7] &a = §aGrün");
                   p.sendMessage("§7[§3Info§7] &b = §bCyan");
                   p.sendMessage("§7[§3Info§7] &c = §cRot");
                   p.sendMessage("§7[§3Info§7] &d = §dMagenta");
                   p.sendMessage("§7[§3Info§7] &e = §eGelb");
                   p.sendMessage("§7[§3Info§7] &f = §fWeiß");
                   p.sendMessage("§7-----------------------------------------------------");
                   p.sendMessage("      §3§n§lFormatierungscodes");
                   p.sendMessage("");
                   p.sendMessage("§7[§3Info§7] &k = §kVerschlüsselt");
                   p.sendMessage("§7[§3Info§7] &l = §lFett");
                   p.sendMessage("§7[§3Info§7] &m = §mDurchgestrichen");
                   p.sendMessage("§7[§3Info§7] &n = §nUnterstrichen");
                   p.sendMessage("§7[§3Info§7] &o = §oKursiv");
                   p.sendMessage("§7[§3Info§7] &r = §rZurückgesetzt");
                   p.sendMessage("§7-----------------------------------------------------");
                   return true;
                }
             }
          }
          return true;
       }
    }
    

    InfoCompleter (open)
    Code:
    package me.themx.info;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.TabCompleter;
    
    public class InfoCompleter implements TabCompleter{
    
        public InfoCompleter(Info main) {
        }
       
        @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String cmdlabel, String[] args) {
            List<String> arg = new ArrayList<>();
           
            arg.add("verkaufen");
            arg.add("kits");
            arg.add("warps");
            arg.add("team");
            arg.add("farbcodes");
            arg.add("team");
            arg.add("plugin");
           
            if(args.length == 1) {
                return arg;
            }
            else {arg.clear();
            return arg;
            }
        }
    }
    
    
     
  2. Offline

    MightyOne

    You don't say what exactly is not working out. But I guess if the TabCompleter's onCommand() works similar to JavaPlugin's one then this part of code
    Code:
    if(args.length == 1) {
        return arg;
    }
    O
    only works if you already wrote one argument in the command line:
    /info alreadysomeargument <here will the arg list be tabable>

    It would also be cleaner to not create the list ober and over again on every command call. You can have a private list in your InfoCompleter and already fill it in the constructer.
     
  3. Offline

    0-o

    Actually, this part of the code
    Code:
    if(args.length == 1) {
        return arg;
    }
    only works if there are no arguments.
    EX: /info <arg list>
     

Share This Page