Solved Cancelling the Throwing of EnderPearls

Discussion in 'Plugin Development' started by m16, Mar 21, 2019.

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

    m16

    Hi there,

    I'm working on a plugin currently, and I need to make it so players cannot throw Ender Pearls.

    I tried using two events, the PlayerInteractEvent, and the ProjectileLaunchEvent

    For the PlayerInteractEvent, I cannot even get it to get past this code:
    Code:
    if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
            {
                Player p = event.getPlayer();
                p.sendMessage(ChatColor.GREEN + "Test");
             }
    That if statement is the first bit of my code (this isn't all of it), but I have no idea why it's not even hitting this statement when I throw the enderPearl.

    I try using the ProjectileLaunchEvent and that kind of works better, but for some reason it will delete the player's enderPearl from their inventory. I even try to add another EnderPearl to their inventory by just setting the item in their hand from the event and update their inventory, but that doesn't work either for some reason.

    Would love to know though why it never works for the PlayerInteractEvent though

    Thanks for the help! :)
     
  2. Offline

    KarimAKL

    @m16
    1. Pretty sure enums are compared with '==', not '.equals()'.
    2. Just making sure, did you remember to register the event?

    EDIT: The ProjectileLaunchEvent is probably fired after the item is used, i'd guess that's the reason why you lose the enderpearl with that event.
     
    Last edited by a moderator: Mar 21, 2019
  3. public void OnPlayerTeleport(PlayerTeleportEvent event)
    {
    // If the event is called by an ender pearl
    if(event.getCause() == TeleportCause.ENDER_PEARL)
    {
    // We cancel it
    event.setCancelled(true);
    }
    }
     
  4. Offline

    KarimAKL

    @Elytes That wouldn't cancel the use of enderpearls, just the teleport.
     
  5. Offline

    Chr0mosom3

    Ok @Elytes, let me tell you some potentional mistakes.

    1. @KarimAKL is right, the enderpearl will still land, what you should do is listen to the projectile throw event (don't remember the name) and then cancel it
    2. Did you put implement listener? public class MyPlugin implements Listener {
    3. Did you register the event in your main method?

    Code:
    this.getServer().getPluginManager().registerEvents(this, ListenerClass);
    Replace ListenerClass with the name of the class where you implemented Listener, if it's in the same class then just type in this.
     
  6. Offline

    m16

    @KarimAKL

    Thanks for reminding me about the ==!

    Yes, I registered the event and implemented listener.

    I looked through my code more and I realized that I also had a (ignoreCancelled = true) in the event Handler, so I got rid of that. I'm not 100% sure how much that specific part of it prevented me from getting the functionality I wanted,
    but now everything works like a charm.

    Thanks guys!
     
    KarimAKL likes this.
  7. @MrDaniel

    ?
    I'm not the author of this post, was talking about a potential idea for his problem that could be better for him, that was only a suggestion, i didn't made mistakes, and i wont put all the code to register the event, i think that he can do it alone ?

    @m16 Did you check if the event is cancelled ? Cause if my memory is good, RIGHT_CLICK_AIR is considered as an cancelled event

    Edit : Just saw that you fixed your problem, great ;)
     
  8. Offline

    Chr0mosom3

    @Elytes, sorry xD, this is the second time this happened.


    @KarimAKL, I have had many times with .equals() and == and it doesnt matter


    @m16, mark thread as solved?
     
  9. Offline

    KarimAKL

Thread Status:
Not open for further replies.

Share This Page