From 6df8740955f0060821e7e2c7b7fbcb0a25496cbc Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Wed, 14 Sep 2022 14:19:56 -0400 Subject: [PATCH] Only register commands on Spigot if the extension has commands --- .../geyser/api/extension/Extension.java | 2 +- .../geyser/api/extension/ExtensionLoader.java | 3 +- .../api/extension/ExtensionManager.java | 36 ++------------- .../platform/spigot/GeyserSpigotPlugin.java | 16 +++---- .../java/org/geysermc/geyser/GeyserImpl.java | 8 +--- .../geyser/command/GeyserCommand.java | 3 +- .../extension/GeyserExtensionManager.java | 45 +++---------------- 7 files changed, 21 insertions(+), 92 deletions(-) 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 8d4e4da64..33fc159de 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 @@ -124,7 +124,7 @@ public interface Extension extends EventRegistrar { */ @NonNull default ExtensionLoader extensionLoader() { - return Objects.requireNonNull(this.extensionManager().extensionLoader(this)); + return Objects.requireNonNull(this.extensionManager().extensionLoader()); } /** diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java index c84c37919..30414d500 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java @@ -34,7 +34,6 @@ import java.nio.file.Path; * The extension loader is responsible for loading, unloading, enabling and disabling extensions */ public abstract class ExtensionLoader { - /** * Gets if the given {@link Extension} is enabled. * @@ -101,6 +100,6 @@ public abstract class ExtensionLoader { * @param extensionManager the extension manager */ protected void register(@NonNull Extension extension, @NonNull ExtensionManager extensionManager) { - extensionManager.register(extension, this); + extensionManager.register(extension); } } \ No newline at end of file diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionManager.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionManager.java index 65d6c66da..a9d0d7376 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionManager.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionManager.java @@ -29,7 +29,6 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Collection; -import java.util.Map; /** * Manages Geyser {@link Extension}s @@ -59,15 +58,6 @@ public abstract class ExtensionManager { */ public abstract void disable(@NonNull Extension extension); - /** - * Gets the {@link ExtensionLoader} responsible for loading - * the given {@link Extension}. - * - * @return the extension loader for loading the given extension - */ - @Nullable - public abstract ExtensionLoader extensionLoader(@NonNull Extension extension); - /** * Gets all the {@link Extension}s currently loaded. * @@ -77,37 +67,19 @@ public abstract class ExtensionManager { public abstract Collection extensions(); /** - * Gets the {@link ExtensionLoader} with the given identifier. + * Gets the {@link ExtensionLoader}. * - * @param identifier the identifier - * @return the extension loader at the given identifier + * @return the extension loader */ @Nullable - public abstract ExtensionLoader extensionLoader(@NonNull String identifier); - - /** - * Registers an {@link ExtensionLoader} with the given identifier. - * - * @param identifier the identifier - * @param extensionLoader the extension loader - */ - public abstract void registerExtensionLoader(@NonNull String identifier, @NonNull ExtensionLoader extensionLoader); - - /** - * Gets all the currently registered {@link ExtensionLoader}s. - * - * @return all the currently registered extension loaders - */ - @NonNull - public abstract Map extensionLoaders(); + public abstract ExtensionLoader extensionLoader(); /** * Registers an {@link Extension} with the given {@link ExtensionLoader}. * * @param extension the extension - * @param loader the loader */ - public abstract void register(@NonNull Extension extension, @NonNull ExtensionLoader loader); + public abstract void register(@NonNull Extension extension); /** * Loads all extensions from the given {@link ExtensionLoader}. 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 4407af6c7..60b1cfa21 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 @@ -62,12 +62,7 @@ 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.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.platform.spigot.world.manager.*; import org.geysermc.geyser.text.GeyserLocale; import org.geysermc.geyser.util.FileUtils; @@ -204,12 +199,14 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { } }, this); + this.geyserCommandManager = new GeyserSpigotCommandManager(geyser); + this.geyserCommandManager.init(); + // 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()) { - + for (Extension extension : this.geyserCommandManager.extensionCommands().keySet()) { // Thanks again, Bukkit try { Constructor constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); @@ -245,9 +242,6 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap { } geyserLogger.debug("Spigot ping passthrough type: " + (this.geyserSpigotPingPassthrough == null ? null : this.geyserSpigotPingPassthrough.getClass())); - this.geyserCommandManager = new GeyserSpigotCommandManager(geyser); - this.geyserCommandManager.init(); - boolean isViaVersion = Bukkit.getPluginManager().getPlugin("ViaVersion") != null; if (isViaVersion) { try { diff --git a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java index ce2474d50..a10e54f90 100644 --- a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java +++ b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java @@ -147,7 +147,7 @@ public class GeyserImpl implements GeyserApi { private final GeyserBootstrap bootstrap; private final EventBus eventBus; - private GeyserExtensionManager extensionManager; + private final GeyserExtensionManager extensionManager; private Metrics metrics; @@ -173,6 +173,7 @@ public class GeyserImpl implements GeyserApi { /* Load Extensions */ this.extensionManager = new GeyserExtensionManager(); this.extensionManager.init(); + this.eventBus.fire(new GeyserPreInitializeEvent(this.extensionManager, this.eventBus)); } public void initialize() { @@ -186,11 +187,6 @@ public class GeyserImpl implements GeyserApi { logger.info(""); logger.info("******************************************"); - - /* Enable extensions */ - this.extensionManager.enableExtensions(); - this.eventBus.fire(new GeyserPreInitializeEvent(this.extensionManager, this.eventBus)); - /* Initialize registries */ Registries.init(); BlockRegistries.init(); diff --git a/core/src/main/java/org/geysermc/geyser/command/GeyserCommand.java b/core/src/main/java/org/geysermc/geyser/command/GeyserCommand.java index 0d020ad08..5808dbc2c 100644 --- a/core/src/main/java/org/geysermc/geyser/command/GeyserCommand.java +++ b/core/src/main/java/org/geysermc/geyser/command/GeyserCommand.java @@ -33,7 +33,6 @@ import org.geysermc.geyser.api.command.Command; import org.geysermc.geyser.session.GeyserSession; import javax.annotation.Nullable; -import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -49,7 +48,7 @@ public abstract class GeyserCommand implements Command { protected final String description; protected final String permission; - private List aliases = new ArrayList<>(); + private List aliases = Collections.emptyList(); public abstract void execute(@Nullable GeyserSession session, GeyserCommandSource sender, String[] args); diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionManager.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionManager.java index 7d80c2cf6..5dd924301 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionManager.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionManager.java @@ -25,8 +25,6 @@ package org.geysermc.geyser.extension; -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import net.kyori.adventure.key.Key; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.geysermc.geyser.GeyserImpl; @@ -39,34 +37,23 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import java.util.stream.Collectors; public class GeyserExtensionManager extends ExtensionManager { - private static final Key BASE_EXTENSION_LOADER_KEY = Key.key("geysermc", "base"); - - private final Map extensionLoaderTypes = new Object2ObjectOpenHashMap<>(); - + private final GeyserExtensionLoader extensionLoader = new GeyserExtensionLoader(); private final Map extensions = new LinkedHashMap<>(); - private final Map extensionsLoaders = new LinkedHashMap<>(); public void init() { GeyserImpl.getInstance().getLogger().info(GeyserLocale.getLocaleStringLog("geyser.extensions.load.loading")); - extensionLoaderTypes.put(BASE_EXTENSION_LOADER_KEY, new GeyserExtensionLoader()); - for (ExtensionLoader loader : this.extensionLoaders().values()) { - this.loadAllExtensions(loader); - } + loadAllExtensions(this.extensionLoader); + enableExtensions(); GeyserImpl.getInstance().getLogger().info(GeyserLocale.getLocaleStringLog("geyser.extensions.load.done", this.extensions.size())); } @Override public Extension extension(@NonNull String name) { - if (this.extensions.containsKey(name)) { - return this.extensions.get(name); - } - - return null; + return this.extensions.get(name); } @Override @@ -121,37 +108,19 @@ public class GeyserExtensionManager extends ExtensionManager { } } - @Override - public ExtensionLoader extensionLoader(@NonNull Extension extension) { - return this.extensionsLoaders.get(extension); - } - @NonNull @Override public Collection extensions() { return Collections.unmodifiableCollection(this.extensions.values()); } - @Nullable @Override - public ExtensionLoader extensionLoader(@NonNull String identifier) { - return this.extensionLoaderTypes.get(Key.key(identifier)); + public @Nullable ExtensionLoader extensionLoader() { + return this.extensionLoader; } @Override - public void registerExtensionLoader(@NonNull String identifier, @NonNull ExtensionLoader extensionLoader) { - this.extensionLoaderTypes.put(Key.key(identifier), extensionLoader); - } - - @NonNull - @Override - public Map extensionLoaders() { - return this.extensionLoaderTypes.entrySet().stream().collect(Collectors.toMap(key -> key.getKey().asString(), Map.Entry::getValue)); - } - - @Override - public void register(@NonNull Extension extension, @NonNull ExtensionLoader loader) { - this.extensionsLoaders.put(extension, loader); + public void register(@NonNull Extension extension) { this.extensions.put(extension.name(), extension); } }