Solved Need help finding error in my Method

Discussion in 'Plugin Development' started by Alias_Me, Jan 18, 2019.

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

    Alias_Me

    So I have written a class for checking when a sign is placed if it is placed on top of a specific structure (sign on top of a cobblestone wall on top of an emerald block).

    Worked perfectly until I moved that check into its own method:

    Code:
    public boolean isGuidepost(Location location, Player user) {
        user.sendMessage("Method Start");World world = (World) location.getWorld();
        if(world.getBlockAt(location.add(0, -1, 0)).getType() == COBBLESTONE_WALL) {
           user.sendMessage("CobbleWall");
             if(world.getBlockAt(location.add(0, -2, 0)).getType() == EMERALD_BLOCK) {
              user.sendMessage("Emerald, true");
             return true;
          } else {
              return false;
          }
      } else {
          return false;
      }
    }
    
    For some reason it stops after the "CobbleWall" send, so for some reason the second if statement seems to return false, even though an Emerald Block is placed below the Wallpiece.

    Heres the full class in a pastebbin because I cant be bothered to format all that in this text editor:
    https://pastebin.com/qtznyPYv
     
  2. Offline

    Zombie_Striker

    @Alias_Me
    The method ".add" for locations does not create a new Location instance, and instead modifies it. What is happening is that the first if statements takes the current location, subtracts 1 from it and then for the second if statement, subtracts another 2 from it, meaning it is checking 3 blocks below.

    An easy way around this would be to add .clone() before the .add method to make sure you are not modifying the original location.
     
  3. Offline

    Alias_Me

Thread Status:
Not open for further replies.

Share This Page