Allow events to be registered by any class

Supersedes & closes #3073

Co-authored-by: Redned <redned235@gmail.com>
This commit is contained in:
ImDaBigBoss 2022-09-04 16:11:08 -05:00 committed by RednedEpic
parent db3b470225
commit f1da9d7072
25 changed files with 353 additions and 115 deletions

View file

@ -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!");

View file

@ -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];
}

View file

@ -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];
}

View file

@ -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];
}

View file

@ -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<EventRegistrar> eventBus();
/**
* Gets the default {@link RemoteServer} configured

View file

@ -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<Extension, Event, EventSubscriber<? extends Event>> {
public interface EventBus<R extends EventRegistrar> extends OwnedEventBus<R, Event, EventSubscriber<R, ? extends Event>> {
@Override
@NonNull
<T extends Event> Set<? extends EventSubscriber<T>> subscribers(@NonNull Class<T> eventClass);
<T extends Event> Set<? extends EventSubscriber<R, T>> subscribers(@NonNull Class<T> eventClass);
}

View file

@ -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);
}
}

View file

@ -36,5 +36,5 @@ import org.geysermc.geyser.api.extension.Extension;
*
* @param <T> the class of the event
*/
public interface EventSubscriber<T extends Event> extends OwnedSubscriber<Extension, T> {
public interface EventSubscriber<R extends EventRegistrar, T extends Event> extends OwnedSubscriber<R, T> {
}

View file

@ -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<Event, EventSubscriber<? extends Event>> {
public interface ExtensionEventBus extends org.geysermc.event.bus.EventBus<Event, EventSubscriber<Extension, ? extends Event>> {
@Override
@NonNull <T extends Event> Set<? extends EventSubscriber<T>> subscribers(@NonNull Class<T> eventClass);
@NonNull <T extends Event> Set<? extends EventSubscriber<EventRegistrar, T>> subscribers(@NonNull Class<T> eventClass);
}

View file

@ -45,6 +45,7 @@ public interface GeyserDefineCustomItemsEvent extends Event {
*
* @return a multimap of all the already registered custom items
*/
@NonNull
Map<String, Collection<CustomItemData>> getExistingCustomItems();
/**
@ -52,6 +53,7 @@ public interface GeyserDefineCustomItemsEvent extends Event {
*
* @return the list of the already registered non-vanilla custom items
*/
@NonNull
List<NonVanillaCustomItemData> getExistingNonVanillaCustomItems();
/**

View file

@ -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<EventRegistrar> eventBus) implements Event {
}

View file

@ -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<EventRegistrar> eventBus) implements Event {
}

View file

@ -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<EventRegistrar> eventBus) implements Event {
}

View file

@ -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();
}

View file

@ -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<Channel> listeners = (Collection<Channel>) 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<Extension, Map<String, Command>> entry : this.geyserCommandManager.extensionCommands().entrySet()) {
Map<String, Command> 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

View file

@ -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<PluginCommand> 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<Extension, Map<String, Command>> entry : this.geyserCommandManager.extensionCommands().entrySet()) {
Map<String, Command> commands = entry.getValue();
if (commands.isEmpty()) {
continue;
}
// Thanks again, Bukkit
try {
Constructor<PluginCommand> 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) {

View file

@ -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<Extension, Map<String, Command>> 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();

View file

@ -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();

View file

@ -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,11 +227,16 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
@Subscribe
public void onProxyBound(ListenerBoundEvent event) {
if (event.getListenerType() == ListenerType.MINECRAFT && geyserInjector != null) {
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);
}
}
}
@Override
public BootstrapDumpInfo getDumpInfo() {

View file

@ -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<EventRegistrar> 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<EventRegistrar> 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() {

View file

@ -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<Extension, Event, EventSubscriber<? extends Event>>
implements EventBus {
public final class GeyserEventBus extends OwnedEventBusImpl<EventRegistrar, Event, EventSubscriber<EventRegistrar, ? extends Event>>
implements EventBus<EventRegistrar> {
@Override
protected <L, T extends Event, B extends OwnedSubscriber<Extension, T>> B makeSubscription(
@NonNull Extension owner,
protected <L, T extends Event, B extends OwnedSubscriber<EventRegistrar, T>> B makeSubscription(
@NonNull EventRegistrar owner,
@NonNull Class<T> eventClass,
@NonNull Subscribe subscribe,
@NonNull L listener,
@ -55,8 +56,8 @@ public final class GeyserEventBus extends OwnedEventBusImpl<Extension, Event, Ev
}
@Override
protected <T extends Event, B extends OwnedSubscriber<Extension, T>> B makeSubscription(
@NonNull Extension owner,
protected <T extends Event, B extends OwnedSubscriber<EventRegistrar, T>> B makeSubscription(
@NonNull EventRegistrar owner,
@NonNull Class<T> eventClass,
@NonNull Consumer<T> handler,
@NonNull PostOrder postOrder) {
@ -65,7 +66,7 @@ public final class GeyserEventBus extends OwnedEventBusImpl<Extension, Event, Ev
@Override
@NonNull
public <T extends Event> Set<? extends EventSubscriber<T>> subscribers(@NonNull Class<T> eventClass) {
public <T extends Event> Set<? extends EventSubscriber<EventRegistrar, T>> subscribers(@NonNull Class<T> eventClass) {
return castGenericSet(super.subscribers(eventClass));
}
}

View file

@ -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 +
'}';
}
}

View file

@ -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<E extends Event> extends OwnedSubscriberImpl<Extension, E>
public final class GeyserEventSubscriber<R extends EventRegistrar, E extends Event> extends OwnedSubscriberImpl<R, E>
implements ExtensionEventSubscriber<E> {
GeyserEventSubscriber(
@NonNull Extension owner,
@NonNull R owner,
@NonNull Class<E> eventClass,
@NonNull Consumer<E> handler,
@NonNull PostOrder postOrder) {
@ -46,7 +47,7 @@ public final class GeyserEventSubscriber<E extends Event> extends OwnedSubscribe
}
<H> GeyserEventSubscriber(
@NonNull Extension owner,
@NonNull R owner,
@NonNull Class<E> eventClass,
@NonNull PostOrder postOrder,
boolean ignoreCancelled,

View file

@ -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<EventRegistrar> eventBus, Extension extension) implements ExtensionEventBus {
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void unsubscribe(@NonNull EventSubscriber<? extends Event> subscription) {
eventBus.unsubscribe(subscription);
public void unsubscribe(@NonNull EventSubscriber<Extension, ? extends Event> subscription) {
eventBus.unsubscribe((EventSubscriber) subscription);
}
@Override
@ -49,7 +52,7 @@ public record GeyserExtensionEventBus(EventBus eventBus, Extension extension) im
}
@Override
public @NonNull <T extends Event> Set<? extends EventSubscriber<T>> subscribers(@NonNull Class<T> eventClass) {
public @NonNull <T extends Event> Set<? extends EventSubscriber<EventRegistrar, T>> subscribers(@NonNull Class<T> eventClass) {
return eventBus.subscribers(eventClass);
}

View file

@ -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<Map<Class<?>, 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;
}