diff --git a/README.md b/README.md index 4e4d097..ee10b3c 100644 --- a/README.md +++ b/README.md @@ -31,23 +31,24 @@ See realeases. 5. Navigate to your ``Grasscutter`` server, find the ``plugins`` folder and paste the ``mojoconsole.jar`` into it. 6. Start your server. -7. Put the all the frontend files into the folder `GRASSCUTTER_RESOURCE/gcstatic/mojo/console.html`. +...Jump to next section... -8. Send command `/mojoconsole` or `/mojo` to server in game, and you will receive mail in your mailbox. Then follow the instructions there. +### Usage + +7. Put the all the frontend files into the folder `GRASSCUTTER_RESOURCE/plugins/mojoconsole/`. Note that your must have `console.html` for now. You are free to put any other dynamiclly loaded file(e.g. `.js`, `.css`) in that folder. Check the last section for current avialable frontend. + +8. Send command `/mojoconsole` or `/mojo` to server in game, and you will receive mail in your mailbox. Then follow the instructions there. Note that you may use `o` option to use the pop out browser instead of in-game webwiew, e.g. `/mojo o`. By default, the console is in-game webview. Your final plugins folder's directory structure should look similar to this ``` plugins │ mojoconsole.jar │ ... -resources -└───gcstatic - │ ... - └───mojo - │ console.html - | ... - └───any other file that you want to include in your frontend - │ ... +└───mojoconsole + │ console.html + | ... + └───any other file that you want to include in your frontend + │ ... ``` @@ -78,24 +79,24 @@ You can use the following function to send the request, just plug it after you f ```javascript function sendCommand(payload){ - var client = new XMLHttpRequest(); - var key = new window.URLSearchParams(window.location.search).get("k"); - var url = '/mojoplus/api'; - client.open("POST", url, true); - client.setRequestHeader("Content-Type", "application/json"); - client.onreadystatechange = function () { - if (client.readyState === 4 && client.status === 200) { - var result = document.getElementById("c2"); - // Print received data from server - result.innerHTML = JSON.parse(this.responseText).payload.replace(/\n/g, "
"); - } - }; - - // Converting JSON data to string - var data = JSON.stringify({ "k": key, "request": "invoke", "payload": payload }); - // Sending data with the request - client.send(data); - } + var client = new XMLHttpRequest(); + var key = new window.URLSearchParams(window.location.search).get("k"); + var url = '/mojoplus/api'; + client.open("POST", url, true); + client.setRequestHeader("Content-Type", "application/json"); + client.onreadystatechange = function () { + if (client.readyState === 4 && client.status === 200) { + var result = document.getElementById("c2"); + // Print received data from server + result.innerHTML = JSON.parse(this.responseText).payload.replace(/\n/g, ""); + } + }; + + // Converting JSON data to string + var data = JSON.stringify({ "k": key, "request": "invoke", "payload": payload }); + // Sending data with the request + client.send(data); +} ``` ### Frontend diff --git a/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java b/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java index 0302d8d..dffce26 100644 --- a/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java +++ b/gc-plugin/src/main/java/com/mojo/consoleplus/ConsolePlus.java @@ -4,9 +4,12 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.command.CommandMap; import emu.grasscutter.plugin.Plugin; +import java.io.File; + import com.mojo.consoleplus.command.PluginCommand; import express.Express; +import io.javalin.http.staticfiles.Location; public class ConsolePlus extends Plugin{ @Override @@ -17,8 +20,14 @@ public class ConsolePlus extends Plugin{ @Override public void onEnable() { Express app = Grasscutter.getDispatchServer().getServer(); + String folder_name = Grasscutter.getConfig().PLUGINS_FOLDER + "/mojoconsole/"; + File folder = new File(folder_name); + if (!folder.exists()) { + Grasscutter.getLogger().warn("To make mojo console works, you have to put your frontend file(console.html) inside" + folder.getAbsolutePath()); + folder.mkdirs(); + } app.post("/mojoplus/api", new RequestHandler()); - + app.raw().config.addStaticFiles("/mojoplus", folder_name, Location.EXTERNAL); CommandMap.getInstance().registerCommand("mojoconsole", new PluginCommand()); Grasscutter.getLogger().info("mojo console enabled"); } diff --git a/gc-plugin/src/main/java/com/mojo/consoleplus/RequestHandler.java b/gc-plugin/src/main/java/com/mojo/consoleplus/RequestHandler.java index be7cedc..f5bedc7 100644 --- a/gc-plugin/src/main/java/com/mojo/consoleplus/RequestHandler.java +++ b/gc-plugin/src/main/java/com/mojo/consoleplus/RequestHandler.java @@ -44,7 +44,8 @@ public final class RequestHandler implements HttpContextHandler { switch (request.request){ case "invoke": try{ - CommandMap.getInstance().invoke(player, request.payload); + // TODO: Enable execut commands to third party + CommandMap.getInstance().invoke(player, player, request.payload); } catch (Exception e) { res.json(new ResponseJson("error", 500, e.getStackTrace().toString())); break; diff --git a/gc-plugin/src/main/java/com/mojo/consoleplus/command/PluginCommand.java b/gc-plugin/src/main/java/com/mojo/consoleplus/command/PluginCommand.java index 48a54e3..a2c779a 100644 --- a/gc-plugin/src/main/java/com/mojo/consoleplus/command/PluginCommand.java +++ b/gc-plugin/src/main/java/com/mojo/consoleplus/command/PluginCommand.java @@ -8,18 +8,25 @@ import emu.grasscutter.command.CommandHandler; import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.player.Player; -@Command(label = "mojoconsole", usage = "mojoconsole", description = "Generate Mojoconsole link (no arguments required)", aliases = { +@Command(label = "mojoconsole", usage = "mojoconsole", description = "Send Mojoconsole link via mail (by default it's in-game webview, but you may use argument `o` for popping out external browser)", aliases = { "mojo" }, permission = "mojo.console") public class PluginCommand implements CommandHandler { @Override - public void execute(Player sender, List