refactor: compatible commonjs loader

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-04-23 18:44:15 +08:00
parent a875a6ca8e
commit 93d7389047
71 changed files with 78 additions and 86 deletions

View File

@ -23,7 +23,7 @@ var global = this;
// 检查类加载器 防止找不到核心文件
loader = checkClassLoader();
// 解压文件到根目录 非调试模式直接从jar解压覆盖
release(root, '(api|core|internal|modules)+/.*', !global.debug);
release(root, '(core|node_modules)+/.*', !global.debug);
load(root + '/core/init.js');
try {
init(root);

View File

@ -1,39 +0,0 @@
/*global Java, base, module, exports, require, __FILE__*/
/**
* 菜单基础扩展脚本
* 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

@ -25,8 +25,6 @@
* 初始化核心
*/
function loadCore() {
// 加载基础模块
load(root + '/core/ext.js');
// 加载Console
load(root + '/core/console.js');
// 探测服务器类型
@ -59,7 +57,7 @@
global.require = engineLoad(root + '/core/require.js')(root);
global.requireInternal = function requireInternal(name, ignoreError) {
try {
return require(root + '/internal/' + DetectServerType + '/' + name + '.js');
return require('internal/' + DetectServerType + '/' + name + '.js');
} catch (ex) {
if (ignoreError) { return {} }
throw ex;

View File

@ -31,7 +31,7 @@
var File = Java.type("java.io.File");
var separatorChar = File.separatorChar;
var cacheDir = parent + separatorChar + "runtime";
var paths = [parent, parent + separatorChar + 'core', parent + separatorChar + 'api', parent + separatorChar + 'modules'];
var paths = [parent, parent + separatorChar + 'node_modules'];
try {
base.delete(cacheDir);
@ -75,8 +75,7 @@
* 按照下列顺序查找
* 当前目录 ./
* 父目录 ../
* 核心目录 /core
* 模块目录 /modules
* 模块目录 /node_modules
* @param name 模块名称
* @param parent 父目录
*/
@ -103,7 +102,7 @@
* @returns {*}
*/
function resolveAsFile(dir, file) {
file = ext.notNull(dir) ? new File(dir, file) : new File(file);
file = dir != undefined ? new File(dir, file) : new File(file);
// 直接文件
if (file.isFile()) {
return file;
@ -125,11 +124,10 @@
* @returns {*}
*/
function resolveAsDirectory(dir, file) {
dir = ext.notNull(dir) ? new File(dir, file) : new File(file);
dir = dir != undefined ? new File(dir, file) : new File(file);
var _package = new File(dir, 'package.json');
if (_package.exists()) {
var json = JSON.parse(base.read(_package));
/** @namespace json.main */
if (json.main) {
return resolveAsFile(dir, json.main);
}

View File

@ -8,7 +8,7 @@ function EventHandlerDefault() {
var Thread = Java.type("java.lang.Thread");
// noinspection JSUnresolvedVariable
this.plugin = require('./server').plugin.self;
this.plugin = require('api/server').plugin.self;
this.mapEvent = [];
this.listenerMap = [];
this.baseEventDir = '';

View File

@ -3,8 +3,8 @@
* MiaoScript脚本插件加载类
*/
/*global Java, module, exports, require, __FILE__*/
var fs = require('core/fs');
var yaml = require('modules/yaml');
var fs = require('fs');
var yaml = require('yaml');
var event = require('./event');
var server = require('./server');
var command = require('./command');

View File

@ -7,7 +7,7 @@ var Files = Java.type("java.nio.file.Files");
var separatorChar = File.separatorChar;
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
// noinspection JSUnusedLocalSymbols
var _toString = function (obj) {
var _toString = function(obj) {
return Object.prototype.toString.call(obj);
};
@ -109,7 +109,9 @@ function read(path) {
*/
function save(path, content, override) {
var file = fs.file(path);
file.getParentFile().mkdirs();
if (file.parentFile) {
file.parentFile.mkdirs();
}
Files.write(file.toPath(), new java.lang.String(content).getBytes("UTF-8"));
}
@ -146,7 +148,7 @@ function del(file) {
// noinspection JSValidateTypes
if (file.isDirectory()) {
// noinspection JSUnresolvedVariable
Files.list(file.toPath()).collect(java.util.stream.Collector.toList()).forEach(function (f) {
Files.list(file.toPath()).collect(java.util.stream.Collector.toList()).forEach(function(f) {
del(f);
})
}

View File

@ -9,6 +9,8 @@ var chatMessageTypes;
var String = Java.type('java.lang.String');
var downgrade = false;
function init() {
nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer");
packetTypeClass = bukkit.nmsCls("PacketPlayOutChat");
@ -18,12 +20,10 @@ function init() {
packetTypeConstructor = c
}
});
// noinspection JSUnusedAssignment
nmsChatMessageTypeClass = packetTypeConstructor.parameterTypes[1];
if (nmsChatMessageTypeClass.isEnum()) {
chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants();
} else {
/** @namespace nmsChatMessageTypeClass.name */
switch (nmsChatMessageTypeClass.name) {
case "int":
nmsChatMessageTypeClass = java.lang.Integer;
@ -36,21 +36,27 @@ function init() {
}
function json(sender, json) {
if (downgrade) {
bukkit.console('/tellraw ' + sender.name + ' ' + json)
} else {
send(sender, json, 0);
}
}
function send(sender, json, type) {
var serialized = nmsChatSerializerClass.a(json);
// noinspection all
var typeObj = chatMessageTypes == null ? nmsChatMessageTypeClass.valueOf(String.valueOf(type)) : chatMessageTypes[type];
sendPacket(sender, new packetTypeClass(serialized, typeObj))
sendPacket(sender, new packetTypeClass(nmsChatSerializerClass.a(json), chatMessageTypes == null ? nmsChatMessageTypeClass.valueOf(String.valueOf(type)) : chatMessageTypes[type]))
}
function sendPacket(player, p) {
player.handle.playerConnection.sendPacket(p);
}
init();
try {
init();
} catch (ex) {
console.console("§6[§bbukkit-chat§6] §cNMS Inject Error §4" + ex + " §6Downgrade to Command Mode...")
downgrade = true;
}
exports = module.exports = {
json: json

View File

@ -4,7 +4,7 @@
*/
/*global Java, base, module, exports, require, __FILE__*/
var ref = require('modules/reflect');
var ref = require('reflect');
var bukkit = require('./server');
var plugin = bukkit.plugin.self;
var commandMap = ref.on(bukkit.plugin.manager).get('commandMap').get();

View File

@ -22,7 +22,8 @@ item.create = function() {
case "[object Number]":
break;
case "[object String]":
idOrType = Material[idOrType];
// 尝试获取老版本枚举
idOrType = Material[idOrType] || Material[Material['LEGACY_PREFIX'] + idOrType];
break;
case "[object Array]":
idOrType.forEach(function(type) {

View File

@ -14,14 +14,13 @@ PlaceholderAPI = {
// 尝试加载 Bukkit 的 PlaceholderAPI
try {
PlaceholderAPI = ext.getStatic("me.clip.placeholderapi.PlaceholderAPI");
PlaceholderAPI = base.getClass("me.clip.placeholderapi.PlaceholderAPI").static;
console.log('[PAPI] Found Bukkit PlaceholderAPI Hooking...')
} catch (ex) {
}
// 尝试加载 Sponge 的 PlaceholderAPI
try {
/** @namespace server.service */
var spongePapi = server.service.get('me.rojo8399.placeholderapi.PlaceholderService');
var TextSerializers = Java.type('org.spongepowered.api.text.serializer.TextSerializers');
var s = TextSerializers.formattingCode('§');

View File

@ -18,6 +18,16 @@ function Reflect(obj) {
this.class = obj.class;
}
this.method = function(name) {
var name = arguments[0];
var params = Array.prototype.slice.call(arguments, 1);
return declaredMethod(this.class, name, types(params));
}
this.methods = function() {
return Java.from(declaredMethods(this.class));
}
this.field = function(name) {
try {
// Try getting a public field
@ -34,7 +44,7 @@ function Reflect(obj) {
};
this.fields = function(declared) {
return declared ? this.class.declaredFields : this.class.fields;
return Java.from(declared ? this.class.declaredFields : this.class.fields);
}
this.values = function(declared) {

View File

@ -13,6 +13,8 @@ var Arrays = Java.type('java.util.Arrays');
var Material = Java.type('org.bukkit.Material');
var ItemStackArray = Java.type('org.bukkit.inventory.ItemStack[]');
var PANE = 'STAINED_GLASS_PANE'
var description = {
name: 'Lottery',
version: '1.0',
@ -25,14 +27,14 @@ var description = {
config: {
title: '§m§s§a幸运抽奖',
control: {
panel: 'GLASS_PANE:13',
ok: 'GLASS_PANE:14',
no: 'GLASS_PANE:15',
panel: PANE + ':13',
ok: PANE + ':14',
no: PANE + ':15',
},
list: [
{
box: {
id: 'GLASS_PANE',
id: PANE,
damage: 1,
name: '§a箱子',
lore: [
@ -40,7 +42,7 @@ var description = {
]
},
key: {
id: 'GLASS_PANE',
id: PANE,
damage: 2,
name: '§b钥匙',
lore: [
@ -52,7 +54,7 @@ var description = {
percent: 10,
command: 'money give %player% 100',
item: {
id: 'GLASS_PANE',
id: PANE,
damage: 3,
name: '§c奖品1',
lore: [
@ -64,7 +66,7 @@ var description = {
percent: 20,
command: 'money give %player% 200',
item: {
id: 'GLASS_PANE',
id: PANE,
damage: 4,
name: '§c奖品2',
lore: [
@ -83,14 +85,13 @@ var config;
var items;
function load() {
panel = item.create('GLASS_PANE', 1, 13);
config = this.config;
panel = newItemFromString(config.control.panel || 'GLASS_PANE:13')
panel = newItemFromString(config.control.panel || PANE + ':13')
items = new ItemStackArray(54);
item.setName(panel, '');
var ok = newItemFromString(config.control.ok || 'GLASS_PANE:14')
var ok = newItemFromString(config.control.ok || PANE + ':14')
item.setName(ok, '§a确定抽奖');
var no = newItemFromString(config.control.no || 'GLASS_PANE:15')
var no = newItemFromString(config.control.no || PANE + ':15')
item.setName(no, '§c取消抽奖');
Arrays.fill(items, 0, 10, panel);
Arrays.fill(items, 11, 16, panel);
@ -133,6 +134,8 @@ function enable() {
// noinspection JSUnusedLocalSymbols
command.on(this, 'l', {
cmd: function(sender, command, args) {
sender.inventory.addItem(newItemFromConfig(config.list[0].box))
sender.inventory.addItem(newItemFromConfig(config.list[0].key))
if (!sender.openInventory) {
console.sender(sender, "§4当前用户无法使用该命令!");
}
@ -144,7 +147,7 @@ function enable() {
});
event.on(this, 'InventoryClick', function click(event) {
var inv = event.inventory;
if (inv.title !== config.title) return;
if (inv && inv.title !== config.title) return;
var player = event.whoClicked;
var slot = event.rawSlot;
if (slot > 53 || slot < 0) {
@ -169,6 +172,10 @@ function enable() {
}
var litem;
var box = inv.getItem(10);
if (!box) {
console.sender(player, '§c请先放入抽奖物品和钥匙!');
return;
}
var key = inv.getItem(16);
if (box && box.typeId !== 0 && key && key.typeId !== 0) {
for (var i = 0; i < config.list.length; i++) {
@ -194,7 +201,7 @@ function enable() {
resultList.push(t);
}
});
var ri = ext.random(resultList.length);
var ri = random(resultList.length);
var result = resultList[ri];
box.amount = box.amount - 1;
key.amount = key.amount - 1;
@ -209,6 +216,11 @@ function enable() {
});
}
function random(max, min) {
min = min === undefined ? 0 : min;
return Math.floor(Math.random() * (max - min) + min);
};
function disable() {
}

View File

@ -94,10 +94,15 @@ function FakeTag(name) {
try {
var ScoreboardBaseCriteria = bukkit.nmsCls('ScoreboardBaseCriteria');
} catch (ex) {
ver1_13 = true;
try {
var IScoreboardCriteria = bukkit.nmsCls('IScoreboardCriteria');
var ScoreboardServer = bukkit.nmsCls("ScoreboardServer");
var ChatComponentText = bukkit.nmsCls('ChatComponentText');
ver1_13 = true;
} catch (ex) {
console.log(ex);
throw ex;
}
}
var PacketPlayOutScoreboardScore = bukkit.nmsCls('PacketPlayOutScoreboardScore');
var PacketPlayOutScoreboardObjective = bukkit.nmsCls('PacketPlayOutScoreboardObjective');