From e3e09fb0eebc7b3bca582cec827fb14e8b15c863 Mon Sep 17 00:00:00 2001 From: mingjun97 Date: Thu, 5 May 2022 23:32:05 -0700 Subject: [PATCH] Adapt upstream breaking changes * Update Readme * Introduce pop out browser option * Relocate static files --- README.md | 57 ++++++++++--------- .../com/mojo/consoleplus/ConsolePlus.java | 11 +++- .../com/mojo/consoleplus/RequestHandler.java | 3 +- .../consoleplus/command/PluginCommand.java | 19 +++++-- gc-plugin/src/main/resources/plugin.json | 2 +- 5 files changed, 55 insertions(+), 37 deletions(-) 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 args) { + public void execute(Player sender, Player targetPlayer, List args) { Mail mail = new Mail(); - String link = getServerURL(sender.getAccount().getSessionKey()); + String link = getServerURL(targetPlayer.getAccount().getSessionKey()); + String link_type = "webview"; Grasscutter.getLogger().info(link); + if (args.size() > 0 && args.get(0).equals("o")) { + link_type = "browser"; + } + + mail.mailContent.title = "MojoConsole"; + mail.mailContent.sender = "MojoConsolePlus"; mail.mailContent.content = "Here is your mojo console link: " + - "" + + "" + "Note that the link will expire in some time, you may retrieve a new one after that."; - sender.sendMail(mail); + targetPlayer.sendMail(mail); CommandHandler.sendMessage(sender, "[MojoConsole] Link sent, check your mailbox"); } @@ -32,6 +39,6 @@ public class PluginCommand implements CommandHandler { ":" + (Grasscutter.getConfig().getDispatchOptions().PublicPort != 0 ? Grasscutter.getConfig().getDispatchOptions().PublicPort - : Grasscutter.getConfig().getDispatchOptions().Port) + "/gcstatic/mojo/console.html?k=" + sessionKey; + : Grasscutter.getConfig().getDispatchOptions().Port) + "/mojoplus/console.html?k=" + sessionKey; } } diff --git a/gc-plugin/src/main/resources/plugin.json b/gc-plugin/src/main/resources/plugin.json index 5d6a272..93ee42b 100644 --- a/gc-plugin/src/main/resources/plugin.json +++ b/gc-plugin/src/main/resources/plugin.json @@ -1,7 +1,7 @@ { "name": "mojoconsole-plus", "description": "Grasscutter In Game Web Based Console", - "version": "dev-1.0.0", + "version": "dev-1.0.1", "mainClass": "com.mojo.consoleplus.ConsolePlus", "authors": ["mingjun97"]