error in java project

Discussion in 'Programming' started by Timislol12, May 5, 2017.

  1. i tried to create a plugin where u get lightning if u click with a fishing_rod

    here is a picture http://i.imgur.com/vT6RdZi.png

    i hope someone can help me, im new at java[​IMG]
     
  2. getTypeId is deprecated, you should use getType() and compare it to the Material.FISHING_ROD.

    The error you get on getTargetBlock(null,200) is because there are to two possible methods to choose there and it doesn't know which one to pick. You can fix this by getTargetBlock((Set<Material>) null, 200), now it will know what method to choose.

    You can find all this information on the spigot javadocs.
     
    #2 JasperJH, May 5, 2017
    Last edited: May 5, 2017
  3. Huh !?? i dont understand it
     
  4. He means that there are two types for that method. One takes HashSet<Material> while the other Set<Material> and since it is null it cannot tell the difference of which one to use.

    Here is how you could implement it

    Code (Text):
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent evt){
        Player p = evt.getPlayer();
        if(p.getItemInHand().getType() == Material.FISHING_ROD) {
            Set<Material> materials = null;
            p.getWorld().strikeLightningEffect(p.getTargetBlock(materials, 200).getLocation());
        }
    }
    also you may want to cancel the event after you strike the lightning so that you do not continue to break or hit blocks.
     
  5. tnx!!! there are no errors anymore but the lightning does not work :p
     
  6. You forgot to register the class and you will also have to implement Listener
     
  7. Choco

    Moderator

    Casting is also another option that people tend to use more frequently.
    Code (Java):
    p.getTargetBlock((Set<Material>) null, 200);
    This removes the need for creating an extra line. Whatever you prefer. ¯\_(ツ)_/¯
     
    • Like Like x 1
  8. The difference is
    getTargetBlock uses a HashSet full of byte values.
    getTargetBlock a Set full of Materials.

    pass a new HashSet<Material>() and you're done.