TabApi

Discussion in 'Resources' started by BlockCat, Dec 22, 2013.

?

Happy?

  1. Yes, thank you oh almighty BlockCat

    6 vote(s)
    85.7%
  2. nay.

    1 vote(s)
    14.3%
Thread Status:
Not open for further replies.
  1. So I decided to made a small tabApi to practice api's...
    It would be great if someone can give me some hints to make it even better.

    API:
    Code:java
    1.  
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Collections;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.util.StringUtil;
    11.  
    12. import com.google.common.collect.ImmutableList;
    13.  
    14.  
    15. public class TabApi {
    16.  
    17. private List<String> values = new ArrayList<String>();
    18.  
    19. private static List<String> materials;
    20. static {
    21. ArrayList<String> materialList = new ArrayList<String>();
    22. for (Material material : Material.values()) {
    23. materialList.add(material.name());
    24. }
    25. Collections.sort(materialList);
    26. materials = ImmutableList.copyOf(materialList);
    27. }
    28.  
    29. public void add(String line) {
    30. line = line.replaceAll("[ ]+", " ");
    31.  
    32. if (!values.contains(line)) {
    33. values.add(line);
    34. }
    35. }
    36.  
    37. public List<String> getPossibleCommandCompletions(String command, String[] args) {
    38. for (String a : args) {
    39. command += " " + a;
    40. }
    41. command = "/" + command;
    42. return getPossibleCompletions(command);
    43. }
    44. public List<String> getPossibleCompletions(String command) {
    45.  
    46. List<String> list = new ArrayList<String>();
    47. List<String> possibleList = new ArrayList<String>();
    48.  
    49. int l = command.lastIndexOf(' ');
    50.  
    51. String start;
    52. if (l == -1) {
    53. start = command;
    54. } else {
    55. start = command.substring(0, l);
    56. }
    57.  
    58. for(String s : values) {
    59. if (this.startsWith(s, start)) {
    60. possibleList.add(s);
    61. try {
    62. String suggestion = s.substring(l + 1);
    63. if (suggestion.contains(" ")) {
    64. suggestion = suggestion.substring(0, suggestion.indexOf(' '));
    65. }
    66.  
    67. if (suggestion.equalsIgnoreCase("{player}")) {
    68. for (Player player : Bukkit.getOnlinePlayers()) {
    69. list.add(player.getName());
    70. }
    71. } else if(suggestion.equalsIgnoreCase("{block}")) {
    72. list.addAll(getBlock(command.substring(l + 1)));
    73. } else if (suggestion.equalsIgnoreCase("{int}") || suggestion.equalsIgnoreCase("{string}")) {
    74.  
    75. } else {
    76. if (!list.contains(suggestion)) {
    77. list.add(suggestion);
    78. }
    79. }
    80. } catch (Exception e) {
    81.  
    82. }
    83. }
    84. }
    85. return StringUtil.copyPartialMatches(command.substring(l + 1), list, new ArrayList<String>());
    86. }
    87.  
    88. /**
    89. * @param suggestion
    90. * @param s2
    91. * @return if (suggestion.startswith(s2)) return true
    92. */
    93. private boolean startsWith(String suggestion, String s2) {
    94. if (!suggestion.contains("{player}") && !suggestion.contains("{block}") && !suggestion.contains("{int}") && !suggestion.contains("{string}")) return suggestion.startsWith(s2);
    95. String[] suggestionArray = suggestion.split(" ");
    96. String[] args2 = s2.split(" ");
    97.  
    98. //if args2 has a larger index than suggestionArray
    99. if (suggestionArray.length < args2.length) return false;
    100. //suggestionArray has more or equals the index of args2
    101.  
    102. for (int i = 0; i < args2.length; i++) {
    103. if (suggestionArray[I].equalsIgnoreCase("{player}") || suggestionArray[I].equalsIgnoreCase("{block}") || suggestionArray[I].equalsIgnoreCase("{int}") || suggestionArray[I].equalsIgnoreCase("{string}")) {[/I][/I][/I][/I]
    104. [I] continue;[/I]
    105. [I] } else {[/I]
    106. [I] if (!suggestionArray[I].equalsIgnoreCase(args2[I])) return false;[/I][/I][/I]
    107. [I] }[/I]
    108. [I] }[/I]
    109. [I] return true;[/I]
    110. [I] }[/I]
    111.  
    112. [I] private List<String> getBlock(String substring) {[/I]
    113. [I] final String arg = substring;[/I]
    114. [I] final List<String> materials = TabApi.materials;[/I]
    115. [I] List<String> completion = null;[/I]
    116.  
    117. [I] final int size = materials.size();[/I]
    118. [I] int i = Collections.binarySearch(materials, arg, String.CASE_INSENSITIVE_ORDER);[/I]
    119.  
    120. [I] if (i < 0) {[/I]
    121. [I] // Insertion (start) index[/I]
    122. [I] i = -1 - i;[/I]
    123. [I] }[/I]
    124.  
    125. [I] for ( ; i < size; i++) {[/I]
    126. [I] String material = materials.get(i);[/I]
    127. [I] if (StringUtil.startsWithIgnoreCase(material, arg)) {[/I]
    128. [I] if (completion == null) {[/I]
    129. [I] completion = new ArrayList<String>();[/I]
    130. [I] }[/I]
    131. [I] completion.add(material);[/I]
    132. [I] } else {[/I]
    133. [I] break;[/I]
    134. [I] }[/I]
    135. [I] }[/I]
    136.  
    137. [I] if (completion != null) {[/I]
    138. [I] return completion;[/I]
    139. [I] }[/I]
    140. [I] return ImmutableList.of();[/I]
    141. [I] }[/I]
    142. [I]}[/I]
    143. [I][/I]


    How to use:
    Code:java
    1. @Override
    2. public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
    3. //If the sender is not a player we wouldn't need to do these tab stuff.
    4. if (!(sender instanceof Player)) {
    5. return new ArrayList<String>();
    6. }
    7.  
    8. //Create a new instance of the tabApi
    9. TabApi tabApi = new TabApi();
    10. //Get the player object from sender.
    11. Player player = (Player) sender;
    12.  
    13. //Start registering the complete commands.
    14. //example: tabApi.add("/catchat channel create {string} {string}");
    15. //Specials:
    16. //{string} means that the player should input a string
    17. //{int} same but for an integer (or any number actually)
    18. //{block} Used for when the player has to enter a block (also has tab completion)
    19. String command = cmd.getName();
    20. tabApi.add("/" + command + " channel");
    21. tabApi.add("/" + command + " channel help");
    22.  
    23. //If the player does not have these permissions then the tab completion won't work for these.
    24. if (CatChat.hasPerms(player, "catchat.channel.create")) {
    25. tabApi.add("/" + command + " channel create {string} {string}");
    26. }
    27.  
    28. if (CatChat.hasPerms(player, "catchat.channel.join")) {
    29. tabApi.add("/" + command + " channel join {string} {string}");
    30. tabApi.add("/" + command + " channel leave");
    31. }
    32.  
    33. if (CatChat.hasPerms(player, "catchat.channel.ban")) {
    34. tabApi.add("/" + command + " channel ban {string}");
    35. tabApi.add("/" + command + " channel unban {string}");
    36. }
    37.  
    38. if(CatChat.hasPerms(player, "catchat.admin")) {
    39. tabApi.add("/" + command + " admin reload");
    40. tabApi.add("/" + command + " admin prefix {string} {string}");
    41. tabApi.add("/" + command + " admin suffix {string} {string}");
    42. tabApi.add("/" + command + " admin priority {string} {int}");
    43. }
    44.  
    45. //Finally, returns the list of possible completions, args are the arguments passed into the command, and command is the command. (so without the '/')
    46. return tabApi.getPossibleCommandCompletions(command, args);
    47. }
     
Thread Status:
Not open for further replies.

Share This Page