4
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								pom.xml
									
									
									
									
									
								
							@@ -3,7 +3,7 @@
 | 
			
		||||
	<modelVersion>4.0.0</modelVersion>
 | 
			
		||||
	<groupId>pw.yumc</groupId>
 | 
			
		||||
	<artifactId>MiaoScript</artifactId>
 | 
			
		||||
	<version>1.0</version>
 | 
			
		||||
	<version>1.1</version>
 | 
			
		||||
	<build>
 | 
			
		||||
		<finalName>${project.name}</finalName>
 | 
			
		||||
		<resources>
 | 
			
		||||
@@ -63,7 +63,7 @@
 | 
			
		||||
	<properties>
 | 
			
		||||
		<update.description></update.description>
 | 
			
		||||
		<update.changes></update.changes>
 | 
			
		||||
		<env.GIT_COMMIT>DEBUG</env.GIT_COMMIT>
 | 
			
		||||
		<env.GIT_COMMIT>开发版本</env.GIT_COMMIT>
 | 
			
		||||
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
			
		||||
	</properties>
 | 
			
		||||
	<repositories>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										72
									
								
								src/main/java/pw/yumc/MiaoScript/ManagerCenter.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								src/main/java/pw/yumc/MiaoScript/ManagerCenter.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,72 @@
 | 
			
		||||
package pw.yumc.MiaoScript;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.MiaoScript.data.ConfigManager;
 | 
			
		||||
import pw.yumc.MiaoScript.data.SQLManager;
 | 
			
		||||
import pw.yumc.MiaoScript.event.EventManager;
 | 
			
		||||
import pw.yumc.MiaoScript.module.ModuleManager;
 | 
			
		||||
import pw.yumc.MiaoScript.script.ScriptManager;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
import pw.yumc.YumCore.config.FileConfig;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 管理中心
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:50:50
 | 
			
		||||
 */
 | 
			
		||||
public class ManagerCenter {
 | 
			
		||||
    private final MiaoScript plugin = P.getPlugin();
 | 
			
		||||
    private final ConfigurationSection dbCfg;
 | 
			
		||||
    private final SQLManager sqlManager;
 | 
			
		||||
    private final EventManager eventManager;
 | 
			
		||||
    private final ModuleManager moduleManager;
 | 
			
		||||
    private final ConfigManager configManager;
 | 
			
		||||
    private final ScriptManager scriptManager;
 | 
			
		||||
 | 
			
		||||
    public ManagerCenter() {
 | 
			
		||||
        dbCfg = plugin.getConfig().getConfigurationSection("DataBase");
 | 
			
		||||
        configManager = new ConfigManager(P.getDataFolder());
 | 
			
		||||
        sqlManager = new SQLManager(dbCfg);
 | 
			
		||||
        eventManager = new EventManager(new FileConfig("event.yml"));
 | 
			
		||||
        scriptManager = new ScriptManager(new FileConfig("script.yml"));
 | 
			
		||||
        moduleManager = new ModuleManager();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 配置管理
 | 
			
		||||
     */
 | 
			
		||||
    public ConfigManager getConfigManager() {
 | 
			
		||||
        return configManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 事件管理
 | 
			
		||||
     */
 | 
			
		||||
    public EventManager getEventManager() {
 | 
			
		||||
        return eventManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 模块管理
 | 
			
		||||
     */
 | 
			
		||||
    public ModuleManager getModuleManager() {
 | 
			
		||||
        return moduleManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 脚本管理
 | 
			
		||||
     */
 | 
			
		||||
    public ScriptManager getScriptManager() {
 | 
			
		||||
        return scriptManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 数据管理
 | 
			
		||||
     */
 | 
			
		||||
    public SQLManager getSQLManager() {
 | 
			
		||||
        return sqlManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,11 @@
 | 
			
		||||
package pw.yumc.MiaoScript;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.util.Enumeration;
 | 
			
		||||
import java.util.jar.JarEntry;
 | 
			
		||||
import java.util.jar.JarFile;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.configuration.file.FileConfiguration;
 | 
			
		||||
import org.bukkit.event.HandlerList;
 | 
			
		||||
@@ -8,19 +13,19 @@ import org.bukkit.plugin.java.JavaPlugin;
 | 
			
		||||
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderAPI;
 | 
			
		||||
import pw.yumc.MiaoScript.commands.MSCommands;
 | 
			
		||||
import pw.yumc.MiaoScript.data.DataManager;
 | 
			
		||||
import pw.yumc.MiaoScript.event.EventManager;
 | 
			
		||||
import pw.yumc.MiaoScript.middleware.EventMiddleware;
 | 
			
		||||
import pw.yumc.MiaoScript.script.ScriptManager;
 | 
			
		||||
import pw.yumc.MiaoScript.javascript.MiaoScriptEngine;
 | 
			
		||||
import pw.yumc.MiaoScript.script.ScriptPlaceholder;
 | 
			
		||||
import pw.yumc.YumCore.config.FileConfig;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 喵式脚本
 | 
			
		||||
 *
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:50:39
 | 
			
		||||
 */
 | 
			
		||||
public class MiaoScript extends JavaPlugin {
 | 
			
		||||
    private FileConfig config;
 | 
			
		||||
    private DataManager dataManager;
 | 
			
		||||
    private EventManager eventManager;
 | 
			
		||||
    private EventMiddleware eventMiddleware;
 | 
			
		||||
    private ScriptManager scriptManager;
 | 
			
		||||
    private ManagerCenter managerCenter;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public FileConfiguration getConfig() {
 | 
			
		||||
@@ -28,62 +33,34 @@ public class MiaoScript extends JavaPlugin {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 数据管理
 | 
			
		||||
     * @return 管理中心
 | 
			
		||||
     */
 | 
			
		||||
    public DataManager getDataManager() {
 | 
			
		||||
        return dataManager;
 | 
			
		||||
    public ManagerCenter getManagerCenter() {
 | 
			
		||||
        return managerCenter;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 事件管理
 | 
			
		||||
     * 全局载入
 | 
			
		||||
     */
 | 
			
		||||
    public EventManager getEventManager() {
 | 
			
		||||
        return eventManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 事件中间件
 | 
			
		||||
     */
 | 
			
		||||
    public EventMiddleware getEventMiddleware() {
 | 
			
		||||
        return eventMiddleware;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 脚本管理
 | 
			
		||||
     */
 | 
			
		||||
    public ScriptManager getScriptManager() {
 | 
			
		||||
        return scriptManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 载入数据与配置
 | 
			
		||||
     */
 | 
			
		||||
    public void loadConfig() {
 | 
			
		||||
        dataManager = new DataManager(new FileConfig("data.yml"));
 | 
			
		||||
        eventManager = new EventManager(new FileConfig("event.yml"));
 | 
			
		||||
        scriptManager = new ScriptManager(new FileConfig("script.yml"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 注册事件
 | 
			
		||||
     */
 | 
			
		||||
    public void loadEvents() {
 | 
			
		||||
        eventManager.registerAll();
 | 
			
		||||
    public void load() {
 | 
			
		||||
        loadConfig();
 | 
			
		||||
        loadManager();
 | 
			
		||||
        loadScript();
 | 
			
		||||
        loadEvents();
 | 
			
		||||
        loadModules();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onEnable() {
 | 
			
		||||
        new MSCommands();
 | 
			
		||||
        loadConfig();
 | 
			
		||||
        loadEvents();
 | 
			
		||||
        load();
 | 
			
		||||
        register();
 | 
			
		||||
        new MSCommands();
 | 
			
		||||
        MiaoScriptEngine.getDefault();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onLoad() {
 | 
			
		||||
        saveDefault();
 | 
			
		||||
        config = new FileConfig();
 | 
			
		||||
        eventMiddleware = new EventMiddleware();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -100,17 +77,49 @@ public class MiaoScript extends JavaPlugin {
 | 
			
		||||
    public void reload() {
 | 
			
		||||
        HandlerList.unregisterAll(this);
 | 
			
		||||
        onLoad();
 | 
			
		||||
        loadConfig();
 | 
			
		||||
        loadEvents();
 | 
			
		||||
        load();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void loadConfig() {
 | 
			
		||||
        config = new FileConfig();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 注册事件
 | 
			
		||||
     */
 | 
			
		||||
    private void loadEvents() {
 | 
			
		||||
        getManagerCenter().getEventManager().registerAll();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 初始管理中心
 | 
			
		||||
     */
 | 
			
		||||
    private void loadManager() {
 | 
			
		||||
        managerCenter = new ManagerCenter();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 载入模块
 | 
			
		||||
     */
 | 
			
		||||
    private void loadModules() {
 | 
			
		||||
        getManagerCenter().getModuleManager().loadModules();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 载入脚本
 | 
			
		||||
     */
 | 
			
		||||
    private void loadScript() {
 | 
			
		||||
        getManagerCenter().getScriptManager().registerAll();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 保存默认文件
 | 
			
		||||
     */
 | 
			
		||||
    private void saveDefault() {
 | 
			
		||||
        saveJs("bed.js");
 | 
			
		||||
        saveJs("welcome.js");
 | 
			
		||||
        saveJs("checkchat.js");
 | 
			
		||||
        try {
 | 
			
		||||
            saveFile("js", "module");
 | 
			
		||||
        } catch (final IOException e) {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -118,10 +127,28 @@ public class MiaoScript extends JavaPlugin {
 | 
			
		||||
     *
 | 
			
		||||
     * @param name
 | 
			
		||||
     *            JS文件名称
 | 
			
		||||
     * @throws IOException
 | 
			
		||||
     */
 | 
			
		||||
    private void saveJs(final String name) {
 | 
			
		||||
        if (!new File(getDataFolder(), "js" + File.separatorChar + name).exists()) {
 | 
			
		||||
            saveResource("js" + File.separatorChar + name, false);
 | 
			
		||||
    private void saveFile(final String... dirs) throws IOException {
 | 
			
		||||
        final URL url = getClassLoader().getResource("plugin.yml");
 | 
			
		||||
        final String upath = url.getFile().substring(url.getFile().indexOf("/") + 1);
 | 
			
		||||
        final String jarPath = upath.substring(0, upath.indexOf('!'));
 | 
			
		||||
        JarFile jar = null;
 | 
			
		||||
        jar = new JarFile(jarPath);
 | 
			
		||||
        final Enumeration<JarEntry> jes = jar.entries();
 | 
			
		||||
        while (jes.hasMoreElements()) {
 | 
			
		||||
            final JarEntry je = jes.nextElement();
 | 
			
		||||
            if (!je.isDirectory()) {
 | 
			
		||||
                for (final String dir : dirs) {
 | 
			
		||||
                    if (je.getName().startsWith(dir)) {
 | 
			
		||||
                        if (!new File(getDataFolder(), je.getName()).exists()) {
 | 
			
		||||
                            saveResource(je.getName(), false);
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        jar.close();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,17 @@
 | 
			
		||||
package pw.yumc.MiaoScript.commands;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
import javax.script.ScriptException;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderAPI;
 | 
			
		||||
import pw.yumc.MiaoScript.MiaoScript;
 | 
			
		||||
import pw.yumc.MiaoScript.event.EventInfo;
 | 
			
		||||
import pw.yumc.MiaoScript.event.EventManager;
 | 
			
		||||
import pw.yumc.MiaoScript.javascript.MiaoScriptEngine;
 | 
			
		||||
import pw.yumc.MiaoScript.misc.MLog;
 | 
			
		||||
import pw.yumc.MiaoScript.script.ScriptInfo;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
import pw.yumc.YumCore.commands.CommandArgument;
 | 
			
		||||
import pw.yumc.YumCore.commands.CommandExecutor;
 | 
			
		||||
@@ -12,8 +19,14 @@ import pw.yumc.YumCore.commands.CommandManager;
 | 
			
		||||
import pw.yumc.YumCore.commands.annotation.Cmd;
 | 
			
		||||
import pw.yumc.YumCore.commands.annotation.Help;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 喵式脚本命令
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:50:58
 | 
			
		||||
 */
 | 
			
		||||
public class MSCommands implements CommandExecutor {
 | 
			
		||||
    MiaoScript plugin = P.getPlugin();
 | 
			
		||||
    private final MiaoScript plugin = P.getPlugin();
 | 
			
		||||
 | 
			
		||||
    public MSCommands() {
 | 
			
		||||
        new CommandManager("ms", this);
 | 
			
		||||
@@ -22,20 +35,16 @@ public class MSCommands implements CommandExecutor {
 | 
			
		||||
    @Cmd(permission = "MiaoScript.debug")
 | 
			
		||||
    @Help("切换调试模式")
 | 
			
		||||
    public void debug(final CommandArgument e) {
 | 
			
		||||
        EventManager.debug = !EventManager.debug;
 | 
			
		||||
        e.getSender().sendMessage("§6调试模式: " + (EventManager.debug ? "§a开启" : "§c关闭"));
 | 
			
		||||
        MLog.setDebug(!MLog.isDebug());
 | 
			
		||||
        e.getSender().sendMessage("§6调试模式: " + (MLog.isDebug() ? "§a开启" : "§c关闭"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Cmd(permission = "MiaoScript.listen")
 | 
			
		||||
    @Help("查看监听列表")
 | 
			
		||||
    public void listen(final CommandArgument e) {
 | 
			
		||||
        final CommandSender sender = e.getSender();
 | 
			
		||||
        for (final EventInfo ei : plugin.getEventManager().getEvents().values()) {
 | 
			
		||||
            sender.sendMessage(String.format("§6名称: §a%s §6事件: §a%s §6优先级: §a%s", ei.getName(), ei.getClazz().substring(ei.getClazz().lastIndexOf(".") + 1), ei.getPriority()));
 | 
			
		||||
            sender.sendMessage("§6脚本列表: ");
 | 
			
		||||
            for (final String script : ei.getScripts()) {
 | 
			
		||||
                sender.sendMessage(String.format("§6- §e%s", script));
 | 
			
		||||
            }
 | 
			
		||||
        for (final EventInfo ei : plugin.getManagerCenter().getEventManager().getEvents().values()) {
 | 
			
		||||
            ei.send(sender);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -45,4 +54,41 @@ public class MSCommands implements CommandExecutor {
 | 
			
		||||
        plugin.reload();
 | 
			
		||||
        e.getSender().sendMessage("§a配置文件已重新载入!");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Cmd(permission = "MiaoScript.run")
 | 
			
		||||
    @Help("解析脚本")
 | 
			
		||||
    public void run(final CommandArgument e) {
 | 
			
		||||
        final CommandSender sender = e.getSender();
 | 
			
		||||
        final String script = merge(e.getArgs(), 0);
 | 
			
		||||
        final MiaoScriptEngine engine = MiaoScriptEngine.getDefault();
 | 
			
		||||
        try {
 | 
			
		||||
            engine.put("Player", e.getSender());
 | 
			
		||||
            final long s = System.currentTimeMillis();
 | 
			
		||||
            final Object result = engine.eval(PlaceholderAPI.setPlaceholders((sender instanceof Player ? (Player) sender : null), script));
 | 
			
		||||
            sender.sendMessage(String.format("%s运行结果: %s 耗时: %s", Log.getPrefix(), String.valueOf(result), System.currentTimeMillis() - s));
 | 
			
		||||
        } catch (final ScriptException ex) {
 | 
			
		||||
            sender.sendMessage(String.format(Log.getPrefix() + "脚本语法错误: %s", ex.getMessage()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Cmd(permission = "MiaoScript.script", minimumArguments = 1)
 | 
			
		||||
    @Help(value = "查看脚本信息", possibleArguments = "<脚本名称>")
 | 
			
		||||
    public void script(final CommandArgument e) {
 | 
			
		||||
        final CommandSender sender = e.getSender();
 | 
			
		||||
        final ScriptInfo s = plugin.getManagerCenter().getScriptManager().getScript(e.getArgs()[0]);
 | 
			
		||||
        if (s == null) {
 | 
			
		||||
            Log.toSender(sender, "脚本不存在!");
 | 
			
		||||
        } else {
 | 
			
		||||
            s.send(sender);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private String merge(final String[] arr, final int index) {
 | 
			
		||||
        final StringBuffer strs = new StringBuffer();
 | 
			
		||||
        for (int i = index; i < arr.length; i++) {
 | 
			
		||||
            strs.append(arr[i]);
 | 
			
		||||
            strs.append(" ");
 | 
			
		||||
        }
 | 
			
		||||
        return strs.toString();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										52
									
								
								src/main/java/pw/yumc/MiaoScript/data/ConfigManager.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/main/java/pw/yumc/MiaoScript/data/ConfigManager.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
package pw.yumc.MiaoScript.data;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.util.WeakHashMap;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.config.FileConfig;
 | 
			
		||||
import pw.yumc.YumCore.config.PlayerConfig;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 配置管理
 | 
			
		||||
 *
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月25日 上午2:02:03
 | 
			
		||||
 */
 | 
			
		||||
public class ConfigManager {
 | 
			
		||||
    private final File dir;
 | 
			
		||||
    private final WeakHashMap<String, PlayerConfig> playerconfigs = new WeakHashMap<>();
 | 
			
		||||
 | 
			
		||||
    private FileConfig config = null;
 | 
			
		||||
 | 
			
		||||
    public ConfigManager(final File dir) {
 | 
			
		||||
        this.dir = dir;
 | 
			
		||||
        final File f = new File(dir, "config.yml");
 | 
			
		||||
        if (f.exists()) {
 | 
			
		||||
            this.config = new FileConfig(f);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 数据配置
 | 
			
		||||
     */
 | 
			
		||||
    public FileConfig get() {
 | 
			
		||||
        return config;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 数据配置
 | 
			
		||||
     */
 | 
			
		||||
    public FileConfig get(final String name) {
 | 
			
		||||
        if (!playerconfigs.containsKey(name)) {
 | 
			
		||||
            playerconfigs.put(name, new PlayerConfig(dir, name));
 | 
			
		||||
        }
 | 
			
		||||
        return playerconfigs.get(name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 保存数据
 | 
			
		||||
     */
 | 
			
		||||
    public void save() {
 | 
			
		||||
        config.save();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,31 +0,0 @@
 | 
			
		||||
package pw.yumc.MiaoScript.data;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.config.FileConfig;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 数据管理
 | 
			
		||||
 *
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月25日 上午2:02:03
 | 
			
		||||
 */
 | 
			
		||||
public class DataManager {
 | 
			
		||||
    FileConfig data;
 | 
			
		||||
 | 
			
		||||
    public DataManager(final FileConfig config) {
 | 
			
		||||
        this.data = config;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 数据配置
 | 
			
		||||
     */
 | 
			
		||||
    public FileConfig getData() {
 | 
			
		||||
        return data;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 保存数据
 | 
			
		||||
     */
 | 
			
		||||
    public void save() {
 | 
			
		||||
        data.save();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										60
									
								
								src/main/java/pw/yumc/MiaoScript/data/SQLManager.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/main/java/pw/yumc/MiaoScript/data/SQLManager.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
package pw.yumc.MiaoScript.data;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
import pw.yumc.YumCore.sql.core.DataBaseCore;
 | 
			
		||||
import pw.yumc.YumCore.sql.core.MySQLCore;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 数据库管理
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:51:10
 | 
			
		||||
 */
 | 
			
		||||
public class SQLManager {
 | 
			
		||||
    Map<String, DataBaseCore> database = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    public SQLManager(final ConfigurationSection cfg) {
 | 
			
		||||
        final Set<String> dbName = cfg.getKeys(false);
 | 
			
		||||
        for (final String db : dbName) {
 | 
			
		||||
            database.put(db, new MySQLCore(cfg.getConfigurationSection(db)));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 检查数据库连接
 | 
			
		||||
     */
 | 
			
		||||
    public void check() {
 | 
			
		||||
        Bukkit.getScheduler().runTaskAsynchronously(P.instance, new Runnable() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
                Log.info("检查数据库配置...");
 | 
			
		||||
                for (final Entry<String, DataBaseCore> entry : database.entrySet()) {
 | 
			
		||||
                    if (entry.getValue().getConnection() == null) {
 | 
			
		||||
                        Log.warning(String.format("数据库 %s 连接失败 请检查配置参数是否正确", entry.getKey()));
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得数据库核心
 | 
			
		||||
     *
 | 
			
		||||
     * @param name
 | 
			
		||||
     *            数据库名称
 | 
			
		||||
     * @return {@link DataBaseCore}
 | 
			
		||||
     */
 | 
			
		||||
    public DataBaseCore get(final String name) {
 | 
			
		||||
        return database.get(name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -5,8 +5,11 @@ import java.util.LinkedList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.MiaoScript.module.ModuleInfo;
 | 
			
		||||
import pw.yumc.MiaoScript.script.ScriptInfo;
 | 
			
		||||
import pw.yumc.YumCore.config.ConfigNode;
 | 
			
		||||
import pw.yumc.YumCore.config.InjectConfigurationSection;
 | 
			
		||||
 | 
			
		||||
@@ -18,7 +21,8 @@ import pw.yumc.YumCore.config.InjectConfigurationSection;
 | 
			
		||||
 */
 | 
			
		||||
public class EventInfo extends InjectConfigurationSection {
 | 
			
		||||
    private transient String name;
 | 
			
		||||
    @ConfigNode(path = "class")
 | 
			
		||||
    private transient ModuleInfo module;
 | 
			
		||||
    @ConfigNode("class")
 | 
			
		||||
    private String clazz;
 | 
			
		||||
    private String priority;
 | 
			
		||||
    private List<String> scripts;
 | 
			
		||||
@@ -53,6 +57,13 @@ public class EventInfo extends InjectConfigurationSection {
 | 
			
		||||
        return clazz;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 获得上层Module 可能为Null
 | 
			
		||||
     */
 | 
			
		||||
    public ModuleInfo getModule() {
 | 
			
		||||
        return module;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 事件显示名称
 | 
			
		||||
     */
 | 
			
		||||
@@ -73,4 +84,28 @@ public class EventInfo extends InjectConfigurationSection {
 | 
			
		||||
    public List<String> getScripts() {
 | 
			
		||||
        return scripts;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送事件信息
 | 
			
		||||
     *
 | 
			
		||||
     * @param sender
 | 
			
		||||
     *            命令发送者
 | 
			
		||||
     */
 | 
			
		||||
    public void send(final CommandSender sender) {
 | 
			
		||||
        sender.sendMessage(String.format("§6名称: §a%s §6事件: §a%s §6优先级: §a%s", getName(), getClazz().substring(getClazz().lastIndexOf(".") + 1), getPriority()));
 | 
			
		||||
        sender.sendMessage("§6脚本列表: ");
 | 
			
		||||
        for (final String script : getScripts()) {
 | 
			
		||||
            sender.sendMessage(String.format("§6- §e%s", script));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param module
 | 
			
		||||
     *            设置上层Module
 | 
			
		||||
     * @return {@link ScriptInfo}
 | 
			
		||||
     */
 | 
			
		||||
    public EventInfo setModule(final ModuleInfo module) {
 | 
			
		||||
        this.module = module;
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,8 +5,11 @@ import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import javax.script.ScriptException;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.event.Event;
 | 
			
		||||
import org.bukkit.event.EventException;
 | 
			
		||||
import org.bukkit.event.EventPriority;
 | 
			
		||||
@@ -14,21 +17,28 @@ import org.bukkit.event.Listener;
 | 
			
		||||
import org.bukkit.event.player.PlayerEvent;
 | 
			
		||||
import org.bukkit.plugin.EventExecutor;
 | 
			
		||||
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderAPI;
 | 
			
		||||
import pw.yumc.MiaoScript.MiaoScript;
 | 
			
		||||
import pw.yumc.MiaoScript.data.ConfigManager;
 | 
			
		||||
import pw.yumc.MiaoScript.javascript.MiaoScriptEngine;
 | 
			
		||||
import pw.yumc.MiaoScript.misc.MLog;
 | 
			
		||||
import pw.yumc.MiaoScript.script.ScriptInfo;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 脚本管理
 | 
			
		||||
 * 事件管理
 | 
			
		||||
 *
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月24日 下午12:51:48
 | 
			
		||||
 */
 | 
			
		||||
public class EventManager implements Listener {
 | 
			
		||||
    public static boolean debug = false;
 | 
			
		||||
    private static final String EVENT_FUNCTION = "handle";
 | 
			
		||||
    private static final String PROCESS_NOT_FOUND = "事件脚本 %s 脚本未包含 hanlde(Event) 函数!";
 | 
			
		||||
    private static final String INVIDE_SCRIPT = "事件脚本 %s 语法错误: %s";
 | 
			
		||||
    private static String identifier = "%s_%s";
 | 
			
		||||
    private final MiaoScript plugin = P.getPlugin();
 | 
			
		||||
    private final ConfigurationSection config;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 未处理的数据
 | 
			
		||||
     */
 | 
			
		||||
@@ -39,19 +49,7 @@ public class EventManager implements Listener {
 | 
			
		||||
    private final Map<String, EventInfo> events = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    public EventManager(final ConfigurationSection config) {
 | 
			
		||||
        for (final String event : config.getKeys(false)) {
 | 
			
		||||
            final ConfigurationSection e = config.getConfigurationSection(event);
 | 
			
		||||
            if (e == null) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            eventInfos.add(new EventInfo(event, e));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void debug(final String msg) {
 | 
			
		||||
        if (debug) {
 | 
			
		||||
            Log.info(msg);
 | 
			
		||||
        }
 | 
			
		||||
        this.config = config;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -105,8 +103,8 @@ public class EventManager implements Listener {
 | 
			
		||||
     *            事件信息
 | 
			
		||||
     */
 | 
			
		||||
    public void printInfo(final EventInfo ei) {
 | 
			
		||||
        debug(String.format("名称: %s 事件: %s 优先级: %s", ei.getName(), ei.getClazz().substring(ei.getClazz().lastIndexOf(".") + 1), ei.getPriority()));
 | 
			
		||||
        debug("脚本列表: ");
 | 
			
		||||
        MLog.debug(String.format("名称: %s 事件: %s 优先级: %s", ei.getName(), ei.getClazz().substring(ei.getClazz().lastIndexOf(".") + 1), ei.getPriority()));
 | 
			
		||||
        MLog.debug("脚本列表: ");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -115,33 +113,63 @@ public class EventManager implements Listener {
 | 
			
		||||
     * @param eventInfo
 | 
			
		||||
     *            事件信息
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    public boolean register(final EventInfo eventInfo) {
 | 
			
		||||
        return register(eventInfo, this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 注册事件
 | 
			
		||||
     *
 | 
			
		||||
     * @param eventInfo
 | 
			
		||||
     *            事件信息
 | 
			
		||||
     * @param listener
 | 
			
		||||
     *            监听器
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    public boolean register(final EventInfo eventInfo, final Listener listener) {
 | 
			
		||||
        try {
 | 
			
		||||
            final Class<? extends Event> clazz = (Class<? extends Event>) Class.forName(eventInfo.getClazz());
 | 
			
		||||
            final EventPriority priority = EventPriority.valueOf(eventInfo.getPriority());
 | 
			
		||||
            events.put(getIdentifier(clazz, priority), eventInfo);
 | 
			
		||||
            Bukkit.getPluginManager().registerEvent(clazz, this, priority, new EventExecutor() {
 | 
			
		||||
            Log.debug(String.format("监听器 %s 注册事件 %s 等级 %s", listener.getClass().getSimpleName(), eventInfo.getClazz(), eventInfo.getPriority()));
 | 
			
		||||
            Bukkit.getPluginManager().registerEvent(clazz, listener, priority, new EventExecutor() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void execute(final Listener listener, final Event event) throws EventException {
 | 
			
		||||
                    final EventInfo ei = plugin.getEventManager().getEvent(event, priority);
 | 
			
		||||
                    final EventInfo ei = plugin.getManagerCenter().getEventManager().getEvent(event, priority);
 | 
			
		||||
                    if (ei == null) {
 | 
			
		||||
                        Log.debug(String.format("事件 %s_%s 未找到对应处理脚本!", event.getEventName(), priority.name()));
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    debug("========== MiaoScript Debug ==========");
 | 
			
		||||
                    MLog.debug("========== MiaoScript Debug ==========");
 | 
			
		||||
                    printInfo(ei);
 | 
			
		||||
                    Player player = null;
 | 
			
		||||
                    final ConfigManager cfgmgr = ei.getModule() != null ? ei.getModule().getConfigManager() : plugin.getManagerCenter().getConfigManager();
 | 
			
		||||
                    if (event instanceof PlayerEvent) {
 | 
			
		||||
                        final PlayerEvent pe = (PlayerEvent) event;
 | 
			
		||||
                        for (final String script : ei.getScripts()) {
 | 
			
		||||
                            final String result = PlaceholderAPI.setPlaceholders(plugin.getEventMiddleware().generate(pe), script);
 | 
			
		||||
                            debug(String.format("- %s 返回值: %s", script, result));
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                    } else {
 | 
			
		||||
                        Log.debug(String.format("事件 %s 未继承 PlayerEvent 可能无法正常调用!", event.getEventName()));
 | 
			
		||||
                        player = ((PlayerEvent) event).getPlayer();
 | 
			
		||||
                    }
 | 
			
		||||
                    debug("========== MiaoScript Debug ==========");
 | 
			
		||||
                    final MiaoScriptEngine engine = MiaoScriptEngine.getDefault();
 | 
			
		||||
                    engine.put("Event", event);
 | 
			
		||||
                    engine.put("Player", player);
 | 
			
		||||
                    engine.put("Config", cfgmgr.get());
 | 
			
		||||
                    engine.put("PlayerConfig", cfgmgr);
 | 
			
		||||
                    for (final String scriptname : ei.getScripts()) {
 | 
			
		||||
                        final ScriptInfo script = plugin.getManagerCenter().getScriptManager().getScript(scriptname);
 | 
			
		||||
                        if (script == null) {
 | 
			
		||||
                            Log.debug(String.format("事件 %s_%s 未找到 %s 脚本!", event.getEventName(), priority.name(), scriptname));
 | 
			
		||||
                        }
 | 
			
		||||
                        Object result = null;
 | 
			
		||||
                        try {
 | 
			
		||||
                            engine.eval(script.getExpression(player));
 | 
			
		||||
                            result = engine.invokeFunction(EVENT_FUNCTION, new Object[] { event });
 | 
			
		||||
                        } catch (final NoSuchMethodException e1) {
 | 
			
		||||
                            Log.warning(String.format(PROCESS_NOT_FOUND, scriptname));
 | 
			
		||||
                        } catch (final ScriptException e1) {
 | 
			
		||||
                            Log.warning(String.format(INVIDE_SCRIPT, scriptname, e1.getMessage()));
 | 
			
		||||
                        }
 | 
			
		||||
                        MLog.debug(String.format("- %s 返回值: %s", scriptname, result));
 | 
			
		||||
                    }
 | 
			
		||||
                    MLog.debug("======================================");
 | 
			
		||||
                }
 | 
			
		||||
            }, plugin);
 | 
			
		||||
            return true;
 | 
			
		||||
@@ -157,12 +185,20 @@ public class EventManager implements Listener {
 | 
			
		||||
     * 注册所有事件
 | 
			
		||||
     */
 | 
			
		||||
    public void registerAll() {
 | 
			
		||||
        eventInfos.clear();
 | 
			
		||||
        for (final String event : config.getKeys(false)) {
 | 
			
		||||
            final ConfigurationSection e = config.getConfigurationSection(event);
 | 
			
		||||
            if (e == null) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            eventInfos.add(new EventInfo(event, e));
 | 
			
		||||
        }
 | 
			
		||||
        int count = 0;
 | 
			
		||||
        for (final EventInfo ei : eventInfos) {
 | 
			
		||||
            if (!ei.getScripts().isEmpty() && register(ei)) {
 | 
			
		||||
                count++;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        Log.info(String.format("已注册 %s 个事件...", count));
 | 
			
		||||
        Log.info(String.format("已注册全局事件 %s 个...", count));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,155 @@
 | 
			
		||||
package pw.yumc.MiaoScript.javascript;
 | 
			
		||||
 | 
			
		||||
import java.io.Reader;
 | 
			
		||||
 | 
			
		||||
import javax.script.Bindings;
 | 
			
		||||
import javax.script.Invocable;
 | 
			
		||||
import javax.script.ScriptContext;
 | 
			
		||||
import javax.script.ScriptEngine;
 | 
			
		||||
import javax.script.ScriptEngineFactory;
 | 
			
		||||
import javax.script.ScriptEngineManager;
 | 
			
		||||
import javax.script.ScriptException;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.MiaoScript.ManagerCenter;
 | 
			
		||||
import pw.yumc.MiaoScript.MiaoScript;
 | 
			
		||||
import pw.yumc.MiaoScript.misc.MLog;
 | 
			
		||||
import pw.yumc.MiaoScript.misc.StaticAgent;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 喵式脚本引擎
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:51:43
 | 
			
		||||
 */
 | 
			
		||||
public class MiaoScriptEngine implements ScriptEngine, Invocable {
 | 
			
		||||
    private static MiaoScriptEngine DEFAULT;
 | 
			
		||||
    private final MiaoScript plugin = P.getPlugin();
 | 
			
		||||
    private final ManagerCenter mCenter = plugin.getManagerCenter();
 | 
			
		||||
    private ScriptEngine engine;
 | 
			
		||||
 | 
			
		||||
    public MiaoScriptEngine(final String engineType) {
 | 
			
		||||
        try {
 | 
			
		||||
            engine = new ScriptEngineManager().getEngineByName(engineType);
 | 
			
		||||
        } catch (final NullPointerException ex) {
 | 
			
		||||
            Log.warning("无效的解析引擎! 已设为默认值 'javascript'");
 | 
			
		||||
            engine = new ScriptEngineManager().getEngineByName("javascript");
 | 
			
		||||
        }
 | 
			
		||||
        engine.put("Bukkit", Bukkit.getServer());
 | 
			
		||||
        engine.put("Server", Bukkit.getServer());
 | 
			
		||||
        engine.put("ActionBar", new StaticAgent.ActionBar());
 | 
			
		||||
        engine.put("Title", new StaticAgent.Title());
 | 
			
		||||
        engine.put("MainConfig", mCenter.getConfigManager().get());
 | 
			
		||||
        engine.put("PlayerConfig", mCenter.getConfigManager());
 | 
			
		||||
        engine.put("SQL", mCenter.getSQLManager());
 | 
			
		||||
        engine.put("Prefix", Log.getPrefix());
 | 
			
		||||
        engine.put("Log", P.getLogger());
 | 
			
		||||
        engine.put("MLog", MLog.LOG);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static MiaoScriptEngine getDefault() {
 | 
			
		||||
        if (DEFAULT == null) {
 | 
			
		||||
            DEFAULT = new MiaoScriptEngine("javascript");
 | 
			
		||||
        }
 | 
			
		||||
        return DEFAULT;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Bindings createBindings() {
 | 
			
		||||
        return engine.createBindings();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object eval(final Reader reader) throws ScriptException {
 | 
			
		||||
        return engine.eval(reader);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object eval(final Reader reader, final Bindings n) throws ScriptException {
 | 
			
		||||
        return engine.eval(reader, n);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object eval(final Reader reader, final ScriptContext context) throws ScriptException {
 | 
			
		||||
        return engine.eval(reader, context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object eval(final String script) throws ScriptException {
 | 
			
		||||
        MLog.debug(script.split("\n"));
 | 
			
		||||
        return engine.eval(script);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object eval(final String script, final Bindings n) throws ScriptException {
 | 
			
		||||
        return engine.eval(script, n);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object eval(final String script, final ScriptContext context) throws ScriptException {
 | 
			
		||||
        return engine.eval(script, context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object get(final String key) {
 | 
			
		||||
        return engine.get(key);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Bindings getBindings(final int scope) {
 | 
			
		||||
        return engine.getBindings(scope);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ScriptContext getContext() {
 | 
			
		||||
        return engine.getContext();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ScriptEngineFactory getFactory() {
 | 
			
		||||
        return engine.getFactory();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public <T> T getInterface(final Class<T> clasz) {
 | 
			
		||||
        return ((Invocable) engine).getInterface(clasz);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public <T> T getInterface(final Object thiz, final Class<T> clasz) {
 | 
			
		||||
        return ((Invocable) engine).getInterface(thiz, clasz);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object invokeFunction(final String name, final Object... args) throws ScriptException, NoSuchMethodException {
 | 
			
		||||
        final Object result = ((Invocable) engine).invokeFunction(name, args);
 | 
			
		||||
        engine.put("Event", null);
 | 
			
		||||
        engine.put("Player", null);
 | 
			
		||||
        engine.put("PlayerConfig", mCenter.getConfigManager());
 | 
			
		||||
        MLog.debug(String.valueOf(result));
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object invokeMethod(final Object thiz, final String name, final Object... args) throws ScriptException, NoSuchMethodException {
 | 
			
		||||
        return ((Invocable) engine).invokeMethod(thiz, name, args);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void put(final String key, final Object value) {
 | 
			
		||||
        engine.put(key, value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setBindings(final Bindings bindings, final int scope) {
 | 
			
		||||
        engine.setBindings(bindings, scope);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setContext(final ScriptContext context) {
 | 
			
		||||
        engine.setContext(context);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,117 +0,0 @@
 | 
			
		||||
package pw.yumc.MiaoScript.middleware;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.event.Event;
 | 
			
		||||
import org.bukkit.event.player.PlayerEvent;
 | 
			
		||||
import org.bukkit.metadata.MetadataValue;
 | 
			
		||||
import org.bukkit.plugin.Plugin;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 事件处理中间件
 | 
			
		||||
 *
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月25日 上午12:45:39
 | 
			
		||||
 */
 | 
			
		||||
public class EventMiddleware {
 | 
			
		||||
    public static String key = "EventMiddleware";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 生成代理玩家
 | 
			
		||||
     *
 | 
			
		||||
     * @param event
 | 
			
		||||
     *            事件
 | 
			
		||||
     * @return 处理后的Player
 | 
			
		||||
     */
 | 
			
		||||
    public Player generate(final PlayerEvent event) {
 | 
			
		||||
        final Player player = event.getPlayer();
 | 
			
		||||
        player.setMetadata(key, new EventMetaData(event));
 | 
			
		||||
        return player;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得玩家数据
 | 
			
		||||
     *
 | 
			
		||||
     * @param player
 | 
			
		||||
     *            代理玩家
 | 
			
		||||
     * @return 事件数据
 | 
			
		||||
     */
 | 
			
		||||
    public Event get(final Player player) {
 | 
			
		||||
        for (final MetadataValue mv : player.getMetadata(key)) {
 | 
			
		||||
            if (mv.getOwningPlugin().getName().equals(P.getName())) {
 | 
			
		||||
                player.removeMetadata(key, P.instance);
 | 
			
		||||
                return (Event) mv.value();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 事件源信息
 | 
			
		||||
     *
 | 
			
		||||
     * @author 喵♂呜
 | 
			
		||||
     * @since 2016年8月25日 上午12:47:53
 | 
			
		||||
     */
 | 
			
		||||
    public class EventMetaData implements MetadataValue {
 | 
			
		||||
        Event event;
 | 
			
		||||
 | 
			
		||||
        public EventMetaData(final Event event) {
 | 
			
		||||
            this.event = event;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public boolean asBoolean() {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public byte asByte() {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public double asDouble() {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public float asFloat() {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public int asInt() {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public long asLong() {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public short asShort() {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public String asString() {
 | 
			
		||||
            return event.getEventName();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public Plugin getOwningPlugin() {
 | 
			
		||||
            return P.instance;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void invalidate() {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public Object value() {
 | 
			
		||||
            return event;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								src/main/java/pw/yumc/MiaoScript/misc/MLog.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/main/java/pw/yumc/MiaoScript/misc/MLog.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
			
		||||
package pw.yumc.MiaoScript.misc;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 喵日志
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:52:02
 | 
			
		||||
 */
 | 
			
		||||
public class MLog {
 | 
			
		||||
    public static MiaoScriptLog LOG = new MiaoScriptLog();
 | 
			
		||||
    private static boolean debug = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 输出调试消息
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg
 | 
			
		||||
     *            调试消息
 | 
			
		||||
     */
 | 
			
		||||
    public static void debug(final String msg) {
 | 
			
		||||
        LOG.debug(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 输出调试消息
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg
 | 
			
		||||
     *            调试消息
 | 
			
		||||
     */
 | 
			
		||||
    public static void debug(final String[] msg) {
 | 
			
		||||
        LOG.debug(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 是否为调试模式
 | 
			
		||||
     */
 | 
			
		||||
    public static boolean isDebug() {
 | 
			
		||||
        return debug;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 设置调试模式
 | 
			
		||||
     *
 | 
			
		||||
     * @param debug
 | 
			
		||||
     *            是否调试
 | 
			
		||||
     */
 | 
			
		||||
    public static void setDebug(final boolean debug) {
 | 
			
		||||
        LOG.setDebug(debug);
 | 
			
		||||
        MLog.debug = debug;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								src/main/java/pw/yumc/MiaoScript/misc/MiaoScriptLog.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/main/java/pw/yumc/MiaoScript/misc/MiaoScriptLog.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
package pw.yumc.MiaoScript.misc;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 喵式脚本日志
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:51:53
 | 
			
		||||
 */
 | 
			
		||||
public class MiaoScriptLog {
 | 
			
		||||
    private boolean debug;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 输出调试消息
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg
 | 
			
		||||
     *            调试消息
 | 
			
		||||
     */
 | 
			
		||||
    public void debug(final String msg) {
 | 
			
		||||
        if (debug) {
 | 
			
		||||
            Log.info(msg);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 输出调试消息
 | 
			
		||||
     *
 | 
			
		||||
     * @param msg
 | 
			
		||||
     *            调试消息
 | 
			
		||||
     */
 | 
			
		||||
    public void debug(final String[] msgs) {
 | 
			
		||||
        if (debug) {
 | 
			
		||||
            for (final String msg : msgs) {
 | 
			
		||||
                Log.info(msg);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isDebug() {
 | 
			
		||||
        return debug;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setDebug(final boolean debug) {
 | 
			
		||||
        this.debug = debug;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										46
									
								
								src/main/java/pw/yumc/MiaoScript/misc/StaticAgent.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/main/java/pw/yumc/MiaoScript/misc/StaticAgent.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
package pw.yumc.MiaoScript.misc;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.World;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.bukkit.compatible.C;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 静态方法代理
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:52:10
 | 
			
		||||
 */
 | 
			
		||||
public class StaticAgent {
 | 
			
		||||
    public static class ActionBar {
 | 
			
		||||
        public void broadcast(final String msg) {
 | 
			
		||||
            C.ActionBar.broadcast(msg);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void broadcast(final String msg, final int time) {
 | 
			
		||||
            C.ActionBar.broadcast(msg, time);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void broadcast(final World world, final String msg, final int time) {
 | 
			
		||||
            C.ActionBar.broadcast(world, msg, 0);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void send(final Player player, final String msg) {
 | 
			
		||||
            C.ActionBar.send(player, msg);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void send(final Player player, final String msg, final int time) {
 | 
			
		||||
            C.ActionBar.send(player, msg, time);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class Title {
 | 
			
		||||
        public void send(final Player player, final String title, final String sub) {
 | 
			
		||||
            C.Title.send(player, title, sub);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void send(final Player player, final String title, final String sub, final int fadeInTime, final int stayTime, final int fadeOutTime) {
 | 
			
		||||
            C.Title.send(player, title, sub, fadeInTime, stayTime, fadeOutTime);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								src/main/java/pw/yumc/MiaoScript/module/MainInfo.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/main/java/pw/yumc/MiaoScript/module/MainInfo.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
package pw.yumc.MiaoScript.module;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.config.InjectConfig;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 主模块信息
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:52:25
 | 
			
		||||
 */
 | 
			
		||||
public class MainInfo extends InjectConfig {
 | 
			
		||||
    private String name;
 | 
			
		||||
    private String description;
 | 
			
		||||
 | 
			
		||||
    public MainInfo(final File file) {
 | 
			
		||||
        super(file);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 模块描述
 | 
			
		||||
     */
 | 
			
		||||
    public String getDescription() {
 | 
			
		||||
        return description;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 模块名称
 | 
			
		||||
     */
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        return name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										170
									
								
								src/main/java/pw/yumc/MiaoScript/module/ModuleInfo.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										170
									
								
								src/main/java/pw/yumc/MiaoScript/module/ModuleInfo.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,170 @@
 | 
			
		||||
package pw.yumc.MiaoScript.module;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.configuration.InvalidConfigurationException;
 | 
			
		||||
import org.bukkit.event.Listener;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.MiaoScript.ManagerCenter;
 | 
			
		||||
import pw.yumc.MiaoScript.MiaoScript;
 | 
			
		||||
import pw.yumc.MiaoScript.data.ConfigManager;
 | 
			
		||||
import pw.yumc.MiaoScript.event.EventInfo;
 | 
			
		||||
import pw.yumc.MiaoScript.script.ScriptInfo;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
import pw.yumc.YumCore.config.FileConfig;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 模块信息
 | 
			
		||||
 *
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:52:32
 | 
			
		||||
 */
 | 
			
		||||
public class ModuleInfo implements Listener {
 | 
			
		||||
    private static final String LOADINFO = "已加载模块 %s 包含脚本/变量 %s 个 事件 %s 个...";
 | 
			
		||||
    private static String MAIN = "main.yml";
 | 
			
		||||
    private static String EVENT = "event.yml";
 | 
			
		||||
    private static String SCRIPT = "script.yml";
 | 
			
		||||
 | 
			
		||||
    private final File dataFolder;
 | 
			
		||||
    private final ConfigManager configManager;
 | 
			
		||||
    private MainInfo main;
 | 
			
		||||
    private List<EventInfo> events = Collections.emptyList();
 | 
			
		||||
    private List<ScriptInfo> scripts = Collections.emptyList();
 | 
			
		||||
 | 
			
		||||
    public ModuleInfo(final File dir) throws FileNotFoundException, InvalidConfigurationException {
 | 
			
		||||
        if (!dir.isDirectory()) {
 | 
			
		||||
            throw new IllegalArgumentException("模块必须是一个目录!");
 | 
			
		||||
        }
 | 
			
		||||
        this.dataFolder = dir;
 | 
			
		||||
        this.configManager = new ConfigManager(dir);
 | 
			
		||||
        load(dir);
 | 
			
		||||
        register();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 配置管理器
 | 
			
		||||
     */
 | 
			
		||||
    public ConfigManager getConfigManager() {
 | 
			
		||||
        return configManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 模块数据目录
 | 
			
		||||
     */
 | 
			
		||||
    public File getDataFolder() {
 | 
			
		||||
        return dataFolder;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 模块注册的事件
 | 
			
		||||
     */
 | 
			
		||||
    public List<EventInfo> getEvents() {
 | 
			
		||||
        return events;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 获得模块主类
 | 
			
		||||
     */
 | 
			
		||||
    public MainInfo getMain() {
 | 
			
		||||
        return main;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 获得模块脚本
 | 
			
		||||
     */
 | 
			
		||||
    public List<ScriptInfo> getScripts() {
 | 
			
		||||
        return scripts;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 载入模块
 | 
			
		||||
     *
 | 
			
		||||
     * @param dir
 | 
			
		||||
     *            模块目录
 | 
			
		||||
     * @throws FileNotFoundException
 | 
			
		||||
     *             主类未找到
 | 
			
		||||
     * @throws InvalidConfigurationException
 | 
			
		||||
     *             主类配置文件错误
 | 
			
		||||
     */
 | 
			
		||||
    private void load(final File dir) throws FileNotFoundException, InvalidConfigurationException {
 | 
			
		||||
        final File mainFile = new File(dir, MAIN);
 | 
			
		||||
        if (!mainFile.exists()) {
 | 
			
		||||
            throw new FileNotFoundException(String.format("模块主文件 %s 未找到!", MAIN));
 | 
			
		||||
        }
 | 
			
		||||
        try {
 | 
			
		||||
            main = new MainInfo(mainFile);
 | 
			
		||||
        } catch (final Exception e) {
 | 
			
		||||
            throw new InvalidConfigurationException(String.format("模块主文件 %s 格式错误!", MAIN), e);
 | 
			
		||||
        }
 | 
			
		||||
        final File eventFile = new File(dir, EVENT);
 | 
			
		||||
        if (eventFile.exists()) {
 | 
			
		||||
            events = loadEvents(eventFile);
 | 
			
		||||
        }
 | 
			
		||||
        final File scriptFile = new File(dir, SCRIPT);
 | 
			
		||||
        if (scriptFile.exists()) {
 | 
			
		||||
            scripts = loadScripts(scriptFile);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 载入事件
 | 
			
		||||
     *
 | 
			
		||||
     * @param file
 | 
			
		||||
     *            模块目录
 | 
			
		||||
     * @return 事件列表
 | 
			
		||||
     */
 | 
			
		||||
    private List<EventInfo> loadEvents(final File file) {
 | 
			
		||||
        final List<EventInfo> eis = new ArrayList<>();
 | 
			
		||||
        final FileConfig cfg = new FileConfig(file);
 | 
			
		||||
        for (final String ek : cfg.getKeys(false)) {
 | 
			
		||||
            eis.add(new EventInfo(ek, cfg.getConfigurationSection(ek)).setModule(this));
 | 
			
		||||
        }
 | 
			
		||||
        return eis;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 载入脚本
 | 
			
		||||
     *
 | 
			
		||||
     * @param file
 | 
			
		||||
     *            模块目录
 | 
			
		||||
     * @return 脚本列表
 | 
			
		||||
     */
 | 
			
		||||
    private List<ScriptInfo> loadScripts(final File file) {
 | 
			
		||||
        final List<ScriptInfo> sis = new ArrayList<>();
 | 
			
		||||
        final FileConfig cfg = new FileConfig(file);
 | 
			
		||||
        for (final String sk : cfg.getKeys(false)) {
 | 
			
		||||
            sis.add(new ScriptInfo(sk, cfg.getConfigurationSection(sk), file.getParentFile()).setModule(this));
 | 
			
		||||
        }
 | 
			
		||||
        return sis;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void register() {
 | 
			
		||||
        final MiaoScript m = P.getPlugin();
 | 
			
		||||
        final ManagerCenter mc = m.getManagerCenter();
 | 
			
		||||
        Log.info(String.format(LOADINFO, main.getName(), registerScripts(mc), registerEvents(mc)));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private int registerEvents(final ManagerCenter mc) {
 | 
			
		||||
        int count = 0;
 | 
			
		||||
        for (final EventInfo eventInfo : events) {
 | 
			
		||||
            if (!eventInfo.getScripts().isEmpty() && mc.getEventManager().register(eventInfo, this)) {
 | 
			
		||||
                count++;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return count;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private int registerScripts(final ManagerCenter mc) {
 | 
			
		||||
        int count = 0;
 | 
			
		||||
        for (final ScriptInfo scriptInfo : scripts) {
 | 
			
		||||
            mc.getScriptManager().register(scriptInfo);
 | 
			
		||||
            count++;
 | 
			
		||||
        }
 | 
			
		||||
        return count;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								src/main/java/pw/yumc/MiaoScript/module/ModuleManager.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/main/java/pw/yumc/MiaoScript/module/ModuleManager.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
package pw.yumc.MiaoScript.module;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 模块管理
 | 
			
		||||
 * 
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月29日 上午7:52:40
 | 
			
		||||
 */
 | 
			
		||||
public class ModuleManager {
 | 
			
		||||
    private static final String LOADERROR = "目录 %s 载入模块失败 异常: %s";
 | 
			
		||||
    private static final String LOADED = "已载入 %s 个模块...";
 | 
			
		||||
    private final File MODULE_FOLDER = new File(P.getDataFolder(), "modules");
 | 
			
		||||
    private final List<ModuleInfo> modules = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    public ModuleManager() {
 | 
			
		||||
        if (!MODULE_FOLDER.exists()) {
 | 
			
		||||
            MODULE_FOLDER.mkdirs();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void loadModules() {
 | 
			
		||||
        modules.clear();
 | 
			
		||||
        int count = 0;
 | 
			
		||||
        for (final File dir : MODULE_FOLDER.listFiles()) {
 | 
			
		||||
            if (dir.isDirectory()) {
 | 
			
		||||
                try {
 | 
			
		||||
                    modules.add(new ModuleInfo(dir));
 | 
			
		||||
                    count++;
 | 
			
		||||
                } catch (final Exception e) {
 | 
			
		||||
                    Log.warning(String.format(LOADERROR, dir.getName(), e.getMessage()));
 | 
			
		||||
                    Log.debug("模块载入异常!", e);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        Log.info(String.format(LOADED, count));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,9 +4,17 @@ import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.nio.charset.Charset;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderAPI;
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
 | 
			
		||||
import pw.yumc.MiaoScript.module.ModuleInfo;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
import pw.yumc.YumCore.config.InjectConfigurationSection;
 | 
			
		||||
@@ -28,18 +36,27 @@ public class ScriptInfo extends InjectConfigurationSection {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private transient String name;
 | 
			
		||||
    private transient ModuleInfo module;
 | 
			
		||||
    private String expression;
 | 
			
		||||
    @Nullable
 | 
			
		||||
    private List<String> commands;
 | 
			
		||||
    @Nullable
 | 
			
		||||
    private String type;
 | 
			
		||||
    @Nullable
 | 
			
		||||
    private String trueResult;
 | 
			
		||||
    @Nullable
 | 
			
		||||
    private String falseResult;
 | 
			
		||||
 | 
			
		||||
    public ScriptInfo(final ConfigurationSection config) {
 | 
			
		||||
    public ScriptInfo(final String name, final ConfigurationSection config) {
 | 
			
		||||
        this(name, config, dir);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ScriptInfo(final String name, final ConfigurationSection config, final File dir) {
 | 
			
		||||
        super(config);
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        if ("boolean".equalsIgnoreCase(type) && (trueResult == null || falseResult == null)) {
 | 
			
		||||
            Log.warning(String.format("脚本 %s 返回缺少返回结果!", config.getCurrentPath()));
 | 
			
		||||
            Log.warning(String.format("脚本 %s 缺少返回结果 将使用默认值!", name));
 | 
			
		||||
        }
 | 
			
		||||
        if (expression.startsWith("file:")) {
 | 
			
		||||
            final String fileName = expression.substring(5).trim();
 | 
			
		||||
@@ -50,6 +67,7 @@ public class ScriptInfo extends InjectConfigurationSection {
 | 
			
		||||
                    Log.warning(String.format("JS文件 %s 不存在 已创建新文件 请添加脚本信息!", fileName));
 | 
			
		||||
                } else {
 | 
			
		||||
                    expression = new String(Files.readAllBytes(file.toPath()), UTF_8);
 | 
			
		||||
                    Log.debug(String.format("脚本 %s 从文件 %s 载入表达式...", name, file.toPath()));
 | 
			
		||||
                }
 | 
			
		||||
            } catch (final IOException e) {
 | 
			
		||||
                Log.warning(String.format("JS文件 %s 读取失败 异常: %s", fileName, e.getMessage()));
 | 
			
		||||
@@ -58,6 +76,16 @@ public class ScriptInfo extends InjectConfigurationSection {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 当前脚本绑定的命令
 | 
			
		||||
     */
 | 
			
		||||
    public List<String> getCommands() {
 | 
			
		||||
        if (commands == null) {
 | 
			
		||||
            commands = Collections.emptyList();
 | 
			
		||||
        }
 | 
			
		||||
        return commands;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 表达式
 | 
			
		||||
     */
 | 
			
		||||
@@ -65,24 +93,80 @@ public class ScriptInfo extends InjectConfigurationSection {
 | 
			
		||||
        return expression;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得PAPI解析的表达式
 | 
			
		||||
     *
 | 
			
		||||
     * @param p
 | 
			
		||||
     *            玩家
 | 
			
		||||
     * @return 获得解析后的表达式
 | 
			
		||||
     */
 | 
			
		||||
    public String getExpression(final Player p) {
 | 
			
		||||
        return PlaceholderAPI.setBracketPlaceholders(p, expression);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return False返回值
 | 
			
		||||
     */
 | 
			
		||||
    public String getFalseResult() {
 | 
			
		||||
        return falseResult;
 | 
			
		||||
        return falseResult == null ? PlaceholderAPIPlugin.booleanFalse() : falseResult;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 获得上层Module
 | 
			
		||||
     */
 | 
			
		||||
    public ModuleInfo getModule() {
 | 
			
		||||
        return module;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 脚本名称
 | 
			
		||||
     */
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        return name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return True返回值
 | 
			
		||||
     */
 | 
			
		||||
    public String getTrueResult() {
 | 
			
		||||
        return trueResult;
 | 
			
		||||
        return trueResult == null ? PlaceholderAPIPlugin.booleanTrue() : trueResult;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return 类型
 | 
			
		||||
     */
 | 
			
		||||
    public String getType() {
 | 
			
		||||
        return type;
 | 
			
		||||
        return type == null ? "string" : type;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送脚本信息
 | 
			
		||||
     *
 | 
			
		||||
     * @param sender
 | 
			
		||||
     *            命令接受者
 | 
			
		||||
     */
 | 
			
		||||
    public void send(final CommandSender sender) {
 | 
			
		||||
        sender.sendMessage(String.format("§6名称: §a%s §6返回值类型: §a%s §6表达式如下: ", getName(), getType()));
 | 
			
		||||
        for (final String exp : expression.split("\n")) {
 | 
			
		||||
            sender.sendMessage("§a" + exp);
 | 
			
		||||
        }
 | 
			
		||||
        final List<String> cmd = getCommands();
 | 
			
		||||
        if (!cmd.isEmpty()) {
 | 
			
		||||
            sender.sendMessage(String.format("§6绑定命令: §a%s", Arrays.toString(cmd.toArray())));
 | 
			
		||||
        }
 | 
			
		||||
        if ("boolean".equalsIgnoreCase(type)) {
 | 
			
		||||
            sender.sendMessage(String.format("§6true返回值: §a%s", getTrueResult()));
 | 
			
		||||
            sender.sendMessage(String.format("§6False返回值: §a%s", getFalseResult()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param module
 | 
			
		||||
     *            设置上层Module
 | 
			
		||||
     * @return {@link ScriptInfo}
 | 
			
		||||
     */
 | 
			
		||||
    public ScriptInfo setModule(final ModuleInfo module) {
 | 
			
		||||
        this.module = module;
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,22 @@
 | 
			
		||||
package pw.yumc.MiaoScript.script;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
import javax.script.ScriptException;
 | 
			
		||||
 | 
			
		||||
import cn.citycraft.PluginHelper.kit.Log;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.configuration.ConfigurationSection;
 | 
			
		||||
import org.bukkit.event.EventHandler;
 | 
			
		||||
import org.bukkit.event.EventPriority;
 | 
			
		||||
import org.bukkit.event.Listener;
 | 
			
		||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
 | 
			
		||||
 | 
			
		||||
import pw.yumc.MiaoScript.javascript.MiaoScriptEngine;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 脚本管理
 | 
			
		||||
@@ -14,15 +24,19 @@ import cn.citycraft.PluginHelper.kit.Log;
 | 
			
		||||
 * @author 喵♂呜
 | 
			
		||||
 * @since 2016年8月24日 下午12:51:48
 | 
			
		||||
 */
 | 
			
		||||
public class ScriptManager {
 | 
			
		||||
    Map<String, ScriptInfo> scripts = new HashMap<>();
 | 
			
		||||
public class ScriptManager implements Listener {
 | 
			
		||||
    private static final String SCRIPTLOADED = "已加载全局变量/脚本 %s 个...";
 | 
			
		||||
    private static final String CMD_FUNCTION = "process";
 | 
			
		||||
    private static final String PROCESS_NOT_FOUND = "命令脚本 %s 脚本未包含 process(Player,Command,Args) 函数!";
 | 
			
		||||
    private static final String INVIDE_SCRIPT = "命令脚本 %s 语法错误: %s";
 | 
			
		||||
    private static final String REGISTER = "命令 %s 已绑定脚本 %s ...";
 | 
			
		||||
    private final ConfigurationSection config;
 | 
			
		||||
    private final Map<String, ScriptInfo> cmds = new HashMap<>();
 | 
			
		||||
    private final Map<String, ScriptInfo> scripts = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    public ScriptManager(final ConfigurationSection config) {
 | 
			
		||||
        final Set<String> keys = config.getKeys(false);
 | 
			
		||||
        for (final String key : keys) {
 | 
			
		||||
            scripts.put(key, new ScriptInfo(config.getConfigurationSection(key)));
 | 
			
		||||
        }
 | 
			
		||||
        Log.info(String.format("已加载 %s 个变量...", keys.size()));
 | 
			
		||||
        this.config = config;
 | 
			
		||||
        Bukkit.getPluginManager().registerEvents(this, P.instance);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -35,4 +49,65 @@ public class ScriptManager {
 | 
			
		||||
    public ScriptInfo getScript(final String name) {
 | 
			
		||||
        return scripts.get(name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @EventHandler(priority = EventPriority.LOWEST)
 | 
			
		||||
    public void onPreCommand(final PlayerCommandPreprocessEvent e) {
 | 
			
		||||
        final String command = e.getMessage().substring(1);
 | 
			
		||||
        final String[] temp = command.split(" ");
 | 
			
		||||
        final String cmd = temp[0];
 | 
			
		||||
        if (cmds.containsKey(cmd)) {
 | 
			
		||||
            e.setCancelled(true);
 | 
			
		||||
            Bukkit.getScheduler().runTaskAsynchronously(P.instance, new Runnable() {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void run() {
 | 
			
		||||
                    final MiaoScriptEngine engine = MiaoScriptEngine.getDefault();
 | 
			
		||||
                    final ScriptInfo script = cmds.get(cmd);
 | 
			
		||||
                    final String[] args = Arrays.copyOfRange(temp, 1, temp.length);
 | 
			
		||||
                    try {
 | 
			
		||||
                        if (script.getModule() != null) {
 | 
			
		||||
                            engine.put("Config", script.getModule().getConfigManager().get());
 | 
			
		||||
                            engine.put("PlayerConfig", script.getModule().getConfigManager());
 | 
			
		||||
                        }
 | 
			
		||||
                        engine.eval(script.getExpression(e.getPlayer()));
 | 
			
		||||
                        engine.invokeFunction(CMD_FUNCTION, new Object[] { e.getPlayer(), cmd, args });
 | 
			
		||||
                    } catch (final NoSuchMethodException e1) {
 | 
			
		||||
                        Log.warning(String.format(PROCESS_NOT_FOUND, script.getName()));
 | 
			
		||||
                    } catch (final ScriptException e1) {
 | 
			
		||||
                        Log.warning(String.format(INVIDE_SCRIPT, script.getName(), e1.getMessage()));
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 注册脚本
 | 
			
		||||
     *
 | 
			
		||||
     * @param name
 | 
			
		||||
     *            脚本名称
 | 
			
		||||
     * @param script
 | 
			
		||||
     *            脚本信息
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    public ScriptInfo register(final ScriptInfo script) {
 | 
			
		||||
        if (script.getCommands() != null) {
 | 
			
		||||
            for (final String cmd : script.getCommands()) {
 | 
			
		||||
                cmds.put(cmd, script);
 | 
			
		||||
                Log.debug(String.format(REGISTER, cmd, script.getName()));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return scripts.put(script.getName(), script);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 注册脚本
 | 
			
		||||
     */
 | 
			
		||||
    public void registerAll() {
 | 
			
		||||
        scripts.clear();
 | 
			
		||||
        final Set<String> keys = config.getKeys(false);
 | 
			
		||||
        for (final String key : keys) {
 | 
			
		||||
            register(new ScriptInfo(key, config.getConfigurationSection(key)));
 | 
			
		||||
        }
 | 
			
		||||
        Log.info(String.format(SCRIPTLOADED, keys.size()));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +1,18 @@
 | 
			
		||||
package pw.yumc.MiaoScript.script;
 | 
			
		||||
 | 
			
		||||
import java.util.WeakHashMap;
 | 
			
		||||
 | 
			
		||||
import javax.script.ScriptEngine;
 | 
			
		||||
import javax.script.ScriptEngineManager;
 | 
			
		||||
import javax.script.ScriptException;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.event.Event;
 | 
			
		||||
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderAPI;
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderHook;
 | 
			
		||||
import pw.yumc.MiaoScript.ManagerCenter;
 | 
			
		||||
import pw.yumc.MiaoScript.MiaoScript;
 | 
			
		||||
import pw.yumc.MiaoScript.javascript.MiaoScriptEngine;
 | 
			
		||||
import pw.yumc.MiaoScript.misc.MLog;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.Log;
 | 
			
		||||
import pw.yumc.YumCore.bukkit.P;
 | 
			
		||||
import pw.yumc.YumCore.config.PlayerConfig;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 脚本执行
 | 
			
		||||
@@ -27,47 +24,26 @@ public class ScriptPlaceholder extends PlaceholderHook {
 | 
			
		||||
    private static String invalid = "无效的脚本: %s";
 | 
			
		||||
    private static String typeError = "脚本 %s 返回值错误";
 | 
			
		||||
    private static String EMPTY = "";
 | 
			
		||||
 | 
			
		||||
    private final MiaoScript plugin = P.getPlugin();
 | 
			
		||||
    private ScriptEngine engine;
 | 
			
		||||
    private final WeakHashMap<String, PlayerConfig> playerdatas = new WeakHashMap<>();
 | 
			
		||||
    private final ManagerCenter mCenter = plugin.getManagerCenter();
 | 
			
		||||
 | 
			
		||||
    public ScriptPlaceholder() {
 | 
			
		||||
        this("javascript");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ScriptPlaceholder(final String engineType) {
 | 
			
		||||
        try {
 | 
			
		||||
            engine = new ScriptEngineManager().getEngineByName(engineType);
 | 
			
		||||
        } catch (final NullPointerException ex) {
 | 
			
		||||
            Log.warning("无效的解析引擎! 已设为默认值 'javascript'");
 | 
			
		||||
            engine = new ScriptEngineManager().getEngineByName("javascript");
 | 
			
		||||
        }
 | 
			
		||||
        engine.put("Bukkit", Bukkit.getServer());
 | 
			
		||||
        engine.put("Server", Bukkit.getServer());
 | 
			
		||||
        engine.put("Data", plugin.getDataManager().getData());
 | 
			
		||||
        engine.put("Prefix", Log.getPrefix());
 | 
			
		||||
        engine.put("Log", P.getLogger());
 | 
			
		||||
    }
 | 
			
		||||
    private final ScriptEngine engine = MiaoScriptEngine.getDefault();
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String onPlaceholderRequest(final Player p, final String key) {
 | 
			
		||||
        final ScriptInfo script = plugin.getScriptManager().getScript(key);
 | 
			
		||||
        final ScriptInfo script = mCenter.getScriptManager().getScript(key);
 | 
			
		||||
        if (script == null) {
 | 
			
		||||
            return EMPTY;
 | 
			
		||||
        }
 | 
			
		||||
        String expression = script.getExpression();
 | 
			
		||||
        expression = PlaceholderAPI.setPlaceholders(p, expression);
 | 
			
		||||
        final String expression = script.getExpression(p);
 | 
			
		||||
        try {
 | 
			
		||||
            engine.put("Player", p);
 | 
			
		||||
            if (!playerdatas.containsKey(p.getName())) {
 | 
			
		||||
                playerdatas.put(p.getName(), new PlayerConfig(p));
 | 
			
		||||
            }
 | 
			
		||||
            engine.put("PlayerData", playerdatas.get(p.getName()));
 | 
			
		||||
            final Event event = plugin.getEventMiddleware().get(p);
 | 
			
		||||
            if (event != null) {
 | 
			
		||||
                engine.put("Event", event);
 | 
			
		||||
            }
 | 
			
		||||
            MLog.debug(String.format("执行脚本 %s 表达式如下: ", key));
 | 
			
		||||
            Object result = engine.eval(expression);
 | 
			
		||||
            engine.put("Event", null);
 | 
			
		||||
            engine.put("Player", null);
 | 
			
		||||
            engine.put("PlayerConfig", mCenter.getConfigManager());
 | 
			
		||||
            if (result == null) {
 | 
			
		||||
                return EMPTY;
 | 
			
		||||
            }
 | 
			
		||||
@@ -81,9 +57,11 @@ public class ScriptPlaceholder extends PlaceholderHook {
 | 
			
		||||
                    result = script.getFalseResult();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return PlaceholderAPI.setPlaceholders(p, String.valueOf(result));
 | 
			
		||||
            result = PlaceholderAPI.setPlaceholders(p, String.valueOf(result));
 | 
			
		||||
            MLog.debug(String.format("返回值: %s", result.toString()));
 | 
			
		||||
            return result.toString();
 | 
			
		||||
        } catch (final ScriptException ex) {
 | 
			
		||||
            Log.warning(String.format("脚本 %s 格式错误...", key));
 | 
			
		||||
            Log.warning(String.format(invalid, key));
 | 
			
		||||
            ex.printStackTrace();
 | 
			
		||||
            return String.format(invalid, key);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,10 @@
 | 
			
		||||
#MySQL数据库配置
 | 
			
		||||
DataBase:
 | 
			
		||||
  #默认数据库
 | 
			
		||||
  def:
 | 
			
		||||
    #数据库需要自行建立
 | 
			
		||||
    database: minecraft
 | 
			
		||||
    username: root
 | 
			
		||||
    password: root
 | 
			
		||||
    ip: 127.0.0.1
 | 
			
		||||
    port: 3306
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +0,0 @@
 | 
			
		||||
dirList:
 | 
			
		||||
- 你妈逼
 | 
			
		||||
- 我操
 | 
			
		||||
@@ -5,7 +5,7 @@ PlayerJoin:
 | 
			
		||||
  priority: LOWEST
 | 
			
		||||
  #Script List
 | 
			
		||||
  scripts:
 | 
			
		||||
  - '%ms_welcome%'
 | 
			
		||||
  - 'welcome'
 | 
			
		||||
PlayerDrop:
 | 
			
		||||
  #Event Class Full Name
 | 
			
		||||
  class: org.bukkit.event.player.PlayerDropItemEvent
 | 
			
		||||
@@ -13,20 +13,4 @@ PlayerDrop:
 | 
			
		||||
  priority: LOWEST
 | 
			
		||||
  #Script List
 | 
			
		||||
  scripts:
 | 
			
		||||
  - '%ms_checkDrop%'
 | 
			
		||||
PlayerChat:
 | 
			
		||||
  #Event Class Full Name
 | 
			
		||||
  class: org.bukkit.event.player.AsyncPlayerChatEvent
 | 
			
		||||
  #EventPriority Allow Value: LOWEST, LOW, NORMAL, HIGH, HIGHEST, MONITOR
 | 
			
		||||
  priority: LOWEST
 | 
			
		||||
  #Script List
 | 
			
		||||
  scripts:
 | 
			
		||||
  - '%ms_checkChat%'
 | 
			
		||||
PlayerCommandPreprocess:
 | 
			
		||||
  #Event Class Full Name
 | 
			
		||||
  class: org.bukkit.event.player.PlayerCommandPreprocessEvent
 | 
			
		||||
  #EventPriority Allow Value: LOWEST, LOW, NORMAL, HIGH, HIGHEST, MONITOR
 | 
			
		||||
  priority: LOWEST
 | 
			
		||||
  #Script List
 | 
			
		||||
  scripts:
 | 
			
		||||
  - '%ms_bed%'
 | 
			
		||||
  - 'checkDrop'
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +0,0 @@
 | 
			
		||||
var msg = Event.getMessage();
 | 
			
		||||
var command = msg.substring(1);
 | 
			
		||||
switch (command) {
 | 
			
		||||
case "setbed":
 | 
			
		||||
	Event.setCancelled(true);
 | 
			
		||||
	PlayerData.set("bed", Player.getLocation());
 | 
			
		||||
	PlayerData.save();
 | 
			
		||||
	Player.sendMessage(Prefix + "&a您的床位设置成功 使用&b/gobed &a即可回家!");
 | 
			
		||||
	break;
 | 
			
		||||
case "gobed":
 | 
			
		||||
	Event.setCancelled(true);
 | 
			
		||||
	if (PlayerData.isSet("bed")) {
 | 
			
		||||
		Player.teleport(PlayerData.getLocation("bed"));
 | 
			
		||||
		Player.sendMessage(Prefix + "&a已传送您回床!");
 | 
			
		||||
	} else {
 | 
			
		||||
		Player.sendMessage(Prefix + "&c请先使用 &b/setbed &c设置您的床位!");
 | 
			
		||||
	}
 | 
			
		||||
default:
 | 
			
		||||
	break;
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +0,0 @@
 | 
			
		||||
var dirs = Data.getStringList("dirChat");
 | 
			
		||||
var msg = Event.getMessage();
 | 
			
		||||
for (i in dirs) {
 | 
			
		||||
	if (msg.contains(dirs[i])) {
 | 
			
		||||
		Event.setCancelled(true);
 | 
			
		||||
		Player.sendMessage("&6[&b警告&6] &c请不要讲脏话!");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
if( Player.getName() == "Mr_jtb" ){
 | 
			
		||||
	Bukkit.broadcastMessage("&6[&a公告&6] &c热烈欢迎  &aMiaoScript &c作者 &b喵♂呜&c!");
 | 
			
		||||
} else {
 | 
			
		||||
	Player.sendMessage("&6[&bMiaoScript&6] &c欢迎来到 &b" + Bukkit.getServerName() + " &c服务器!");
 | 
			
		||||
function handle(Event) {
 | 
			
		||||
    if (Player.getName() == "Mr_jtb") {
 | 
			
		||||
        Bukkit.broadcastMessage("&6[&a公告&6] &c热烈欢迎  &aMiaoScript &c作者 &b喵♂呜&c!");
 | 
			
		||||
    } else {
 | 
			
		||||
        Player.sendMessage("&6[&bMiaoScript&6] &c欢迎来到 &b" + Bukkit.getServerName() + " &c服务器!");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								src/main/resources/modules/bed/bed.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/main/resources/modules/bed/bed.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
function process(Player, Command, Args) {
 | 
			
		||||
    var path = "bed.def"
 | 
			
		||||
    var bname = "";
 | 
			
		||||
    if (Args.length > 0) {
 | 
			
		||||
        banem = Args[0];
 | 
			
		||||
        path = "bed." + bname;
 | 
			
		||||
    }
 | 
			
		||||
    var pconfig = PlayerConfig.get(Player.getName());
 | 
			
		||||
    switch (Command) {
 | 
			
		||||
    case "setbed":
 | 
			
		||||
        pconfig.set(path, Player.getLocation());
 | 
			
		||||
        pconfig.save();
 | 
			
		||||
        Player.sendMessage(Prefix + "&a您的家设置成功 使用&b/gobed " + bname + " &a即可回家!");
 | 
			
		||||
        return true;
 | 
			
		||||
    case "gobed":
 | 
			
		||||
        if (pconfig.isSet(path)) {
 | 
			
		||||
            Player.teleport(pconfig.getLocation(path));
 | 
			
		||||
            Player.sendMessage(Prefix + "&a已传送您回家!");
 | 
			
		||||
        } else {
 | 
			
		||||
            Player.sendMessage(Prefix + "&c请先使用 &b/setbed " + bname + " &c设置您的家!");
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    default:
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										4
									
								
								src/main/resources/modules/bed/main.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/main/resources/modules/bed/main.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
#模块名称
 | 
			
		||||
name: bed
 | 
			
		||||
#模块描述
 | 
			
		||||
description: 用于设置家 以及回家
 | 
			
		||||
							
								
								
									
										8
									
								
								src/main/resources/modules/bed/script.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/main/resources/modules/bed/script.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
#脚本名称
 | 
			
		||||
bed:
 | 
			
		||||
  #脚本表达式
 | 
			
		||||
  expression: 'file: bed.js'
 | 
			
		||||
  #脚本绑定命令
 | 
			
		||||
  commands:
 | 
			
		||||
  - gobed
 | 
			
		||||
  - setbed
 | 
			
		||||
							
								
								
									
										4
									
								
								src/main/resources/modules/chatClear/config.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/main/resources/modules/chatClear/config.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
dirList:
 | 
			
		||||
- 日狗
 | 
			
		||||
- 你妹
 | 
			
		||||
- 我操
 | 
			
		||||
							
								
								
									
										8
									
								
								src/main/resources/modules/chatClear/event.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/main/resources/modules/chatClear/event.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
PlayerChat:
 | 
			
		||||
  #Event Class Full Name
 | 
			
		||||
  class: org.bukkit.event.player.AsyncPlayerChatEvent
 | 
			
		||||
  #EventPriority Allow Value: LOWEST, LOW, NORMAL, HIGH, HIGHEST, MONITOR
 | 
			
		||||
  priority: LOWEST
 | 
			
		||||
  #Script List
 | 
			
		||||
  scripts:
 | 
			
		||||
  - 'checkChat'
 | 
			
		||||
							
								
								
									
										4
									
								
								src/main/resources/modules/chatClear/main.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/main/resources/modules/chatClear/main.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
#模块名称
 | 
			
		||||
name: chatClear
 | 
			
		||||
#模块描述
 | 
			
		||||
description: 清理玩家不文明的对话
 | 
			
		||||
							
								
								
									
										14
									
								
								src/main/resources/modules/chatClear/script.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/main/resources/modules/chatClear/script.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
#脚本名称
 | 
			
		||||
checkChat:
 | 
			
		||||
  #脚本表达式
 | 
			
		||||
  expression: |
 | 
			
		||||
    function handle(Event){
 | 
			
		||||
      var dirs = Config.getStringList("dirList");
 | 
			
		||||
      var msg = Event.getMessage();
 | 
			
		||||
      for (i in dirs) {
 | 
			
		||||
        if (msg.contains(dirs[i])) {
 | 
			
		||||
          Event.setCancelled(true);
 | 
			
		||||
          Player.sendMessage("&6[&b警告&6] &c请不要讲脏话!");
 | 
			
		||||
        }
 | 
			
		||||
      } 
 | 
			
		||||
    }
 | 
			
		||||
@@ -30,7 +30,29 @@
 | 
			
		||||
#      - setCancelled(true): 用于取消事件
 | 
			
		||||
#      更多的方法请查询: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerEvent.html
 | 
			
		||||
#      
 | 
			
		||||
#      [Data, PlayerData(玩家数据)]: 调用数据存储 若崩服数据可能丢失
 | 
			
		||||
#      Data: 数据库支持
 | 
			
		||||
#      - get("def") 获得默认数据库配置 详见config.yml
 | 
			
		||||
#      例: 
 | 
			
		||||
#      var db = Data.get("def");
 | 
			
		||||
#      == 创建表 ======================================
 | 
			
		||||
#      var sql = 'CREATE TABLE IF NOT EXISTS `MiaoScript` ( `id` INTEGER PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(30), `script` VARCHAR(600)) ENGINE = InnoDB DEFAULT CHARSET=UTF8"';
 | 
			
		||||
#      db.execute(sql);
 | 
			
		||||
       #增删改 返回值为数据库改动条数 
 | 
			
		||||
#      == 插入数据 ====================================
 | 
			
		||||
#      var sql = 'INSERT INTO `MiaoScript` (`name`, `script`) VALUES ("setOp", "Player.setOp(false)")';
 | 
			
		||||
#      var result = db.update(sql);
 | 
			
		||||
#      if ( result >1 ){
 | 
			
		||||
#         Log.info("数据添加成功!");
 | 
			
		||||
#      }
 | 
			
		||||
#      == 更新数据 ====================================
 | 
			
		||||
#      var sql = 'UPDATE `MiaoScript` SET `script` = "Player.setOp(true)" WHERE name = "setOp"';
 | 
			
		||||
#      var result = db.update(sql);
 | 
			
		||||
#      == 删除数据 ====================================
 | 
			
		||||
#      var sql = 'DELETE FROM `MiaoScript` WHERE name = "setOp"';
 | 
			
		||||
#      var result = db.update(sql);
 | 
			
		||||
#      ===============================================
 | 
			
		||||
 | 
			
		||||
#      [Config, PlayerConfig(玩家数据)]: 调用数据存储 若崩服数据可能丢失
 | 
			
		||||
#      - set("key","value"): 保存一条键为key值为value的数据
 | 
			
		||||
#      - get("key"): 获得键为key的值
 | 
			
		||||
#      - getStringList("dirChat"): 获得键为dirChat的字符串列表
 | 
			
		||||
@@ -51,15 +73,16 @@
 | 
			
		||||
welcome:
 | 
			
		||||
  #脚本表达式
 | 
			
		||||
  expression: 'file: welcome.js'
 | 
			
		||||
  
 | 
			
		||||
#物品丢弃检测
 | 
			
		||||
checkDrop:
 | 
			
		||||
  expression: |
 | 
			
		||||
    if( Event.getItemDrop().getItemStack().getType().name().contains("DIAMOND") ){
 | 
			
		||||
      Player.sendMessage("&6[&b警告&6] &c您可能丢弃了一件贵重物品!");
 | 
			
		||||
    function handle(Event){
 | 
			
		||||
      if( Event.getItemDrop().getItemStack().getType().name().contains("DIAMOND") ){
 | 
			
		||||
        Player.sendMessage("&6[&b警告&6] &c您可能丢弃了一件贵重物品!");
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
#聊天检测
 | 
			
		||||
checkChat:
 | 
			
		||||
  expression: 'file: checkchat.js'
 | 
			
		||||
 | 
			
		||||
#获得玩家前缀    
 | 
			
		||||
getPrefix:
 | 
			
		||||
  expression: Player.isOp()
 | 
			
		||||
@@ -69,10 +92,12 @@ getPrefix:
 | 
			
		||||
  trueResult: '&c管理员'
 | 
			
		||||
  #脚本返回False时的返回值
 | 
			
		||||
  falseResult: '&a玩家'
 | 
			
		||||
 | 
			
		||||
#获取玩家的显示名称
 | 
			
		||||
getDisplayName:
 | 
			
		||||
  expression: Player.getDisplayName()
 | 
			
		||||
  type: string
 | 
			
		||||
 | 
			
		||||
#开启玩家飞行模式
 | 
			
		||||
flyon:
 | 
			
		||||
  expression: 'Player.setAllowFlight(true);Player.sendMessage("&6[&bMiaoScript&6] &a已为您开启飞行模式!")'
 | 
			
		||||
@@ -81,6 +106,9 @@ flyoff:
 | 
			
		||||
  expression: |
 | 
			
		||||
    Player.setAllowFlight(false);
 | 
			
		||||
    Player.sendMessage("&6[&bMiaoScript&6] &a已为您关闭飞行模式!");
 | 
			
		||||
#设置床
 | 
			
		||||
bed: 
 | 
			
		||||
  expression: 'file: bed.js'
 | 
			
		||||
 | 
			
		||||
#top命令
 | 
			
		||||
top:
 | 
			
		||||
  expression: |
 | 
			
		||||
    Player.teleport(Player.getWorld().getHighestBlockAt(Player.getLocation()).getLocation());
 | 
			
		||||
  commands: [top]
 | 
			
		||||
		Reference in New Issue
	
	Block a user