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

Respect @SubscribeEvent.receiveCanceled #33

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public void testCommonSubscribeEventErrors() {
doTest(new CommonSubscribeEventErrorsTest() {});
}

@Test
public void testCancelledEvent() {
doTest(new CancelledEventTest() {});
}

@Test
public void testAbstractEventClasses() {
doTest(new AbstractEventClassesTest() {});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.bus.test.general;

import net.neoforged.bus.api.BusBuilder;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.bus.test.ITestHandler;
import net.neoforged.bus.testjar.DummyEvent;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class CancelledEventTest implements ITestHandler {
@Override
public void test(Supplier<BusBuilder> builder) {
IEventBus bus = builder.get().build();

Listener listener = new Listener();
bus.register(listener);

DummyEvent.CancellableEvent event = new DummyEvent.CancellableEvent();
event.setCanceled(true);
bus.post(event);

assertTrue(listener.receivedCanceled, "Received the canceled event");
}

public static class Listener {

boolean receivedCanceled = false;

@SubscribeEvent(receiveCanceled = true)
public void onEvent(DummyEvent.CancellableEvent event) {
this.receivedCanceled = true;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void invoke(Event event)
if (handler != null)
{
// The cast is safe because the check is removed if the event is not cancellable
if (!((ICancellableEvent) event).isCanceled()) {
if (subInfo.receiveCanceled() || !((ICancellableEvent) event).isCanceled()) {
handler.invoke(event);
}
}
Expand Down
Loading