Solved Trying to test the location of the block clicked and match it to one in a hashmap

Discussion in 'Spigot Plugin Development' started by PurpleWumbat, May 31, 2017.

  1. Hey guys! I'm fairly new to coding and I'm making a lot of mistakes but I'm making progress! I usually try to find the answer to my questions from various parts of the internet so I don't have to bother anyone, sadly, I don't even know the question to ask here. I honestly can't find the problem. The second if statement, the one I try to match the locations seems to evaluate to false even when the block locations should match. Here's the if statement code:
    Code (Text):
    public void HorsesStart(PlayerInteractEvent event)
            Player p = event.getPlayer();
            Location b = event.getClickedBlock().getLocation();
            Player player = event.getPlayer();
            if(event.getClickedBlock().getType() == Material.DIAMOND_BLOCK)
            player.sendMessage("Logic gate 1 worked");
    // This message sends
            if (b == WhereStack.get(p))
                player.sendMessage(org.bukkit.ChatColor.DARK_RED + "Success");
    \* I know this code for spawning an entity probably doesn't work but that's not the problem as the above message doesn't send */
                    World w = player.getWorld();
                    CraftHorse mob = (CraftHorse)player.getWorld().spawnEntity(b, EntityType.HORSE);
                    Location Center = new Location(w,10,100,10);
                    EntityCreature e = ((CraftCreature)mob).getHandle();
                    NavigationAbstract nav = e.getNavigation();
    Then The code under the BlockPlaceEvent that saves the location,
    Code (Text):
    Player player = event.getPlayer();
        if(event.getBlock().getType() == Material.DIAMOND_BLOCK )
          World w = player.getWorld();
          Location loc = event.getBlock().getLocation();
          WhereStack.put(player, loc);
    The event here continues and works fine.
  2. you could try checking if the X Y and Z coords match, or I heard somewhere (This might be false) that you check locations with .equals, which sounds correct. That might be your best bet. To make sure they do equal each other, try printing X Y and Z for each location in console and make sure they are the same.
  3. I got it work by just checking to see if X, Y and Z match instead of using location.
  4. When dealing with class instances (Location), == compares the memory location, equals on the other hand compares the "contents" of the class instance.
    This is why you use equals instead of == when dealing with strings.
    Primitive types (int, long, double) and enums work with ==.
    Another post about this, check Choco's and my reply: