Full module source: 1 implementation
Every implementation below is the complete module file, verbatim. Expand a client to read its code.
NicotinePlayersrc/client/java/nicotine/mod/mods/player/PortalScreen.javaJava · 25 lines · 498 B
| 1 | package nicotine.mod.mods.player;
|
| 2 |
|
| 3 | import nicotine.events.InPortalEvent;
|
| 4 | import nicotine.mod.Mod;
|
| 5 | import nicotine.mod.ModCategory;
|
| 6 | import nicotine.util.EventBus;
|
| 7 |
|
| 8 | public class PortalScreen extends Mod {
|
| 9 |
|
| 10 | public PortalScreen() {
|
| 11 | super(ModCategory.Player, "PortalScreen");
|
| 12 | }
|
| 13 |
|
| 14 | @Override
|
| 15 | protected void init() {
|
| 16 | EventBus.register(InPortalEvent.class, event -> {
|
| 17 | if (!this.enabled)
|
| 18 | return true;
|
| 19 |
|
| 20 | return false;
|
| 21 |
|
| 22 | });
|
| 23 | }
|
| 24 | }
|
| 25 |
|
Utilities (2)
EventBus.javaJava · 35 lines · 1.0 KB
| 1 | package nicotine.util;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.concurrent.ConcurrentHashMap;
|
| 7 |
|
| 8 | public class EventBus {
|
| 9 | private static final Map<Class<?>, List<EventListener<?>>> listeners = new ConcurrentHashMap<>();
|
| 10 |
|
| 11 | public static <T> void register(Class<T> eventType, EventListener<T> listener) {
|
| 12 | listeners.computeIfAbsent(eventType, k -> new ArrayList<>()).add(listener);
|
| 13 | }
|
| 14 |
|
| 15 | public static <T> boolean post(T event) {
|
| 16 | List<EventListener<?>> eventListeners = listeners.get(event.getClass());
|
| 17 | if (eventListeners != null) {
|
| 18 | for (EventListener<?> listener : eventListeners) {
|
| 19 | @SuppressWarnings("unchecked")
|
| 20 | EventListener<T> typedListener = (EventListener<T>) listener;
|
| 21 | if (!typedListener.onEvent(event)) {
|
| 22 | return false;
|
| 23 | }
|
| 24 | }
|
| 25 | }
|
| 26 | return true;
|
| 27 | }
|
| 28 |
|
| 29 | @FunctionalInterface
|
| 30 | public interface EventListener<T> {
|
| 31 | boolean onEvent(T event);
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 |
|
ModCategory.javaJava · 6 lines · 108 B
| 1 | package nicotine.mod;
|
| 2 |
|
| 3 | public enum ModCategory {
|
| 4 | Render, Player, Movement, Combat, Misc, HUD, General
|
| 5 | }
|
| 6 |
|