feat: support mjs.json js compile & add API
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
		
							
								
								
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							@@ -2,7 +2,7 @@
 | 
				
			|||||||
    <modelVersion>4.0.0</modelVersion>
 | 
					    <modelVersion>4.0.0</modelVersion>
 | 
				
			||||||
    <groupId>pw.yumc</groupId>
 | 
					    <groupId>pw.yumc</groupId>
 | 
				
			||||||
    <artifactId>MiaoScript</artifactId>
 | 
					    <artifactId>MiaoScript</artifactId>
 | 
				
			||||||
    <version>0.18.0</version>
 | 
					    <version>0.19.5</version>
 | 
				
			||||||
    <developers>
 | 
					    <developers>
 | 
				
			||||||
        <developer>
 | 
					        <developer>
 | 
				
			||||||
            <id>502647092</id>
 | 
					            <id>502647092</id>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,7 @@
 | 
				
			|||||||
package pw.yumc.MiaoScript;
 | 
					package pw.yumc.MiaoScript;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import pw.yumc.MiaoScript.api.MiaoScriptAPI;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.nio.charset.StandardCharsets;
 | 
					import java.nio.charset.StandardCharsets;
 | 
				
			||||||
@@ -13,15 +15,14 @@ import java.nio.file.Path;
 | 
				
			|||||||
 * Created on 2017/10/9 12:40.
 | 
					 * Created on 2017/10/9 12:40.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class Base {
 | 
					public class Base {
 | 
				
			||||||
    public static final String VERSION = "0.18.0";
 | 
					 | 
				
			||||||
    private Object instance;
 | 
					    private Object instance;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Base(Object instance) {
 | 
					    Base(Object instance) {
 | 
				
			||||||
        this.instance = instance;
 | 
					        this.instance = instance;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public String getVersion() {
 | 
					    public String getVersion() {
 | 
				
			||||||
        return Base.VERSION;
 | 
					        return MiaoScriptAPI.VERSION;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Class<?> getClass(String name) throws ClassNotFoundException {
 | 
					    public Class<?> getClass(String name) throws ClassNotFoundException {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,6 +60,13 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public MiaoScriptEngine(ScriptEngineManager engineManager, final String engineType, String engineRoot) {
 | 
					    public MiaoScriptEngine(ScriptEngineManager engineManager, final String engineType, String engineRoot) {
 | 
				
			||||||
 | 
					        // JDK11 Polyfill 存在类效验问题 直接用OpenJDK的Nashorn
 | 
				
			||||||
 | 
					        if (System.getProperty("java.version").startsWith("11.") && engineRoot != null) {
 | 
				
			||||||
 | 
					            this.loadNetworkNashorn(engineRoot);
 | 
				
			||||||
 | 
					            if (engine == null)
 | 
				
			||||||
 | 
					                throw new UnsupportedOperationException("当前环境 JDK11 不支持 Nashorn 脚本类型!");
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            engine = engineManager.getEngineByName(engineType);
 | 
					            engine = engineManager.getEngineByName(engineType);
 | 
				
			||||||
        } catch (final NullPointerException ignored) {
 | 
					        } catch (final NullPointerException ignored) {
 | 
				
			||||||
@@ -67,22 +74,26 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
 | 
				
			|||||||
        if (engine == null) {
 | 
					        if (engine == null) {
 | 
				
			||||||
            val extDirs = System.getProperty("java.ext.dirs");
 | 
					            val extDirs = System.getProperty("java.ext.dirs");
 | 
				
			||||||
            if (extDirs != null) {
 | 
					            if (extDirs != null) {
 | 
				
			||||||
                val dirs = extDirs.split(File.pathSeparator);
 | 
					                this.loadLocalNashorn(extDirs, engineType);
 | 
				
			||||||
                for (String dir : dirs) {
 | 
					 | 
				
			||||||
                    File nashorn = new File(dir, "nashorn.jar");
 | 
					 | 
				
			||||||
                    if (nashorn.exists()) {
 | 
					 | 
				
			||||||
                        this.loadJar(nashorn);
 | 
					 | 
				
			||||||
                        this.createEngineByName(engineType);
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            } else if (engineRoot != null) {
 | 
					            } else if (engineRoot != null) {
 | 
				
			||||||
                this.loadLocalNashorn(engineRoot);
 | 
					                this.loadNetworkNashorn(engineRoot);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (engine == null)
 | 
					        if (engine == null)
 | 
				
			||||||
            throw new UnsupportedOperationException("当前环境不支持 " + engineType + " 脚本类型!");
 | 
					            throw new UnsupportedOperationException("当前环境不支持 " + engineType + " 脚本类型!");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private void loadLocalNashorn(String extDirs, String engineType) {
 | 
				
			||||||
 | 
					        val dirs = extDirs.split(File.pathSeparator);
 | 
				
			||||||
 | 
					        for (String dir : dirs) {
 | 
				
			||||||
 | 
					            File nashorn = new File(dir, "nashorn.jar");
 | 
				
			||||||
 | 
					            if (nashorn.exists()) {
 | 
				
			||||||
 | 
					                this.loadJar(nashorn);
 | 
				
			||||||
 | 
					                this.createEngineByName(engineType);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void initReflect() {
 | 
					    private void initReflect() {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
 | 
					            ClassLoader loader = Thread.currentThread().getContextClassLoader();
 | 
				
			||||||
@@ -107,7 +118,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @SneakyThrows
 | 
					    @SneakyThrows
 | 
				
			||||||
    private void loadLocalNashorn(String engineRoot) {
 | 
					    private void loadNetworkNashorn(String engineRoot) {
 | 
				
			||||||
        initReflect();
 | 
					        initReflect();
 | 
				
			||||||
        File libRootFile = new File(engineRoot, "lib");
 | 
					        File libRootFile = new File(engineRoot, "lib");
 | 
				
			||||||
        libRootFile.mkdirs();
 | 
					        libRootFile.mkdirs();
 | 
				
			||||||
@@ -117,7 +128,10 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
 | 
				
			|||||||
        downloadJar(libRoot, "org.ow2.asm", "asm-commons", "9.2");
 | 
					        downloadJar(libRoot, "org.ow2.asm", "asm-commons", "9.2");
 | 
				
			||||||
        downloadJar(libRoot, "org.ow2.asm", "asm-tree", "9.2");
 | 
					        downloadJar(libRoot, "org.ow2.asm", "asm-tree", "9.2");
 | 
				
			||||||
        downloadJar(libRoot, "org.ow2.asm", "asm-util", "9.2");
 | 
					        downloadJar(libRoot, "org.ow2.asm", "asm-util", "9.2");
 | 
				
			||||||
        this.createEngineByName("nashorn");
 | 
					        Class<?> NashornScriptEngineFactory = Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory");
 | 
				
			||||||
 | 
					        Method getScriptEngine = NashornScriptEngineFactory.getMethod("getScriptEngine");
 | 
				
			||||||
 | 
					        Object factory = NashornScriptEngineFactory.newInstance();
 | 
				
			||||||
 | 
					        engine = (ScriptEngine) getScriptEngine.invoke(factory);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @SneakyThrows
 | 
					    @SneakyThrows
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,7 @@ import org.spongepowered.api.event.game.state.GameStartedServerEvent;
 | 
				
			|||||||
import org.spongepowered.api.event.game.state.GameStartingServerEvent;
 | 
					import org.spongepowered.api.event.game.state.GameStartingServerEvent;
 | 
				
			||||||
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
 | 
					import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
 | 
				
			||||||
import org.spongepowered.api.plugin.Plugin;
 | 
					import org.spongepowered.api.plugin.Plugin;
 | 
				
			||||||
 | 
					import pw.yumc.MiaoScript.api.MiaoScriptAPI;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -19,7 +20,7 @@ import java.io.File;
 | 
				
			|||||||
 * @author 喵♂呜
 | 
					 * @author 喵♂呜
 | 
				
			||||||
 * Created on 2017/10/25 20:35.
 | 
					 * Created on 2017/10/25 20:35.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@Plugin(id = "miaoscript", name = "MiaoScript", version = Base.VERSION, authors = "MiaoWoo")
 | 
					@Plugin(id = "miaoscript", name = "MiaoScript", version = MiaoScriptAPI.VERSION, authors = "MiaoWoo")
 | 
				
			||||||
public class MiaoScriptSponge {
 | 
					public class MiaoScriptSponge {
 | 
				
			||||||
    private ScriptEngine engine;
 | 
					    private ScriptEngine engine;
 | 
				
			||||||
    @Inject
 | 
					    @Inject
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package pw.yumc.MiaoScript;
 | 
					package pw.yumc.MiaoScript;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import lombok.SneakyThrows;
 | 
					import lombok.SneakyThrows;
 | 
				
			||||||
 | 
					import pw.yumc.MiaoScript.api.MiaoScriptAPI;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.nio.file.Files;
 | 
					import java.nio.file.Files;
 | 
				
			||||||
import java.nio.file.Path;
 | 
					import java.nio.file.Path;
 | 
				
			||||||
@@ -24,6 +25,7 @@ public class ScriptEngine {
 | 
				
			|||||||
        this.root = root;
 | 
					        this.root = root;
 | 
				
			||||||
        this.logger = logger;
 | 
					        this.logger = logger;
 | 
				
			||||||
        this.base = new Base(instance);
 | 
					        this.base = new Base(instance);
 | 
				
			||||||
 | 
					        MiaoScriptAPI.setEngine(this);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void createEngine() {
 | 
					    public void createEngine() {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										17
									
								
								src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					package pw.yumc.MiaoScript.api;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import pw.yumc.MiaoScript.MiaoScriptEngine;
 | 
				
			||||||
 | 
					import pw.yumc.MiaoScript.ScriptEngine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class MiaoScriptAPI {
 | 
				
			||||||
 | 
					    public static final String VERSION = "0.19.5";
 | 
				
			||||||
 | 
					    private static ScriptEngine scriptEngine;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static void setEngine(ScriptEngine scriptEngine) {
 | 
				
			||||||
 | 
					        MiaoScriptAPI.scriptEngine = scriptEngine;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static MiaoScriptEngine getEngine() {
 | 
				
			||||||
 | 
					        return MiaoScriptAPI.scriptEngine.getEngine();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										68
									
								
								src/main/java/pw/yumc/MiaoScript/api/bukkit/ScriptEvent.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/main/java/pw/yumc/MiaoScript/api/bukkit/ScriptEvent.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
				
			|||||||
 | 
					package pw.yumc.MiaoScript.api.bukkit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.bukkit.event.Cancellable;
 | 
				
			||||||
 | 
					import org.bukkit.event.Event;
 | 
				
			||||||
 | 
					import org.bukkit.event.HandlerList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.script.Bindings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class ScriptEvent extends Event implements Cancellable {
 | 
				
			||||||
 | 
					    private final String plugin;
 | 
				
			||||||
 | 
					    private final String event;
 | 
				
			||||||
 | 
					    private final Bindings data;
 | 
				
			||||||
 | 
					    private boolean cancelled = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ScriptEvent(String plugin, String event, Bindings data) {
 | 
				
			||||||
 | 
					        this.plugin = plugin;
 | 
				
			||||||
 | 
					        this.event = event;
 | 
				
			||||||
 | 
					        this.data = data;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Which Plugin Call Event
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return PluginName
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public String getPlugin() {
 | 
				
			||||||
 | 
					        return plugin;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Plugin Event Name
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return EventName
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public String getEvent() {
 | 
				
			||||||
 | 
					        return event;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Plugin Event Data
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return EventData
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public Bindings getData() {
 | 
				
			||||||
 | 
					        return data;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public boolean isCancelled() {
 | 
				
			||||||
 | 
					        return this.cancelled;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void setCancelled(boolean b) {
 | 
				
			||||||
 | 
					        this.cancelled = b;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static final HandlerList handlerList = new HandlerList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static HandlerList getHandlerList() {
 | 
				
			||||||
 | 
					        return handlerList;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public HandlerList getHandlers() {
 | 
				
			||||||
 | 
					        return handlerList;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -241,7 +241,7 @@
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            cacheModules[id] = module
 | 
					            cacheModules[id] = module
 | 
				
			||||||
            var cfile = _canonical(file)
 | 
					            var cfile = _canonical(file)
 | 
				
			||||||
            if (cfile.endsWith('.js')) {
 | 
					            if (cfile.endsWith('.js') || cfile.endsWith('.mjs.json')) {
 | 
				
			||||||
                compileJs(module, file, __assign(optional, { id: id }))
 | 
					                compileJs(module, file, __assign(optional, { id: id }))
 | 
				
			||||||
            } else if (cfile.endsWith('.json')) {
 | 
					            } else if (cfile.endsWith('.json')) {
 | 
				
			||||||
                compileJson(module, file)
 | 
					                compileJson(module, file)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,9 +6,13 @@ api-version: 1.13
 | 
				
			|||||||
author: MiaoWoo
 | 
					author: MiaoWoo
 | 
				
			||||||
website: ${ciManagement.url}
 | 
					website: ${ciManagement.url}
 | 
				
			||||||
softdepend:
 | 
					softdepend:
 | 
				
			||||||
 | 
					  - OriginAttribute
 | 
				
			||||||
 | 
					  - AttributeSystem
 | 
				
			||||||
 | 
					  - ItemLoreOrigin
 | 
				
			||||||
  - PlaceholderAPI
 | 
					  - PlaceholderAPI
 | 
				
			||||||
  - AttributePlus
 | 
					  - AttributePlus
 | 
				
			||||||
  - PlayerPoints
 | 
					  - PlayerPoints
 | 
				
			||||||
 | 
					  - SX-Attribute
 | 
				
			||||||
  - CrazyCrates
 | 
					  - CrazyCrates
 | 
				
			||||||
  - ProtocolLib
 | 
					  - ProtocolLib
 | 
				
			||||||
  - DragonCore
 | 
					  - DragonCore
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user