Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@SubscribeEvent.receiveCanceled is not respected #32

Closed
sciwhiz12 opened this issue Aug 6, 2024 · 1 comment · Fixed by #33
Closed

@SubscribeEvent.receiveCanceled is not respected #32

sciwhiz12 opened this issue Aug 6, 2024 · 1 comment · Fixed by #33
Labels
bug Something isn't working

Comments

@sciwhiz12
Copy link
Member

Event listeners ordinarily do not receive cancelled events. However, they can specifically opt-in to receiving cancelled events through either the receiveCanceled parameter of the IEventBus#addListener methods or the receiveCanceled field of the @SubscribeEvent annotation.

However, code investigation and testing (prompted by @PlatinPython, thanks btw) shows that the receiveCanceled field of the @SubscribeEvent annotation is not respected, thus causing cancelled events to never reach such an annotated listener. (The addListener methods are unaffected as far as I can tell.)

Sample output from the code below:

Sending non-canceled event
StaticListener: Received event; canceled: false
InstanceListener: Received event; canceled: false
Sending canceled event

Sample code:

import net.neoforged.bus.api.*;

class Scratch {
    public static void main(String[] args) {
        final IEventBus bus = BusBuilder.builder().build();
        final TestEvent event = new TestEvent();

        bus.register(StaticListener.class);
        final InstanceListener listener = new InstanceListener();
        bus.register(listener);

        System.out.println("Sending non-canceled event");
        bus.post(event);

        event.setCanceled(true);
        System.out.println("Sending canceled event");
        bus.post(event);
    }

    static class StaticListener {
        @SubscribeEvent(receiveCanceled = true)
        public static void listen(TestEvent event) {
            System.out.println("StaticListener: Received event; canceled: " + event.isCanceled());
        }
    }

    static class InstanceListener {
        @SubscribeEvent(receiveCanceled = true)
        public void listen(TestEvent event) {
            System.out.println("InstanceListener: Received event; canceled: " + event.isCanceled());
        }
    }

    public static class TestEvent extends Event implements ICancellableEvent {
    }
}
@sciwhiz12 sciwhiz12 added the bug Something isn't working label Aug 6, 2024
AterAnimAvis added a commit to AterAnimAvis/Bus that referenced this issue Aug 6, 2024
Re-adds the dropped bypass, that was accidentally dropped in neoforged#20

Fixes neoforged#32
@neoforged-releases
Copy link

🚀 This issue has been resolved in Bus version 8.0.2, as part of #33.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant