feat: 全新架构 只保留一个Java类初始化

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-09-14 20:41:34 +08:00
parent 54f6f9999d
commit fdeb2619ff
38 changed files with 462 additions and 1874 deletions

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MiaoScript</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

Binary file not shown.

105
obf.dict Normal file
View File

@ -0,0 +1,105 @@
™™
▄™
▒™
░™
▓™
™▄
▄▄
▒▄
░▄
▓▄
™▒
▄▒
▒▒
░▒
▓▒
™░
▄░
▒░
░░
▓░
™▓
▄▓
▒▓
░▓
▓▓
™▄
™▒
™░
™▓
▄™
▄▄
▄▒
▄░
▄▓
▒™
▒▄
▒░
▒▓
░™
░▄
░▒
░▓
▓™
▓▄
▓▒
▓░
™™™
▄™▄
▒™▒
░™░
▓™▓
™▄™
▄▄▄
▒▄▒
░▄░
▓▄▓
™▒™
▄▒▄
▒▒▒
░▒░
▓▒▓
™░™
▄░▄
▒░▒
░░░
▓░▓
™▓™
▄▓▄
▒▓▒
░▓░
▓▓▓
™ ™
™▄™
™▒™
™░™
™▓™
▄™▄
▄ ▄
▄▒▄
▄░▄
▄▓▄
▒™▒
▒▄▒
▒ ▒
▒░▒
▒▓▒
░™░
░▄░
░▒░
░ ░
░▓░
▓™▓
▓▄▓
▓▒▓
▓░▓
▓ ▓

223
pom.xml
View File

@ -1,113 +1,114 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId>
<version>1.1</version>
<build>
<finalName>${project.name}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>pw.yumc:YumCore</include>
<include>cn.citycraft:PluginHelper</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>pw.yumc.YumCore</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
<relocation>
<pattern>cn.citycraft.PluginHelper</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<ciManagement>
<system>Jenkins</system>
<url>http://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement>
<properties>
<update.description></update.description>
<update.changes></update.changes>
<env.GIT_COMMIT>开发版本</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>yumc-repo</id>
<url>http://repo.yumc.pw/content/groups/public/</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>jtb</id>
<name>YUMC</name>
<url>http://repo.yumc.pw/content/repositories/yumcenter/</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<type>jar</type>
<version>1.10.2-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>PluginHelper</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>pw.yumc</groupId>
<artifactId>PlaceholderAPI</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/PlaceholderAPI.jar</systemPath>
</dependency>
</dependencies>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId>
<version>1.0</version>
<developers>
<developer>
<id>502647092</id>
<name>喵♂呜</name>
<email>admin@yumc.pw</email>
<url>http://www.yumc.pw</url>
</developer>
</developers>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>pw.yumc:YumCore</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>pw.yumc.YumCore</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.13</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
<configuration>
<options>
<option>-repackageclasses \ʼ.ʽ.ʾ.${project.artifactId}</option>
<option>-keep class ${project.groupId}.${project.artifactId}.${project.artifactId}</option>
<option>-keep class ${project.groupId}.${project.artifactId}.${project.artifactId}Bungee</option>
</options>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
</libs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<ciManagement>
<system>Jenkins</system>
<url>http://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement>
<properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>yumc-repo</id>
<url>http://repo.yumc.pw/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>yumc-repo</id>
<url>http://repo.yumc.pw/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<repository>
<id>jtb</id>
<name>YUMC</name>
<url>http://repo.yumc.pw/content/repositories/yumcenter/</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId>
<type>jar</type>
<version>[1.8,)</version>
</dependency>
</dependencies>
</project>

55
proguard.conf Normal file
View File

@ -0,0 +1,55 @@
# -----不优化-----
-dontoptimize
# -----忽略所有警告-----
-dontwarn
-dontnote
# -----混淆时应用侵入式重载-----
-overloadaggressively
# -----启用混淆字典-----
-obfuscationdictionary obf.dict
-classobfuscationdictionary obf.dict
-packageobfuscationdictionary obf.dict
# -----保留所有属性
-keepattributes **
# -----保护所有实体中的字段名称-----
-keepclassmembers class * implements java.io.Serializable { <fields>; }
# -----保护监听方法不被清理-----
-keepclassmembers class * implements org.bukkit.event.Listener {
@org.bukkit.event.EventHandler <methods>;
}
-keepclassmembers class * implements net.md_5.bungee.api.plugin.Listener {
@net.md_5.bungee.event.EventHandler <methods>;
}
# -----保护继承事件不被清理-----
-keep class ** extends org.bukkit.event.Event {*;}
# -----保护枚举方法的完整性-----
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# -----保护配置注入不被清理-----
-keepclassmembers class * extends **.config.inject.Inject** {
<fields>;
public <init>(org.bukkit.configuration.ConfigurationSection);
}
# -----保护注解命令方法不被清理-----
-keepclassmembers class **.commands.annotation.** {<methods>;}
-keepclassmembers class * implements **.commands.interfaces.Executor {<methods>;}
# -----保护注解NotProguard标记-----
-keep class **.NotProguard
-keep @**.NotProguard class * {*;}
-keepclassmembers class * {
@**.NotProguard <fields>;
@**.NotProguard <methods>;
}

View File

@ -1,72 +0,0 @@
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;
}
}

View File

@ -1,21 +1,21 @@
package pw.yumc.MiaoScript;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.io.InputStreamReader;
import java.nio.file.Files;
import javax.script.ScriptEngineManager;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
import me.clip.placeholderapi.PlaceholderAPI;
import pw.yumc.MiaoScript.commands.MSCommands;
import pw.yumc.MiaoScript.javascript.MiaoScriptEngine;
import pw.yumc.MiaoScript.script.ScriptPlaceholder;
import pw.yumc.YumCore.config.FileConfig;
import pw.yumc.YumCore.annotation.NotProguard;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C;
import pw.yumc.YumCore.engine.MiaoScriptEngine;
import pw.yumc.YumCore.mc.MinecraftTools;
/**
* 喵式脚本
@ -24,131 +24,76 @@ import pw.yumc.YumCore.config.FileConfig;
* @since 2016年8月29日 上午7:50:39
*/
public class MiaoScript extends JavaPlugin {
private FileConfig config;
private ManagerCenter managerCenter;
@Override
public FileConfiguration getConfig() {
return config;
}
/**
* @return 管理中心
*/
public ManagerCenter getManagerCenter() {
return managerCenter;
}
/**
* 全局载入
*/
public void load() {
loadConfig();
loadManager();
loadScript();
loadEvents();
loadModules();
}
private MiaoScriptEngine engine;
@Override
public void onEnable() {
load();
register();
new MSCommands();
MiaoScriptEngine.getDefault();
saveScript();
loadEngine();
}
@Override
public void onLoad() {
saveDefault();
private void saveScript() {
P.saveFile("modules");
}
/**
* 注册变量
*/
public void register() {
PlaceholderAPI.registerPlaceholderHook("miaoscript", new ScriptPlaceholder());
PlaceholderAPI.registerPlaceholderHook("ms", new ScriptPlaceholder());
}
/**
* 重新载入
*/
public void reload() {
HandlerList.unregisterAll(this);
onLoad();
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() {
private void loadEngine() {
Thread currentThread = Thread.currentThread();
ClassLoader previousClassLoader = currentThread.getContextClassLoader();
currentThread.setContextClassLoader(getClassLoader());
try {
saveFile("js", "module");
} catch (final IOException e) {
ScriptEngineManager manager = new ScriptEngineManager();
this.engine = new MiaoScriptEngine(manager);
this.engine.put("base", new Base());
this.engine.eval(new InputStreamReader(this.getResource("bios.js")));
engine.invokeFunction("boot", this, engine);
} catch (Exception e) {
Log.w("脚本引擎初始化失败! %s:%s", e.getClass().getName(), e.getMessage());
} finally {
currentThread.setContextClassLoader(previousClassLoader);
}
}
/**
* 保存案例
*
* @param name
* JS文件名称
* @throws IOException
*/
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);
}
}
}
}
@NotProguard
public static class Base {
public Class getClass(String name) throws ClassNotFoundException {
return Class.forName(name);
}
public Class getLog() {
return Log.class;
}
public String read(String path) throws IOException {
Log.d("读取文件 %s ...", path);
return new String(Files.readAllBytes(new File(path).toPath()), "UTF-8");
}
public void save(String path, String content) throws IOException {
Log.d("保存文件 %s ...", path);
File file = new File(path);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes("UTF-8"));
fos.close();
}
public Class getActionBar() {
return C.ActionBar.class;
}
public Class getTitle() {
return C.Title.class;
}
public Class getPlayer() {
return C.Player.class;
}
public Class getTools() {
return MinecraftTools.class;
}
jar.close();
}
}

View File

@ -1,94 +0,0 @@
package pw.yumc.MiaoScript.commands;
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.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;
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 {
private final MiaoScript plugin = P.getPlugin();
public MSCommands() {
new CommandManager("ms", this);
}
@Cmd(permission = "MiaoScript.debug")
@Help("切换调试模式")
public void debug(final CommandArgument e) {
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.getManagerCenter().getEventManager().getEvents().values()) {
ei.send(sender);
}
}
@Cmd(permission = "MiaoScript.reload")
@Help("重载配置文件")
public void reload(final CommandArgument e) {
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();
}
}

View File

@ -1,51 +0,0 @@
package pw.yumc.MiaoScript.data;
import java.io.File;
import java.util.WeakHashMap;
import pw.yumc.YumCore.config.FileConfig;
/**
* 配置管理
*
* @author
* @since 2016年8月25日 上午2:02:03
*/
public class ConfigManager {
private final File dir;
private final WeakHashMap<String, FileConfig> 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 FileConfig(dir, name));
}
return playerconfigs.get(name);
}
/**
* 保存数据
*/
public void save() {
config.save();
}
}

View File

@ -1,60 +0,0 @@
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);
}
}

View File

@ -1,111 +0,0 @@
package pw.yumc.MiaoScript.event;
import java.util.HashSet;
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;
/**
* 事件信息
*
* @author
* @since 2016年8月24日 下午1:06:34
*/
public class EventInfo extends InjectConfigurationSection {
private transient String name;
private transient ModuleInfo module;
@ConfigNode("class")
private String clazz;
private String priority;
private List<String> scripts;
public EventInfo(final String name, final ConfigurationSection config) {
super(config);
this.name = name;
clearEmpty();
}
/**
* 清理空字串脚本
*
* @param scripts
* 脚本
* @return 整理后的脚本
*/
public void clearEmpty() {
final Set<String> cq = new HashSet<>();
for (final String s : scripts) {
if (!s.trim().isEmpty()) {
cq.add(s);
}
}
scripts = new LinkedList<>(cq);
}
/**
* @return 获得类名称
*/
public String getClazz() {
return clazz;
}
/**
* @return 获得上层Module 可能为Null
*/
public ModuleInfo getModule() {
return module;
}
/**
* @return 事件显示名称
*/
public String getName() {
return name;
}
/**
* @return 监听等级
*/
public String getPriority() {
return priority == null || "".equalsIgnoreCase(priority) ? "NORMAL" : priority;
}
/**
* @return 获得执行的脚本
*/
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;
}
}

View File

@ -1,204 +0,0 @@
package pw.yumc.MiaoScript.event;
import java.util.ArrayList;
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;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.plugin.EventExecutor;
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 {
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;
/**
* 未处理的数据
*/
private final List<EventInfo> eventInfos = new ArrayList<>();
/**
* 处理后的数据
*/
private final Map<String, EventInfo> events = new HashMap<>();
public EventManager(final ConfigurationSection config) {
this.config = config;
}
/**
* 获得事件信息
*
* @param event
* 事件
* @param priority
* 事件等级
* @return 事件处理信息
*/
public EventInfo getEvent(final Event event, final EventPriority priority) {
return getEvent(getIdentifier(event.getClass(), priority));
}
/**
* 获得事件信息
*
* @param name
* 事件占位符
* @return 事件处理信息
*/
public EventInfo getEvent(final String name) {
return events.get(name);
}
/**
* @return 获得注册的事件
*/
public Map<String, EventInfo> getEvents() {
return events;
}
/**
* 获得占位符
*
* @param event
* 事件
* @param priority
* 优先级
* @return 占位符
*/
public String getIdentifier(final Class<? extends Event> event, final EventPriority priority) {
return String.format(identifier, event.getSimpleName(), priority.name());
}
/**
* 调试
*
* @param ei
* 事件信息
*/
public void printInfo(final EventInfo ei) {
MLog.debug(String.format("名称: %s 事件: %s 优先级: %s", ei.getName(), ei.getClazz().substring(ei.getClazz().lastIndexOf(".") + 1), ei.getPriority()));
MLog.debug("脚本列表: ");
}
/**
* 注册事件
*
* @param eventInfo
* 事件信息
*/
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);
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.getManagerCenter().getEventManager().getEvent(event, priority);
if (ei == null) {
Log.debug(String.format("事件 %s_%s 未找到对应处理脚本!", event.getEventName(), priority.name()));
return;
}
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) {
player = ((PlayerEvent) event).getPlayer();
}
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;
} catch (final ClassNotFoundException e) {
Log.warning(String.format("事件 %s 的监听类 %s 未找到!", eventInfo.getName(), eventInfo.getClazz()));
} catch (final Exception e) {
Log.warning(String.format("事件 %s 的注册失败 %s: %s!", eventInfo.getName(), e.getClass().getName(), e.getMessage()));
}
return false;
}
/**
* 注册所有事件
*/
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));
}
}

