Solved How to make a players argument become the arraylist they are in.

Discussion in 'Plugin Development' started by XlegitXcrazymanX, Feb 22, 2013.

Thread Status:
Not open for further replies.
  1. So I am attempting to create a grouping plugin where players make there own groups, and they will be put in an arraylist but I want to name the arraylist the second argument that they choose. It would be like
    /t create <teamname>

    //new ArrayList with the teamname.
    Here is what I have so far...
    Code:java
    1. public boolean Create(Command sender, Command cmd, String[] args) {
    2. Player player = (Player) sender;
    3. if (args[0].equalsIgnoreCase("create") &&
    4. //create the arraylist here
    5. args[1].equalsIgnoreCase("")) {
    6. if(sender instanceof Player) {
    7. ArrayList<String> team = new ArrayList<String>();
    8. team.add(player.getName());
    9. for (Player p : Bukkit.getOnlinePlayers()) {
    10. p.sendMessage(ChatColor.BLUE+player.getName()+"has created a new team");
    11. }
    12. }
    13. }
    14. return true;
    15. }
    16. }
    17. [/syntax=java]
    18. Thanks in advanced! ;D
     
  2. Offline

    ZeusAllMighty11

    Pretty sure there's no way to parse an arraylist... But not positive


    Why don't you instead store a temporary string, and turn that into a HashMap with the player who created it and the name of the team. Then when you want to retrieve the team name, which could be alternatively stored in another file for safekeeping, you can do it pretty easily by just getting the team creator's name or whatever
     
  3. ZeusAllMighty11 I know how to do the rest, I am not sure how to achieve on making the second argument a string though, got an example? :D
     
  4. Offline

    Geekola

    Something like this?

    Code:java
    1.  
    2. public boolean Create(Command sender, Command cmd, String[] args) {
    3.  
    4. //This map is just for illustration.
    5. //It should be out of this function or it will go out of scope
    6. Map<String, List<String>> teams = new HashMap<String, List<String>>();
    7.  
    8. Player player = (Player) sender;
    9.  
    10. if ( args.length < 2 ) {
    11. player.sendMessage("You didn't say THE NAME OF YOUR FRICKEN TEAM. FAIL.");
    12. return false;
    13. }
    14.  
    15. String verb = args[0].toLowerCase();
    16. String team = args[1].toLowerCase();
    17.  
    18. if ( verb.equals("create") ) {
    19.  
    20. if ( teams.containsKey( team ) ) {
    21. player.sendMessage("Team already exists");
    22. return true;
    23. } else {
    24. teams.put( team, new ArrayList<String>( Arrays.asList( player.getName().toLowerCase() ) ) );
    25. }
    26.  
    27. for ( int x = 2; x < args.length; x++ ) {
    28. teams.get( team ).add( args[2].toLowerCase() );
    29. }
    30.  
    31. } else if ( verb.equals( "join" ) ) {
    32.  
    33. if ( teams.containsKey( team ) ) {
    34.  
    35. teams.get( team ).add( player.getName().toLowerCase() );
    36.  
    37. }
    38.  
    39. } else if ( verb.equals( "quit" ) ) {
    40.  
    41. if ( teams.containsKey( team ) ) {
    42. teams.get( team ).remove( player.getName().toLowerCase() );
    43. }
    44.  
    45. } else if ( verb.equals( "disband" ) ) {
    46.  
    47. if ( teams.containsKey( team ) ) {
    48. teams.remove( team );
    49. }
    50.  
    51. } else if ( verb.equals( "kick" ) && args.length == 3 ) {
    52.  
    53. if ( teams.containsKey( team ) ) {
    54. teams.get( team ).remove( args[2].toLowerCase() );
    55. }
    56. }
    57.  
    58. return true;
    59.  
    60. }
    61.  
     
  5. Geekola A bit like that. Thanks I will be sure to use parts of this :D
     
Thread Status:
Not open for further replies.

Share This Page