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 ); } }
Your map does not contain itself as a value, so it will obviously return false. What exactly are you trying to do?
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?
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.
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.
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...
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.
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 If not, do whatever you want. Code: public HashSet<String> selectPlayers = new HashSet<String>();
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"); } } } }