ericgolde555

Example of making TabAutoCompleat Commands bukkit&spigot

Jun 28th, 2015
3,283
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.golde.bukkit.tab; //name if your plugin (like a UUID but with words and dots)
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6.  
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandSender;
  9.  
  10. import org.bukkit.plugin.java.JavaPlugin;
  11.  
  12. import org.bukkit.entity.*;
  13.  
  14. //Imports everything needed
  15.  
  16.  
  17. public class MyPlugin extends JavaPlugin { //creates your plugin and tells the server to register your plugin as a bukkit plugin
  18.  
  19.  
  20. //when the server enables the plugin print a message in the console saying that it has been enabled
  21.  
  22. public void onEnable() {
  23. getCommand("tab").setExecutor(this); //tells bukkit to register a command
  24. getLogger().info("TAB plugin is starting."); //prints message
  25. }
  26.  
  27.  
  28. //when the server disables the plugin print a message in the console saying that it has been disabled
  29. public void onDisable() {
  30. getLogger().info("TAB plugin in stopping."); //prints message
  31. }
  32.  
  33.  
  34. //
  35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  36. if(cmd.getName().equalsIgnoreCase("tab")){ // your command name
  37. if (! (sender instanceof Player)) //this makes sure its a player executing the command
  38. return true;
  39.  
  40. //argument checker and definer
  41. //also prints the correct syntax if the user gets it wrong
  42. if(args.length < 2 ){
  43. sender.sendMessage("Incorrect Syntax!!"); //prints "incorrect syntax"
  44. sender.sendMessage("/tab <1-2> <a-b>"); // prints the correct syntax
  45. return true;
  46. }
  47.  
  48.  
  49.  
  50.  
  51. //Store which argument goes with which possibility
  52. String S12 = args[0]; //first argument
  53. String Sab = args[1]; //second argument
  54.  
  55.  
  56.  
  57.  
  58.  
  59. if(S12.equalsIgnoreCase("1")){ //Retrieving and plugging in the arguments that are stored and running different commands depending on which arguments are were
  60. if(Sab.equalsIgnoreCase("A")) //Retrieving and plugging in the arguments that are stored and running different commands depending on which arguments are were
  61. sender.sendMessage("1 a"); //sends a message to the player who send the command depending what the arguments are
  62. else
  63. sender.sendMessage("1 b"); //sends a message to the player who send the command depending what the arguments are
  64. }
  65. else
  66. {
  67. if(Sab.equalsIgnoreCase("a")) //Retrieving and plugging in the arguments that are stored and running different commands depending on which arguments are were
  68. sender.sendMessage("2 a"); //sends a message to the player who send the command depending what the arguments are
  69. else
  70. sender.sendMessage("2 b"); //sends a message to the player who send the command depending what the arguments are
  71. }
  72. }
  73. return false; //this is a little confusing but just put it there
  74. //it just returns false if nothing matches the arguments needed
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. //list all possible tab arguments
  82.  
  83. public List<String> onTabComplete(CommandSender sender, //registers the auto tab completer
  84. Command command,
  85. String alias,
  86. String[] args){
  87. if(command.getName().equalsIgnoreCase("tab")){ //your command name
  88. List<String> l = new ArrayList<String>(); //makes a ArrayList
  89.  
  90.  
  91. //define the possible possibility's for argument 1
  92. if (args.length==1){
  93. l.add("1"); //Possibility #1
  94. l.add("2"); //Possibility #2
  95.  
  96.  
  97. }
  98.  
  99. //define the possible possibility's for argument 2
  100. else if (args.length==2){
  101. l.add("a"); //Possibility #1
  102. l.add("b"); //Possibility #2
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. return l; //returns the possibility's to the client
  112.  
  113.  
  114. }
  115. return null; //this is a little confusing but just put it there
  116. //it just returns NULL if absolutely nothing has happened
  117. }}
RAW Paste Data