/*
 * This file is part of Sponge, 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.common.inject.plugin;

import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import org.slf4j.Logger;
import org.spongepowered.api.asset.Asset;
import org.spongepowered.api.asset.AssetId;
import org.spongepowered.api.event.cause.EventContextKey;
import org.spongepowered.api.network.ChannelBinding;
import org.spongepowered.api.network.ChannelId;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.scheduler.AsynchronousExecutor;
import org.spongepowered.api.scheduler.SpongeExecutorService;
import org.spongepowered.api.scheduler.SynchronousExecutor;
import org.spongepowered.common.inject.InjectionPointProvider;
import org.spongepowered.common.inject.provider.ChannelBindingProvider;
import org.spongepowered.common.inject.provider.PluginAssetProvider;
import org.spongepowered.common.inject.provider.SpongeExecutorServiceProvider;
import org.spongepowered.common.inject.provider.config.PluginConfigurationModule;

/**
 * A module installed for each plugin.
 */
public class PluginModule extends AbstractModule {

    private final PluginContainer container;
    private final Class<?> pluginClass;

    public PluginModule(final PluginContainer container, final Class<?> pluginClass) {
        this.container = container;
        this.pluginClass = pluginClass;
    }

    @Override
    protected void configure() {
        this.bind(this.pluginClass).in(Scopes.SINGLETON);

        this.install(new InjectionPointProvider());

        this.bind(PluginContainer.class).toInstance(this.container);
        this.bind(Logger.class).toInstance(this.container.getLogger());

        this.bind(SpongeExecutorService.class).annotatedWith(SynchronousExecutor.class).toProvider(SpongeExecutorServiceProvider.Synchronous.class);
        this.bind(SpongeExecutorService.class).annotatedWith(AsynchronousExecutor.class).toProvider(SpongeExecutorServiceProvider.Asynchronous.class);
        this.bind(ChannelBinding.IndexedMessageChannel.class).annotatedWith(ChannelId.class).toProvider(ChannelBindingProvider.Indexed.class);
        this.bind(ChannelBinding.RawDataChannel.class).annotatedWith(ChannelId.class).toProvider(ChannelBindingProvider.Raw.class);
        this.bind(Asset.class).annotatedWith(AssetId.class).toProvider(PluginAssetProvider.class);

        this.bindEventContextKey(this.pluginClass);

        this.install(new PluginConfigurationModule());
    }

    @SuppressWarnings("unchecked")
    private <T> void bindEventContextKey(final Class<T> klass) {
        final TypeToken<EventContextKey<T>> token = new TypeToken<EventContextKey<T>>() {}.where(new TypeParameter<T>() {}, klass);
        final TypeLiteral<EventContextKey<T>> literal = (TypeLiteral<EventContextKey<T>>) TypeLiteral.get(token.getType());
        this.bind(literal).toProvider(EventContextKey.builder(klass).id(this.container.getId()).name(this.container.getName())::build);
    }
}

@@ -1294,100 +1294,8 @@
 n;%0A%0A
-import com.google.common.reflect.TypeParameter;%0Aimport com.google.common.reflect.TypeToken;%0A
 impo
@@ -1368,46 +1368,8 @@
 es;%0A
-import com.google.inject.TypeLiteral;%0A
 impo
@@ -1479,66 +1479,8 @@
 Id;%0A
-import org.spongepowered.api.event.cause.EventContextKey;%0A
 impo
@@ -3526,599 +3526,47 @@
 his.
-bindEventContextKey(this.pluginClass);%0A%0A        this.install(new PluginConfigurationModule());%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    private %3CT%3E void bindEventContextKey(final Class%3CT%3E klass) %7B%0A        final TypeToken%3CEventContextKey%3CT%3E%3E token = new TypeToken%3CEventContextKey%3CT%3E%3E() %7B%7D.where(new TypeParameter%3CT%3E() %7B%7D, klass);%0A        final TypeLiteral%3CEventContextKey%3CT%3E%3E literal = (TypeLiteral%3CEventContextKey%3CT%3E%3E) TypeLiteral.get(token.getType());%0A        this.bind(literal).toProvider(EventContextKey.builder(klass).id(this.container.getId()).name(this.container.getName())::build
+install(new PluginConfigurationModule()
 );%0A 

Revert "Add support for injecting a plugin's EventContextKey"