View File

@ -1,155 +0,0 @@
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);
}
}

View File

@ -1,50 +0,0 @@
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;
}
}

View File

@ -1,47 +0,0 @@
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;
}
}

View File

@ -1,46 +0,0 @@
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);
}
}
}

View File

@ -1,35 +0,0 @@
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;
}
}

View File

@ -1,170 +0,0 @@
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;
}
}

View File

@ -1,44 +0,0 @@
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));
}
}

View File

@ -1,172 +0,0 @@
package pw.yumc.MiaoScript.script;
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;
import pw.yumc.YumCore.config.Nullable;
/**
* 脚本信息
*
* @author
* @since 2016年8月24日 下午12:52:09
*/
public class ScriptInfo extends InjectConfigurationSection {
public transient static final File dir = new File(P.getDataFolder(), "js");
public transient static Charset UTF_8 = Charset.forName("UTF-8");
static {
if (!dir.exists()) {
dir.mkdirs();
}
}
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 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 缺少返回结果 将使用默认值!", name));
}
if (expression.startsWith("file:")) {
final String fileName = expression.substring(5).trim();
try {
final File file = new File(dir, fileName);
if (!file.exists()) {
file.createNewFile();
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()));
expression = "";
}
}
}
/**
* @return 当前脚本绑定的命令
*/
public List<String> getCommands() {
if (commands == null) {
commands = Collections.emptyList();
}
return commands;
}
/**
* @return 表达式
*/
public String getExpression() {
return expression;
}
/**
* 获得PAPI解析的表达式
*
* @param p
* 玩家
* @return 获得解析后的表达式
*/
public String getExpression(final Player p) {
return PlaceholderAPI.setBracketPlaceholders(p, expression);
}
/**
* @return False返回值
*/
public String getFalseResult() {
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 == null ? PlaceholderAPIPlugin.booleanTrue() : trueResult;
}
/**
* @return 类型
*/
public String getType() {
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;
}
}

View File

@ -1,113 +0,0 @@
package pw.yumc.MiaoScript.script;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.script.ScriptException;
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;
/**
* 脚本管理
*
* @author
* @since 2016年8月24日 下午12:51:48
*/
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) {
this.config = config;
Bukkit.getPluginManager().registerEvents(this, P.instance);
}
/**
* 获取脚本信息
*
* @param name
* 获得脚本
* @return 脚本信息
*/
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()));
}
}

View File

@ -1,69 +0,0 @@
package pw.yumc.MiaoScript.script;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import org.bukkit.entity.Player;
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;
/**
* 脚本执行
*
* @author
* @since 2016年8月24日 下午12:51:59
*/
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 final ManagerCenter mCenter = plugin.getManagerCenter();
private final ScriptEngine engine = MiaoScriptEngine.getDefault();
@Override
public String onPlaceholderRequest(final Player p, final String key) {
final ScriptInfo script = mCenter.getScriptManager().getScript(key);
if (script == null) {
return EMPTY;
}
final String expression = script.getExpression(p);
try {
engine.put("Player", p);
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;
}
if ("boolean".equalsIgnoreCase(script.getType())) {
if (!(result instanceof Boolean)) {
return String.format(typeError, key);
}
if (((Boolean) result).booleanValue()) {
result = script.getTrueResult();
} else {
result = script.getFalseResult();
}
}
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(invalid, key));
ex.printStackTrace();
return String.format(invalid, key);
}
}
}

View File

@ -0,0 +1,12 @@
var boot;
/**
* 初始化框架引擎
*/
(function () {
boot = function (plugin, engine) {
engine.put('root', plugin.getDataFolder());
engine.put('rootDir', plugin.getDataFolder().getCanonicalPath());
load(rootDir + '/modules/init.js');
init(plugin, engine);
}
})();

