Solved HashMap not containing key?

Discussion in 'Spigot Plugin Development' started by Waldxn, May 29, 2017.

  1. So In this bounty plugin I'm working with, you can place the bounty fine, and remove the bounty fine, but on the player move event (Just using this event to test the claiming of bounties), it is printing "Event Fired" and "Doesn't Contain". What am I missing here?

    Code:
    Code (Text):
    public class Functions implements CommandExecutor, Listener {

        public Map<Player, Double> bounty = new HashMap<>();
        private Map<Player, Player> bountyplacer = new HashMap<>();

        @Override
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {

            if (sender instanceof Player) {
                Player player = (Player) sender;
                player.sendMessage("Casting");
                if (sender.hasPermission("bounties.player")) {
                    player.sendMessage("Permission");
                    if (args.length == 3) {
                        player.sendMessage("Length");
                        if (args[0].equalsIgnoreCase("place")) {
                            player.sendMessage("place args");
                            Player target = Bukkit.getPlayer(args[1]);
                            if (target != null) {
                                player.sendMessage("null");
                                double price = Double.parseDouble(args[2]);
                                try {
                                    if (Economy.hasEnough(player.getName(), BigDecimal.valueOf(price))) {
                                        bounty.put(target, price);
                                        bountyplacer.put(target, player);
                                        player.sendMessage(ChatColor.GOLD + "The money will be sent on completion of the placed Bounty!");
                                        Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty of $" + price + " has been placed on " + target.getDisplayName());
                                    } else {
                                        player.sendMessage("You don't have enough money to place a bounty!");
                                    }
                                } catch (UserDoesNotExistException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                sender.sendMessage("Player isn't online!");
                            }
                        }
                    } else if (args.length == 2 && args[0].equalsIgnoreCase("remove")) {
                        Player target = Bukkit.getPlayer(args[1]);
                        if (bountyplacer.containsKey(player) && bountyplacer.containsValue(target)) {
                            bounty.remove(target);
                            bountyplacer.remove(player);
                            Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty for " + target.getDisplayName() + " has been removed!");
                        } else {
                            sender.sendMessage("Bounty for that player doesn't exist!");
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "Usage: /bounty [place/remove] [player] [price]");
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + "You don't have permission to place/remove bounties!");
                }
            } else {
                sender.sendMessage(ChatColor.RED + "You must be a player to place/remove bounties!");
            }
            return false;
        }


        @SuppressWarnings("deprecation")
        @EventHandler
        public void bountyClaim(PlayerMoveEvent e) throws UserDoesNotExistException, NoLoanPermittedException {
            Player target = e.getPlayer();
            target.sendMessage("Event fired");
            if (bounty.containsKey(target)) {

                Economy.add(target.getName().toString(), bounty.get(target));
                Economy.substract(bountyplacer.get(target).getName(), BigDecimal.valueOf(bounty.get(target)));
                Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty on " + target.getDisplayName() + " was claimed by " + target.getDisplayName() + "!");
                bountyplacer.remove(target);
                bounty.remove(target);
            }
            else {
                target.sendMessage("Doesn't contain");
            }
        }
     
  2. Hmm, could you try storing UUIDs instead and testing if the UUIDs are equal? Storing UUIDs instead of Player objects is a lighter operation anyway.
     
    • Like Like x 1
  3. Same results:

    Code (Text):
    public class Functions implements CommandExecutor, Listener {

        private Map<UUID, Double> bounty = new HashMap<>();
        private Map<UUID, UUID> bountyplacer = new HashMap<>();

        @Override
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {

            if (sender instanceof Player) {
                Player player = (Player) sender;
                UUID playeruuid = player.getUniqueId();
                player.sendMessage("Casting");
                if (sender.hasPermission("bounties.player")) {
                    player.sendMessage("Permission");
                    if (args.length == 3) {
                        player.sendMessage("Length");
                        if (args[0].equalsIgnoreCase("place")) {
                            player.sendMessage("place args");
                            Player target = Bukkit.getPlayer(args[1]);
                            if (target != null) {
                                UUID targetuuid = target.getUniqueId();
                                player.sendMessage("null");
                                double price = Double.parseDouble(args[2]);
                                try {
                                    if (Economy.hasEnough(player.getName(), BigDecimal.valueOf(price))) {
                                        bounty.put(targetuuid, price);
                                        bountyplacer.put(targetuuid, playeruuid);
                                        player.sendMessage(ChatColor.GOLD + "The money will be sent on completion of the placed Bounty!");
                                        Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty of $" + price + " has been placed on " + target.getDisplayName());
                                    } else {
                                        player.sendMessage("You don't have enough money to place a bounty!");
                                    }
                                } catch (UserDoesNotExistException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                sender.sendMessage("Player isn't online!");
                            }
                        }
                    } else if (args.length == 2 && args[0].equalsIgnoreCase("remove")) {
                        Player target = Bukkit.getPlayer(args[1]);
                        UUID targetuuid = target.getUniqueId();
                        if (bountyplacer.containsKey(playeruuid) && bountyplacer.containsValue(targetuuid)) {
                            bounty.remove(targetuuid);
                            bountyplacer.remove(playeruuid);
                            Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty for " + target.getDisplayName() + " has been removed!");
                        } else {
                            sender.sendMessage("Bounty for that player doesn't exist!");
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "Usage: /bounty [place/remove] [player] [price]");
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + "You don't have permission to place/remove bounties!");
                }
            } else {
                sender.sendMessage(ChatColor.RED + "You must be a player to place/remove bounties!");
            }
            return false;
        }


        @SuppressWarnings("deprecation")
        @EventHandler
        public void bountyClaim(PlayerMoveEvent e) throws UserDoesNotExistException, NoLoanPermittedException {
            Player target = e.getPlayer();
            UUID targetuuid = target.getUniqueId();
            target.sendMessage("Event fired");
            if (bounty.containsKey(targetuuid)) {

                Economy.add(target.getName(), bounty.get(targetuuid));
                Economy.substract(target.getName(), BigDecimal.valueOf(bounty.get(targetuuid)));
                Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty on " + target.getDisplayName() + " was claimed by " + target.getDisplayName() + "!");
                bountyplacer.remove(targetuuid);
                bounty.remove(targetuuid);
            } else {
                target.sendMessage("Doesn't contain");
            }
        }
     
  4. Off topic, but make sure to add a NoLoanPermitted exception, unless you are allowing loans.
     
  5. Instead of using #containsKey() try checking if #get(targetuuid) doesn't return null. The reason behind that is because containsKey() checks the hash code of objects and I believe there could be an issue with checking that.
     
  6. not sure about it how about error logs do you get?
    insert debug statements Sysout("saved");
    Code (Java):
    private Map<UUID, Double> bounty = new HashMap<UUID, Double>(); // or will this take a difference try there is nothing to lose if yo do
    sysout("hashmap created");
     

  7. I have been just sending the player messages for debug messages. The HashMap is being created because once placing the bounty I can remove the bounty successfully.

    Ok, testing now.
     
  8. also example to your code
    Code (Java):
    if (bountyplacer.containsKey(playeruuid) && bountyplacer.containsValue(targetuuid)) {
                            bounty.remove(targetuuid);
                            bountyplacer.remove(playeruuid);
                            Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty for " + target.getDisplayName() + " has been removed!");
                        } else {
                            sender.sendMessage("Bounty for that player doesn't exist!");
                        }
    after you checked if player contains no key you are just sending a message how about putting a values on it so after checking if they containskey in future they have a value.
     
  9. Not sure what you are saying here.

    I tried that method and it didn't work.
     
  10. I mean if the !hashmap#containsKey(uuid) {
    then you should atleast put a 0.0 as their double don't leave it null.
     
  11. bounty.get(targetuuid) returns null?
     
  12. probably it will scan the code again.
     
  13. Correct.
     
  14. so validate if it is null then put atleast 0.0 like what I said.
     
  15. I'm not understanding what you mean. In this line of code:

    Code (Text):
     if (bountyplacer.containsKey(playeruuid) && bountyplacer.containsValue(targetuuid)) {
                            bounty.remove(targetuuid);
                            bountyplacer.remove(playeruuid);
                            Bukkit.broadcastMessage(ChatColor.GOLD + "|" + ChatColor.RED + "BOUNTY" + ChatColor.GOLD + "|" + ChatColor.YELLOW + " Bounty for " + target.getDisplayName() + " has been removed!");
                        } else {
                            sender.sendMessage("Bounty for that player doesn't exist!");
                        }
    This is for if the command is /bounty remove [username].

    This is only ran under those circumstances. It checks if there is already a bounty for that player and removes it
     
  16. He's inserting a UUID and a double into the hashmap when the command is called. Read over the code more carefully.

    Inside of your event, could you iterate through the hashmap and verify that there are keys inside of it?
     
  17. Threw this inside of the event:
    Code (Text):
    Iterator it = bounty.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                it.remove();
            }
    Not printing anything though
     
  18. Okay, there must not be anything inside of the hashmap. Is that place command working right?
     
  19. It is running. It is broadcasting the appropriate message. Right now I am recoding the entire class. Give me a little and I'll post the new class
     
  20. return statement instead of else will give you neat code.