/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered.org <http://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.mod;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Guice;
import com.google.inject.Injector;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.DummyModContainer;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.LoadController;
import net.minecraftforge.fml.common.ModContainerFactory;
import net.minecraftforge.fml.common.ModMetadata;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.Type;
import org.spongepowered.api.Game;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.mod.command.CommandSponge;
import org.spongepowered.mod.configuration.SpongeConfig;
import org.spongepowered.api.service.ProviderExistsException;
import org.spongepowered.api.service.command.CommandService;
import org.spongepowered.api.service.command.SimpleCommandService;
import org.spongepowered.mod.event.SpongeEventHooks;
import org.spongepowered.mod.guice.SpongeGuiceModule;
import org.spongepowered.mod.plugin.SpongePluginContainer;
import org.spongepowered.mod.registry.SpongeGameRegistry;
import org.spongepowered.mod.util.SpongeHooks;

import java.io.File;

public class SpongeMod extends DummyModContainer implements PluginContainer {

    public static SpongeMod instance;

    private final Game game;
    private Injector spongeInjector = Guice.createInjector(new SpongeGuiceModule());
    private Logger logger = LogManager.getLogger("SpongeAPIMod");
    private LoadController controller;
    private SpongeGameRegistry registry;
    private SpongeConfig globalConfig;
    private File configFile;

    // This is a special Mod, provided by the IFMLLoadingPlugin. It will be instantiated before FML scans the system
    // For mods (or plugins)
    public SpongeMod() {
        super(new ModMetadata());
        // Register our special instance creator with FML
        ModContainerFactory.instance().registerContainerType(Type.getType(Plugin.class), SpongePluginContainer.class);

        this.getMetadata().name = "SpongeAPIMod";
        this.getMetadata().modId = "SpongeAPIMod";
        SpongeMod.instance = this;
        this.game = this.spongeInjector.getInstance(Game.class);
        this.registry = (SpongeGameRegistry) this.game.getRegistry();
    }

    @Override
    public Object getMod() {
        return this;
    }

    public Game getGame() {
        return this.game;
    }

    public Injector getInjector() {
        return this.spongeInjector;
    }

    public LoadController getController() {
        return this.controller;
    }

    public Logger getLogger() {
        return this.logger;
    }

    public SpongeConfig getGlobalConfig() {
        return this.globalConfig;
    }

    public File getSuggestedConfigFile() {
        return this.configFile;
    }

    @Override
    public boolean registerBus(EventBus bus, LoadController controller) {
        bus.register(this);
        this.controller = controller;
        return true;
    }

    @Subscribe
    public void onPreInit(FMLPreInitializationEvent e) {
        MinecraftForge.EVENT_BUS.register(new SpongeEventHooks());

        // Add the SyncScheduler as a listener for ServerTickEvents
        FMLCommonHandler.instance().bus().register(this.getGame().getSyncScheduler());
        this.configFile = e.getSuggestedConfigurationFile();
        this.globalConfig = new SpongeConfig(SpongeConfig.Type.GLOBAL, "global.cfg");
        if (e.getSide() == Side.SERVER) {
            SpongeHooks.enableThreadContentionMonitoring();
        }
    }

    @Subscribe
    public void onInitialization(FMLInitializationEvent e) {
        this.registry.init();
        try {
            getGame().getServiceManager().setProvider(this, CommandService.class, new SimpleCommandService(getGame().getPluginManager()));
        } catch (ProviderExistsException e1) {
            this.logger.warn("Non-Sponge CommandService already registered: " + e1.getLocalizedMessage());
        }
    }

    @Subscribe
    public void onInitialization(FMLPostInitializationEvent e) {
        this.registry.postInit();
    }

    @Subscribe
    public void onServerStarting(FMLServerStartingEvent e) {
        e.registerServerCommand(new CommandSponge());
    }

    @Override
    public String getId() {
        return getModId();
    }

    @Override
    public Object getInstance() {
        return getMod();
    }
}

@@ -4063,24 +4063,341 @@
 Registry();%0A
+        try %7B%0A            this.game.getServiceManager().setProvider(this, CommandService.class, new SimpleCommandService(this.game.getPluginManager()));%0A        %7D catch (ProviderExistsException e1) %7B%0A            this.logger.warn(%22Non-Sponge CommandService already registered: %22 + e1.getLocalizedMessage());%0A        %7D%0A
     %7D%0A%0A    @
@@ -5772,325 +5772,8 @@
 ();%0A
-        try %7B%0A            getGame().getServiceManager().setProvider(this, CommandService.class, new SimpleCommandService(getGame().getPluginManager()));%0A        %7D catch (ProviderExistsException e1) %7B%0A            this.logger.warn(%22Non-Sponge CommandService already registered: %22 + e1.getLocalizedMessage());%0A        %7D%0A
     

Fix CommandService provider registration.