feat: add bungee support & ProxyClass

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-01-17 09:17:13 +08:00
parent 1c21792572
commit 0b0cea9e50
9 changed files with 141 additions and 8 deletions

14
pom.xml
View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<developers>
<developer>
<id>502647092</id>
@ -54,6 +54,8 @@
<properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<update.changes>
§620-01-14 §afeat: 新增 Bungee 支持;
      §afeat: 新增 instance 实例获取;
§619-09-24 §cremove: 移除 okhttp3 类库;
      §afeat: 新增require自动下载模块功能;
§619-09-21 §afeat: 新增 okhttp3 类库;
@ -175,5 +177,15 @@
<artifactId>spongeapi</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.12-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.nukkit</groupId>
<artifactId>nukkit</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -15,10 +15,24 @@ import lombok.val;
* Created on 2017/10/9 12:40.
*/
public class Base {
private Object instance;
public Base(Object instance) {
this.instance = instance;
}
public Class getClass(String name) throws ClassNotFoundException {
return Class.forName(name);
}
public Object getInstance() {
return this.instance;
}
public Class getProxyClass() {
return ProxyClass.class;
}
public String read(String path) throws IOException {
return new String(Files.readAllBytes(new File(path).toPath()), "UTF-8");
}

View File

@ -2,7 +2,6 @@ package pw.yumc.MiaoScript;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.Thread;
import lombok.SneakyThrows;
/**
@ -18,7 +17,7 @@ public class MiaoScript extends JavaPlugin {
@SneakyThrows
public void onEnable() {
Thread.currentThread().setContextClassLoader(getClassLoader());
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger());
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger(), this);
engine.enableEngine();
}

View File

@ -0,0 +1,26 @@
package pw.yumc.MiaoScript;
import lombok.SneakyThrows;
import net.md_5.bungee.api.plugin.Plugin;
/**
* Created with IntelliJ IDEA
*
* @author MiaoWoo
* Created on 2020/1/14 16:02.
*/
public class MiaoScriptBungee extends Plugin {
private ScriptEngine engine;
@Override
@SneakyThrows
public void onEnable() {
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger(), this);
engine.enableEngine();
}
@Override
public void onDisable() {
engine.disableEngine();
}
}

View File

@ -0,0 +1,23 @@
package pw.yumc.MiaoScript;
import cn.nukkit.plugin.PluginBase;
import lombok.SneakyThrows;
/**
* @author MiaoWoo
*/
public class MiaoScriptNukkit extends PluginBase {
private ScriptEngine engine;
@Override
@SneakyThrows
public void onEnable() {
engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger(), this);
engine.enableEngine();
}
@Override
public void onDisable() {
engine.disableEngine();
}
}

View File

@ -5,9 +5,9 @@ import java.io.File;
import org.slf4j.Logger;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.plugin.Plugin;
import com.google.inject.Inject;
@ -32,7 +32,7 @@ public class MiaoScriptSponge {
@Listener
@SneakyThrows
public void onStart(GameStartedServerEvent event) {
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger);
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger, this);
engine.enableEngine();
}
@ -46,7 +46,7 @@ public class MiaoScriptSponge {
@SneakyThrows
public void reload(GameReloadEvent event) {
engine.disableEngine();
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger);
engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger, this);
engine.enableEngine();
}
}

View File

@ -0,0 +1,51 @@
package pw.yumc.MiaoScript;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
/**
* Created with IntelliJ IDEA
*
* @author MiaoWoo
* Created on 2020/1/16 9:04.
*/
public class ProxyClass {
private ScriptEngine engine;
private String script;
private Bindings bindings;
public ProxyClass(ScriptEngine engine, String script, Bindings bindings) {
this.engine = engine;
this.script = script;
this.bindings = bindings;
}
public Object method(Object args) throws ScriptException {
bindings.put("args", args);
return engine.eval(script, bindings);
}
public Object one(Object arg1) throws ScriptException {
bindings.put("arg1", arg1);
return engine.eval(script, bindings);
}
public Object two(Object arg1, Object arg2) throws ScriptException {
bindings.put("arg1", arg1);
bindings.put("arg2", arg2);
return engine.eval(script, bindings);
}
public Object three(Object arg1, Object arg2, Object arg3) throws ScriptException {
bindings.put("arg1", arg1);
bindings.put("arg2", arg2);
bindings.put("arg3", arg3);
return engine.eval(script, bindings);
}
public Object args(Object... args) throws ScriptException {
bindings.put("args", args);
return engine.eval(script, bindings);
}
}

View File

@ -8,6 +8,7 @@ import java.nio.file.Paths;
import javax.script.ScriptEngineManager;
import lombok.SneakyThrows;
/**
* Created with IntelliJ IDEA
*
@ -17,19 +18,21 @@ import lombok.SneakyThrows;
public class ScriptEngine {
private String root;
private Object logger;
private Base base;
private MiaoScriptEngine engine;
private ScriptEngineManager manager;
public ScriptEngine(String root, Object logger) {
public ScriptEngine(String root, Object logger, Object instance) {
this.root = root;
this.logger = logger;
this.base = new Base(instance);
this.manager = new ScriptEngineManager();
}
@SneakyThrows
public void enableEngine() {
this.engine = new MiaoScriptEngine(manager, "nashorn");
this.engine.put("base", new Base());
this.engine.put("base", this.base);
this.engine.put("ScriptEngineContextHolder", this);
Path bios = Paths.get(root, "bios.js");
// 如果存在自定义bios就加载自定义的

View File

@ -0,0 +1,5 @@
name: ${project.artifactId}
description: ${project.description}
main: ${project.groupId}.${project.artifactId}.${project.artifactId}Bungee
version: ${project.version}
author: MiaoWoo