Can someone tell me why this does not work.

Discussion in 'Plugin Development' started by MrDplugins, Sep 3, 2015.

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

    MrDplugins

    Hi, I'm trying to send a player a message when he steps on a pressure plate in a location. But it won't send the message if I remove the Location Check it works fine. Is it something wrong with my location?

    Code:
          
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            World w = Bukkit.getServer().getWorld(getConfig().getString("start.world"));
            double x = getConfig().getDouble("start.x");
            double y = getConfig().getDouble("start.y");
            double z = getConfig().getDouble("start.z");
    
            Location start =  new Location(w, x, y, z);
    
    
    final Player p = e.getPlayer();
            if (e.getTo().getBlock().getRelative(BlockFace.SELF).getType() == Material.GOLD_PLATE) {
             
                if (p.getLocation() == start) {
                p.sendMessage("You started the parkour!");
                }
            }
     
  2. Offline

    teej107

    @MrDplugins Compare Objects using the .equals() method.
     
  3. Offline

    MrDplugins

    @teej107 nothing happens, same as before. No stacktrace and no errors in eclipse.


    Code:
            if (e.getTo().getBlock().getRelative(BlockFace.SELF).getType().equals( Material.GOLD_PLATE)) {
                if (p.getLocation().equals(start)) {
    
     
    Last edited: Sep 3, 2015
  4. Offline

    mythbusterma

    @MrDplugins

    In addition, you may have not registered your listener.
     
  5. Offline

    Oxyorum

    @MrDplugins Did you do the same thing with:
    Code:
    p.getLocation() == start
    You cannot compare Location objects with ==, you need to use .equals(), as said by
    @teej107.
     
  6. Offline

    MrDplugins

  7. Offline

    DuaneTheDev_

    @MrDplugins

    You're probably saving using a double, returning a decimal. Meaning the player has to match the exact location as the coordinates inside the config.

    Save the coordinates using .getBlockX(),.getBlockY(),.getBlockZ() which will return the value as an integer. Hopefully this will fix the problem.
     
    Last edited: Sep 3, 2015
  8. Offline

    stormneo7

    The player would have to be EXACTLY at that location for this to trigger (if .equals() works correctly).

    If that is the case, you should be comparing the Block's Location instead of the Player's Location as the Block Location will always remain static.

    I suggest that you try out .distance(Location) or .distanceSquared(Location) as a more viable comparison.
     
  9. Offline

    MrDplugins

    Nope still the same problem!

    @stormneo7 I will try this.
     
  10. Offline

    caderape

    @MrDplugins
    Use PlayerInteractEvent, Action physical. Get the clicked block and compare with your location
     
    MrDplugins likes this.
  11. Offline

    MrDplugins

Thread Status:
Not open for further replies.

Share This Page