Help With HashMaps

Discussion in 'Plugin Development' started by Bobfan, Aug 21, 2012.

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


    I am almost done with a plugin I am making, but I don't know how to use HashMaps. I've tried to use a hashmap before, but it wasn't working, and I think I was using it entirely wrong. So I want to know how you would assign a Hashmap to a person, so that when they type a command, than that person will be able to do something?
  2. Offline


    A HashMap is a list of values. Each value must have a key associated with it. You retrieve values from the list by giving a corresponding key. Keys and values can be any type (String, Integer, MyCustomClass etc).

            HashMap<String, Integer> map = new HashMap<String, Integer>();
            map.put("crow", 2); // Assigns a 2 to "crow" key
            map.put("apple", 6); // Assigns a 6 to "apple" key
            map.get("apple"); // Gets a value associated with "apple" key, which is 6
            map.get("tree"); // Gets a value associated with "tree" key, which is null, because you haven't assigned one
            map.get("crow"); // Gets a value associated with "crow" key, which is 2
    Im guessing you need to so something like:
            HashMap<String, SomeClass> map = new HashMap<String, SomeClass>();
            map.put(player.getName(), something); // Assigns a "something" to a players name
            SomeClass something = map.get(player.getName()); // Retrieves that "something" assigned to a players name
    You need to specify what you want to do if you want more details.
    MasterDoctor, LordFox and waremanu like this.
  3. Offline


    Ok, so I have the SomeClass part as a boolean, and I put it so when you type a command, it makes that person go into quest maker mode, but I don't know how to make it read if that person is in quest maker mode. I put: if(quest.equals(p.getName(), true)) { but it doesn't want me to keep the , true part. What should I do to get it to read if it's true?
  4. Offline


    Is quest your HashMap?

    If so, it should be something like:

    1. if(quest.get(player.getName()))

    If you've put the player name and an associated boolean true into the HashMap, you can use the get() method to retrieve the boolean associated with that player name.

    Alternatively, if you're just using a true/false, you might consider just using an ArrayList. If the player enters quest maker mode, you add their name to the list. When they leave quest maker mode, you remove their name from the list. Then, to check if they are in quest maker mode, it's as easy as checking if the list contains that player name.
  5. Offline


    Ok, how would I use an ArrayList, I've never used one before?
  6. Offline


    Actually, a HashSet would be better since lists can contain duplicates.
    1. HashSet<String> quest = new HashSet<String>();
    2. quest.add(playerName);
    3. boolean b = quest.contains(playerName);
    4. quest.remove(playerName);
    maxmar628 and MucTweezer like this.
  7. Offline

    Sagacious_Zed Bukkit Docs

  8. Offline


    It doesn't work, am I doing it right? So I have when you type the command, than quest.add(p.getName()). Than, in the eventhandler, if quest.contains(p.getName()), than go on with the action, which has quest.remove(p.getName()). Is this right or did I do something wrong? (p is my player)

    || HON95 ||
  9. Offline


    Sounds right. What isn't working?
  10. Offline


    I'll post the code:
    public class QuestMaker extends Start implements Listener, CommandExecutor {
        public HashSet <String> quest = new HashSet <String >();
        Player p;
        public boolean onCommand(CommandSender sender1, Command command, String commandLabel, String[] args) {
            p = (Player) sender1;
            if(command.getName().equalsIgnoreCase("NewQuest")) {
                p.sendMessage("Click your sign you want for the quest");
                return true;
        return false;
        public void onInteractEvent(PlayerInteractEvent event) {
            if(quest.contains(p.getName())) {
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
                Block b = event.getClickedBlock();
                if(b.getType().equals(Material.SIGN_POST) || b.getType().equals(Material.WALL_SIGN)) {
                    int x = event.getClickedBlock().getX();
                    int y = event.getClickedBlock().getY();
                    int z = event.getClickedBlock().getZ();
                    config.createSection("" + x + "," + y + "," + z + ".Name");
                    config.createSection("" + x + "," + y + "," + z + ".Message");
                    config.createSection("" + x + "," + y + "," + z + ".Players.Started");
                    config.createSection("" + x + "," + y + "," + z + ".Players.Compleated");
                    config.createSection("" + x + "," + y + "," + z + ".Reward");
                    config.createSection("" + x + "," + y + "," + z + ".MobSpawn.Numbers");
                    config.createSection("" + x + "," + y + "," + z + ".MobSpawn.Mob");
                    config.createSection("" + x + "," + y + "," + z + ".Fee");
                    config.createSection("" + x + "," + y + "," + z + ".PrevQuest.Coords");
                    config.createSection("" + x + "," + y + "," + z + ".PrevQuest.NotCompleated");
    It only get's to the if(quest.contains(p.getName())) { Than it doesn't go any farther for some reason?
  11. Offline


    What do you mean "it doesn't go farther"? Does it throw an error (a NullPointerException before you enter the command I'm guessing)? And you need to get the player from the event (event.getPlayer()), not some global var set to who ever entered the last command and null before someone did.

    BTW, never store player objects, as they contains references to E.G. the world they are in. Therefore the world will stay in memory and take up your RAM, even if it was unloaded.
  12. Offline


    Oh ok. I'm not using a hashset now though anyways, I'm using a boolean, and permissions, because they will still work.
  13. Offline


    package com.vtboy2000.tablist;

    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;

    public class Main extends JavaPlugin implements Listener {
    HashMap<String, Player> map = new HashMap<String, Player>();

    public void onEnbable() {
    PluginManager pm = Bukkit.getServer().getPluginManager();


    public boolean onCommand(CommandSender sender, Command command,
    String label, String[] args) {
    // TODO Auto-generated method stub

    Player me = (Player) sender;
    if (label.equalsIgnoreCase("tpall")) {
    map.put( me.getName(), me);
    for (Player player : Bukkit.getOnlinePlayers()) {
    if (map.containsValue(player.getName()) || map.containsKey(player.getName())) {

    return false;


Thread Status:
Not open for further replies.

Share This Page