Get variable from config and use as command! Help

Discussion in 'Plugin Development' started by kevinator0122, Apr 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    kevinator0122

    So i am making a kits plugin where you can save the kits while in game. I have most of it done but i dont know how to get the line of the kit name into a command to get the kit. Here is what i have:
    Code:java
    1. package me.kevin0122.kits;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.logging.Level;
    7. import java.util.logging.Logger;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.plugin.PluginDescriptionFile;
    16. import org.bukkit.plugin.PluginManager;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Main extends JavaPlugin implements Listener {
    20. public final Logger logger = Logger.getLogger("Minecraft");
    21. private FileConfiguration Kits = null;
    22. private File KitsFile = null;
    23.  
    24. public void onEnable() {
    25. PluginDescriptionFile pdfFile = getDescription();
    26. logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
    27. + " Has Been Enabled");
    28. PluginManager reg = getServer().getPluginManager();
    29. reg.registerEvents(this, this);
    30. reloadKits();
    31. }
    32.  
    33. public void onDisable() {
    34. PluginDescriptionFile pdfFile = getDescription();
    35. logger.info(pdfFile.getName() + " Has Been Disabled");
    36. saveKits();
    37. }
    38.  
    39. public void reloadKits() {
    40. if (KitsFile == null) {
    41. KitsFile = new File(getDataFolder(), "Kits.yml");
    42. }
    43. Kits = YamlConfiguration.loadConfiguration(KitsFile);
    44.  
    45. InputStream defConfigStream = this.getResource("Kits.yml");
    46. if (defConfigStream != null) {
    47. YamlConfiguration defConfig = YamlConfiguration
    48. .loadConfiguration(defConfigStream);
    49. Kits.setDefaults(defConfig);
    50. }
    51. }
    52.  
    53. public FileConfiguration getKits() {
    54. if (Kits == null) {
    55. this.reloadKits();
    56. }
    57. return Kits;
    58. }
    59.  
    60. public void saveKits() {
    61. if (Kits == null || KitsFile == null) {
    62. return;
    63. }
    64. try {
    65. getKits().save(KitsFile);
    66. } catch (IOException ex) {
    67. this.getLogger().log(Level.SEVERE,
    68. "Could not save config to " + KitsFile, ex);
    69.  
    70. }
    71. }
    72.  
    73. public boolean onCommand(CommandSender sender, Command cmd,
    74. String commandLabel, String[] args) {
    75. Player player = (Player) sender;
    76. if ((sender instanceof Player)) {
    77. if (commandLabel.equalsIgnoreCase("kits")) {
    78. if (args.length == 0) {
    79. player.sendMessage(ChatColor.GREEN
    80. + "Plugin made by kevinator0122");
    81. } else if (args.length == 2) {
    82. if (args[0].equalsIgnoreCase("setkit")) {
    83. if (player.hasPermission("kit.admin")) {
    84. String kitname = args[1].toString().toLowerCase();
    85. getKits().set(kitname + ".inventory",
    86. player.getInventory().getContents());
    87. getKits().set(kitname + ".armor",
    88. player.getInventory().getArmorContents());
    89. player.sendMessage(ChatColor.GREEN + "Kit " + ChatColor.GREEN + kitname + " Saved");
    90. saveKits();
    91. }
    92. } else if (args.length == 2) {
    93. if (args[0].equalsIgnoreCase("delkit")) {
    94. if (player.hasPermission("kit.admin")) {
    95. String kitname = null;
    96. try {
    97. kitname = args[1].toString().toLowerCase();
    98. } catch (NullPointerException e) {
    99. player.sendMessage(ChatColor.RED
    100. + "No kit found");
    101. }
    102. getKits().set(kitname + ".inventory", null);
    103. getKits().set(kitname + ".armor", null);
    104. getKits().set(kitname, null);
    105. player.sendMessage(ChatColor.GREEN + "Kit " + ChatColor.GREEN + kitname + " Erased");
    106. saveKits();
    107. }
    108. }else if (commandLabel.equalsIgnoreCase(kitname)){ //This part right here
    109.  
    110. }
    111. }
    112. }
    113.  
    114. }
    115. }
    116. return false;
    117. }
    118. }


    Still need help. Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    Onlineids

    Dont bump in like 2 hours -_-
     
    kevinator0122 likes this.
  3. Offline

    BillyGalbreath

    Its not easy having configurable command names. Bukkit requires the commands be registered in plugin.yml - The easiest way to do what you want is to have 1 command registered (/kit) and the argument be variable kit name (/kit weapons).
     
    kevinator0122 likes this.
  4. Offline

    kevinator0122

    ok, i guess ill do that
     
  5. Offline

    coasterman10

    Best option would probably be /kit <name>. You could also listen in on command preprocessing to see if a player typed "/<name>" but that could obscure other plugins' commands if they end up with the same name as your kit, not to mention the javadoc recommends against interrupting command preprocessing except when absolutely necessary.
     
Thread Status:
Not open for further replies.

Share This Page