From f1da9d70728ad50cbff5c345052f8b18fabb06f2 Mon Sep 17 00:00:00 2001 From: ImDaBigBoss <67973871+imdabigboss@users.noreply.github.com> Date: Sun, 4 Sep 2022 16:11:08 -0500 Subject: [PATCH] Allow events to be registered by any class Supersedes & closes #3073 Co-authored-by: Redned --- .../main/java/org/geysermc/api/Geyser.java | 1 + .../geysermc/api/util/BedrockPlatform.java | 3 + .../java/org/geysermc/api/util/InputMode.java | 3 + .../java/org/geysermc/api/util/UiProfile.java | 3 + .../org/geysermc/geyser/api/GeyserApi.java | 3 +- .../geysermc/geyser/api/event/EventBus.java | 4 +- .../geyser/api/event/EventRegistrar.java | 45 ++++++++ .../geyser/api/event/EventSubscriber.java | 2 +- .../geyser/api/event/ExtensionEventBus.java | 5 +- .../GeyserDefineCustomItemsEvent.java | 2 + .../lifecycle/GeyserPostInitializeEvent.java | 3 +- .../lifecycle/GeyserPreInitializeEvent.java | 3 +- .../event/lifecycle/GeyserShutdownEvent.java | 3 +- .../geyser/api/extension/Extension.java | 15 ++- .../bungeecord/GeyserBungeePlugin.java | 88 ++++++++++---- .../platform/spigot/GeyserSpigotPlugin.java | 108 ++++++++++++------ .../platform/sponge/GeyserSpongePlugin.java | 17 ++- .../standalone/GeyserStandaloneBootstrap.java | 4 +- .../velocity/GeyserVelocityPlugin.java | 36 +++--- .../java/org/geysermc/geyser/GeyserImpl.java | 46 +++++--- .../geysermc/geyser/event/GeyserEventBus.java | 15 +-- .../geyser/event/GeyserEventRegistrar.java | 38 ++++++ .../geyser/event/GeyserEventSubscriber.java | 7 +- .../event/GeyserExtensionEventBus.java | 11 +- .../loader/ProviderRegistryLoader.java | 3 + 25 files changed, 353 insertions(+), 115 deletions(-) create mode 100644 api/geyser/src/main/java/org/geysermc/geyser/api/event/EventRegistrar.java create mode 100644 core/src/main/java/org/geysermc/geyser/event/GeyserEventRegistrar.java diff --git a/api/base/src/main/java/org/geysermc/api/Geyser.java b/api/base/src/main/java/org/geysermc/api/Geyser.java index d0bdf3b5e..7543d1661 100644 --- a/api/base/src/main/java/org/geysermc/api/Geyser.java +++ b/api/base/src/main/java/org/geysermc/api/Geyser.java @@ -39,6 +39,7 @@ public class Geyser { * * @return the base api */ + @NonNull public static GeyserApiBase api() { if (api == null) { throw new RuntimeException("Api has not been registered yet!"); diff --git a/api/base/src/main/java/org/geysermc/api/util/BedrockPlatform.java b/api/base/src/main/java/org/geysermc/api/util/BedrockPlatform.java index d66077a87..e486f73bc 100644 --- a/api/base/src/main/java/org/geysermc/api/util/BedrockPlatform.java +++ b/api/base/src/main/java/org/geysermc/api/util/BedrockPlatform.java @@ -25,6 +25,8 @@ package org.geysermc.api.util; +import org.checkerframework.checker.nullness.qual.NonNull; + public enum BedrockPlatform { UNKNOWN("Unknown"), GOOGLE("Android"), @@ -56,6 +58,7 @@ public enum BedrockPlatform { * @param id the BedrockPlatform identifier * @return The BedrockPlatform or {@link #UNKNOWN} if the platform wasn't found */ + @NonNull public static BedrockPlatform fromId(int id) { return id < VALUES.length ? VALUES[id] : VALUES[0]; } diff --git a/api/base/src/main/java/org/geysermc/api/util/InputMode.java b/api/base/src/main/java/org/geysermc/api/util/InputMode.java index eadb457ab..70346ffa5 100644 --- a/api/base/src/main/java/org/geysermc/api/util/InputMode.java +++ b/api/base/src/main/java/org/geysermc/api/util/InputMode.java @@ -25,6 +25,8 @@ package org.geysermc.api.util; +import org.checkerframework.checker.nullness.qual.NonNull; + public enum InputMode { UNKNOWN, KEYBOARD_MOUSE, @@ -40,6 +42,7 @@ public enum InputMode { * @param id the InputMode identifier * @return The InputMode or {@link #UNKNOWN} if the mode wasn't found */ + @NonNull public static InputMode fromId(int id) { return VALUES.length > id ? VALUES[id] : VALUES[0]; } diff --git a/api/base/src/main/java/org/geysermc/api/util/UiProfile.java b/api/base/src/main/java/org/geysermc/api/util/UiProfile.java index c28ff869c..cddb97260 100644 --- a/api/base/src/main/java/org/geysermc/api/util/UiProfile.java +++ b/api/base/src/main/java/org/geysermc/api/util/UiProfile.java @@ -25,6 +25,8 @@ package org.geysermc.api.util; +import org.checkerframework.checker.nullness.qual.NonNull; + public enum UiProfile { CLASSIC, POCKET; @@ -36,6 +38,7 @@ public enum UiProfile { * @param id the UiProfile identifier * @return The UiProfile or {@link #CLASSIC} if the profile wasn't found */ + @NonNull public static UiProfile fromId(int id) { return VALUES.length > id ? VALUES[id] : VALUES[0]; } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/GeyserApi.java b/api/geyser/src/main/java/org/geysermc/geyser/api/GeyserApi.java index e73727104..f86206d36 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/GeyserApi.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/GeyserApi.java @@ -31,6 +31,7 @@ import org.geysermc.api.Geyser; import org.geysermc.api.GeyserApiBase; import org.geysermc.geyser.api.connection.GeyserConnection; import org.geysermc.geyser.api.event.EventBus; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.extension.ExtensionManager; import org.geysermc.geyser.api.network.BedrockListener; import org.geysermc.geyser.api.network.RemoteServer; @@ -86,7 +87,7 @@ public interface GeyserApi extends GeyserApiBase { * @return the event bus */ @NonNull - EventBus eventBus(); + EventBus eventBus(); /** * Gets the default {@link RemoteServer} configured diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventBus.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventBus.java index c42698d47..801bfa45f 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventBus.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventBus.java @@ -36,8 +36,8 @@ import java.util.Set; * Represents a bus capable of subscribing * or "listening" to events and firing them. */ -public interface EventBus extends OwnedEventBus> { +public interface EventBus extends OwnedEventBus> { @Override @NonNull - Set> subscribers(@NonNull Class eventClass); + Set> subscribers(@NonNull Class eventClass); } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventRegistrar.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventRegistrar.java new file mode 100644 index 000000000..7a2cc0071 --- /dev/null +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventRegistrar.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.api.event; + +import org.geysermc.geyser.api.GeyserApi; + +/** + * Represents an owner for an event that allows it + * to be registered through an {@link EventBus}. + */ +public interface EventRegistrar { + + /** + * Creates an {@link EventRegistrar} instance. + * + * @param object the object to wrap around + * @return an event registrar instance + */ + static EventRegistrar of(Object object) { + return GeyserApi.api().provider(EventRegistrar.class, object); + } +} diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventSubscriber.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventSubscriber.java index 7ce5b7883..7f91d09a3 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventSubscriber.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/EventSubscriber.java @@ -36,5 +36,5 @@ import org.geysermc.geyser.api.extension.Extension; * * @param the class of the event */ -public interface EventSubscriber extends OwnedSubscriber { +public interface EventSubscriber extends OwnedSubscriber { } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/ExtensionEventBus.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/ExtensionEventBus.java index 172c0f9de..a58d35891 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/event/ExtensionEventBus.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/ExtensionEventBus.java @@ -27,6 +27,7 @@ package org.geysermc.geyser.api.event; import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.event.Event; +import org.geysermc.geyser.api.extension.Extension; import java.util.Set; @@ -34,7 +35,7 @@ import java.util.Set; * An {@link EventBus} with additional methods that implicitly * set the extension instance. */ -public interface ExtensionEventBus extends org.geysermc.event.bus.EventBus> { +public interface ExtensionEventBus extends org.geysermc.event.bus.EventBus> { @Override - @NonNull Set> subscribers(@NonNull Class eventClass); + @NonNull Set> subscribers(@NonNull Class eventClass); } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java index bfed5d534..0957b8551 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java @@ -45,6 +45,7 @@ public interface GeyserDefineCustomItemsEvent extends Event { * * @return a multimap of all the already registered custom items */ + @NonNull Map> getExistingCustomItems(); /** @@ -52,6 +53,7 @@ public interface GeyserDefineCustomItemsEvent extends Event { * * @return the list of the already registered non-vanilla custom items */ + @NonNull List getExistingNonVanillaCustomItems(); /** diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPostInitializeEvent.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPostInitializeEvent.java index 9e88c017b..8d145f615 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPostInitializeEvent.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPostInitializeEvent.java @@ -28,6 +28,7 @@ package org.geysermc.geyser.api.event.lifecycle; import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.event.Event; import org.geysermc.geyser.api.event.EventBus; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.extension.ExtensionManager; /** @@ -36,5 +37,5 @@ import org.geysermc.geyser.api.extension.ExtensionManager; * @param extensionManager the extension manager * @param eventBus the event bus */ -public record GeyserPostInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event { +public record GeyserPostInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event { } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPreInitializeEvent.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPreInitializeEvent.java index 2be0272dc..8be89dafd 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPreInitializeEvent.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserPreInitializeEvent.java @@ -28,6 +28,7 @@ package org.geysermc.geyser.api.event.lifecycle; import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.event.Event; import org.geysermc.geyser.api.event.EventBus; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.extension.ExtensionManager; /** @@ -36,5 +37,5 @@ import org.geysermc.geyser.api.extension.ExtensionManager; * @param extensionManager the extension manager * @param eventBus the event bus */ -public record GeyserPreInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event { +public record GeyserPreInitializeEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event { } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserShutdownEvent.java b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserShutdownEvent.java index a1c68d876..7793ef997 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserShutdownEvent.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserShutdownEvent.java @@ -28,10 +28,11 @@ package org.geysermc.geyser.api.event.lifecycle; import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.event.Event; import org.geysermc.geyser.api.event.EventBus; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.extension.ExtensionManager; /** * Called when Geyser is shutting down. */ -public record GeyserShutdownEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event { +public record GeyserShutdownEvent(@NonNull ExtensionManager extensionManager, @NonNull EventBus eventBus) implements Event { } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/Extension.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/Extension.java index 2982a76fb..8d4e4da64 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/Extension.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/Extension.java @@ -25,16 +25,19 @@ package org.geysermc.geyser.api.extension; +import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.api.GeyserApiBase; import org.geysermc.geyser.api.GeyserApi; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.event.ExtensionEventBus; import java.nio.file.Path; +import java.util.Objects; /** * Represents an extension within Geyser. */ -public interface Extension { +public interface Extension extends EventRegistrar { /** * Gets if the extension is enabled @@ -59,6 +62,7 @@ public interface Extension { * * @return the extension's data folder */ + @NonNull default Path dataFolder() { return this.extensionLoader().dataFolder(this); } @@ -68,6 +72,7 @@ public interface Extension { * * @return the extension event bus */ + @NonNull default ExtensionEventBus eventBus() { return this.extensionLoader().eventBus(this); } @@ -77,6 +82,7 @@ public interface Extension { * * @return the extension manager */ + @NonNull default ExtensionManager extensionManager() { return this.geyserApi().extensionManager(); } @@ -86,6 +92,7 @@ public interface Extension { * * @return the extension's name */ + @NonNull default String name() { return this.description().name(); } @@ -95,6 +102,7 @@ public interface Extension { * * @return the extension's description */ + @NonNull default ExtensionDescription description() { return this.extensionLoader().description(this); } @@ -104,6 +112,7 @@ public interface Extension { * * @return the extension's logger */ + @NonNull default ExtensionLogger logger() { return this.extensionLoader().logger(this); } @@ -113,8 +122,9 @@ public interface Extension { * * @return the extension loader */ + @NonNull default ExtensionLoader extensionLoader() { - return this.extensionManager().extensionLoader(this); + return Objects.requireNonNull(this.extensionManager().extensionLoader(this)); } /** @@ -122,6 +132,7 @@ public interface Extension { * * @return the geyser api instance */ + @NonNull default GeyserApi geyserApi() { return GeyserApi.api(); } diff --git a/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePlugin.java b/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePlugin.java index ed2c340db..eb2a3b9c4 100644 --- a/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePlugin.java +++ b/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePlugin.java @@ -25,6 +25,8 @@ package org.geysermc.geyser.platform.bungeecord; +import io.netty.channel.Channel; +import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.config.ListenerInfo; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.protocol.ProtocolConstants; @@ -47,12 +49,15 @@ import org.geysermc.geyser.util.FileUtils; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Collection; import java.util.Map; import java.util.UUID; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap { @@ -66,23 +71,9 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap { private GeyserImpl geyser; @Override - public void onEnable() { + public void onLoad() { GeyserLocale.init(this); - // Copied from ViaVersion. - // https://github.com/ViaVersion/ViaVersion/blob/b8072aad86695cc8ec6f5e4103e43baf3abf6cc5/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java#L43 - try { - ProtocolConstants.class.getField("MINECRAFT_1_19_1"); - } catch (NoSuchFieldException e) { - getLogger().warning(" / \\"); - getLogger().warning(" / \\"); - getLogger().warning(" / | \\"); - getLogger().warning(" / | \\ " + GeyserLocale.getLocaleStringLog("geyser.bootstrap.unsupported_proxy", getProxy().getName())); - getLogger().warning(" / \\ " + GeyserLocale.getLocaleStringLog("geyser.may_not_work_as_intended_all_caps")); - getLogger().warning(" / o \\"); - getLogger().warning("/_____________\\"); - } - if (!getDataFolder().exists()) getDataFolder().mkdir(); @@ -121,6 +112,25 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap { this.geyserLogger = new GeyserBungeeLogger(getLogger(), geyserConfig.isDebugMode()); GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger); + this.geyser = GeyserImpl.load(PlatformType.BUNGEECORD, this); + } + + @Override + public void onEnable() { + // Copied from ViaVersion. + // https://github.com/ViaVersion/ViaVersion/blob/b8072aad86695cc8ec6f5e4103e43baf3abf6cc5/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java#L43 + try { + ProtocolConstants.class.getField("MINECRAFT_1_19_1"); + } catch (NoSuchFieldException e) { + getLogger().warning(" / \\"); + getLogger().warning(" / \\"); + getLogger().warning(" / | \\"); + getLogger().warning(" / | \\ " + GeyserLocale.getLocaleStringLog("geyser.bootstrap.unsupported_proxy", getProxy().getName())); + getLogger().warning(" / \\ " + GeyserLocale.getLocaleStringLog("geyser.may_not_work_as_intended_all_caps")); + getLogger().warning(" / o \\"); + getLogger().warning("/_____________\\"); + } + // Remove this in like a year if (getProxy().getPluginManager().getPlugin("floodgate-bungee") != null) { geyserLogger.severe(GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.outdated", "https://ci.opencollab.dev/job/GeyserMC/job/Floodgate/job/master/")); @@ -138,7 +148,41 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap { geyserConfig.loadFloodgate(this); - this.geyser = GeyserImpl.start(PlatformType.BUNGEECORD, this); + // Big hack - Bungee does not provide us an event to listen to, so schedule a repeating + // task that waits for a field to be filled which is set after the plugin enable + // process is complete + this.awaitStartupCompletion(0); + } + + @SuppressWarnings("unchecked") + private void awaitStartupCompletion(int tries) { + // After 20 tries give up waiting. This will happen + // just after 3 minutes approximately + if (tries >= 20) { + this.geyserLogger.warning("BungeeCord plugin startup is taking abnormally long, so Geyser is starting now. " + + "If all your plugins are loaded properly, this is a bug! " + + "If not, consider cutting down the amount of plugins on your proxy as it is causing abnormally slow starting times."); + this.postStartup(); + return; + } + + try { + Field listenersField = BungeeCord.getInstance().getClass().getDeclaredField("listeners"); + listenersField.setAccessible(true); + + Collection listeners = (Collection) listenersField.get(BungeeCord.getInstance()); + if (!listeners.isEmpty()) { + this.getProxy().getScheduler().schedule(this, this::postStartup, tries, TimeUnit.SECONDS); + } else { + this.awaitStartupCompletion(++tries); + } + } catch (NoSuchFieldException | IllegalAccessException ex) { + ex.printStackTrace(); + } + } + + private void postStartup() { + GeyserImpl.start(); this.geyserInjector = new GeyserBungeeInjector(this); this.geyserInjector.initializeLocalChannel(this); @@ -146,12 +190,6 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap { this.geyserCommandManager = new GeyserBungeeCommandManager(geyser); this.geyserCommandManager.init(); - if (geyserConfig.isLegacyPingPassthrough()) { - this.geyserBungeePingPassthrough = GeyserLegacyPingPassthrough.init(geyser); - } else { - this.geyserBungeePingPassthrough = new GeyserBungeePingPassthrough(getProxy()); - } - this.getProxy().getPluginManager().registerCommand(this, new GeyserBungeeCommandExecutor("geyser", this.geyser, this.geyserCommandManager.getCommands())); for (Map.Entry> entry : this.geyserCommandManager.extensionCommands().entrySet()) { Map commands = entry.getValue(); @@ -161,6 +199,12 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap { this.getProxy().getPluginManager().registerCommand(this, new GeyserBungeeCommandExecutor(entry.getKey().description().id(), this.geyser, commands)); } + + if (geyserConfig.isLegacyPingPassthrough()) { + this.geyserBungeePingPassthrough = GeyserLegacyPingPassthrough.init(geyser); + } else { + this.geyserBungeePingPassthrough = new GeyserBungeePingPassthrough(getProxy()); + } } @Override diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java index e3b794578..2000dc114 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPlugin.java @@ -34,6 +34,9 @@ import me.lucko.commodore.CommodoreProvider; import org.bukkit.Bukkit; import org.bukkit.command.CommandMap; import org.bukkit.command.PluginCommand; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServerLoadEvent; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.Plugin; @@ -59,7 +62,12 @@ import org.geysermc.geyser.platform.spigot.command.GeyserSpigotCommandManager; import org.geysermc.geyser.platform.spigot.command.SpigotCommandSource; import org.geysermc.geyser.platform.spigot.world.GeyserPistonListener; import org.geysermc.geyser.platform.spigot.world.GeyserSpigotBlockPlaceListener; -import org.geysermc.geyser.platform.spigot.world.manager.*; +import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigot1_12NativeWorldManager; +import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigot1_12WorldManager; +import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigotFallbackWorldManager; +import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigotLegacyNativeWorldManager; +import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigotNativeWorldManager; +import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigotWorldManager; import org.geysermc.geyser.text.GeyserLocale; import org.geysermc.geyser.util.FileUtils; @@ -95,7 +103,7 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { private String minecraftVersion; @Override - public void onEnable() { + public void onLoad() { GeyserLocale.init(this); // This is manually done instead of using Bukkit methods to save the config because otherwise comments get removed @@ -113,6 +121,30 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { return; } + // By default this should be localhost but may need to be changed in some circumstances + if (this.geyserConfig.getRemote().address().equalsIgnoreCase("auto")) { + geyserConfig.setAutoconfiguredRemote(true); + // Don't use localhost if not listening on all interfaces + if (!Bukkit.getIp().equals("0.0.0.0") && !Bukkit.getIp().equals("")) { + geyserConfig.getRemote().setAddress(Bukkit.getIp()); + } + geyserConfig.getRemote().setPort(Bukkit.getPort()); + } + + if (geyserConfig.getBedrock().isCloneRemotePort()) { + geyserConfig.getBedrock().setPort(Bukkit.getPort()); + } + + this.geyserLogger = GeyserPaperLogger.supported() ? new GeyserPaperLogger(this, getLogger(), geyserConfig.isDebugMode()) + : new GeyserSpigotLogger(getLogger(), geyserConfig.isDebugMode()); + + GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger); + + this.geyser = GeyserImpl.load(PlatformType.SPIGOT, this); + } + + @Override + public void onEnable() { try { // AvailableCommandsSerializer_v291 complains otherwise ByteBuf.class.getMethod("writeShortLE", int.class); @@ -144,24 +176,6 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { } } - // By default this should be localhost but may need to be changed in some circumstances - if (this.geyserConfig.getRemote().address().equalsIgnoreCase("auto")) { - geyserConfig.setAutoconfiguredRemote(true); - // Don't use localhost if not listening on all interfaces - if (!Bukkit.getIp().equals("0.0.0.0") && !Bukkit.getIp().equals("")) { - geyserConfig.getRemote().setAddress(Bukkit.getIp()); - } - geyserConfig.getRemote().setPort(Bukkit.getPort()); - } - - if (geyserConfig.getBedrock().isCloneRemotePort()) { - geyserConfig.getBedrock().setPort(Bukkit.getPort()); - } - - this.geyserLogger = GeyserPaperLogger.supported() ? new GeyserPaperLogger(this, getLogger(), geyserConfig.isDebugMode()) - : new GeyserSpigotLogger(getLogger(), geyserConfig.isDebugMode()); - GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger); - // Remove this in like a year if (Bukkit.getPluginManager().getPlugin("floodgate-bukkit") != null) { geyserLogger.severe(GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.outdated", Constants.FLOODGATE_DOWNLOAD_LOCATION)); @@ -172,7 +186,6 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { if (geyserConfig.getRemote().authType() == AuthType.FLOODGATE && Bukkit.getPluginManager().getPlugin("floodgate") == null) { geyserLogger.severe(GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.not_installed") + " " + GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.disabling")); this.getPluginLoader().disablePlugin(this); - return; } else if (geyserConfig.isAutoconfiguredRemote() && Bukkit.getPluginManager().getPlugin("floodgate") != null) { // Floodgate installed means that the user wants Floodgate authentication geyserLogger.debug("Auto-setting to Floodgate authentication."); @@ -181,11 +194,43 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { geyserConfig.loadFloodgate(this); + // Needs to be an anonymous inner class otherwise Bukkit complains about missing classes + Bukkit.getPluginManager().registerEvents(new Listener() { + + @EventHandler + public void onServerLoaded(ServerLoadEvent event) { + // Wait until all plugins have loaded so Geyser can start + postStartup(); + } + }, this); + + // Because Bukkit locks its command map upon startup, we need to + // add our plugin commands in onEnable, but populating the executor + // can happen at any time + CommandMap commandMap = GeyserSpigotCommandManager.getCommandMap(); + for (Extension extension : this.geyser.extensionManager().extensions()) { + + // Thanks again, Bukkit + try { + Constructor constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + constructor.setAccessible(true); + + PluginCommand pluginCommand = constructor.newInstance(extension.description().id(), this); + pluginCommand.setDescription("The main command for the " + extension.name() + " Geyser extension!"); + + commandMap.register(extension.description().id(), "geyserext", pluginCommand); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { + this.geyserLogger.error("Failed to construct PluginCommand for extension " + extension.description().name(), ex); + } + } + } + + private void postStartup() { + GeyserImpl.start(); + // Turn "(MC: 1.16.4)" into 1.16.4. this.minecraftVersion = Bukkit.getServer().getVersion().split("\\(MC: ")[1].split("\\)")[0]; - this.geyser = GeyserImpl.start(PlatformType.SPIGOT, this); - if (geyserConfig.isLegacyPingPassthrough()) { this.geyserSpigotPingPassthrough = GeyserLegacyPingPassthrough.init(geyser); } else { @@ -275,25 +320,18 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { PluginCommand geyserCommand = this.getCommand("geyser"); geyserCommand.setExecutor(new GeyserSpigotCommandExecutor(geyser, geyserCommandManager.getCommands())); - CommandMap commandMap = GeyserSpigotCommandManager.getCommandMap(); for (Map.Entry> entry : this.geyserCommandManager.extensionCommands().entrySet()) { Map commands = entry.getValue(); if (commands.isEmpty()) { continue; } - // Thanks again, Bukkit - try { - Constructor constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); - constructor.setAccessible(true); - - PluginCommand pluginCommand = constructor.newInstance(entry.getKey().description().id(), this); - pluginCommand.setExecutor(new GeyserSpigotCommandExecutor(this.geyser, commands)); - pluginCommand.setDescription("The main command for the " + entry.getKey().name() + " Geyser extension!"); - commandMap.register(entry.getKey().description().id(), "geyserext", pluginCommand); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { - this.geyserLogger.error("Failed to construct PluginCommand for extension " + entry.getKey().description().name(), ex); + PluginCommand command = this.getCommand(entry.getKey().description().id()); + if (command == null) { + continue; } + + command.setExecutor(new GeyserSpigotCommandExecutor(this.geyser, commands)); } if (!INITIALIZED) { diff --git a/bootstrap/sponge/src/main/java/org/geysermc/geyser/platform/sponge/GeyserSpongePlugin.java b/bootstrap/sponge/src/main/java/org/geysermc/geyser/platform/sponge/GeyserSpongePlugin.java index d912d28d8..42040f6ab 100644 --- a/bootstrap/sponge/src/main/java/org/geysermc/geyser/platform/sponge/GeyserSpongePlugin.java +++ b/bootstrap/sponge/src/main/java/org/geysermc/geyser/platform/sponge/GeyserSpongePlugin.java @@ -72,8 +72,7 @@ public class GeyserSpongePlugin implements GeyserBootstrap { private GeyserImpl geyser; - @Override - public void onEnable() { + public void onLoad() { GeyserLocale.init(this); if (!configDir.exists()) @@ -114,7 +113,13 @@ public class GeyserSpongePlugin implements GeyserBootstrap { this.geyserLogger = new GeyserSpongeLogger(logger, geyserConfig.isDebugMode()); GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger); - this.geyser = GeyserImpl.start(PlatformType.SPONGE, this); + + this.geyser = GeyserImpl.load(PlatformType.SPONGE, this); + } + + @Override + public void onEnable() { + GeyserImpl.start(); if (geyserConfig.isLegacyPingPassthrough()) { this.geyserSpongePingPassthrough = GeyserLegacyPingPassthrough.init(geyser); @@ -124,6 +129,7 @@ public class GeyserSpongePlugin implements GeyserBootstrap { this.geyserCommandManager = new GeyserSpongeCommandManager(Sponge.getCommandManager(), geyser); this.geyserCommandManager.init(); + Sponge.getCommandManager().register(this, new GeyserSpongeCommandExecutor(geyser, geyserCommandManager.getCommands()), "geyser"); for (Map.Entry> entry : this.geyserCommandManager.extensionCommands().entrySet()) { @@ -166,6 +172,11 @@ public class GeyserSpongePlugin implements GeyserBootstrap { return configDir.toPath(); } + @Listener + public void onServerStarting() { + onLoad(); + } + @Listener public void onServerStart(GameStartedServerEvent event) { onEnable(); diff --git a/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneBootstrap.java b/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneBootstrap.java index 052a41439..80d17f6a7 100644 --- a/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneBootstrap.java +++ b/bootstrap/standalone/src/main/java/org/geysermc/geyser/platform/standalone/GeyserStandaloneBootstrap.java @@ -217,7 +217,9 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap { // Allow libraries like Protocol to have their debug information passthrough logger.get().setLevel(geyserConfig.isDebugMode() ? Level.DEBUG : Level.INFO); - geyser = GeyserImpl.start(PlatformType.STANDALONE, this); + geyser = GeyserImpl.load(PlatformType.STANDALONE, this); + GeyserImpl.start(); + geyserCommandManager = new GeyserStandaloneCommandManager(geyser); geyserCommandManager.init(); diff --git a/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPlugin.java b/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPlugin.java index 30d8deccd..dc31b3fdd 100644 --- a/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPlugin.java +++ b/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPlugin.java @@ -88,15 +88,6 @@ public class GeyserVelocityPlugin implements GeyserBootstrap { @Override public void onEnable() { - try { - Codec.class.getMethod("codec", Codec.Decoder.class, Codec.Encoder.class); - } catch (NoSuchMethodException e) { - // velocitypowered.com has a build that is very outdated - logger.error("Please download Velocity from https://papermc.io/downloads#Velocity - the 'stable' Velocity version " + - "that has likely been downloaded is very outdated and does not support 1.19."); - return; - } - GeyserLocale.init(this); try { @@ -131,6 +122,17 @@ public class GeyserVelocityPlugin implements GeyserBootstrap { this.geyserLogger = new GeyserVelocityLogger(logger, geyserConfig.isDebugMode()); GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger); + this.geyser = GeyserImpl.load(PlatformType.VELOCITY, this); + + try { + Codec.class.getMethod("codec", Codec.Decoder.class, Codec.Encoder.class); + } catch (NoSuchMethodException e) { + // velocitypowered.com has a build that is very outdated + logger.error("Please download Velocity from https://papermc.io/downloads#Velocity - the 'stable' Velocity version " + + "that has likely been downloaded is very outdated and does not support 1.19."); + return; + } + // Remove this in like a year try { // Should only exist on 1.0 @@ -153,7 +155,10 @@ public class GeyserVelocityPlugin implements GeyserBootstrap { geyserConfig.loadFloodgate(this, proxyServer, configFolder.toFile()); - this.geyser = GeyserImpl.start(PlatformType.VELOCITY, this); + } + + private void postStartup() { + GeyserImpl.start(); this.geyserInjector = new GeyserVelocityInjector(proxyServer); // Will be initialized after the proxy has been bound @@ -222,9 +227,14 @@ public class GeyserVelocityPlugin implements GeyserBootstrap { @Subscribe public void onProxyBound(ListenerBoundEvent event) { - if (event.getListenerType() == ListenerType.MINECRAFT && geyserInjector != null) { - // After this bound, we know that the channel initializer cannot change without it being ineffective for Velocity, too - geyserInjector.initializeLocalChannel(this); + if (event.getListenerType() == ListenerType.MINECRAFT) { + // Once listener is bound, do our startup process + this.postStartup(); + + if (geyserInjector != null) { + // After this bound, we know that the channel initializer cannot change without it being ineffective for Velocity, too + geyserInjector.initializeLocalChannel(this); + } } } diff --git a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java index 24a5f5d2c..115a7245e 100644 --- a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java +++ b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java @@ -57,6 +57,7 @@ import org.geysermc.floodgate.crypto.FloodgateCipher; import org.geysermc.floodgate.news.NewsItemAction; import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.api.event.EventBus; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.event.lifecycle.GeyserPostInitializeEvent; import org.geysermc.geyser.api.event.lifecycle.GeyserPreInitializeEvent; import org.geysermc.geyser.api.event.lifecycle.GeyserShutdownEvent; @@ -143,8 +144,8 @@ public class GeyserImpl implements GeyserApi { private final PlatformType platformType; private final GeyserBootstrap bootstrap; - private final EventBus eventBus; - private final GeyserExtensionManager extensionManager; + private final EventBus eventBus; + private GeyserExtensionManager extensionManager; private Metrics metrics; @@ -162,9 +163,19 @@ public class GeyserImpl implements GeyserApi { this.platformType = platformType; this.bootstrap = bootstrap; + GeyserLocale.finalizeDefaultLocale(this); + + /* Initialize event bus */ + this.eventBus = new GeyserEventBus(); + + /* Load Extensions */ + this.extensionManager = new GeyserExtensionManager(); + this.extensionManager.init(); + } + + public void initialize() { long startupTime = System.currentTimeMillis(); - GeyserLocale.finalizeDefaultLocale(this); GeyserLogger logger = bootstrap.getGeyserLogger(); logger.info("******************************************"); @@ -173,13 +184,8 @@ public class GeyserImpl implements GeyserApi { logger.info(""); logger.info("******************************************"); - /* Initialize event bus */ - this.eventBus = new GeyserEventBus(); - - /* Load Extensions */ - this.extensionManager = new GeyserExtensionManager(); - this.extensionManager.init(); + /* Enable extensions */ this.extensionManager.enableExtensions(); this.eventBus.fire(new GeyserPreInitializeEvent(this.extensionManager, this.eventBus)); @@ -193,7 +199,7 @@ public class GeyserImpl implements GeyserApi { MessageTranslator.init(); MinecraftLocale.init(); - start(); + startInstance(); GeyserConfiguration config = bootstrap.getGeyserConfig(); @@ -225,7 +231,7 @@ public class GeyserImpl implements GeyserApi { } } - private void start() { + private void startInstance() { this.scheduledThread = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("Geyser Scheduled Thread")); GeyserLogger logger = bootstrap.getGeyserLogger(); @@ -589,7 +595,7 @@ public class GeyserImpl implements GeyserApi { @Override @NonNull - public EventBus eventBus() { + public EventBus eventBus() { return this.eventBus; } @@ -612,18 +618,26 @@ public class GeyserImpl implements GeyserApi { return Integer.parseInt(BUILD_NUMBER); } - public static GeyserImpl start(PlatformType platformType, GeyserBootstrap bootstrap) { + public static GeyserImpl load(PlatformType platformType, GeyserBootstrap bootstrap) { if (instance == null) { return new GeyserImpl(platformType, bootstrap); } + return instance; + } + + public static void start() { + if (instance == null) { + throw new RuntimeException("Geyser has not been loaded yet!"); + } + // We've been reloaded if (instance.isShuttingDown()) { instance.shuttingDown = false; - instance.start(); + instance.startInstance(); + } else { + instance.initialize(); } - - return instance; } public GeyserLogger getLogger() { diff --git a/core/src/main/java/org/geysermc/geyser/event/GeyserEventBus.java b/core/src/main/java/org/geysermc/geyser/event/GeyserEventBus.java index f634931f4..9593e327e 100644 --- a/core/src/main/java/org/geysermc/geyser/event/GeyserEventBus.java +++ b/core/src/main/java/org/geysermc/geyser/event/GeyserEventBus.java @@ -32,6 +32,7 @@ import org.geysermc.event.bus.impl.OwnedEventBusImpl; import org.geysermc.event.subscribe.OwnedSubscriber; import org.geysermc.event.subscribe.Subscribe; import org.geysermc.geyser.api.event.EventBus; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.event.EventSubscriber; import org.geysermc.geyser.api.extension.Extension; @@ -40,11 +41,11 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; @SuppressWarnings("unchecked") -public final class GeyserEventBus extends OwnedEventBusImpl> - implements EventBus { +public final class GeyserEventBus extends OwnedEventBusImpl> + implements EventBus { @Override - protected > B makeSubscription( - @NonNull Extension owner, + protected > B makeSubscription( + @NonNull EventRegistrar owner, @NonNull Class eventClass, @NonNull Subscribe subscribe, @NonNull L listener, @@ -55,8 +56,8 @@ public final class GeyserEventBus extends OwnedEventBusImpl> B makeSubscription( - @NonNull Extension owner, + protected > B makeSubscription( + @NonNull EventRegistrar owner, @NonNull Class eventClass, @NonNull Consumer handler, @NonNull PostOrder postOrder) { @@ -65,7 +66,7 @@ public final class GeyserEventBus extends OwnedEventBusImpl Set> subscribers(@NonNull Class eventClass) { + public Set> subscribers(@NonNull Class eventClass) { return castGenericSet(super.subscribers(eventClass)); } } diff --git a/core/src/main/java/org/geysermc/geyser/event/GeyserEventRegistrar.java b/core/src/main/java/org/geysermc/geyser/event/GeyserEventRegistrar.java new file mode 100644 index 000000000..85c36a132 --- /dev/null +++ b/core/src/main/java/org/geysermc/geyser/event/GeyserEventRegistrar.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.event; + +import org.geysermc.geyser.api.event.EventRegistrar; + +public record GeyserEventRegistrar(Object owner) implements EventRegistrar { + + @Override + public String toString() { + return "GeyserEventRegistrar{" + + "owner=" + this.owner + + '}'; + } +} diff --git a/core/src/main/java/org/geysermc/geyser/event/GeyserEventSubscriber.java b/core/src/main/java/org/geysermc/geyser/event/GeyserEventSubscriber.java index 5012037bb..d33de8cdd 100644 --- a/core/src/main/java/org/geysermc/geyser/event/GeyserEventSubscriber.java +++ b/core/src/main/java/org/geysermc/geyser/event/GeyserEventSubscriber.java @@ -29,16 +29,17 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.event.Event; import org.geysermc.event.PostOrder; import org.geysermc.event.subscribe.impl.OwnedSubscriberImpl; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.event.ExtensionEventSubscriber; import org.geysermc.geyser.api.extension.Extension; import java.util.function.BiConsumer; import java.util.function.Consumer; -public final class GeyserEventSubscriber extends OwnedSubscriberImpl +public final class GeyserEventSubscriber extends OwnedSubscriberImpl implements ExtensionEventSubscriber { GeyserEventSubscriber( - @NonNull Extension owner, + @NonNull R owner, @NonNull Class eventClass, @NonNull Consumer handler, @NonNull PostOrder postOrder) { @@ -46,7 +47,7 @@ public final class GeyserEventSubscriber extends OwnedSubscribe } GeyserEventSubscriber( - @NonNull Extension owner, + @NonNull R owner, @NonNull Class eventClass, @NonNull PostOrder postOrder, boolean ignoreCancelled, diff --git a/core/src/main/java/org/geysermc/geyser/extension/event/GeyserExtensionEventBus.java b/core/src/main/java/org/geysermc/geyser/extension/event/GeyserExtensionEventBus.java index 7294d4345..f56b254a6 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/event/GeyserExtensionEventBus.java +++ b/core/src/main/java/org/geysermc/geyser/extension/event/GeyserExtensionEventBus.java @@ -30,6 +30,7 @@ import org.geysermc.event.Event; import org.geysermc.event.PostOrder; import org.geysermc.event.subscribe.Subscriber; import org.geysermc.geyser.api.event.EventBus; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.event.EventSubscriber; import org.geysermc.geyser.api.event.ExtensionEventBus; import org.geysermc.geyser.api.extension.Extension; @@ -37,10 +38,12 @@ import org.geysermc.geyser.api.extension.Extension; import java.util.Set; import java.util.function.Consumer; -public record GeyserExtensionEventBus(EventBus eventBus, Extension extension) implements ExtensionEventBus { +public record GeyserExtensionEventBus(EventBus eventBus, Extension extension) implements ExtensionEventBus { + + @SuppressWarnings({"rawtypes", "unchecked"}) @Override - public void unsubscribe(@NonNull EventSubscriber subscription) { - eventBus.unsubscribe(subscription); + public void unsubscribe(@NonNull EventSubscriber subscription) { + eventBus.unsubscribe((EventSubscriber) subscription); } @Override @@ -49,7 +52,7 @@ public record GeyserExtensionEventBus(EventBus eventBus, Extension extension) im } @Override - public @NonNull Set> subscribers(@NonNull Class eventClass) { + public @NonNull Set> subscribers(@NonNull Class eventClass) { return eventBus.subscribers(eventClass); } diff --git a/core/src/main/java/org/geysermc/geyser/registry/loader/ProviderRegistryLoader.java b/core/src/main/java/org/geysermc/geyser/registry/loader/ProviderRegistryLoader.java index e7837688c..99a9213fe 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/loader/ProviderRegistryLoader.java +++ b/core/src/main/java/org/geysermc/geyser/registry/loader/ProviderRegistryLoader.java @@ -26,11 +26,13 @@ package org.geysermc.geyser.registry.loader; import org.geysermc.geyser.api.command.Command; +import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.extension.Extension; import org.geysermc.geyser.api.item.custom.CustomItemData; import org.geysermc.geyser.api.item.custom.CustomItemOptions; import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData; import org.geysermc.geyser.command.GeyserCommandManager; +import org.geysermc.geyser.event.GeyserEventRegistrar; import org.geysermc.geyser.item.GeyserCustomItemData; import org.geysermc.geyser.item.GeyserCustomItemOptions; import org.geysermc.geyser.item.GeyserNonVanillaCustomItemData; @@ -49,6 +51,7 @@ public class ProviderRegistryLoader implements RegistryLoader, Prov providers.put(CustomItemData.Builder.class, args -> new GeyserCustomItemData.CustomItemDataBuilder()); providers.put(CustomItemOptions.Builder.class, args -> new GeyserCustomItemOptions.CustomItemOptionsBuilder()); providers.put(NonVanillaCustomItemData.Builder.class, args -> new GeyserNonVanillaCustomItemData.NonVanillaCustomItemDataBuilder()); + providers.put(EventRegistrar.class, args -> new GeyserEventRegistrar(args[0])); return providers; }