From 0b0cea9e50ac8228cadee4b3ce49fdf49dc08342 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 17 Jan 2020 09:17:13 +0800 Subject: [PATCH] feat: add bungee support & ProxyClass Signed-off-by: MiaoWoo --- pom.xml | 14 ++++- src/main/java/pw/yumc/MiaoScript/Base.java | 14 +++++ .../java/pw/yumc/MiaoScript/MiaoScript.java | 3 +- .../pw/yumc/MiaoScript/MiaoScriptBungee.java | 26 ++++++++++ .../pw/yumc/MiaoScript/MiaoScriptNukkit.java | 23 +++++++++ .../pw/yumc/MiaoScript/MiaoScriptSponge.java | 6 +-- .../java/pw/yumc/MiaoScript/ProxyClass.java | 51 +++++++++++++++++++ .../java/pw/yumc/MiaoScript/ScriptEngine.java | 7 ++- src/main/resources/bungee.yml | 5 ++ 9 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 src/main/java/pw/yumc/MiaoScript/MiaoScriptBungee.java create mode 100644 src/main/java/pw/yumc/MiaoScript/MiaoScriptNukkit.java create mode 100644 src/main/java/pw/yumc/MiaoScript/ProxyClass.java create mode 100644 src/main/resources/bungee.yml diff --git a/pom.xml b/pom.xml index be49870..fa98f4e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 pw.yumc MiaoScript - 0.1.0 + 0.1.1 502647092 @@ -54,6 +54,8 @@ DEV + §620-01-14 §afeat: 新增 Bungee 支持; +       §afeat: 新增 instance 实例获取; §619-09-24 §cremove: 移除 okhttp3 类库;       §afeat: 新增require自动下载模块功能; §619-09-21 §afeat: 新增 okhttp3 类库; @@ -175,5 +177,15 @@ spongeapi 7.1.0 + + net.md-5 + bungeecord-api + 1.12-SNAPSHOT + + + cn.nukkit + nukkit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/src/main/java/pw/yumc/MiaoScript/Base.java b/src/main/java/pw/yumc/MiaoScript/Base.java index c7029af..6315b31 100644 --- a/src/main/java/pw/yumc/MiaoScript/Base.java +++ b/src/main/java/pw/yumc/MiaoScript/Base.java @@ -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"); } diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java index 58d2742..5582ea0 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java @@ -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(); } diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScriptBungee.java b/src/main/java/pw/yumc/MiaoScript/MiaoScriptBungee.java new file mode 100644 index 0000000..f25d7a6 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScriptBungee.java @@ -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(); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScriptNukkit.java b/src/main/java/pw/yumc/MiaoScript/MiaoScriptNukkit.java new file mode 100644 index 0000000..f2fe3bc --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScriptNukkit.java @@ -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(); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java b/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java index a21137f..53fd25d 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java @@ -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(); } } diff --git a/src/main/java/pw/yumc/MiaoScript/ProxyClass.java b/src/main/java/pw/yumc/MiaoScript/ProxyClass.java new file mode 100644 index 0000000..17369d0 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/ProxyClass.java @@ -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); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index cdf5417..c6e0206 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -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就加载自定义的 diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml new file mode 100644 index 0000000..00d1d10 --- /dev/null +++ b/src/main/resources/bungee.yml @@ -0,0 +1,5 @@ +name: ${project.artifactId} +description: ${project.description} +main: ${project.groupId}.${project.artifactId}.${project.artifactId}Bungee +version: ${project.version} +author: MiaoWoo \ No newline at end of file