feat: 调整类库加载逻辑 添加papi兼容
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
9556008bee
commit
2228863d56
@ -4,12 +4,15 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import lombok.val;
|
||||
import pw.yumc.YumCore.annotation.NotProguard;
|
||||
import pw.yumc.YumCore.bukkit.Log;
|
||||
import pw.yumc.YumCore.bukkit.P;
|
||||
@ -80,7 +83,25 @@ public class MiaoScript extends JavaPlugin {
|
||||
|
||||
public void save(String path, String content) throws IOException {
|
||||
Log.d("保存文件 %s ...", path);
|
||||
Files.write(new File(path).toPath(), content.getBytes("UTF-8"));
|
||||
File file = new File(path);
|
||||
file.getParentFile().mkdirs();
|
||||
Files.write(file.toPath(), content.getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
public void delete(String path) throws IOException {
|
||||
delete(new File(path).toPath());
|
||||
}
|
||||
|
||||
public void delete(Path path) throws IOException {
|
||||
val file = path.toFile();
|
||||
if (!file.exists()) { return; }
|
||||
Log.d("删除文件 %s ...", path);
|
||||
if (file.isDirectory()) {
|
||||
for (Path f : Files.list(file.toPath()).collect(Collectors.toList())) {
|
||||
delete(f);
|
||||
}
|
||||
}
|
||||
Files.delete(path);
|
||||
}
|
||||
|
||||
public Class getActionBar() {
|
||||
|
@ -1,3 +1,4 @@
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
/**
|
||||
* 菜单基础扩展脚本
|
||||
* Created by 蒋天蓓 on 2017/2/8 0008.
|
||||
|
@ -83,7 +83,9 @@ exports.read = function (file) {
|
||||
* @param override 是否覆盖
|
||||
*/
|
||||
exports.save = function (path, content, override) {
|
||||
Files.write(new File(path).toPath(), content.getBytes("UTF-8"), StandardCopyOption[override ? 'REPLACE_EXISTING' : 'ATOMIC_MOVE']);
|
||||
Files.write(new File(path).toPath(),
|
||||
content.getBytes("UTF-8"),
|
||||
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']);
|
||||
};
|
||||
/**
|
||||
* 列出目录文件
|
||||
@ -96,4 +98,15 @@ exports.list = function (path) {
|
||||
}
|
||||
log.w("路径 %s 不是一个目录 返回空数组!");
|
||||
return [];
|
||||
};
|
||||
/**
|
||||
* 移动文件
|
||||
* @param src 原始目录
|
||||
* @param des 目标目录
|
||||
* @param override 是否覆盖
|
||||
*/
|
||||
exports.move = function (src, des, override) {
|
||||
Files.move(exports.file(src).toPath(),
|
||||
exports.file(des).toPath(),
|
||||
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE'])
|
||||
};
|
@ -45,11 +45,12 @@ function loadRequire() {
|
||||
*/
|
||||
function loadPlugins(plugin) {
|
||||
// 初始化本体插件
|
||||
var self = require('modules/plugin');
|
||||
self.init(plugin, plugins_dir);
|
||||
if (!self.$) {
|
||||
self.load();
|
||||
self.enable();
|
||||
global.pluginManager = require('modules/plugin');
|
||||
pluginManager.init(plugin, plugins_dir);
|
||||
// 只有当在正式环境运行的时候才加载
|
||||
if (pluginManager.$) {
|
||||
pluginManager.load();
|
||||
pluginManager.enable();
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,5 +59,7 @@ function loadPlugins(plugin) {
|
||||
* 关闭插件Hook
|
||||
*/
|
||||
function disablePlugins() {
|
||||
require('modules/plugin').disable();
|
||||
if (pluginManager.$) {
|
||||
pluginManager.disable();
|
||||
}
|
||||
}
|
@ -6,9 +6,6 @@
|
||||
(function (parent, core_dir, miao_module_dir) {
|
||||
'use strict';
|
||||
var File = Java.type("java.io.File");
|
||||
var Files = Java.type("java.nio.file.Files");
|
||||
var String = Java.type("java.lang.String");
|
||||
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
|
||||
|
||||
/**
|
||||
* 解析模块名称为文件
|
||||
@ -51,18 +48,15 @@
|
||||
|
||||
/**
|
||||
* 预编译模块
|
||||
* @param name
|
||||
* @param src
|
||||
* @param file
|
||||
* @returns {Object}
|
||||
*/
|
||||
function compileJs(name, src) {
|
||||
var head = "(function (module, exports, require) {\n";
|
||||
var tail = "\n});";
|
||||
var fulljs = head + src + tail;
|
||||
var cacheFile = cacheDir + "/" + name + ".js";
|
||||
log.d(cacheFile);
|
||||
base.save(cacheFile, fulljs);
|
||||
return load(cacheFile);
|
||||
function compileJs(file) {
|
||||
var cacheFile = _cacheFile(file);
|
||||
base.save(cacheFile, "(function (module, exports, require) {" + base.read(file) + "});");
|
||||
var obj = load(cacheFile);
|
||||
base.delete(cacheFile);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -76,6 +70,10 @@
|
||||
return file.canonicalPath;
|
||||
}
|
||||
|
||||
function _cacheFile(file) {
|
||||
return cacheDir + "/" + file.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载模块
|
||||
* @param name 模块名称
|
||||
@ -99,20 +97,18 @@
|
||||
exports: {},
|
||||
require: exports(file.parentFile)
|
||||
};
|
||||
var src = base.read(file);
|
||||
try {
|
||||
// 预编译模块
|
||||
var compiledWrapper = compileJs(name, src);
|
||||
var compiledWrapper = compileJs(file);
|
||||
compiledWrapper.apply(module.exports, [
|
||||
module, module.exports, module.require
|
||||
]);
|
||||
log.d('模块 %s 编译成功!', name);
|
||||
module.loaded = true;
|
||||
} catch (ex) {
|
||||
log.w("模块 %s 编译失败!", name);
|
||||
log.d(ex);
|
||||
return;
|
||||
}
|
||||
log.d('模块 %s 编译成功!', name);
|
||||
module.loaded = true;
|
||||
cacheModules[id] = module;
|
||||
return cacheModules[id];
|
||||
}
|
||||
@ -131,7 +127,7 @@
|
||||
var cacheDir = parent + "/cache";
|
||||
|
||||
// 等于 undefined 说明 parent 是一个字符串 需要转成File
|
||||
// 可能更加准确的方案
|
||||
// 可能有更加准确的方案
|
||||
if (_canonical(parent) === undefined) {
|
||||
parent = new File(parent);
|
||||
}
|
||||
|
@ -1,6 +1,3 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
|
@ -1,11 +1,26 @@
|
||||
'use strict';
|
||||
/**
|
||||
* Bukkit基础操作
|
||||
* Created by 蒋天蓓 on 2017/2/9 0009.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
||||
var PluginManager = Bukkit.pluginManager;
|
||||
|
||||
exports.plugin = {
|
||||
manager: PluginManager,
|
||||
get: function (name) {
|
||||
return PluginManager.getPlugin(name);
|
||||
},
|
||||
load: function (name) {
|
||||
var plugin = this.get(name);
|
||||
if (ext.notNull(plugin) && !plugin.isEnabled()) {
|
||||
PluginManager.enablePlugin(plugin);
|
||||
}
|
||||
return PluginManager.isPluginEnabled(name);
|
||||
}
|
||||
};
|
||||
exports.broadcast = function (message) {
|
||||
Bukkit.broadcastMessage(message);
|
||||
};
|
||||
|
@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Bukkit 事件相关类
|
||||
*/
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Thread = Java.type("java.lang.Thread");
|
||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
||||
@ -37,7 +39,7 @@ function mapEventName() {
|
||||
if (isVaildEvent(clz)) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var simpleName = clz.simpleName.toLowerCase();
|
||||
log.d("Mapping Event [%s] => %s", clz.name, simpleName);
|
||||
log.fd("Mapping Event [%s] => %s", clz.name, simpleName);
|
||||
mapEvent[simpleName] = clz;
|
||||
}
|
||||
} catch (ex) {
|
||||
@ -77,7 +79,6 @@ function listen(event, exec, priority, ignoreCancel) {
|
||||
if (ignoreCancel === undefined) {
|
||||
ignoreCancel = false;
|
||||
}
|
||||
var listener = new Java.extend(Listener, {});
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
/**
|
||||
* @param event Event type to register
|
||||
@ -89,7 +90,7 @@ function listen(event, exec, priority, ignoreCancel) {
|
||||
*/
|
||||
Bukkit.getPluginManager().registerEvent(
|
||||
eventCls,
|
||||
listener,
|
||||
new Listener({}),
|
||||
EventPriority[priority],
|
||||
new Java.extend(EventExecutor, {
|
||||
execute: function (listener, event) {
|
||||
|
24
src/main/resources/modules/ext/papi.js
Normal file
24
src/main/resources/modules/ext/papi.js
Normal file
@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
/**
|
||||
* PAPI扩展类
|
||||
*/
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var PlaceholderAPI;
|
||||
var bukkit = require('modules/bukkit');
|
||||
if (bukkit.plugin.load("PlaceholderAPI")) {
|
||||
PlaceholderAPI = ext.getStatic("me.clip.placeholderapi.PlaceholderAPI");
|
||||
} else {
|
||||
log.w("PlaceholderAPI 未找到 变量替换功能失效!");
|
||||
PlaceholderAPI = {
|
||||
setPlaceholders: function () {
|
||||
return arguments[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.$ = function (player, str) {
|
||||
if (arguments.length > 1) {
|
||||
return PlaceholderAPI.setPlaceholders(arguments[0], arguments[1]);
|
||||
} else {
|
||||
return PlaceholderAPI.setPlaceholders(null, arguments[0]);
|
||||
}
|
||||
};
|
@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* MiaoScript脚本插件加载类
|
||||
*/
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var zip = require("core/zip");
|
||||
var fs = require('core/fs');
|
||||
@ -25,9 +27,9 @@ function loadPlugins(path) {
|
||||
* @param path
|
||||
*/
|
||||
function updatePlugins(path) {
|
||||
var dir = fs.file(path, "update");
|
||||
fs.list(dir).forEach(function (file) {
|
||||
|
||||
var update = fs.file(path, "update");
|
||||
fs.list(update).forEach(function (file) {
|
||||
fs.move(fs.file(update, file.name), fs.file(path, file.name), true);
|
||||
})
|
||||
}
|
||||
|
||||
@ -66,6 +68,7 @@ exports.$ = undefined;
|
||||
exports.plugins = [];
|
||||
exports.init = function (plugin, path) {
|
||||
if (plugin !== null) {
|
||||
// 如果过plugin不等于null 则代表是正式环境
|
||||
exports.$ = plugin;
|
||||
log.i("Init MiaoScript Engine Version: %s", plugin.description.version);
|
||||
require('./event');
|
||||
|
@ -1,25 +1,37 @@
|
||||
'use strict';
|
||||
/**
|
||||
* Hello Wrold 测试插件
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var papi = require("modules/ext/papi");
|
||||
var event = require('modules/event');
|
||||
var joinCancel;
|
||||
/*global Java, base, module, exports, require*/
|
||||
exports.description = {
|
||||
name: 'HelloWorld'
|
||||
var description = {
|
||||
name: 'HelloWorld',
|
||||
version: '1.0'
|
||||
};
|
||||
exports.load = function () {
|
||||
|
||||
function load() {
|
||||
log.i('载入 Hello Wrold 测试插件!');
|
||||
};
|
||||
exports.enable = function () {
|
||||
}
|
||||
|
||||
function enable() {
|
||||
log.i('启用 Hello Wrold 测试插件!');
|
||||
joinCancel = event.on('playerloginevent', function (event) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
event.player.sendMessage('§a欢迎来到 §bMiaoScript §a的世界!');
|
||||
});
|
||||
};
|
||||
exports.disable = function () {
|
||||
}
|
||||
|
||||
function disable() {
|
||||
log.i('卸载 Hello Wrold 测试插件!');
|
||||
event.off(joinCancel);
|
||||
}
|
||||
|
||||
exports = {
|
||||
description: description,
|
||||
load: load,
|
||||
enable: enable,
|
||||
disable: disable
|
||||
};
|
Loading…
Reference in New Issue
Block a user