Only register commands on Spigot if the extension has commands

This commit is contained in:
Camotoy 2022-09-14 14:19:56 -04:00
parent a99afe4418
commit 6df8740955
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
7 changed files with 21 additions and 92 deletions

View file

@ -124,7 +124,7 @@ public interface Extension extends EventRegistrar {
*/ */
@NonNull @NonNull
default ExtensionLoader extensionLoader() { default ExtensionLoader extensionLoader() {
return Objects.requireNonNull(this.extensionManager().extensionLoader(this)); return Objects.requireNonNull(this.extensionManager().extensionLoader());
} }
/** /**

View file

@ -34,7 +34,6 @@ import java.nio.file.Path;
* The extension loader is responsible for loading, unloading, enabling and disabling extensions * The extension loader is responsible for loading, unloading, enabling and disabling extensions
*/ */
public abstract class ExtensionLoader { public abstract class ExtensionLoader {
/** /**
* Gets if the given {@link Extension} is enabled. * Gets if the given {@link Extension} is enabled.
* *
@ -101,6 +100,6 @@ public abstract class ExtensionLoader {
* @param extensionManager the extension manager * @param extensionManager the extension manager
*/ */
protected void register(@NonNull Extension extension, @NonNull ExtensionManager extensionManager) { protected void register(@NonNull Extension extension, @NonNull ExtensionManager extensionManager) {
extensionManager.register(extension, this); extensionManager.register(extension);
} }
} }

View file

@ -29,7 +29,6 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
/** /**
* Manages Geyser {@link Extension}s * Manages Geyser {@link Extension}s
@ -59,15 +58,6 @@ public abstract class ExtensionManager {
*/ */
public abstract void disable(@NonNull Extension extension); 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. * Gets all the {@link Extension}s currently loaded.
* *
@ -77,37 +67,19 @@ public abstract class ExtensionManager {
public abstract Collection<Extension> extensions(); public abstract Collection<Extension> extensions();
/** /**
* Gets the {@link ExtensionLoader} with the given identifier. * Gets the {@link ExtensionLoader}.
* *
* @param identifier the identifier * @return the extension loader
* @return the extension loader at the given identifier
*/ */
@Nullable @Nullable
public abstract ExtensionLoader extensionLoader(@NonNull String identifier); public abstract ExtensionLoader extensionLoader();
/**
* 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<String, ExtensionLoader> extensionLoaders();
/** /**
* Registers an {@link Extension} with the given {@link ExtensionLoader}. * Registers an {@link Extension} with the given {@link ExtensionLoader}.
* *
* @param extension the extension * @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}. * Loads all extensions from the given {@link ExtensionLoader}.

View file

@ -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.command.SpigotCommandSource;
import org.geysermc.geyser.platform.spigot.world.GeyserPistonListener; import org.geysermc.geyser.platform.spigot.world.GeyserPistonListener;
import org.geysermc.geyser.platform.spigot.world.GeyserSpigotBlockPlaceListener; 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.*;
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.text.GeyserLocale;
import org.geysermc.geyser.util.FileUtils; import org.geysermc.geyser.util.FileUtils;
@ -204,12 +199,14 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserBootstrap {
} }
}, this); }, this);
this.geyserCommandManager = new GeyserSpigotCommandManager(geyser);
this.geyserCommandManager.init();
// Because Bukkit locks its command map upon startup, we need to // Because Bukkit locks its command map upon startup, we need to
// add our plugin commands in onEnable, but populating the executor // add our plugin commands in onEnable, but populating the executor
// can happen at any time // can happen at any time
CommandMap commandMap = GeyserSpigotCommandManager.getCommandMap(); CommandMap commandMap = GeyserSpigotCommandManager.getCommandMap();
for (Extension extension : this.geyser.extensionManager().extensions()) { for (Extension extension : this.geyserCommandManager.extensionCommands().keySet()) {
// Thanks again, Bukkit // Thanks again, Bukkit
try { try {
Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); Constructor<PluginCommand> 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())); 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; boolean isViaVersion = Bukkit.getPluginManager().getPlugin("ViaVersion") != null;
if (isViaVersion) { if (isViaVersion) {
try { try {

View file

@ -147,7 +147,7 @@ public class GeyserImpl implements GeyserApi {
private final GeyserBootstrap bootstrap; private final GeyserBootstrap bootstrap;
private final EventBus<EventRegistrar> eventBus; private final EventBus<EventRegistrar> eventBus;
private GeyserExtensionManager extensionManager; private final GeyserExtensionManager extensionManager;
private Metrics metrics; private Metrics metrics;
@ -173,6 +173,7 @@ public class GeyserImpl implements GeyserApi {
/* Load Extensions */ /* Load Extensions */
this.extensionManager = new GeyserExtensionManager(); this.extensionManager = new GeyserExtensionManager();
this.extensionManager.init(); this.extensionManager.init();
this.eventBus.fire(new GeyserPreInitializeEvent(this.extensionManager, this.eventBus));
} }
public void initialize() { public void initialize() {
@ -186,11 +187,6 @@ public class GeyserImpl implements GeyserApi {
logger.info(""); logger.info("");
logger.info("******************************************"); logger.info("******************************************");
/* Enable extensions */
this.extensionManager.enableExtensions();
this.eventBus.fire(new GeyserPreInitializeEvent(this.extensionManager, this.eventBus));
/* Initialize registries */ /* Initialize registries */
Registries.init(); Registries.init();
BlockRegistries.init(); BlockRegistries.init();

View file

@ -33,7 +33,6 @@ import org.geysermc.geyser.api.command.Command;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -49,7 +48,7 @@ public abstract class GeyserCommand implements Command {
protected final String description; protected final String description;
protected final String permission; protected final String permission;
private List<String> aliases = new ArrayList<>(); private List<String> aliases = Collections.emptyList();
public abstract void execute(@Nullable GeyserSession session, GeyserCommandSource sender, String[] args); public abstract void execute(@Nullable GeyserSession session, GeyserCommandSource sender, String[] args);

View file

@ -25,8 +25,6 @@
package org.geysermc.geyser.extension; 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.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
@ -39,34 +37,23 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
public class GeyserExtensionManager extends ExtensionManager { public class GeyserExtensionManager extends ExtensionManager {
private static final Key BASE_EXTENSION_LOADER_KEY = Key.key("geysermc", "base"); private final GeyserExtensionLoader extensionLoader = new GeyserExtensionLoader();
private final Map<Key, ExtensionLoader> extensionLoaderTypes = new Object2ObjectOpenHashMap<>();
private final Map<String, Extension> extensions = new LinkedHashMap<>(); private final Map<String, Extension> extensions = new LinkedHashMap<>();
private final Map<Extension, ExtensionLoader> extensionsLoaders = new LinkedHashMap<>();
public void init() { public void init() {
GeyserImpl.getInstance().getLogger().info(GeyserLocale.getLocaleStringLog("geyser.extensions.load.loading")); GeyserImpl.getInstance().getLogger().info(GeyserLocale.getLocaleStringLog("geyser.extensions.load.loading"));
extensionLoaderTypes.put(BASE_EXTENSION_LOADER_KEY, new GeyserExtensionLoader()); loadAllExtensions(this.extensionLoader);
for (ExtensionLoader loader : this.extensionLoaders().values()) { enableExtensions();
this.loadAllExtensions(loader);
}
GeyserImpl.getInstance().getLogger().info(GeyserLocale.getLocaleStringLog("geyser.extensions.load.done", this.extensions.size())); GeyserImpl.getInstance().getLogger().info(GeyserLocale.getLocaleStringLog("geyser.extensions.load.done", this.extensions.size()));
} }
@Override @Override
public Extension extension(@NonNull String name) { public Extension extension(@NonNull String name) {
if (this.extensions.containsKey(name)) { return this.extensions.get(name);
return this.extensions.get(name);
}
return null;
} }
@Override @Override
@ -121,37 +108,19 @@ public class GeyserExtensionManager extends ExtensionManager {
} }
} }
@Override
public ExtensionLoader extensionLoader(@NonNull Extension extension) {
return this.extensionsLoaders.get(extension);
}
@NonNull @NonNull
@Override @Override
public Collection<Extension> extensions() { public Collection<Extension> extensions() {
return Collections.unmodifiableCollection(this.extensions.values()); return Collections.unmodifiableCollection(this.extensions.values());
} }
@Nullable
@Override @Override
public ExtensionLoader extensionLoader(@NonNull String identifier) { public @Nullable ExtensionLoader extensionLoader() {
return this.extensionLoaderTypes.get(Key.key(identifier)); return this.extensionLoader;
} }
@Override @Override
public void registerExtensionLoader(@NonNull String identifier, @NonNull ExtensionLoader extensionLoader) { public void register(@NonNull Extension extension) {
this.extensionLoaderTypes.put(Key.key(identifier), extensionLoader);
}
@NonNull
@Override
public Map<String, ExtensionLoader> 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);
this.extensions.put(extension.name(), extension); this.extensions.put(extension.name(), extension);
} }
} }