Solved My plugin breaks Cancellable class. What may be reasons?

Discussion in 'Spigot Plugin Development' started by Vert3x, Apr 28, 2017.

Thread Status:
Not open for further replies.
  1. Hello, I'm sure enough that Cancellable class got broke from a plugin I made. Obviously I assumed this by doing some tests around my plugin testing server and I think this plugin won't even be published at all. So, it is based on events listening and I don't know where, I look to have broken Cancellable class of bukkit; I'm saying this because any event is smoothly fired and anything I do around it is done with no problem, but once it has to be cancelled, both if the event belongs my plugin and if the event belongs whatever other plugin, it is not being cancelled and isCancelled method is returning the assigned value without having it to be actually used for the event cancelling. I'm not here, obviously, to paste all of the code I've made cause it would be pretty senseless, I'm here instead to ask you if you know, in any way, what may be the cause(s) of this; here is something "suspect" I may have done to cause this issue:
    - Using more than one @EventHandler and relative method per class. Actually I already did this before I
    encountered this issue, but as I don't know anything about it, I'm just asking it.
    - Using nms code reflections based methods through the event listeners. This one too was something I already
    did before this problem, but still you never know.
    - Having created many actual listeners. Perhaps I exceeded a limit of listeners which broke something, this the least likely but at the same time the most sensed.
    Tell me whatever you think about this.
  2. What is the event that is not firing? Code pls

    Sent from my iPhone using Tapatalk
  3. Did you modify the class?
  4. Have I ever said an event isn't being fired? Actually I stated that it IS smoothly firing but the problem is only around cancelling it. So far I've seen it only onto PlayerInteractEvent and I think/hope it's the only event where there's this issue. There is nothing particular in my code to show, here is a class which MAY have caused this though:
    Code (Java):

    public class ThrowableTntHandler implements Listener {

        public void onPlayerInteract(PlayerInteractEvent event) {
            if (event.getHand() == EquipmentSlot.HAND || event.getHand() == EquipmentSlot.OFF_HAND) {
                ItemStack item = event.getItem() == null ? event.getHand() == EquipmentSlot.HAND ? event.getPlayer().getInventory().getItemInMainHand() : event.getPlayer().getInventory().getItemInOffHand() : event.getItem();
                if (SpecialItem.compareItemsIgnoreAmount(item, SpecialItem.THROWABLE_TNT.getItem(), false)) {
                    if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        TNTPrimed tnt = (TNTPrimed) event.getPlayer().getWorld().spawnEntity(event.getPlayer().getLocation().add(0, 1, 0), EntityType.PRIMED_TNT);
                        if (event.getPlayer().getGameMode() != GameMode.CREATIVE) {
                            item.setAmount(item.getAmount() - 1);
                        event.getPlayer().getWorld().playSound(event.getPlayer().getLocation(), Sound.ENTITY_WITHER_SHOOT, SoundCategory.PLAYERS, 16.0F, 1.85F);
                        Main.getInstance().swingArm(event.getPlayer(), event.getHand() == EquipmentSlot.OFF_HAND);

    Tell me if you see something suspect. Methods and things you may not know are something really simple and with nothing really strange within them. Anyways I noticed WorldEdit doesn't cancel the event but still fires it either, and I'm 100% this is not a coincidence.
    Do you mean Reflections? Anyways no, I just created really easy things which may be useful or not, but there is nothing particularly weird.

  5. Sent from my iPhone using Tapatalk
    • Like Like x 1
  6. I did never return after cancelling events and nothing has ever happened. About debugging, I just tried at the end too; the value was true (so the event was successfully cancelled and the setCancelled method was working fine) but still the block was being placed.
  7. Something has to be un-cancelling the event after your plugin cancels it then, lower the priority on that event and debug again, continue lowering it and I bet it'll start working

    Sent from my iPhone using Tapatalk
    • Like Like x 1
  8. I was leaving it with no priority (so, as default, it was normal) I tried both low and then lowest but none of them did actually change anything: the debugging still says the event is cancelled but it actually isn't.
    I'm afraid I will have to cancel the event "manually" by replacing the block with air and giving back the used item to the player...
    #8 Vert3x, Apr 29, 2017
    Last edited: Apr 29, 2017
  9. Set it to monitor, which is the lowest and report back.

    In this event is it falsely cancelling the event? The way your code looks like it is written the only thing that should cancel in that event is if it is special TNT. If it is happening on other blocks there should be some console errors if the server event cancel code is firing.

    Also set ignoreCancelled = false

    And set a debug line as the very first line to see if it is cancelled elsewhere before this event, then at the bottom before the method ends check it again. That way you know that this event is the one in control.
    Sent from my iPhone using Tapatalk
    #9 dNiym, Apr 29, 2017
    Last edited: Apr 29, 2017
    • Useful Useful x 1
  10. The order I found into the code is this:
    Code (Java):

    . It sounds pretty strange to me that monitor is the lowest value; still I just tired and your theory was correct.
    It was, and it is still for many other events not setting the priority to monitor, whatever it actually is (e.g. WorldEdit left click listening when getting the first position).
    Yes, but I'm not talking only about my method.
    Huh? What variable is that?
    Edit: I just assumed that is an optional variable for @EventHandler. Well, its default value is actually false so that wouldn't make any sense (even the ide I'm using is warning me about it).
    I did that too. Now indeed it's correctly returning what it is supposed to return, but still the problem was not my specific method, but every other method ever being ran in my server being falsely cancelled if the priority isn't monitor. Do you know any cause this may have?
  11. If you have multiple listeners or multiple methods for the same event and one of them cancels they are ALL cancelled. So it could be any one of those or any other plugin really.

    You should not leave the priority to monitor fwiw because if all plugins are set to monitor then priority becomes useless.

    Sent from my iPhone using Tapatalk
    • Friendly Friendly x 1
  12. Yes, but the listener date of creation was matching with that I begun seeing some issues. I'm 100% sure it's my plugin because I tried using only that and the issue was still there when by removing it any other listener was working fine. Well, I guess this is actually the only likely thing which may have happened. Perhaps it's an update I've made around older event handlers.
    I use to set all the events to normal (leave them to default, basically).
    Before post edit: Indeed I just found out I was passing a boolean variable directly to the setCancelled method in one of my listeners, this made it so that the event was forced not to be cancelled when the boolean was false. Before closing this thread, I want to thank @dNiym for his help!
Thread Status:
Not open for further replies.