Solved Target block

Discussion in 'Spigot Plugin Development' started by ImperialSwag, May 24, 2016.

  1. Well, I am probably blind or really tired, but I can't figure out why the plugin can't find sign block the player is looking at :S

    Code (Text):

                HashSet<Material> hs = new HashSet<Material>();
                Block block = p.getTargetBlock(hs, 10);
                if(block.getType() != Material.SIGN_POST || block.getType() != Material.WALL_SIGN){
                    p.sendMessage(ChatColor.DARK_GRAY+"["+ChatColor.DARK_GREEN+"Economy"+ChatColor.DARK_GRAY+"]"+ChatColor.RED + "Target block must be a sign!");
                    return false;
                }
    This is the piece of code that I am trying to use. It fails that if check. Isn't 10 the block range or should I add like the sign materials to the HashSet? I am so confused right now...
     
  2. For the getTargetBlock, the first parameter is a set of materials that you want to be "transparent"; if you set that parameter to null, then it will only assume you want air to be transparent and will get the very first block the player sees, the second parameter is the maximum range you want the code to look.

    Perhaps try adding some messages in your code that just sends a message to the player to debug some things like, numbers to see what is/is not being fired in your code, block type that the player is looking at, and the HashSet you created to see if there are mismatches or something you aren't accounting for.
     
    • Agree Agree x 1
  3. try doing

    HashSet<Material> hs = null;
    Block block = p.getTargetBlock(hs, 10);

    It's kinda a goofy method if you ask me
     
    • Like Like x 1
  4. Oh lol, thanks a lot @Hunky524 :p, I realized I have to put Material.AIR to the hash set, the condition check was wrong too :p It can never be 2 different materials! I am just tired :( For anyone else checking this for later, here is the changed version:

    Code (Text):
    HashSet<Material> hs = new HashSet<Material>();
                hs.add(Material.AIR);
                Block block = p.getTargetBlock(hs, 10);
                if(!block.getType().equals(Material.SIGN_POST) && !block.getType().equals(Material.WALL_SIGN)){
                    p.sendMessage(ChatColor.DARK_GRAY+"["+ChatColor.DARK_GREEN+"Economy"+ChatColor.DARK_GRAY+"]"+ChatColor.RED + "Target block must be a sign!");
                    return false;
                }