/**
 * This file is part of XtraCasino, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2016 - 2016 XtraStudio <https://github.com/XtraStudio>
 * 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 com.xtra.casino;

import org.slf4j.Logger;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.service.ChangeServiceProviderEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.economy.EconomyService;

import com.google.inject.Inject;
import com.xtra.casino.serializer.GsonHandler;
import com.xtra.casino.util.SlotBlockHandler;
import com.xtra.core.Core;
import com.xtra.core.command.CommandHandler;
import com.xtra.core.listener.ListenerHandler;
import com.xtra.core.text.HelpPaginationHandler;

@Plugin(id = PluginInfo.ID, name = PluginInfo.NAME, version = PluginInfo.VERSION, description = PluginInfo.DESCRIPTION)
public class XtraCasino {

    private @Inject Logger logger;
    private static XtraCasino instance;
    private GsonHandler gsonHandler;
    private SlotBlockHandler blockHandler;
    private EconomyService economy;

    @Listener
    public void onPreInit(GamePreInitializationEvent event) {
        logger.info("Initializing XtraCasino version ", PluginInfo.VERSION);
        instance = this;
        Core.initialize(this);
    }

    @Listener
    public void onInit(GameInitializationEvent event) {
        CommandHandler.create();
        HelpPaginationHandler.create();
        this.gsonHandler = new GsonHandler();
        this.blockHandler = new SlotBlockHandler();
        ListenerHandler.registerListeners();
    }

    @Listener
    public void onEconomyChange(ChangeServiceProviderEvent event) {
        if (event.getService().equals(EconomyService.class)) {
            this.economy = (EconomyService) event.getNewProvider();
        }
    }

    public static XtraCasino instance() {
        return instance;
    }

    public Logger logger() {
        return this.logger;
    }
    
    public GsonHandler getGsonHandler() {
        return this.gsonHandler;
    }

    public SlotBlockHandler getBlockHandler() {
        return this.blockHandler;
    }

    public EconomyService getEconomy() {
        return this.economy;
    }
}

@@ -1340,74 +1340,353 @@
 api.
-event.Listener;%0Aimport org.spongepowered.api.event.game.state.Game
+command.CommandResult;%0Aimport org.spongepowered.api.command.CommandSource;%0Aimport org.spongepowered.api.command.args.CommandContext;%0Aimport org.spongepowered.api.event.Listener;%0Aimport org.spongepowered.api.event.Order;%0Aimport org.spongepowered.api.event.game.state.GameInitializationEvent;%0Aimport org.spongepowered.api.event.game.state.GamePost
 Init
@@ -1951,16 +1951,109 @@
 Service;
+%0Aimport org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.format.TextColors;
 %0A%0Aimport
@@ -2236,32 +2236,307 @@
 CommandHandler;%0A
+import com.xtra.core.command.runnable.CommandPhase;%0Aimport com.xtra.core.command.runnable.CommandRunnable;%0Aimport com.xtra.core.command.runnable.CommandRunnableHandler;%0Aimport com.xtra.core.command.runnable.CommandRunnableResult;%0Aimport com.xtra.core.command.runnable.RunAt;%0A
 import com.xtra.
@@ -3039,16 +3039,21 @@
         
+this.
 logger.i
@@ -3461,32 +3461,793 @@
 eners();%0A    %7D%0A%0A
+    @Listener(order = Order.LATE)%0A    public void onPostInit(GamePostInitializationEvent event) %7B%0A        if (this.economy == null) %7B%0A            this.logger.error(%22Economy plugin not detected! XtraCasino will not function properly!%22);%0A%0A            CommandRunnableHandler.createForAllCommands(new CommandRunnable() %7B%0A                @Override%0A                @RunAt(phase = CommandPhase.PRE)%0A                public CommandRunnableResult run(CommandSource source, CommandContext args) %7B%0A                    source.sendMessage(Text.of(TextColors.RED, %22An economy plugin has not been installed. XtraCasino cannot function properly.%22));%0A                    return new CommandRunnableResult(CommandResult.empty());%0A                %7D%0A            %7D);%0A        %7D%0A    %7D%0A%0A
     @Listener%0A  

Disable commands if an economy plugin is not installed