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.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptEngineManager;
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import lombok.val;
|
||||||
import pw.yumc.YumCore.annotation.NotProguard;
|
import pw.yumc.YumCore.annotation.NotProguard;
|
||||||
import pw.yumc.YumCore.bukkit.Log;
|
import pw.yumc.YumCore.bukkit.Log;
|
||||||
import pw.yumc.YumCore.bukkit.P;
|
import pw.yumc.YumCore.bukkit.P;
|
||||||
@ -80,7 +83,25 @@ public class MiaoScript extends JavaPlugin {
|
|||||||
|
|
||||||
public void save(String path, String content) throws IOException {
|
public void save(String path, String content) throws IOException {
|
||||||
Log.d("保存文件 %s ...", path);
|
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() {
|
public Class getActionBar() {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/*global Java, base, module, exports, require, __FILE__*/
|
||||||
/**
|
/**
|
||||||
* 菜单基础扩展脚本
|
* 菜单基础扩展脚本
|
||||||
* Created by 蒋天蓓 on 2017/2/8 0008.
|
* Created by 蒋天蓓 on 2017/2/8 0008.
|
||||||
|
@ -83,7 +83,9 @@ exports.read = function (file) {
|
|||||||
* @param override 是否覆盖
|
* @param override 是否覆盖
|
||||||
*/
|
*/
|
||||||
exports.save = function (path, content, 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']);
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 列出目录文件
|
* 列出目录文件
|
||||||
@ -97,3 +99,14 @@ exports.list = function (path) {
|
|||||||
log.w("路径 %s 不是一个目录 返回空数组!");
|
log.w("路径 %s 不是一个目录 返回空数组!");
|
||||||
return [];
|
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) {
|
function loadPlugins(plugin) {
|
||||||
// 初始化本体插件
|
// 初始化本体插件
|
||||||
var self = require('modules/plugin');
|
global.pluginManager = require('modules/plugin');
|
||||||
self.init(plugin, plugins_dir);
|
pluginManager.init(plugin, plugins_dir);
|
||||||
if (!self.$) {
|
// 只有当在正式环境运行的时候才加载
|
||||||
self.load();
|
if (pluginManager.$) {
|
||||||
self.enable();
|
pluginManager.load();
|
||||||
|
pluginManager.enable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,5 +59,7 @@ function loadPlugins(plugin) {
|
|||||||
* 关闭插件Hook
|
* 关闭插件Hook
|
||||||
*/
|
*/
|
||||||
function disablePlugins() {
|
function disablePlugins() {
|
||||||
require('modules/plugin').disable();
|
if (pluginManager.$) {
|
||||||
|
pluginManager.disable();
|
||||||
|
}
|
||||||
}
|
}
|
@ -6,9 +6,6 @@
|
|||||||
(function (parent, core_dir, miao_module_dir) {
|
(function (parent, core_dir, miao_module_dir) {
|
||||||
'use strict';
|
'use strict';
|
||||||
var File = Java.type("java.io.File");
|
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 file
|
||||||
* @param src
|
|
||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
function compileJs(name, src) {
|
function compileJs(file) {
|
||||||
var head = "(function (module, exports, require) {\n";
|
var cacheFile = _cacheFile(file);
|
||||||
var tail = "\n});";
|
base.save(cacheFile, "(function (module, exports, require) {" + base.read(file) + "});");
|
||||||
var fulljs = head + src + tail;
|
var obj = load(cacheFile);
|
||||||
var cacheFile = cacheDir + "/" + name + ".js";
|
base.delete(cacheFile);
|
||||||
log.d(cacheFile);
|
return obj;
|
||||||
base.save(cacheFile, fulljs);
|
|
||||||
return load(cacheFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,6 +70,10 @@
|
|||||||
return file.canonicalPath;
|
return file.canonicalPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _cacheFile(file) {
|
||||||
|
return cacheDir + "/" + file.name;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载模块
|
* 加载模块
|
||||||
* @param name 模块名称
|
* @param name 模块名称
|
||||||
@ -99,20 +97,18 @@
|
|||||||
exports: {},
|
exports: {},
|
||||||
require: exports(file.parentFile)
|
require: exports(file.parentFile)
|
||||||
};
|
};
|
||||||
var src = base.read(file);
|
|
||||||
try {
|
try {
|
||||||
// 预编译模块
|
// 预编译模块
|
||||||
var compiledWrapper = compileJs(name, src);
|
var compiledWrapper = compileJs(file);
|
||||||
compiledWrapper.apply(module.exports, [
|
compiledWrapper.apply(module.exports, [
|
||||||
module, module.exports, module.require
|
module, module.exports, module.require
|
||||||
]);
|
]);
|
||||||
|
log.d('模块 %s 编译成功!', name);
|
||||||
|
module.loaded = true;
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
log.w("模块 %s 编译失败!", name);
|
log.w("模块 %s 编译失败!", name);
|
||||||
log.d(ex);
|
log.d(ex);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
log.d('模块 %s 编译成功!', name);
|
|
||||||
module.loaded = true;
|
|
||||||
cacheModules[id] = module;
|
cacheModules[id] = module;
|
||||||
return cacheModules[id];
|
return cacheModules[id];
|
||||||
}
|
}
|
||||||
@ -131,7 +127,7 @@
|
|||||||
var cacheDir = parent + "/cache";
|
var cacheDir = parent + "/cache";
|
||||||
|
|
||||||
// 等于 undefined 说明 parent 是一个字符串 需要转成File
|
// 等于 undefined 说明 parent 是一个字符串 需要转成File
|
||||||
// 可能更加准确的方案
|
// 可能有更加准确的方案
|
||||||
if (_canonical(parent) === undefined) {
|
if (_canonical(parent) === undefined) {
|
||||||
parent = new File(parent);
|
parent = new File(parent);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/*global Java, base, module, exports, require, __FILE__*/
|
/*global Java, base, module, exports, require, __FILE__*/
|
||||||
|
@ -1,11 +1,26 @@
|
|||||||
|
'use strict';
|
||||||
/**
|
/**
|
||||||
* Bukkit基础操作
|
* Bukkit基础操作
|
||||||
* Created by 蒋天蓓 on 2017/2/9 0009.
|
* Created by 蒋天蓓 on 2017/2/9 0009.
|
||||||
*/
|
*/
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/*global Java, base, module, exports, require, __FILE__*/
|
/*global Java, base, module, exports, require, __FILE__*/
|
||||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
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) {
|
exports.broadcast = function (message) {
|
||||||
Bukkit.broadcastMessage(message);
|
Bukkit.broadcastMessage(message);
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
/**
|
||||||
|
* Bukkit 事件相关类
|
||||||
|
*/
|
||||||
/*global Java, base, module, exports, require, __FILE__*/
|
/*global Java, base, module, exports, require, __FILE__*/
|
||||||
var Thread = Java.type("java.lang.Thread");
|
var Thread = Java.type("java.lang.Thread");
|
||||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
var Bukkit = Java.type("org.bukkit.Bukkit");
|
||||||
@ -37,7 +39,7 @@ function mapEventName() {
|
|||||||
if (isVaildEvent(clz)) {
|
if (isVaildEvent(clz)) {
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
var simpleName = clz.simpleName.toLowerCase();
|
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;
|
mapEvent[simpleName] = clz;
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
@ -77,7 +79,6 @@ function listen(event, exec, priority, ignoreCancel) {
|
|||||||
if (ignoreCancel === undefined) {
|
if (ignoreCancel === undefined) {
|
||||||
ignoreCancel = false;
|
ignoreCancel = false;
|
||||||
}
|
}
|
||||||
var listener = new Java.extend(Listener, {});
|
|
||||||
// noinspection JSUnusedGlobalSymbols
|
// noinspection JSUnusedGlobalSymbols
|
||||||
/**
|
/**
|
||||||
* @param event Event type to register
|
* @param event Event type to register
|
||||||
@ -89,7 +90,7 @@ function listen(event, exec, priority, ignoreCancel) {
|
|||||||
*/
|
*/
|
||||||
Bukkit.getPluginManager().registerEvent(
|
Bukkit.getPluginManager().registerEvent(
|
||||||
eventCls,
|
eventCls,
|
||||||
listener,
|
new Listener({}),
|
||||||
EventPriority[priority],
|
EventPriority[priority],
|
||||||
new Java.extend(EventExecutor, {
|
new Java.extend(EventExecutor, {
|
||||||
execute: function (listener, event) {
|
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';
|
'use strict';
|
||||||
|
/**
|
||||||
|
* MiaoScript脚本插件加载类
|
||||||
|
*/
|
||||||
/*global Java, base, module, exports, require, __FILE__*/
|
/*global Java, base, module, exports, require, __FILE__*/
|
||||||
var zip = require("core/zip");
|
var zip = require("core/zip");
|
||||||
var fs = require('core/fs');
|
var fs = require('core/fs');
|
||||||
@ -25,9 +27,9 @@ function loadPlugins(path) {
|
|||||||
* @param path
|
* @param path
|
||||||
*/
|
*/
|
||||||
function updatePlugins(path) {
|
function updatePlugins(path) {
|
||||||
var dir = fs.file(path, "update");
|
var update = fs.file(path, "update");
|
||||||
fs.list(dir).forEach(function (file) {
|
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.plugins = [];
|
||||||
exports.init = function (plugin, path) {
|
exports.init = function (plugin, path) {
|
||||||
if (plugin !== null) {
|
if (plugin !== null) {
|
||||||
|
// 如果过plugin不等于null 则代表是正式环境
|
||||||
exports.$ = plugin;
|
exports.$ = plugin;
|
||||||
log.i("Init MiaoScript Engine Version: %s", plugin.description.version);
|
log.i("Init MiaoScript Engine Version: %s", plugin.description.version);
|
||||||
require('./event');
|
require('./event');
|
||||||
|
@ -1,25 +1,37 @@
|
|||||||
|
'use strict';
|
||||||
/**
|
/**
|
||||||
* Hello Wrold 测试插件
|
* Hello Wrold 测试插件
|
||||||
*/
|
*/
|
||||||
'use strict';
|
|
||||||
|
|
||||||
|
var papi = require("modules/ext/papi");
|
||||||
var event = require('modules/event');
|
var event = require('modules/event');
|
||||||
var joinCancel;
|
var joinCancel;
|
||||||
/*global Java, base, module, exports, require*/
|
/*global Java, base, module, exports, require*/
|
||||||
exports.description = {
|
var description = {
|
||||||
name: 'HelloWorld'
|
name: 'HelloWorld',
|
||||||
|
version: '1.0'
|
||||||
};
|
};
|
||||||
exports.load = function () {
|
|
||||||
|
function load() {
|
||||||
log.i('载入 Hello Wrold 测试插件!');
|
log.i('载入 Hello Wrold 测试插件!');
|
||||||
};
|
}
|
||||||
exports.enable = function () {
|
|
||||||
|
function enable() {
|
||||||
log.i('启用 Hello Wrold 测试插件!');
|
log.i('启用 Hello Wrold 测试插件!');
|
||||||
joinCancel = event.on('playerloginevent', function (event) {
|
joinCancel = event.on('playerloginevent', function (event) {
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
event.player.sendMessage('§a欢迎来到 §bMiaoScript §a的世界!');
|
event.player.sendMessage('§a欢迎来到 §bMiaoScript §a的世界!');
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
exports.disable = function () {
|
|
||||||
|
function disable() {
|
||||||
log.i('卸载 Hello Wrold 测试插件!');
|
log.i('卸载 Hello Wrold 测试插件!');
|
||||||
event.off(joinCancel);
|
event.off(joinCancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports = {
|
||||||
|
description: description,
|
||||||
|
load: load,
|
||||||
|
enable: enable,
|
||||||
|
disable: disable
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user