Adapt upstream breaking changes

* Update Readme
* Introduce pop out browser option
* Relocate static files
This commit is contained in:
mingjun97 2022-05-05 23:32:05 -07:00
parent d997c5d3d8
commit e3e09fb0ee
5 changed files with 55 additions and 37 deletions

View File

@ -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, "<p/>");
}
};
// 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, "<p/>");
}
};
// Converting JSON data to string
var data = JSON.stringify({ "k": key, "request": "invoke", "payload": payload });
// Sending data with the request
client.send(data);
}
```
### Frontend

View File

@ -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");
}

View File

@ -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;

View File

@ -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<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> 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: " +
"<type=\"webview\" text=\"Mojo Console\" href=\"" + link + "\"/>" +
"<type=\""+ link_type + "\" text=\"Mojo Console\" href=\"" + link + "\"/>" +
"Note that the link will <b>expire</b> 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;
}
}

View File

@ -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"]