Adding Player Name to String List w/ Command

Discussion in 'Plugin Development' started by WeaselBuilds, Aug 1, 2013.

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

    WeaselBuilds

    Hey there! I've been trying to add a player to a string list! As I've asked about creating new (School) Classes, I want players to join by using 2 arguments. The command works but it doesn't add the player's name to the list.

    Code:

    Part of onCommand():

    Code:java
    1. public class ClassCommands implements CommandExecutor {
    2.  
    3. public Player player;
    4.  
    5. SchoolPluginMain plugin;
    6.  
    7. public ClassCommands(SchoolPluginMain plugin){
    8. this.plugin = plugin;
    9. }
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender sender, Command cmd, String label,
    13. String[] args) {
    14. if(cmd.getName().equalsIgnoreCase("class")){
    15. if(sender instanceof Player){
    16. if(args[0].equalsIgnoreCase("tpgui")){
    17. if(args.length != 1){
    18.  
    19. }
    20. }
    21. if(args.length != 2){
    22. sender.sendMessage(ChatColor.RED + "Usage: /class <option> [value]");
    23. return true;
    24. }
    25. if(args[0].equalsIgnoreCase("create")){
    26. String className = args[1].toString();
    27. String[] list = {"Test"};
    28. if(plugin.getConfig().get("Classes." + className) == null){
    29. plugin.getConfig().addDefault("Classes." + className, Arrays.asList(list));
    30. plugin.saveConfig();
    31. sender.sendMessage(ChatColor.GREEN + "You have successfully created the " + className + " class!");
    32. }else{
    33. sender.sendMessage(ChatColor.RED + "That class already exists!");
    34. }
    35. }else if(args[0].equalsIgnoreCase("join")){
    36. Player player = Bukkit.getPlayer(sender.getName());
    37. String className = args[1].toString();
    38. if(!plugin.getConfig().getStringList("Classes." + className).contains(player.toString())){
    39. plugin.getConfig().getStringList("Classes." + className).add(player.toString());
    40. plugin.saveConfig();
    41. sender.sendMessage(ChatColor.GREEN + "You have successfully joined the " + className + " class!");
    42. }else{
    43. sender.sendMessage(ChatColor.RED + "You are already in this class!");
    44. }
    45. }else if(args[0].equalsIgnoreCase("leave")){
    46. Player player = Bukkit.getPlayer(sender.getName());
    47. String className = args[1].toString();
    48. if(plugin.getConfig().getStringList("Classes." + className).contains(player.toString())){
    49. plugin.getConfig().getStringList("Classes." + className).remove(player.toString());
    50. plugin.saveConfig();
    51. sender.sendMessage(ChatColor.GREEN + "You have successfully left the " + className + " class!");
    52. }else{
    53. sender.sendMessage(ChatColor.RED + "You are not in this class!");
    54. }
    55. }
    56. }
    57. return true;
    58. }
    59. return false;
    60. }
    61. }
    62.  


    Also it doesn't give any error in the command prompt.
     
  2. Offline

    gomeow

    You need to get the list, save it to a variable, add to it, then set the list to the config, then save
     
  3. Offline

    WeaselBuilds

    I don't understand.. I updated the code with my class file.
     
  4. Offline

    Weasel_Squeezer

    First of all, to get the player, you can simply just do
    Code:java
    1. Player player = (Player)sender;

    since the Player class extends CommandSender

    and when you are calling
    Code:java
    1. player.toString()

    that will print out a string representation of the Player object. to get the players name use this instead:
    Code:java
    1. player.getName()


    This should fix your problems with adding the player to the list
     
  5. Offline

    WeaselBuilds

    I tried this and still doesn't work.
    Look at the code:
    Code:java
    1. if(args[0].equalsIgnoreCase("join")){
    2. Player player = (Player)sender;
    3. String className = args[1].toString();
    4. if(!plugin.getConfig().getStringList("Classes." + className).contains(player.getName())){
    5. plugin.getConfig().getStringList("Classes." + className).add(player.getName());
    6. plugin.saveConfig();
    7. sender.sendMessage(ChatColor.GREEN + "You have successfully joined the " + className + " class!");
    8. }else{
    9. sender.sendMessage(ChatColor.RED + "You are already in this class!");
    10. }
     
  6. Offline

    Weasel_Squeezer

    Ohhh it is because you are adding the player to the list rather then setting the config with a new list. config.getStringList() returns a new list, and adding things to that list will be useless unless you add that list back to the config. so do this instead
    Code:java
    1. List<String> classes = plugin.getConfig().getStringList("Classes." + className);
    2. if(!classes.contains(player.getName())){
    3. classes.add(player.getName());
    4. plugin.getConfig().set("Classes." + className, classes);
    5. plugin.saveConfig();
    6. sender.sendMessage(ChatColor.GREEN + "You have successfully joined the " + className + " class!");
    7. }
     
    ampayne2 and WeaselBuilds like this.
  7. Offline

    WeaselBuilds

  8. Offline

    gomeow

    I sort of just said that...

     
  9. Offline

    MauGro

    Code:java
    1. //First you must make a String list
    2.  
    3. //Creating an ArrayList for the Players with the values of a String
    4.  
    5. List<String>players = new ArrayList<String>();
    6.  
    7. //And then you save the StringList in a Config
     
  10. Offline

    bobthefish

    Soory to bring back an old tread, but I have a problem with this too, I have almost that exact thing ( just some string namesa re different) but when ever I try and add something to the list, it replaces the other one, any help?
     
  11. Offline

    BillyGalbreath

    Simplest example:
    Code:java
    1.  
    2. List<String> names = getConfig().getStringList("list-of-names");
    3. if (!names.contains(player.getName()))
    4. names.add(player.getName());
    5. getConfig().set("list-of-names", names);
    6.  
     
  12. Offline

    bobthefish

    I tried that, This is what I have:

    Code:java
    1. List<String> groups = getConfig().getStringList("groups");
    2. groups.add(args[0]);
    3. getConfig().set("Groups", groups);
    4. saveConfig();


    BillyGalbreath
    Well I dont have the if statement would that be whats wrong, even though the two groups Im adding are different?

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

    BillyGalbreath

    Might be your capitalization. Groups != groups
     
  14. Offline

    bobthefish

    BillyGalbreath

    ohhh thx, Ill try that

    BillyGalbreath
    Yup fixed it thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page