package io.luna.net.msg.in;

import io.luna.LunaContext;
import io.luna.game.event.Event;
import io.luna.game.event.impl.CommandEvent;
import io.luna.game.model.mob.Player;
import io.luna.game.model.mob.PlayerRights;
import io.luna.game.plugin.PluginBootstrap;
import io.luna.net.msg.GameMessage;
import io.luna.net.msg.GameMessageReader;
import io.luna.net.msg.out.GameChatboxMessageWriter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.function.Consumer;

/**
 * A {@link GameMessageReader} implementation that decodes data sent when a {@link Player} tries to
 * activate a command.
 *
 * @author lare96 <http://github.org/lare96>
 */
public final class CommandMessageReader extends GameMessageReader {

    /**
     * The asynchronous logger.
     */
    private static final Logger LOGGER = LogManager.getLogger();

    @Override
    public Event read(Player player, GameMessage msg) throws Exception {
        String string = msg.getPayload().getString();
        string = string.toLowerCase();
        int index = string.indexOf(' ');

        // Has to be done in Java because of classloader conflicts.
        PlayerRights rights = player.getRights();
        if (string.equals("hotfix") && rights.equalOrGreater(PlayerRights.DEVELOPER)) {
            startHotfix(player);
            return null;
        }

        if (index == -1) {
            return new CommandEvent(player, string);
        }

        String name = string.substring(0, index);
        String[] args = string.substring(index + 1).split(" ");
        return new CommandEvent(player, name, args);
    }

    /**
     * Constructs and loads another bootstrap instance.
     *
     * @param player The player.
     */
    private void startHotfix(Player player) {
        LunaContext ctx = player.getContext();
        Consumer<String> sendMessage = msg ->
                player.queue(new GameChatboxMessageWriter(msg));

        sendMessage.accept("Hotfix request received.");
        ctx.getService().submit(() -> {
            try {
                PluginBootstrap bootstrap = new PluginBootstrap(ctx);
                bootstrap.init(false);
                sendMessage.accept("Hotfix request finished successfully.");
            } catch (Exception e) {
                sendMessage.accept("Hotfix request failed. Please check console for error message.");
                LOGGER.catching(e);
            }
        });
    }
}

@@ -26,36 +26,8 @@
 n;%0A%0A
-import io.luna.LunaContext;%0A
 impo
@@ -157,329 +157,67 @@
 una.
-game.model.mob.PlayerRights;%0Aimport io.luna.game.plugin.PluginBootstrap;%0Aimport io.luna.net.msg.GameMessage;%0Aimport io.luna.net.msg.GameMessageReader;%0Aimport io.luna.net.msg.out.GameChatboxMessageWriter;%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0Aimport java.util.function.Consum
+net.msg.GameMessage;%0Aimport io.luna.net.msg.GameMessageRead
 er;%0A
@@ -469,122 +469,8 @@
  %7B%0A%0A
-    /**%0A     * The asynchronous logger.%0A     */%0A    private static final Logger LOGGER = LogManager.getLogger();%0A%0A
     
@@ -536,25 +536,8 @@
 msg)
- throws Exception
  %7B%0A 
@@ -682,1354 +682,255 @@
     
-// Has to be done in Java because of classloader conflicts.%0A        PlayerRights rights = player.getRights();%0A        if (string.equals(%22hotfix%22) && rights.equalOrGreater(PlayerRights.DEVELOPER)) %7B%0A            startHotfix(player);%0A            return null;%0A        %7D%0A%0A        if (index == -1) %7B%0A            return new CommandEvent(player, string);%0A        %7D%0A%0A        String name = string.substring(0, index);%0A        String%5B%5D args = string.substring(index + 1).split(%22 %22);%0A        return new CommandEvent(player, name, args);%0A    %7D%0A%0A    /**%0A     * Constructs and loads another bootstrap instance.%0A     *%0A     * @param player The player.%0A     */%0A    private void startHotfix(Player player) %7B%0A        LunaContext ctx = player.getContext();%0A        Consumer%3CString%3E sendMessage = msg -%3E%0A                player.queue(new GameChatboxMessageWriter(msg));%0A%0A        sendMessage.accept(%22Hotfix request received.%22);%0A        ctx.getService().submit(() -%3E %7B%0A            try %7B%0A                PluginBootstrap bootstrap = new PluginBootstrap(ctx);%0A                bootstrap.init(false);%0A                sendMessage.accept(%22Hotfix request finished successfully.%22);%0A            %7D catch (Exception e) %7B%0A                sendMessage.accept(%22Hotfix request failed. Please check console for error message.%22);%0A                LOGGER.catching(e);%0A            %7D%0A        %7D
+if (index == -1) %7B%0A            return new CommandEvent(player, string);%0A        %7D%0A%0A        String name = string.substring(0, index);%0A        String%5B%5D args = string.substring(index + 1).split(%22 %22);%0A        return new CommandEvent(player, name, args
 );%0A 

Remove hotfixing command