User Defined Arguments

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

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

    xAstraah

    So, Im making a plugin and this guy want it so he can create command arguments
    eg. /title create <TitleName> <Prefix>

    So far i can only work out how to save Titles along with prefix's to a config and pull them again but how can i make it so that he / admins can create command arguments so the players can use the <TITLENAME> they have set to pull that along with information from the configuration.

    /title set <TitleName> using arguments.
     
  2. xAstraah Can you post the full code you have at the moment, please? Will be easier in the long run.
     
  3. Offline

    xAstraah

    AdamQpzm This is what i have so far but i don't think any of it contains code for supporting custom commands.... :(

    Code:java
    1. package io.github.xAstraah;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. import java.util.ArrayList;
    11. import java.util.List;
    12.  
    13. /**
    14. * Created by Stephen on 23/04/2014.
    15. */
    16. public class Commands implements CommandExecutor
    17. {
    18.  
    19. public Titles plugin;
    20.  
    21. public Commands(Titles plugin)
    22. {
    23. this.plugin = plugin;
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
    27. {
    28. Player player = (Player) sender;
    29.  
    30. if(commandLabel.equalsIgnoreCase("Title"))
    31. {
    32.  
    33. if(args.length < 1)
    34. {
    35. player.sendMessage(ChatColor.RED + "Not enough arguments!");
    36. return true;
    37. }
    38.  
    39. if(args[0].equalsIgnoreCase("create"))
    40. {
    41. if(args.length < 3)
    42. {
    43. player.sendMessage(ChatColor.RED + "Not enough arguments!");
    44. return true;
    45. }
    46.  
    47. List<String> Prefix = new ArrayList<String>();
    48. Prefix.add(args[2]);
    49. plugin.getConfig().set("Titles." + args[1], Prefix);
    50. plugin.saveConfig();
    51. Prefix.clear();
    52. plugin.reloadConfig();
    53. player.sendMessage(ChatColor.GREEN + "Title: " + args[2] + " has been added to the config!");
    54. return true;
    55.  
    56. }
    57.  
    58. if(args[0].equalsIgnoreCase("set"))
    59. {
    60. if(player.hasPermission(plugin.getConfig().getString("Titles." + args[1] + ".Permission")))
    61. {
    62.  
    63. }
    64.  
    65. if(plugin.getConfig().getString("Titles." + args[1]) == null)
    66. {
    67. player.sendMessage(ChatColor.RED + "Sorry, That title does not exist!");
    68. return true;
    69. }
    70.  
    71. if(args.length < 1)
    72. {
    73. player.sendMessage(ChatColor.RED + "Not enough arguments!");
    74. return true;
    75. }
    76. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "manudelv " + player.getName() + " prefix ");
    77. plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), "manuaddv " + player.getName() + " prefix " + plugin.getConfig().getString("Titles." + args[1]));
    78. player.sendMessage(ChatColor.GREEN + "Your title has been set to " + args[1]);
    79. return true;
    80. }
    81.  
    82. }
    83.  
    84. return false;
    85. }
    86.  
    87. }
    88.  
     
  4. xAstraah Sorry, I'm lost. What is it that you want to do now?
     
  5. Offline

    xAstraah

    AdamQpzm, I want to make it so a admin can do /title create Elite and it will make it so that it makes a new sub command called /title set Elite, So basicly Elite is the new argument that i want to set.

    Example:

    -No Commands Generated
    -Admin Performs /Title Create Elite
    -Elite Sub Comand Created (/Title set Elite)
    -Prefix and Command Name Saved To Config.
     
  6. xAstraah When using create, you save the args[1] (which in this case is Elite) in the config file. When doing set, all you need to do is check if the config contains the args[1] (again, Elite in this case). Understand?
     
  7. Offline

    xAstraah

    AdamQpzm, This is far as i could figure out? Doesn't work so how would i go about this?

    Code:java
    1. if(args[0].equalsIgnoreCase(plugin.getConfig().getString(args[0])))
    2. {
    3. if(plugin.getConfig().getString(args[0]) != null)
    4. {
    5. if(player.hasPermission(args[0] + ".use"))
    6. {
    7. player.setDisplayName(ChatColor.GREEN + player.getName());
    8. return true;
    9. }
    10. else
    11. {
    12. player.sendMessage(ChatColor.RED + "Sorry, You do not have " + args[0] + ".use!");
    13. }
    14. }
    15. else
    16. {
    17. player.sendMessage(ChatColor.RED + "Sorry, This command does not exist!");
    18. return true;
    19. }
    20. }
    21. }
     
  8. xAstraah I'm guessing it always outputs that the command doesn't exist, even when it does?
     
  9. Offline

    xAstraah

    AdamQpzm, No, outputs nothing at all...
     
  10. xAstraah Then are you sure that the code is executed at all? Put some debug code in to check. Also check the section that sets the display name.
     
  11. Offline

    xAstraah

    AdamQpzm, So what should i put in the Catch statement then?

    EDIT: Print stacktrace or something?
     
  12. Offline

    xAstraah

    AdamQpzm, I haven't made one because i do not know how to use them. Like i know how to use them i just dont know what i need to catch.
     
  13. xAstraah Right. Well just try this and tell me what numbers print to console:
    PHP:
    System.out.println(1);
    if(
    args[0].equalsIgnoreCase(plugin.getConfig().getString(args[0])))
    {
        
    System.out.println(2);
        if(
    plugin.getConfig().getString(args[0]) != null)
        {
            
    System.out.println(3);
            if(
    player.hasPermission(args[0] + ".use"))
            {
                
    System.out.println(4);
                
    player.setDisplayName(ChatColor.GREEN player.getName());
                return 
    true;
            }
            else
            {
                
    System.out.println(5);
                
    player.sendMessage(ChatColor.RED "Sorry, You do not have " args[0] + ".use!");
            }
        }
        else
        {
            
    System.out.println(6);
            
    player.sendMessage(ChatColor.RED "Sorry, This command does not exist!");
            return 
    true;
        }
    }
    }
     
  14. Offline

    xAstraah

    AdamQpzm, So i tried running this code while using a command in the configuration and without and the same answer occurred.

    Screenshot
     
  15. xAstraah Oh right, I think I know now. To check, though, can you post your config?
     
  16. Offline

    xAstraah

  17. Offline

    xAstraah

    AdamQpzm, Indeed. I belive its because there are no Strings to get like:

    Code:
    Name:
      - Stephen
      - Adam
      - Jay
      - Liam
     
  18. xAstraah Then rather than getString(args[0]) != null try contains(args[0])
     
  19. Offline

    xAstraah

  20. xAstraah Oh sorry, your code confused me. See this:

    PHP:
                    if(plugin.getConfig().contains(args[0])
                    {
                        if(
    player.hasPermission(args[0] + ".use"))
                        {
                            
    player.setDisplayName(ChatColor.GREEN player.getName());
                            return 
    true;
                        }
                        else
                        {
                            
    player.sendMessage(ChatColor.RED "Sorry, You do not have " args[0] + ".use!");
                        }
                    }
                    else
                    {
                        
    player.sendMessage(ChatColor.RED "Sorry, This command does not exist!");
                        return 
    true;
                    }
                }
            
     
  21. Offline

    xAstraah

    Code i used:
    Code:java
    1. if(commandLabel.equalsIgnoreCase("Title"))
    2. {
    3. if(args[0].equalsIgnoreCase(args[0]))
    4. {
    5. if(getConfig().contains(args[0]))
    6. {
    7. player.sendMessage(ChatColor.GREEN + "Sorry, But this command was found :) :D");
    8. return true;
    9. }
    10. else
    11. {
    12. player.sendMessage(ChatColor.RED + "Sorry, This command does not exist!");
    13. return true;
    14. }
    15. }
    16. }

    Always prints the Else statement...
     
  22. xAstraah Are you sure that the config and the thing you're entering match? (case sensitive)
     
  23. Offline

    xAstraah

    AdamQpzm, Yes because i always type in Capitals.
     
  24. getConfig().getStringList("name")
    getConfig().set("name", arraylist)
     
  25. Offline

    xAstraah

    DJSkepter, I already have a method i will be using, Also this is just about how i can get the user to create command arguments via commands.
     
  26. Offline

    xAstraah

  27. xAstraah If the else statement always prints, then the config doesn't contain the path. Try printing this as debug somewhere:

    Code:
    System.out.println(args0);
    for(String s : getConfig().getValues(false).keySet()) {
    System.out.println(s);
    }
    And tell me the output
     
  28. Offline

    xAstraah

    AdamQpzm, Tried it same result.

    Config:
    Code:
    Welcome:
    Console Log:
    Code:
    [23:32:41] [Server thread/INFO]: TmPxsHoTTz issued server command: /title welcome
    [23:32:41] [Server thread/INFO]: welcome
    [23:32:45] [Server thread/INFO]: TmPxsHoTTz issued server command: /title Welcome
    [23:32:45] [Server thread/INFO]: Welcome
    
    Plugin Code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
    2. {
    3. Player p = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("Title") || commandLabel.equalsIgnoreCase("Titles"))
    6. {
    7. if(args[0].equalsIgnoreCase(args[0]))
    8. {
    9. if(plugin.getConfig().contains(args[0]))
    10. {
    11. p.sendMessage(ChatColor.GOLD + ">> " + ChatColor.BLUE + "Well done! This command now works!");
    12. }
    13. else
    14. {
    15. System.out.println(args[0]);
    16. for(String s : plugin.getConfig().getValues(false).keySet())
    17. {
    18. System.out.println(s);
    19. }
    20. p.sendMessage(ChatColor.GOLD + ">> " + ChatColor.RED + "Like usual this command is broken!");
    21. }
    22. }
    23. }
    24.  
    25. return false;
    26. }
     
Thread Status:
Not open for further replies.

Share This Page