@Override
public <T> void setProvider(Object plugin, Class<T> service, T provider) {
checkNotNull(plugin, "plugin");
checkNotNull(service, "service");
checkNotNull(provider, "provider");
Optional<PluginContainer> containerOptional = this.pluginManager.fromInstance(plugin);
if (!containerOptional.isPresent()) {
throw new IllegalArgumentException(
"The provided plugin object does not have an associated plugin container "
+ "(in other words, is 'plugin' actually your plugin object?)");
}
PluginContainer container = containerOptional.get();
ProviderRegistration<?> oldProvider = this.providers.put(service, new Provider<>(container, service, provider));
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContextKeys.SERVICE_MANAGER, this);
frame.pushCause(container);
Sponge.getEventManager().post(SpongeEventFactory.createChangeServiceProviderEvent(frame.getCurrentCause(),
this.providers.get(service), Optional.ofNullable(oldProvider)));
}
}