Creating a HashMap of an Array of Locations

Discussion in 'Plugin Development' started by Niq, May 3, 2012.

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

    Niq

    Is there a way of doing this?
    In my plugin the players need to select multiple blocks before typing a command to build a structure based on those blocks, so having a hashmap containing an array of the locations of the selected blocks seems like the way to go... easy to get the lists for different players, and easy to access individual block Locations...
    Does anyone know how to do this?

    My current and only attempt so far has been...
    Code:
    public Map<Player, ArrayList<Location>> selection = new HashMap<Player, ArrayList<Location>>();
    Any and all help is greatly appreciated. :)
     
  2. Offline

    Komak57

    public Map<Player, Location> selection = new HashMap<Player, Location>();
    This will create a single location locked to any player. (acts like spawn points)
    public Map<Location, Player> selection = new HashMap<Location, Player>();
    This will lock 1 player per coordinate. (more like block protection)
     
  3. Offline

    Njol

    This is exactly how I would do it. Do you need help on how to use it in the onCommand or were you asking whether this would work at all?
     
  4. Offline

    Niq

    Well, my question would be whether or not this would work at all. I would have thought that the Location array still needed to be initialized... Really though, I don't have a clue.

    And I'm not quite sure how I would use it. I was hoping there would be an easy way to add blocks to the array and read them back out(something like "selection(player).put(location);" but it looks like the only way to read and write to hashmaps is using the methods that Java provides...

    I've been working on the plugin, and this is the relevant part of it. Does this look correct?

    Code:
    public class Song extends JavaPlugin implements Listener{
     
     
        //player Maps
        public Map<Player, ArrayList<Location>> selection = new HashMap<Player, ArrayList<Location>>();
        public Map<Player, ArrayList<Location>> backup = new HashMap<Player, ArrayList<Location>>();
     
     
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
        }
     
     
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event){
            if(!selection.containsKey(event.getPlayer().getName())){
                selection.put(event.getPlayer(), new ArrayList<Location>());
            }
            if(!backup.containsKey(event.getPlayer().getName())){
                backup.put(event.getPlayer(), new ArrayList<Location>());
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  5. Offline

    Vipar

    How about making a new small class, and all it holds, is an array?
    Code:
    public class BlockLocationSet {
        private ArrayList<Location> list;
     
        public BlockLocationSet() {
              list = new ArrayList<Location>();
        }
     
        public ArrayList<Location> getList() {
              return list;
        }
     
        public void setList(ArrayList<Location> newList) {
              list = newList;
        }
    }
    Then in your plugin you should be able to figure it out:
    Code:
    public class Song extends JavaPlugin implements Listener{
     
     
        //player Maps
        public Map<Player, BlockLocationSet> selection = new HashMap<Player,  BlockLocationSet>();
        public Map<Player, BlockLocationSet > backup = new HashMap<Player, BlockLocationSet >();
     
     
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
        }
     
     
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event){
            if(!selection.containsKey(event.getPlayer().getName())){
                selection.put(event.getPlayer(), new ArrayList<Location>());
            }
            if(!backup.containsKey(event.getPlayer().getName())){
                backup.put(event.getPlayer(), new ArrayList<Location>());
            }
        }
    }
     
  6. Offline

    Niq

    That doesn't really solve my original problem of how to create the arrraylist hashmap thingy, but it does help me get a firmer grasp on things.
    I was reading earlier, and apparently, ArrayList is actually a class that you instance that basically gives arrays a couple extra features(man, everything in java is classes...) Your class basically does the same thing for ArrayLists, but I think it may be obsolete if this will do what I think it does...
    Code:
    ArrayList<Location> list = selection.get(player);
    Anyway, your example helped knock a couple of these concepts into my thick skull, so thank you.

    I think I see now... I don't need to initialize the ArrayList itself until I add the player key...
    Code:
    selection.put(event.getPlayer(), new ArrayList<Location>());
    I hope I'm putting all this together right... As you can see, I'm not super-duper good at this stuff.
     
  7. Offline

    Vipar

    Well, my solution would kind of help, because whenever you have a new player on the server, you make a new object of class BlockLocationSet, add it to your HashMap, and use the Player as key.
    Because when you make the BlockLocationSet object, you get the ArrayList with it.

    An Array is a fixed size data structure, which have basic functionality.

    An ArrayList is like an Array, but it doesn't have a fixed size, so it can expand as long as there is memory. This also adds a bit extra functionality. It also keeps the index.

    A HashMap uses an advanced algorithm to calculate where in the HashMap the object you request to put, would fit best. We don't care about the placement of this object, just that we can get it and manipulate it. The HashMap will take care of the rest.
     
Thread Status:
Not open for further replies.

Share This Page