From c6a2cee050e43d056aea74908afe7ad1a048a023 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Sat, 11 Mar 2023 23:56:49 -0500 Subject: [PATCH] Add automatic destination registration through DestinationFeature. --- .../MultiverseCore/MultiverseCore.java | 12 ----- .../MultiverseCore/api/Destination.java | 3 +- .../inject/features/DestinationFeature.java | 47 +++++++++++++++++++ 3 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/inject/features/DestinationFeature.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 5ef6eb8b..eefc90a6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -16,13 +16,11 @@ import java.util.stream.Collectors; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.anchor.AnchorManager; -import com.onarandombox.MultiverseCore.api.Destination; import com.onarandombox.MultiverseCore.api.MVCore; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; -import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.inject.PluginInjection; import com.onarandombox.MultiverseCore.utils.TestingMode; import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; @@ -117,7 +115,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore { // TODO consider moving this into the AnchorManager constructor serviceLocator.getService(AnchorManager.class).loadAnchors(); this.setUpLocales(); - this.registerDestinations(); this.setupMetrics(); this.saveMVConfig(); this.logEnableMessage(); @@ -167,15 +164,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore { commandManager.getLocales().addMessageBundles("multiverse-core"); } - /** - * Register all the destinations. - */ - private void registerDestinations() { - // TODO add automatic destination registration through hk2 - var destinationsProvider = serviceLocator.getService(DestinationsProvider.class); - serviceLocator.getAllServices(Destination.class).forEach(destinationsProvider::registerDestination); - } - /** * Setup bstats Metrics. */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/Destination.java b/src/main/java/com/onarandombox/MultiverseCore/api/Destination.java index 8fd8d17f..1329fd87 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/Destination.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/Destination.java @@ -3,12 +3,13 @@ package com.onarandombox.MultiverseCore.api; import java.util.Collection; import co.aikar.commands.BukkitCommandIssuer; +import com.onarandombox.MultiverseCore.inject.AutoLoadedService; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jvnet.hk2.annotations.Contract; @Contract -public interface Destination { +public interface Destination extends AutoLoadedService { /** * Returns the identifier or prefix that is required for this destination. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/inject/features/DestinationFeature.java b/src/main/java/com/onarandombox/MultiverseCore/inject/features/DestinationFeature.java new file mode 100644 index 00000000..bef20cdc --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/inject/features/DestinationFeature.java @@ -0,0 +1,47 @@ +package com.onarandombox.MultiverseCore.inject.features; + +import com.onarandombox.MultiverseCore.api.Destination; +import com.onarandombox.MultiverseCore.destination.DestinationsProvider; +import com.onarandombox.MultiverseCore.inject.InjectionFeature; +import com.onarandombox.MultiverseCore.inject.registration.AbstractAutoRegistration; +import com.onarandombox.MultiverseCore.inject.registration.AutoRegistrationBinder; +import com.onarandombox.MultiverseCore.inject.registration.RegistrationFilter; +import jakarta.inject.Inject; +import jakarta.inject.Provider; +import org.glassfish.hk2.api.ServiceLocator; +import org.glassfish.hk2.utilities.ServiceLocatorUtilities; +import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; + +@Service +class DestinationFeature implements InjectionFeature { + + @Override + public void postServicesCreation(ServiceLocator locator) { + ServiceLocatorUtilities.bind(locator, AutoRegistrationBinder.with(DestinationAutoRegistration.class)); + } + + @Service + private static final class DestinationAutoRegistration extends AbstractAutoRegistration { + + private final @NotNull Provider destinationsProviderProvider; + + @Inject + private DestinationAutoRegistration( + @NotNull Provider destinationsProviderProvider, + @NotNull Provider filterProvider + ) { + super(filterProvider, Destination.class); + this.destinationsProviderProvider = destinationsProviderProvider; + } + + private DestinationsProvider getDestinationsProvider() { + return destinationsProviderProvider.get(); + } + + @Override + protected void register(Destination instance) { + getDestinationsProvider().registerDestination(instance); + } + } +}