commit 015a39fb83f413f182cdd407877d6cbc73b28dab Author: 502647092 Date: Thu Aug 25 03:55:15 2016 +0800 init: 预发行1.0版本 Signed-off-by: 502647092 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..1921f5d --- /dev/null +++ b/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..043fc2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Eclipse stuff +/.settings + +# netbeans +/nbproject + +# we use maven! +/build.xml + +# maven +/target +/repo + +# vim +.*.sw[a-p] + +# various other potential build files +/build +/bin +/dist +/manifest.mf + +/world + +# Mac filesystem dust +*.DS_Store + +# intellij +*.iml +*.ipr +*.iws +.idea/ + +# Project Stuff +/src/main/resources/Soulbound + +# Atlassian Stuff +/atlassian-ide-plugin.xml \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..7ebb49d --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + MiaoScript + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/lib/PlaceholderAPI.jar b/lib/PlaceholderAPI.jar new file mode 100644 index 0000000..87bd3ab Binary files /dev/null and b/lib/PlaceholderAPI.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..aca299a --- /dev/null +++ b/pom.xml @@ -0,0 +1,113 @@ + + 4.0.0 + pw.yumc + MiaoScript + 1.0 + + ${project.name} + + + src/main/resources + true + + + + + maven-compiler-plugin + 3.3 + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-shade-plugin + 2.3 + + false + true + + + pw.yumc:YumCore + cn.citycraft:PluginHelper + + + + + pw.yumc.YumCore + ${project.groupId}.${project.artifactId} + + + cn.citycraft.PluginHelper + ${project.groupId}.${project.artifactId} + + + + + + package + + shade + + + + + + + + Jenkins + http://ci.yumc.pw/job/${project.artifactId}/ + + + + + DEBUG + UTF-8 + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + yumc-repo + http://repo.yumc.pw/content/groups/public/ + + + + + jtb + YUMC + http://repo.yumc.pw/content/repositories/yumcenter/ + + + + + org.spigotmc + spigot-api + jar + 1.10.2-R0.1-SNAPSHOT + + + cn.citycraft + PluginHelper + jar + 1.0 + + + pw.yumc + YumCore + jar + 1.0 + + + pw.yumc + PlaceholderAPI + 1.0 + system + ${project.basedir}/lib/PlaceholderAPI.jar + + + \ No newline at end of file diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java new file mode 100644 index 0000000..38a10f1 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java @@ -0,0 +1,127 @@ +package pw.yumc.MiaoScript; + +import java.io.File; + +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.data.DataManager; +import pw.yumc.MiaoScript.event.EventManager; +import pw.yumc.MiaoScript.middleware.EventMiddleware; +import pw.yumc.MiaoScript.script.ScriptManager; +import pw.yumc.MiaoScript.script.ScriptPlaceholder; +import pw.yumc.YumCore.config.FileConfig; + +public class MiaoScript extends JavaPlugin { + private FileConfig config; + private DataManager dataManager; + private EventManager eventManager; + private EventMiddleware eventMiddleware; + private ScriptManager scriptManager; + + @Override + public FileConfiguration getConfig() { + return config; + } + + /** + * @return 数据管理 + */ + public DataManager getDataManager() { + return dataManager; + } + + /** + * @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(); + } + + @Override + public void onEnable() { + new MSCommands(); + loadConfig(); + loadEvents(); + register(); + } + + @Override + public void onLoad() { + saveDefault(); + config = new FileConfig(); + eventMiddleware = new EventMiddleware(); + } + + /** + * 注册变量 + */ + public void register() { + PlaceholderAPI.registerPlaceholderHook("miaoscript", new ScriptPlaceholder()); + PlaceholderAPI.registerPlaceholderHook("ms", new ScriptPlaceholder()); + } + + /** + * 重新载入 + */ + public void reload() { + HandlerList.unregisterAll(this); + onLoad(); + loadConfig(); + loadEvents(); + } + + /** + * 保存默认文件 + */ + private void saveDefault() { + saveJs("bed.js"); + saveJs("welcome.js"); + saveJs("checkchat.js"); + } + + /** + * 保存案例 + * + * @param name + * JS文件名称 + */ + private void saveJs(final String name) { + if (!new File(getDataFolder(), "js" + File.separatorChar + name).exists()) { + saveResource("js" + File.separatorChar + name, false); + } + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/commands/MSCommands.java b/src/main/java/pw/yumc/MiaoScript/commands/MSCommands.java new file mode 100644 index 0000000..09c4c08 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/commands/MSCommands.java @@ -0,0 +1,48 @@ +package pw.yumc.MiaoScript.commands; + +import org.bukkit.command.CommandSender; + +import pw.yumc.MiaoScript.MiaoScript; +import pw.yumc.MiaoScript.event.EventInfo; +import pw.yumc.MiaoScript.event.EventManager; +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; + +public class MSCommands implements CommandExecutor { + MiaoScript plugin = P.getPlugin(); + + public MSCommands() { + new CommandManager("ms", this); + } + + @Cmd(permission = "MiaoScript.debug") + @Help("切换调试模式") + public void debug(final CommandArgument e) { + EventManager.debug = !EventManager.debug; + e.getSender().sendMessage("§6调试模式: " + (EventManager.debug ? "§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)); + } + } + } + + @Cmd(permission = "MiaoScript.reload") + @Help("重载配置文件") + public void reload(final CommandArgument e) { + plugin.reload(); + e.getSender().sendMessage("§a配置文件已重新载入!"); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/data/DataManager.java b/src/main/java/pw/yumc/MiaoScript/data/DataManager.java new file mode 100644 index 0000000..20aece5 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/data/DataManager.java @@ -0,0 +1,31 @@ +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(); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/event/EventInfo.java b/src/main/java/pw/yumc/MiaoScript/event/EventInfo.java new file mode 100644 index 0000000..625d0a9 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/event/EventInfo.java @@ -0,0 +1,76 @@ +package pw.yumc.MiaoScript.event; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +import org.bukkit.configuration.ConfigurationSection; + +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; + @ConfigNode(path = "class") + private String clazz; + private String priority; + private List scripts; + + public EventInfo(final String name, final ConfigurationSection config) { + super(config); + this.name = name; + clearEmpty(); + } + + /** + * 清理空字串脚本 + * + * @param scripts + * 脚本 + * @return 整理后的脚本 + */ + public void clearEmpty() { + final Set 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 事件显示名称 + */ + public String getName() { + return name; + } + + /** + * @return 监听等级 + */ + public String getPriority() { + return priority == null || "".equalsIgnoreCase(priority) ? "NORMAL" : priority; + } + + /** + * @return 获得执行的脚本 + */ + public List getScripts() { + return scripts; + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/event/EventManager.java b/src/main/java/pw/yumc/MiaoScript/event/EventManager.java new file mode 100644 index 0000000..a11b936 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/event/EventManager.java @@ -0,0 +1,168 @@ +package pw.yumc.MiaoScript.event; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +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 me.clip.placeholderapi.PlaceholderAPI; +import pw.yumc.MiaoScript.MiaoScript; +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 String identifier = "%s_%s"; + private final MiaoScript plugin = P.getPlugin(); + /** + * 未处理的数据 + */ + private final List eventInfos = new ArrayList<>(); + /** + * 处理后的数据 + */ + private final Map 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); + } + } + + /** + * 获得事件信息 + * + * @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 getEvents() { + return events; + } + + /** + * 获得占位符 + * + * @param event + * 事件 + * @param priority + * 优先级 + * @return 占位符 + */ + public String getIdentifier(final Class event, final EventPriority priority) { + return String.format(identifier, event.getSimpleName(), priority.name()); + } + + /** + * 调试 + * + * @param ei + * 事件信息 + */ + public void printInfo(final EventInfo ei) { + debug(String.format("名称: %s 事件: %s 优先级: %s", ei.getName(), ei.getClazz().substring(ei.getClazz().lastIndexOf(".") + 1), ei.getPriority())); + debug("脚本列表: "); + } + + /** + * 注册事件 + * + * @param eventInfo + * 事件信息 + */ + @SuppressWarnings("unchecked") + public boolean register(final EventInfo eventInfo) { + try { + final Class clazz = (Class) 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() { + @Override + public void execute(final Listener listener, final Event event) throws EventException { + final EventInfo ei = plugin.getEventManager().getEvent(event, priority); + if (ei == null) { + Log.debug(String.format("事件 %s_%s 未找到对应处理脚本!", event.getEventName(), priority.name())); + return; + } + debug("========== MiaoScript Debug =========="); + printInfo(ei); + 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())); + } + debug("========== MiaoScript 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() { + int count = 0; + for (final EventInfo ei : eventInfos) { + if (!ei.getScripts().isEmpty() && register(ei)) { + count++; + } + } + Log.info(String.format("已注册 %s 个事件...", count)); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/middleware/EventMiddleware.java b/src/main/java/pw/yumc/MiaoScript/middleware/EventMiddleware.java new file mode 100644 index 0000000..d3545b2 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/middleware/EventMiddleware.java @@ -0,0 +1,117 @@ +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; + } + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/script/ScriptInfo.java b/src/main/java/pw/yumc/MiaoScript/script/ScriptInfo.java new file mode 100644 index 0000000..dbe051f --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/script/ScriptInfo.java @@ -0,0 +1,88 @@ +package pw.yumc.MiaoScript.script; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; + +import org.bukkit.configuration.ConfigurationSection; + +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 String expression; + @Nullable + private String type; + @Nullable + private String trueResult; + @Nullable + private String falseResult; + + public ScriptInfo(final ConfigurationSection config) { + super(config); + if ("boolean".equalsIgnoreCase(type) && (trueResult == null || falseResult == null)) { + Log.warning(String.format("脚本 %s 返回缺少返回结果!", config.getCurrentPath())); + } + 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); + } + } catch (final IOException e) { + Log.warning(String.format("JS文件 %s 读取失败 异常: %s", fileName, e.getMessage())); + expression = ""; + } + } + } + + /** + * @return 表达式 + */ + public String getExpression() { + return expression; + } + + /** + * @return False返回值 + */ + public String getFalseResult() { + return falseResult; + } + + /** + * @return True返回值 + */ + public String getTrueResult() { + return trueResult; + } + + /** + * @return 类型 + */ + public String getType() { + return type; + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/script/ScriptManager.java b/src/main/java/pw/yumc/MiaoScript/script/ScriptManager.java new file mode 100644 index 0000000..67a5fa1 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/script/ScriptManager.java @@ -0,0 +1,38 @@ +package pw.yumc.MiaoScript.script; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.bukkit.configuration.ConfigurationSection; + +import cn.citycraft.PluginHelper.kit.Log; + +/** + * 脚本管理 + * + * @author 喵♂呜 + * @since 2016年8月24日 下午12:51:48 + */ +public class ScriptManager { + Map scripts = new HashMap<>(); + + public ScriptManager(final ConfigurationSection config) { + final Set keys = config.getKeys(false); + for (final String key : keys) { + scripts.put(key, new ScriptInfo(config.getConfigurationSection(key))); + } + Log.info(String.format("已加载 %s 个变量...", keys.size())); + } + + /** + * 获取脚本信息 + * + * @param name + * 获得脚本 + * @return 脚本信息 + */ + public ScriptInfo getScript(final String name) { + return scripts.get(name); + } +} diff --git a/src/main/java/pw/yumc/MiaoScript/script/ScriptPlaceholder.java b/src/main/java/pw/yumc/MiaoScript/script/ScriptPlaceholder.java new file mode 100644 index 0000000..293172f --- /dev/null +++ b/src/main/java/pw/yumc/MiaoScript/script/ScriptPlaceholder.java @@ -0,0 +1,91 @@ +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.MiaoScript; +import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.bukkit.P; +import pw.yumc.YumCore.config.PlayerConfig; + +/** + * 脚本执行 + * + * @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 ScriptEngine engine; + private final WeakHashMap playerdatas = new WeakHashMap<>(); + + 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()); + } + + @Override + public String onPlaceholderRequest(final Player p, final String key) { + final ScriptInfo script = plugin.getScriptManager().getScript(key); + if (script == null) { + return EMPTY; + } + String expression = script.getExpression(); + expression = PlaceholderAPI.setPlaceholders(p, expression); + 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); + } + Object result = engine.eval(expression); + 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(); + } + } + return PlaceholderAPI.setPlaceholders(p, String.valueOf(result)); + } catch (final ScriptException ex) { + Log.warning(String.format("脚本 %s 格式错误...", key)); + ex.printStackTrace(); + return String.format(invalid, key); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/data.yml b/src/main/resources/data.yml new file mode 100644 index 0000000..73f2011 --- /dev/null +++ b/src/main/resources/data.yml @@ -0,0 +1,3 @@ +dirList: +- 你妈逼 +- 我操 \ No newline at end of file diff --git a/src/main/resources/event.yml b/src/main/resources/event.yml new file mode 100644 index 0000000..08ba85f --- /dev/null +++ b/src/main/resources/event.yml @@ -0,0 +1,32 @@ +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: + - '%ms_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: + - '%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%' diff --git a/src/main/resources/js/bed.js b/src/main/resources/js/bed.js new file mode 100644 index 0000000..6052806 --- /dev/null +++ b/src/main/resources/js/bed.js @@ -0,0 +1,20 @@ +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; +} \ No newline at end of file diff --git a/src/main/resources/js/checkchat.js b/src/main/resources/js/checkchat.js new file mode 100644 index 0000000..a31af95 --- /dev/null +++ b/src/main/resources/js/checkchat.js @@ -0,0 +1,8 @@ +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请不要讲脏话!"); + } +} \ No newline at end of file diff --git a/src/main/resources/js/welcome.js b/src/main/resources/js/welcome.js new file mode 100644 index 0000000..fbd534a --- /dev/null +++ b/src/main/resources/js/welcome.js @@ -0,0 +1,5 @@ +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服务器!"); +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..b9bcccb --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,23 @@ +name: ${project.artifactId} +description: ${project.description} +main: ${project.groupId}.${project.artifactId}.${project.artifactId} +version: ${project.version}-git-${env.GIT_COMMIT} +author: 喵♂呜 +website: ${ciManagement.url} +depend: +- PlaceholderAPI +commands: + ${project.artifactId}: + description: ${project.artifactId} - ${project.description} + aliases: + - ms + usage: §b使用/${project.artifactId} help 查看帮助! + permission: ${project.artifactId}.reload + permission-message: §c你没有 的权限来执行此命令! +permissions: + ${project.artifactId}.use: + description: ${project.artifactId} 使用! + default: true + ${project.artifactId}.reload: + description: 重新载入插件! + default: op \ No newline at end of file diff --git a/src/main/resources/script.yml b/src/main/resources/script.yml new file mode 100644 index 0000000..c237303 --- /dev/null +++ b/src/main/resources/script.yml @@ -0,0 +1,86 @@ +#脚本名称 +#xxxxx: +# 脚本表达式 +# expression: | [这里写"|"则下方缩进均为一行 请看案例] +# if( x = y ) { +# 省略代码 +# } +# expression: "Player.getDisplayName()" +# PS: JS表达式 支持调用Bukkit的内部方法 +# 当前支持Bukkit(Server),Player,Event +# Bukkit(或Server): 调用的是服务器数据 +# - getOnlinePlayer().size(): 在线玩家数量 +# - broadcastMessage('xxx'): 公告xxx +# - getConsoleSender(): 获得控制台 +# - dispatchCommand(Bukkit.getConsoleSender(),"xxx"): 控制台执行xxx命令 +# - dispatchCommand(Player,"xxx"): 玩家执行xxx命令 +# 更多的方法请查询: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Bukkit.html +# +# Player: 调用玩家对象 常用方法有: +# - getName(): 玩家名称 +# - getDisplayName(): 玩家显示名称 +# - hasPermission("xxx"): 判断玩家是否有xxx权限 +# - sendMessage("xxx"): 向玩家发送xxx消息 +# - isOp():是否为OP +# - setFlying(true 或 false): 设置飞行 +# - setHealth(20): 设置玩家血量 +# 更多的方法请查询: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Player.html +# +# Event: 调用事件 不同事件有不同的参数 常用方法如下: +# - setCancelled(true): 用于取消事件 +# 更多的方法请查询: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerEvent.html +# +# [Data, PlayerData(玩家数据)]: 调用数据存储 若崩服数据可能丢失 +# - set("key","value"): 保存一条键为key值为value的数据 +# - get("key"): 获得键为key的值 +# - getStringList("dirChat"): 获得键为dirChat的字符串列表 +# - save(): 保存数据到硬盘 +# 更多的方法请查询: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/configuration/ConfigurationSection.html +# +# Log: 调用插件日志系统 +# - info("xxx"): 输出信息xxx到日志 +# - warning("xxx"): 输出警告xxx +# 更多的方法请查询: http://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html +# +# type: "boolean" 或 "string" 不写 默认为string +# #下面两个参数 当type为boolean时才有效 +# trueResult: '&c管理员' [脚本返回True时的返回值] +# falseResult: '&a玩家' [脚本返回False时的返回值] +# +#脚本名称 +welcome: + #脚本表达式 + expression: 'file: welcome.js' +#物品丢弃检测 +checkDrop: + expression: | + if( Event.getItemDrop().getItemStack().getType().name().contains("DIAMOND") ){ + Player.sendMessage("&6[&b警告&6] &c您可能丢弃了一件贵重物品!"); + } +#聊天检测 +checkChat: + expression: 'file: checkchat.js' +#获得玩家前缀 +getPrefix: + expression: Player.isOp() + #脚本返回值 + type: boolean + #脚本返回True时的返回值 + trueResult: '&c管理员' + #脚本返回False时的返回值 + falseResult: '&a玩家' +#获取玩家的显示名称 +getDisplayName: + expression: Player.getDisplayName() + type: string +#开启玩家飞行模式 +flyon: + expression: 'Player.setAllowFlight(true);Player.sendMessage("&6[&bMiaoScript&6] &a已为您开启飞行模式!")' +#关闭玩家飞行模式 +flyoff: + expression: | + Player.setAllowFlight(false); + Player.sendMessage("&6[&bMiaoScript&6] &a已为您关闭飞行模式!"); +#设置床 +bed: + expression: 'file: bed.js' \ No newline at end of file