HashMap

Discussion in 'Plugin Development' started by LightnessPL, Mar 13, 2012.

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

    LightnessPL

    HashMap always return false... May be I wrote something wrong?

    Code:
    public Map<Player, Boolean> playersInSelectionMode = new HashMap<Player, Boolean>();
    Code:
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent event)
        {
                final Player p = event.getPlayer();
                final Action action = event.getAction();
                final Map<Player, Boolean> l = playersInSelectionMode;
                final Boolean m = l.containsValue(l);
                            if (action == Action.LEFT_CLICK_BLOCK) {
                                playersInSelectionMode.put(p, true);
                            p.sendMessage(ChatColor.AQUA + "" + m );
                            }
                        }
     
  2. Offline

    nisovin

    Your map does not contain itself as a value, so it will obviously return false. What exactly are you trying to do?
     
  3. Offline

    Sorrow

    What is the purpose of this line? Are you checking if your hashmap contains your hashmap? :O
    Code:
    final Boolean m = l.containsValue(l);

    And why making the "l" variable if you can simply access to its trough playersInSelectionMode without allocating more memory?
     
  4. Offline

    LightnessPL

    I want to print on the chat value of my HashMap (true or false).

    A few days ago I started making plugins and I thought that I must to make new variable every time.
     
  5. Offline

    Sorrow

    Then you should use:
    Code:
    final Boolean m = l.get(p);
    But it get false when you are set in selection mode for the first time, then returns true.
     
    LightnessPL likes this.
  6. Offline

    LightnessPL

    Ok, this return first time null then true. Now when I have this in commands:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                    Player player = (Player) sender;
               
                    final Map<Player, Boolean> set = plugin.getBlockListener().playersInSelectionMode;
                   
                if(cmd.getName().equalsIgnoreCase("select")){
                    if(set.containsKey(player)){
                        if(set.get(player)){
                            set.put(player, false);
                            player.sendMessage(new StringBuilder(chatPrefix).append("Selection mode: ").append(ChatColor.GREEN).append("OFF").toString());
                        } else {
                            set.put(player, true);
                            player.sendMessage(new StringBuilder(chatPrefix).append("Selection mode: ").append(ChatColor.GREEN).append("ON").toString());
                        }
                    } else {
                        set.put(player, false); //If you want plugin enabled by default change this value to false.
                        player.sendMessage(new StringBuilder(chatPrefix).append("Selection mode: ").append(ChatColor.GREEN).append("ON").toString());
                    }
                }
                return false;
        }
    When I hit block it send null and when I type command /select it send null too...
     
  7. Offline

    Sorrow

    Well, I don't understand what you really want to obtain, but this code says that if there is no occurencies of the command executor in the hashmap, then it creates an occurrence in the hashmap and then sets the selection mode to false. Maybe this isn't what you want.
     
    LightnessPL likes this.
  8. use an WeakHashMap when using Players as key, to prevent memory leaks
     
  9. Offline

    LightnessPL

    I want to allow make left_block_click event after type command /select.
     
  10. Offline

    Lolmewn

    When they do the command, place the person's name in a HashSet.
    When left_block_click happens, check if the player's name is in the HashSet. if it is, go ahead :D If not, do whatever you want.

    Code:
    public HashSet<String> selectPlayers = new HashSet<String>();
     
    LightnessPL likes this.
  11. Offline

    LightnessPL

    I think I did it right, it works perfectly! Thanks for help

    Code:
    public HashSet<String> selectPlayers = new HashSet<String>();
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                    Player player = (Player) sender;
                    String p = player.getName();
                   
                if(cmd.getName().equalsIgnoreCase("select")){
                    if (!selectPlayers.contains(p)) {
                        selectPlayers.add(p);
                        player.sendMessage(new StringBuilder(chatPrefix).append("Selection mode: ").append(ChatColor.GREEN).append("ON").toString());
                        return true;
                } else {
                    selectPlayers.remove(p);
                    player.sendMessage(new StringBuilder(chatPrefix).append("Selection mode: ").append(ChatColor.GREEN).append("OFF").toString());
                    return true;
                    }
                }
                return false;
        }
    Code:
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent event)
        {
                HashSet<String> set1 = plugin.getCommands().selectPlayers;
                final Player p = event.getPlayer();
                final String playerName = p.getName();
                final Action action = event.getAction();
                    if(set1.contains(playerName)) {
                            if (action == Action.LEFT_CLICK_BLOCK) {
                                if(p.getItemInHand().getTypeId() == 271) {
                                p.sendMessage(ChatColor.AQUA + "Work");
                                }
                            }
                }
        }
     
  12. Offline

    Lolmewn

    Good job ;)
     
  13. Offline

    dsmyth1915

    Someone mind explaining what a hashmap is, and how to use it correctly?
     
  14. Offline

    LightnessPL

Thread Status:
Not open for further replies.

Share This Page