View File

@ -1,16 +0,0 @@
PlayerJoin:
#Event Class Full Name
class: org.bukkit.event.player.PlayerJoinEvent
#EventPriority Allow Value: LOWEST, LOW, NORMAL, HIGH, HIGHEST, MONITOR
priority: LOWEST
#Script List
scripts:
- 'welcome'
PlayerDrop:
#Event Class Full Name
class: org.bukkit.event.player.PlayerDropItemEvent
#EventPriority Allow Value: LOWEST, LOW, NORMAL, HIGH, HIGHEST, MONITOR
priority: LOWEST
#Script List
scripts:
- 'checkDrop'

View File

@ -1,7 +0,0 @@
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服务器!");
}
}

View File

@ -1,26 +0,0 @@
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;
}
}

View File

@ -1,4 +0,0 @@
#模块名称
name: bed
#模块描述
description: 用于设置家 以及回家

View File

@ -1,8 +0,0 @@
#脚本名称
bed:
#脚本表达式
expression: 'file: bed.js'
#脚本绑定命令
commands:
- gobed
- setbed

View File

@ -1,4 +0,0 @@
dirList:
- 日狗
- 你妹
- 我操

View File

@ -1,8 +0,0 @@
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'

View File

@ -1,4 +0,0 @@
#模块名称
name: chatClear
#模块描述
description: 清理玩家不文明的对话

View File

@ -1,14 +0,0 @@
#脚本名称
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请不要讲脏话!");
}
}
}

View File

@ -0,0 +1,38 @@
/**
* 菜单基础扩展脚本
* Created by 蒋天蓓 on 2017/2/8 0008.
*/
var ext = {};
/**
* 获得静态类
* @param name 类名
* @returns {*}
*/
ext.getStatic = function (name) {
return base.getClass(name).static;
};
/**
* 获得随机数
* @param max 最大值
* @param min 最小值
*/
ext.random = function (max, min) {
min = min === undefined ? 0 : min;
return Math.floor(Math.random() * (max - min) + min);
};
/**
* 判断对象是否为Null
* @param obj 对象
* @returns {boolean} notNull返回True
*/
ext.notNull = function (obj) {
return obj !== undefined && obj !== null;
};
/**
* 判断对象是否为Null
* @param obj 对象
* @returns {boolean} Null返回True
*/
ext.isNull = function (obj) {
return obj === undefined || obj === null;
};

View File

@ -0,0 +1,9 @@
'use strict';
/*global require*/
var global = this;
load(rootDir + '/modules/ext.js');
load(rootDir + '/modules/static.js');
function init(plugin, engine) {
log.d("Version: %s", plugin.getDescription().getVersion());
}

View File

@ -0,0 +1,24 @@
/**
* 基础静态类
* Created by 蒋天蓓 on 2017/2/9 0009.
*/
/**
* 日志类
*/
var log = base.getLog().static;
/**
* ActionBar类
*/
var actionbar = base.getActionBar().static;
/**
* Title类
*/
var title = base.getTitle().static;
/**
* 玩家兼容类
*/
var cplayer = base.getPlayer().static;
/**
* 工具类
*/
var mctools = base.getTools().static;

View File

@ -0,0 +1,37 @@
package pw.yumc.MiaoScript;
import java.io.FileReader;
import javax.script.ScriptEngineManager;
import org.junit.Test;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.engine.MiaoScriptEngine;
/**
* Created with IntelliJ IDEA
*
* @author
* Created on 2017/9/14 10:08.
*/
public class MiaoScriptTest {
private MiaoScriptEngine engine;
@Test
public void testBoot() {
Thread currentThread = Thread.currentThread();
ClassLoader previousClassLoader = currentThread.getContextClassLoader();
currentThread.setContextClassLoader(getClass().getClassLoader());
try {
ScriptEngineManager manager = new ScriptEngineManager();
this.engine = new MiaoScriptEngine(manager);
this.engine.eval(new FileReader("src/main/resources/bios.js"));
engine.invokeFunction("boot", this, engine);
} catch (Exception e) {
Log.w("脚本引擎初始化失败! %s:%s", e.getClass().getName(), e.getMessage());
} finally {
currentThread.setContextClassLoader(previousClassLoader);
}
}
}