The method getTargetBlock(HashSet<Byte>, int) is ambiguous for the type Player

Discussion in 'Spigot Plugin Development' started by robotnik, May 10, 2016.

  1. Hi, Im trying to get the block a player is looking at for my plugin, and i've googled and tried a few methods but still cant get it to work.

    attempt 1

    Code (Text):
                Player player = (Player) sender;
                Block lookingatblock = player.getTargetBlock((Set<Material>) null, 15);
    I tried this, but eclipse gives me the error

    Code (Text):
    The method getTargetBlock(HashSet<Byte>, int) in the type LivingEntity is not applicable for the arguments (Set<Material>, int)

    So then I tried this

    attempt 2

    Code (Text):
                HashSet<Material> transparent = new HashSet<Material>();
                transparent.add(Material.AIR);
               
                Player player = (Player) sender;
                Block lookingatblock = player.getTargetBlock(transparent, 15);
    and eclipse gives me this error

    Code (Text):
    The method getTargetBlock(HashSet<Byte>, int) in the type LivingEntity is not applicable for the arguments (HashSet<Material>, int)

    So im not sure what to do now
     
  2. Take your second attempt and just use Set<Material> transparent = new HashSet<Material>(); instead
     
  3. You need a hashset with bytes instead.
    Code (Text):
    HashSet<Byte> transparent = new HashSet<Byte>();
    transparent.add(Material.AIR.getId());
    EDIT
    Your first attempt was better, use that one instead. But same there, you need a Byte, not a Material.
     
  4. That is not true.
     
  5. The method using HashSet<Byte> is deprecated, you should not use it.
     
  6. It's deprecated because item IDs can change at all time. However it's impossible for an empty byte hashset to change the values :p If you can get the one with the material working, sure. If not, it's not a problem to use the byte instead.
     

  7. I tried using your code, but eclipse now gives me this error

    Code (Text):
    The method getId() is undefined for the type Material

    Tried and it still gives me the same error

    Code (Text):
    The method getTargetBlock(HashSet<Byte>, int) in the type LivingEntity is not applicable for the arguments (Set<Material>, int)
     
  8. Just use getTargetBlock(((HashSet<Byte>) null), 30);
     
    • Like Like x 1
    • Winner Winner x 1
  9. I'm sorry about the ambiguity. I'll get around to making another PR to remove the deprecated Byte methods soon. Only reason I left it in before was so it didnt break all plugins that used the deprecated Byte methods. Its been over a year now, so should be safe for me to remove them ^_^

    Just be sure you're using the Material methods so your plugin doesnt break when I make this change.
     
    • Like Like x 1
    • Agree Agree x 1
  10. The following works just fine:
    Code (Java):
    Player player = (Player)sender;
    Block targetBlock = player.getTargetBlock((HashSet<Material>)null, 15);
    Make sure you've imported HashSet and Material.
     
    • Agree Agree x 1
  11. Try changing the version of your API.
     
  12. Try using this:
    Code (Text):

    player.getTargetBlock((HashSet<Byte>)null, {your max distance}).getLocation();