Solved HashMap ContainsKey

Discussion in 'Plugin Development' started by Fuzzwolf, Jan 28, 2013.

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

    Fuzzwolf

    I'm currently trying to determine whether or not a block which is broken by a player is within a HashMap containing both a Location and an Integer. Of course, the Location of the block is used. I've printed both HashCodes of the respected Locations being compared, and they were identical. It would appear I am missing something, so any and all help is greatly appreciated. There are no compiler errors, the if(blockRespawn.containsKey(loc)){ just never seems to pass.

    The onCommand method places the block directly in front of the player which is not air in to the first Map "BlockRespawn" as a Location with the first argument parsed to an integer as the value for the key (and appears to work in game due to some current debug functionality). The specified location is later checked to see if that matches up with the block a player breaks.

    Code:java
    1. public Map<Location,Integer> blockRespawn = new HashMap<Location,Integer>();
    2. public Map<Location,Integer> blockQueue = new HashMap<Location,Integer>();
    3.  
    4. public void onEnable(){
    5. getConfig().options().copyDefaults(true);
    6. saveConfig();
    7. getServer().getPluginManager().registerEvents(new Resources(), this);
    8. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    9.  
    10. public void run(){
    11. //to be implemented
    12. }
    13.  
    14.  
    15. }
    16.  
    17. }, getConfig().getInt("delay"),getConfig().getInt("interval"));
    18.  
    19. }
    20.  
    21. public void onDisable(){
    22.  
    23.  
    24. }
    25.  
    26. @Override
    27. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    28. if(cmd.getName().equalsIgnoreCase("resourceset") && sender.hasPermission("antiochresources.setresource") && args.length > 0 && sender instanceof Player){
    29. Player pl = (Player) sender;
    30.  
    31. Block target = pl.getTargetBlock(null, 1);
    32. int health;
    33.  
    34. if(target.getType().equals(Material.AIR)){
    35. sender.sendMessage(ChatColor.RED + "Block is invalid, try again.");
    36. return false;
    37. }
    38.  
    39. Location targetLoc = target.getLocation();
    40.  
    41.  
    42.  
    43. try{
    44. health = Integer.parseInt(args[0]);
    45. }catch (NumberFormatException nx) {
    46. return false;
    47. }
    48.  
    49. blockRespawn.put(targetLoc, health);
    50. System.out.println(targetLoc.hashCode());
    51. sender.sendMessage(ChatColor.GREEN + targetLoc.toString() + " added to the blockRespawn listing.");
    52. sender.sendMessage(ChatColor.YELLOW + target.getType().toString());
    53.  
    54. return true;
    55. }
    56.  
    57.  
    58. return false;
    59. }
    60.  
    61. @EventHandler
    62. public void onBlockBreak(BlockBreakEvent b){
    63. b.setCancelled(true);
    64. Block bl = b.getBlock();
    65. Location loc = bl.getLocation();
    66.  
    67. if(blockRespawn.containsKey(loc)){
    68. bl.setType(Material.BEDROCK);
    69. blockQueue.put(loc,blockRespawn.get(loc));
    70. b.getPlayer().sendMessage(ChatColor.AQUA + loc.toString() + " added to blockQueue.");
    71. }
    72.  
    73. bl.breakNaturally();
    74.  
    75. }
    76.  
    77. }
    78.  
     
  2. Offline

    Hoot215

    You should be using strings instead of the locations themselves as keys. The containsKey method first checks if the objects' hash codes are equal, and if that returns true, then checks if the objects themselves are equal (using their equals method, which would return false, unless you were using the exact same location object as before). You might want to use Location's toString method as the key.
     
  3. Offline

    evilmidget38

    That's not true. Location has an implementation of both hashcode and equals.
     
  4. Offline

    Hoot215

    Thanks for pointing that out to me. I hadn't noticed. However, I would still recommend using strings as keys anyway, seeing as comparing worlds may not be reliable. I would try and see if using strings as keys fixes anything.

    Edit: Ignore that link. I just noticed it's extremely outdated information. Regardless, I would still try using strings.
     
  5. Offline

    evilmidget38

  6. Offline

    Fuzzwolf

    evilmidget38 Hoot215 Thanks for the help both of you, I actually figured out the solution but was not able to implement it until I got home. I needed to restrain the Maps to a static context so that it would belong to the class instead of an instance. It's been a while, heh...
     
Thread Status:
Not open for further replies.

Share This Page