Solved Right Clicking Event Help

Discussion in 'Plugin Development' started by johnny_boy, Apr 9, 2017.

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

    johnny_boy

    I am wandering why this code I made doesn't work. It should (when someone right click with a stick) fire an arrow. I am using 1.10.2 (1.11.2 was just too weird and it kind of broke everything in eclipse)
    Code:Java
    1.  
    2. package me.p250.firearrow;
    3.  
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.entity.Arrow;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.block.BlockPlaceEvent;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14.  
    15. public class ChopTree implements Listener {
    16.  
    17. @EventHandler(priority=EventPriority.HIGH)
    18. public void onBreakEvent(PlayerInteractEvent e) {
    19. Player player = e.getPlayer();
    20.  
    21. Action act = e.getAction();
    22.  
    23. if ((act == Action.RIGHT_CLICK_AIR || act == Action.RIGHT_CLICK_BLOCK) && player.getInventory().getItemInMainHand().equals(Material.STICK)) {
    24. player.launchProjectile(Arrow.class);
    25. }
    26. }
    27.  
    28. }
     
  2. Offline

    timtower Moderator Moderator

    @MemeplexOwner Please post your onEnable and all methods that it calls
     
  3. Offline

    johnny_boy

    Code:Java
    1.  
    2. package me.p250.firearrow;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class MainClass extends JavaPlugin {
    8.  
    9.  
    10. public void onEnable() {
    11. Bukkit.getPluginManager().registerEvents(new ChopTree(), this);
    12.  
    13. }
    14.  
    15.  
    16.  
    17. }
     
  4. Offline

    timtower Moderator Moderator

  5. Offline

    johnny_boy

    Yes, and there are no errors in the console..

    And I can't use getItemInHand() since its 1.10.2
     
  6. Offline

    martian3333

    @MemeplexOwner
    You could try broadcasting the results of each of your boolean expressions to see if specific ones are not returning the results that you think they should for some reason.

    Edit: Or just broadcast a message from inside your if statement to see if the logic is firing the way it should.
     
    Last edited: Apr 9, 2017
  7. Offline

    johnny_boy

    I will try that.

    Still doesn't work even when I just put this:
    Code:Java
    1.  
    2. if (act == Action.RIGHT_CLICK_AIR) {
    3. player.sendMessage("works1");
    4. }
    5.  


    So what is actually going on.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 9, 2017
  8. Offline

    martian3333

    @MemeplexOwner
    It looks like you might be able to try broadcasting act.toString() before the if statement but I've never tried it so I don't know what the result will be, or if it will work at all. If it works the way I think it should it will tell you what action is happening.
     
  9. Offline

    timtower Moderator Moderator

  10. Offline

    Zombie_Striker

    Do you mind explaining what broke? Either you did not configure the buildpath correctly, or there is something wrong with the 1.11 jar.

    Certain items do not trigger on right clicks. If you try it with other items like quartz and blaze rods, you will see those don't work either. What will work are tools, maps and dyes.

    This is happening because you are not meant to do anything when you right click a stick, so minecraft does not send the packet when that happens. However, right clicking a map or a dye triggers the event because something is supposed to happen when you click it.

    Try using another item.

    BTW: The item in the hand can be null, so null check it first before getting the type. Also, use == for comparing enums.
     
  11. Offline

    johnny_boy

    Right, here is what I have for ya and I think i know why its not working but I don't know how to solve it..

    When I right click with a stick works1 prints but not works. It also shows RIGHT_CLICK_AIR. When i right click with nothing, it shows nothing. I think I know what the problem is.
    Code:Java
    1. player.getInventory().getItemInMainHand().equals(Material.STICK)

    It is not detecting this bit. How do I fix?? @timtower @martian3333
     
  12. Offline

    timtower Moderator Moderator

    @MemeplexOwner Enums are compared with ==
    And get the type of the item in hand first, don't compare the itemstack with the material.
     
  13. Offline

    johnny_boy

    I did see that error yes, so I do getType() == blah?

    EDIT: It's not getType..
     
  14. Offline

    timtower Moderator Moderator

  15. Offline

    johnny_boy

    Code:Java
    1.  
    2. ItemStack stick = new ItemStack(Material.STICK);
    3.  
    4. if ((act == Action.RIGHT_CLICK_AIR || act == Action.RIGHT_CLICK_BLOCK) && player.getInventory().getItemInMainHand() == stick) {
    5. player.launchProjectile(Arrow.class);
    6. player.sendMessage("works");
    7. }
    8.  

    I also tried Material.STICK and getType() but none worked.

    EDIT: player.getInventory().getItemInMainHand().getType() == Material.STICK

    IT WORKS NOW. HOLY. Thanks <3
     
  16. Offline

    Zombie_Striker

    @MemeplexOwner
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page