package io.luna.net.msg.in;

import io.luna.LunaContext;
import io.luna.game.GameService;
import io.luna.game.event.Event;
import io.luna.game.event.impl.CommandEvent;
import io.luna.game.model.mobile.Player;
import io.luna.game.plugin.PluginBootstrap;
import io.luna.game.plugin.PluginManager;
import io.luna.net.msg.GameMessage;
import io.luna.net.msg.InboundMessageReader;

/**
 * An {@link InboundMessageReader} 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 InboundMessageReader {

    @Override
    public Event read(Player player, GameMessage msg) throws Exception {
        String string = msg.getPayload().getString();
        string = string.toLowerCase();
        int index = string.indexOf(' ');
        if (index == -1) {
            return new CommandEvent(string);
        }

        String name = string.substring(0, index);
        String[] args = string.substring(index + 1).split(" ");

        // Has to be done in Java because of classpath conflicts.
        if (name.equals("reloadplugins")) {
            // TODO: implement some sort of additional cache that will allow for "hot fixes" without potentially interrupting
            // gameplay. for now just clear plugin cache and do async reload of bootstrap
            LunaContext ctx = player.getContext();
            PluginManager plugins = ctx.getPlugins();
            GameService service = ctx.getService();
            plugins.clear();
            service.execute(new PluginBootstrap(ctx));
            return null;
        }

        return new CommandEvent(name, args);
    }
}

@@ -26,69 +26,8 @@
 n;%0A%0A
-import io.luna.LunaContext;%0Aimport io.luna.game.GameService;%0A
 impo
@@ -165,30 +165,33 @@
 ame.
-plugin.PluginBootstrap
+model.mobile.PlayerRights
 ;%0Aim
@@ -215,27 +215,22 @@
 .plugin.
-PluginManag
+Hotfix
 er;%0Aimpo
@@ -305,16 +305,69 @@
 eReader;
+%0Aimport io.luna.net.msg.out.GameChatboxMessageWriter;
 %0A%0A/**%0A *
@@ -833,16 +833,251 @@
 f(' ');%0A
+%0A        // Has to be done in Java because of classpath conflicts.%0A        if (string.equals(%22hotfix%22) && player.getRights().equalOrGreater(PlayerRights.DEVELOPER)) %7B%0A            initHotfix(player);%0A            return null;%0A        %7D%0A%0A
         
@@ -1137,16 +1137,36 @@
 t(string
+, player.getRights()
 );%0A     
@@ -1285,25 +1285,24 @@
 t(%22 %22);%0A
-%0A
         
 // Has t
@@ -1297,439 +1297,385 @@
     
-// Has to be done in Java because of classpath conflicts.%0A        if (name.equals(%22reloadplugins%22)) %7B
+return new CommandEvent(name, args, player.getRights());
 %0A    
+%7D%0A%0A
     
-    // TODO: implement some sort of additional cache that will allow for %22hot fixes%22 without potentially interrupting%0A            // gameplay. for now just clear plugin cache and do async reload of bootstrap
+/**%0A     * Initiates a bootstrapping process in which all plugins will be 'reloaded' dynamically. Also popularly known as%0A     * 'hotfixing'.
 %0A     
-   
+*/%0A
     
-LunaContext ctx = player.getContext();%0A            PluginManager plugins = ctx.getPlugins(
+private void initHotfix(Player player) %7B%0A        player.queue(new GameChatboxMessageWriter(%22Hotfix request received, initializing Hotfixer...%22)
 );%0A
+%0A
         
     
@@ -1674,214 +1674,164 @@
     
-    GameService service = ctx.getService();%0A            plugins.clear();%0A            service.execute(new PluginBootstrap(ctx));%0A            return null;%0A        %7D%0A%0A        return new CommandEvent(name, args
+Hotfixer.newHotfixer(player.getContext()).%0A            addListener(it -%3E player.queue(new GameChatboxMessageWriter(%22Hotfix successfully completed!%22))).init(
 );%0A 

Send player a message when doing ::hotfix