Putting lists of lists in hashmaps?

Discussion in 'Plugin Development' started by gomeow, Oct 29, 2012.

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

    gomeow

    How can I make a list for every player that does a command? (They will be adding things to it)
     
  2. Offline

    Infamous Jeezy

    gomeow
    I'm not quite sure what you're asking for here.
    Are you trying to make your server log who executes a command?
    If so, all commands or just a specific one?
     
  3. Offline

    gomeow

    What I need is for when a player types a command, it needs to put args[2] into a list and args[3] into another list.
    But my problem is it needs to be a per player list(each player has their own 2 lists)
    I could easily do this with a yml file, but I was wondering if there was a way around that
     
  4. Offline

    Infamous Jeezy

    Nothing that I can think of that wouldn't include parsing strings, I recommend using the .yml file.
     
  5. Offline

    Timr

    HashMap<String, List<String>>,

    The first parameter is the player's name, the second is their string list. Use .get(Player.getName()) to get their list, .add(String) to add to that list, then .put(Player.getName(), ReferenceToList) to put it back in the map.
     
  6. Offline

    Infamous Jeezy

    I think he's trying to make two separate lists for each player, if I'm reading your idea correctly that'd only have one.
     
  7. Offline

    Timr

    Heh, HashMap<String, List<List<String>>>;
     
    gomeow and Infamous Jeezy like this.
  8. Offline

    gomeow

    That might work... Didn't think of that. I will try this when I get a chance

    One thing though, outing a list inside a list seem co fusing to me, how would I use it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  9. Offline

    gomeow

  10. Offline

    ZeusAllMighty11

    I think it's easier to use a stringbuilder
     
  11. Offline

    gomeow

    Do you mean to put the 2 things in the same list? I think I know how to get the last few digits of a number. How would I get whatever is after a certain symbol?
     
  12. Offline

    Cjreek

    Timr: You don't have to put the list back into the hashmap.

    You could also create a class (Dummy) which holds 2 lists (args[2]-list and args[3]-list) and have a HashMap<String, List<Dummy>>.
     
  13. Offline

    gomeow

    Could you explain?
     
  14. Offline

    cman1885

    [quote uid=90728305 name="gomeow" post=1396744]Could you explain?[/quote]
    <Edit by Moderator: Redacted bit url>
     
    Last edited by a moderator: Feb 18, 2017
  15. Offline

    Sushi

    If you do this be forewarned you could create a massive memory hole.
     
  16. what do you mean whit this?
     
  17. Offline

    Sushi

    Well, if you keep putting string and player objects into one hashmap, it would essentially grow and grow and grow, if I'm not mistaken.
     
  18. Offline

    gomeow

    They would eventually get taken out
     
  19. Offline

    Cjreek


    Something like that. But as mentioned above you should clear those lists sometime because else they wouldn't stop
    growing until you reload/restart your server.

    Code:
    public class ArgLists
    {
        public List<String> args2 = new ArrayList<String>();
        public List<String> args3 = new ArrayList<String>();
    }
     
    public class yourPlugin extends JavaPlugin
    {
        private HashMap<String, ArgLists> playerargs = new HashMap<String, ArgLists>();
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
          // do something
     
            if (sender instanceof Player)
            {
                Player player = (Player)sender;
     
                if (!playerargs .containsKey(player.getName())
                    playerargs .put(player.getName(), new ArgLists());
           
                if (args.length >= 3)
                    playerargs.get(player.getName()).args2.add(args[2]);
                if (args.length >= 4)
                    playerargs.get(player.getName()).args3.add(args[3]);
            }
     
            return false;
        }
    }
     
  20. Offline

    gomeow

    I figured out how to do this, I put them in the same string, then separated them when I wanted to take them out
     
Thread Status:
Not open for further replies.

Share This Page