Code doesn't spawn lightning?

Discussion in 'Plugin Development' started by trg601, May 31, 2013.

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

    trg601

    I am trying to make a plugin where if you have an iron axe with my custom name
    and you rightclick, then lightning will spawn where you click...
    I have a command that spawns the iron axe with the name "§9Hammer" (that works)

    Here is the code I am using:
    Code:
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
        Player player = event.getPlayer();
        World world = player.getWorld();
            if(player.getInventory().getItemInHand().equals(Material.IRON_AXE)){
            if (player.getItemInHand().getItemMeta().getDisplayName().equals("§9Hammer")) {
                    Block targetBlock = player.getTargetBlock(null, 100);
                    Location location = targetBlock.getLocation();
                    world.strikeLightning(location);
            }
        }
        }
    I don't know why this does not work I was basing it off of a tutorial (the tutorial used a command though :/)

    If you can tell what is wrong please fix it :(
    Thanks for reading this thread :D

    EDIT: download the project folder at
    [Link removed]
     
  2. Offline

    InfamousSheep

    I'm not sure about the lightning part, but I think this should work:
    Code:
     @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    ItemStack item = player.getItemInHand();
    World world = player.getWorld();
    if (item == null) {
    return;
    } else if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    if (!(player.getInventory().getItemInHand().getType() == Material.IRON_AXE)) {
    return;
    } else if(player.getInventory().getItemInHand().equals(Material.IRON_AXE)){
    if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Hammer")) {
    Block targetBlock = player.getTargetBlock(null, 100);
    Location location = targetBlock.getLocation();
    world.strikeLightning(location);
    }
    }
    }
    } 
    It makes a null check, gets that the player right clicks and Instead of using §9 I changed it to ChatColor.BLUE.
    Hope this achieves what you wanted :)
     
  3. trg601
    So the problem is that you need to add some stuff to your Block targetBlock = player.getTargetBlock(null, 100); It should be:
    Code:
    Block targetBlock = player.getTargetBlock(null, 50);

    So the code would end up being:
    Code:
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
        Player player = event.getPlayer();
        World world = player.getWorld();
            if(player.getInventory().getItemInHand().equals(Material.IRON_AXE)){
            if (player.getItemInHand().getItemMeta().getDisplayName().equals("§9Hammer")) {
                    Block targetBlock = player.getTargetBlock(null, 100);
                    Location location = targetBlock.getLocation();
                    world.strikeLightning(location);
            }
        }
        }
     
  4. Offline

    trg601

    Thanks for responding :D

    I tried both and neither of them worked :/

    EDIT: I did a test where when you right-clicked it would say a message...
    It didn't say the message so I know that it isn't being activated :/
     
  5. trg601
    Oh! Then I know! You probably didn't register the event. In the onEnable add this:
    Code:
    getServer().getPluginManager().registerEvents(this, this); 
     
  6. Offline

    trg601

    Where do I put that? I already have it in onEnable
     
  7. Offline

    Skyshayde

    Is the event in the main class? That needs to change if it isn't. If the plugin is open source, it might help if we can see the entire thing.
     
  8. Offline

    trg601

    The plugin isn't open source but I can make it open source!
    Maybe there is a better way to make it open source, but I can give you a zip of the project folder?
    [Link removed]

    Oh and it is in the main file I think... (because the other half of the plugin works)
     
  9. Offline

    trg601

    I am not sure how long bumps can be... but
    bump, does anyone know how to fix this? I tried lots of different things and nothing works :(
     
  10. Offline

    trg601

    Really? 3 downloads and no one knows how to fix this :(
     
  11. Offline

    Zach_1919

    trg601 Did you add the @EventHandler annotation above the onPlayerUse method? That's pretty important...
     
  12. Offline

    WolfClash

    try this:
    Code:
    @EventHandler
          public void onPlayerInteract(PlayerInteractEvent event) {
          Player player = event.getPlayer();
          int blockId = player.getItemInHand().getType().getId();
          if (player.getItemInHand().getItemMeta().getDisplayName().equals("§9Hammer")) {
          if(blockId == 258){
          Block block = player.getTargetBlock(null, 50);
          Location location = block.getLocation();
          World world = player.getWorld();
            world.strikeLightning(location);
     
  13. Offline

    XvBaseballkidvX

    Try using this, I think you just needed to add the Player Action, hopefully this will work :D

    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event){
    player player = event.getPlayer();
    if(event.getAction() == Action.RIGHT_CLICK_AIR){
    if(player.getItemInHand().getType == Material.IRON_AXE{
    Block block = player.getTargetBlock(null, 50);
    Location location = block.getLocation();
    World world = player.getWorld();
    world.strikeLightning(location);
     
  14. Offline

    trg601

    I tried both, idk why they don't work i'm hoping somebody that saw the whole project file (download at first post)
    Figures out why it isn't being triggered...
    I used the same code for the item use as my other (working) plugin..
     
  15. Offline

    Minnymin3

    trg601
    You have to change player.getInventory().getItemInHand().equals(Material.IRON_AXE) to player.getInventory().getItemInHand().getType().equals(Material.IRON_AXE)
     
  16. Offline

    trg601

    Thank you so much :D
    It works awesomely now :D
     
Thread Status:
Not open for further replies.

Share This Page