Skip to content

Replace Service Manager with Provider #2133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/main/java/org/spongepowered/api/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import org.spongepowered.api.plugin.PluginManager;
import org.spongepowered.api.registry.GameRegistry;
import org.spongepowered.api.scheduler.Scheduler;
import org.spongepowered.api.service.ServiceManager;
import org.spongepowered.api.world.ServerLocation;
import org.spongepowered.api.service.ServiceProvider;
import org.spongepowered.api.world.TeleportHelper;

import java.nio.file.Path;
Expand Down Expand Up @@ -180,18 +180,6 @@ default ConfigManager getConfigManager() {
return Sponge.getConfigManager();
}

/**
* Gets the game's instance of the service manager, which is the gateway
* to various services provided by Sponge (command registration and so on).
*
* <p>Services registered by other plugins may be available too.</p>
*
* @return The service manager
*/
default ServiceManager getServiceManager() {
return Sponge.getServiceManager();
}

/**
* Gets the {@link ChannelRegistrar} for creating network channels.
*
Expand Down Expand Up @@ -219,4 +207,17 @@ default TeleportHelper getTeleportHelper() {
default CauseStackManager getCauseStackManager() {
return Sponge.getCauseStackManager();
}

/**
* Gets the {@link ServiceProvider}, used to provide services that plugins
* may provide.
*
* <p>The provider will not be available during plugin construction and will
* throw an {@link IllegalStateException} if there is an attempt to access
* this before the provider is ready.</p>
*
* @return The service manager
*/
ServiceProvider getServiceProvider();

}
28 changes: 12 additions & 16 deletions src/main/java/org/spongepowered/api/Sponge.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.spongepowered.api.plugin.PluginManager;
import org.spongepowered.api.registry.GameRegistry;
import org.spongepowered.api.scheduler.Scheduler;
import org.spongepowered.api.service.ServiceManager;
import org.spongepowered.api.service.ServiceProvider;
import org.spongepowered.api.util.metric.MetricsConfigManager;
import org.spongepowered.api.world.ServerLocation;
import org.spongepowered.api.world.TeleportHelper;
Expand All @@ -48,7 +48,6 @@
* A static all access class granting static access to various systems
* for the API.
*/
@SuppressWarnings("NullableProblems")
public final class Sponge {

@Inject private static Game game;
Expand All @@ -59,14 +58,13 @@ public final class Sponge {
@Inject private static EventManager eventManager;
@Inject private static AssetManager assetManager;
@Inject private static ConfigManager configManager;
@Inject private static ServiceManager serviceManager;
@Inject private static ChannelRegistrar channelRegistrar;
@Inject private static TeleportHelper teleportHelper;
@Inject private static CauseStackManager causeStackManager;
@Inject private static MetricsConfigManager metricsConfigManager;
@Inject private static CommandManager commandManager;

private static <T> T check(@Nullable T instance) {
private static <T> T check(@Nullable final T instance) {
checkState(instance != null, "Sponge has not been initialized!");
return instance;
}
Expand Down Expand Up @@ -146,18 +144,6 @@ public static ConfigManager getConfigManager() {
return check(configManager);
}

/**
* Gets the game's instance of the service manager, which is the gateway
* to various services provided by Sponge (command registration and so on).
*
* <p>Services registered by other plugins may be available too.</p>
*
* @return The service manager
*/
public static ServiceManager getServiceManager() {
return check(serviceManager);
}

/**
* Gets the {@link ChannelRegistrar} for creating network channels.
*
Expand Down Expand Up @@ -251,4 +237,14 @@ public static Scheduler getAsyncScheduler() {
public static CommandManager getCommandManager() {
return check(commandManager);
}

/**
* Gets the {@link ServiceProvider} for providing services.
*
* @return The service provider.
*/
public static ServiceProvider getServiceProvider() {
return getGame().getServiceProvider();
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/spongepowered/api/event/GenericEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import com.google.common.reflect.TypeToken;

/**
* All {@link Event}s that want to one generic
* type should implement this interface.
* All {@link Event}s that require a generic type should implement this
* interface.
*
* @param <T> The generic type
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.api.projectile.source.ProjectileSource;
import org.spongepowered.api.service.ServiceManager;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.text.channel.MessageChannel;
import org.spongepowered.api.text.channel.MessageReceiver;
Expand Down Expand Up @@ -272,11 +271,6 @@ public final class EventContextKeys {
*/
public static final Supplier<EventContextKey<Vector3d>> ROTATION = Sponge.getRegistry().getCatalogRegistry().provideSupplier(EventContextKey.class, "ROTATION");

/**
* Represents the {@link ServiceManager}.
*/
public static final Supplier<EventContextKey<ServiceManager>> SERVICE_MANAGER = Sponge.getRegistry().getCatalogRegistry().provideSupplier(EventContextKey.class, "SERVICE_MANAGER");

/**
* Represents the {@link SpawnType} of an entity spawn.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* 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.
*/
package org.spongepowered.api.event.service;

import org.spongepowered.api.event.GenericEvent;

import java.util.function.Supplier;

/**
* An event that allows plugins to suggest their own implementation for a given
* service.
*
* <p>Service providers should <strong>not</strong> construct the service object
* prior to selection. Instead, they should wait for if, and only if, their
* service factory as supplied in {@link #suggest(Supplier)} has been called.
* Further, each plugin may only supply <strong>one</strong> service provider
* for each service.</p>
*
* <p>It is not guaranteed that this event will fire for the indicated service
* for a plugin that registers this listener. This may happen if the server
* is configured to select a particular service, or that another plugin has
* already been offered the chance to provide the implementation and has done
* so.</p>
*
* @param <T> The service to provide.
*/
public interface ProvideServiceEvent<T> extends GenericEvent<T> {

/**
* Provides a suggestion for the given service. <strong>This may only be
* called once by any given plugin for a given service.</strong>
*
* @param serviceFactory A {@link Supplier} that can construct the service
* if this service is selected
*/
void suggest(Supplier<T> serviceFactory);

}
Loading