40
src/main/resources/modules/bukkit.js
Normal file
40
src/main/resources/modules/bukkit.js
Normal file
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Bukkit基础操作
|
||||
* Created by 蒋天蓓 on 2017/2/9 0009.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
||||
exports.broadcast = function (message) {
|
||||
Bukkit.broadcastMessage(message);
|
||||
};
|
||||
/**
|
||||
* 执行名称
|
||||
* @param player 玩家
|
||||
* @param command 命令
|
||||
*/
|
||||
exports.command = function (player, command) {
|
||||
Bukkit.dispatchCommand(player, command);
|
||||
};
|
||||
/**
|
||||
* 执行控制台命令
|
||||
* @param command 命令
|
||||
*/
|
||||
exports.console = function (command) {
|
||||
exports.command(Bukkit.getConsoleSender(), command);
|
||||
};
|
||||
/**
|
||||
* 玩家以OP权限执行命令
|
||||
* @param player
|
||||
* @param exper
|
||||
*/
|
||||
exports.opcommand = function (player, exper) {
|
||||
var origin = player.isOp();
|
||||
player.setOp(true);
|
||||
try {
|
||||
exports.command(player, exper);
|
||||
} finally {
|
||||
player.setOp(origin);
|
||||
}
|
||||
};
|
118
src/main/resources/modules/event.js
Normal file
118
src/main/resources/modules/event.js
Normal file
@ -0,0 +1,118 @@
|
||||
'use strict';
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var Thread = Java.type("java.lang.Thread");
|
||||
var Bukkit = Java.type("org.bukkit.Bukkit");
|
||||
var Listener = Java.type("org.bukkit.event.Listener");
|
||||
var Modifier = Java.type("java.lang.reflect.Modifier");
|
||||
var BukkitEvent = Java.type("org.bukkit.event.Event");
|
||||
var EventPriority = Java.type("org.bukkit.event.EventPriority");
|
||||
var EventExecutor = Java.type("org.bukkit.plugin.EventExecutor");
|
||||
|
||||
var mapEvent = [];
|
||||
|
||||
var plugin = require('plugin').$;
|
||||
|
||||
/**
|
||||
* 映射事件名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
||||
*/
|
||||
function mapEventName() {
|
||||
var eventPackageDir = "org/bukkit/event";
|
||||
|
||||
var dirs = Thread.currentThread().getContextClassLoader().getResources(eventPackageDir);
|
||||
while (dirs.hasMoreElements()) {
|
||||
var url = dirs.nextElement();
|
||||
var protocol = url.protocol;
|
||||
if (protocol === "jar") {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var jar = url.openConnection().jarFile;
|
||||
var entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
var entry = entries.nextElement();
|
||||
var name = entry.name;
|
||||
if (name.startsWith(eventPackageDir) && name.endsWith(".class")) {
|
||||
var i = name.replaceAll('/', '.');
|
||||
try {
|
||||
var clz = base.getClass(i.substring(0, i.length - 6));
|
||||
if (isVaildEvent(clz)) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var simpleName = clz.simpleName.toLowerCase();
|
||||
log.d("Mapping Event [%s] => %s", clz.name, simpleName);
|
||||
mapEvent[simpleName] = clz;
|
||||
}
|
||||
} catch (ex) {
|
||||
//ignore already loaded class
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isVaildEvent(clz) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
return BukkitEvent.class.isAssignableFrom(clz) && Modifier.isPublic(clz.getModifiers()) && !Modifier.isAbstract(clz.getModifiers());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加事件监听
|
||||
* @param event
|
||||
* @param exec {function}
|
||||
* @param priority
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
function listen(event, exec, priority, ignoreCancel) {
|
||||
var eventCls = mapEvent[event];
|
||||
if (!eventCls) {
|
||||
try {
|
||||
eventCls = base.getClass(eventCls);
|
||||
} catch (ex) {
|
||||
log.w("事件 %s 未找到!");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (priority === undefined) {
|
||||
priority = 'NORMAL'
|
||||
}
|
||||
if (ignoreCancel === undefined) {
|
||||
ignoreCancel = false;
|
||||
}
|
||||
var listener = new Java.extend(Listener, {});
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
/**
|
||||
* @param event Event type to register
|
||||
* @param listener Listener to register
|
||||
* @param priority Priority to register this event at
|
||||
* @param executor EventExecutor to register
|
||||
* @param plugin Plugin to register
|
||||
* @param ignoreCancel
|
||||
*/
|
||||
Bukkit.getPluginManager().registerEvent(
|
||||
eventCls,
|
||||
listener,
|
||||
EventPriority[priority],
|
||||
new Java.extend(EventExecutor, {
|
||||
execute: function (listener, event) {
|
||||
exec(event);
|
||||
}
|
||||
}),
|
||||
plugin,
|
||||
ignoreCancel);
|
||||
return {
|
||||
event: eventCls,
|
||||
listener: listener
|
||||
}
|
||||
}
|
||||
|
||||
// 映射事件名称
|
||||
mapEventName();
|
||||
|
||||
exports.on = listen;
|
||||
/**
|
||||
* 取消事件监听
|
||||
* @param listener 监听结果
|
||||
*/
|
||||
exports.off = function (listener) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
listener.event.handlerList.unregister(listener.listener);
|
||||
};
|
@ -1,38 +0,0 @@
|
||||
/**
|
||||
* 菜单基础扩展脚本
|
||||
* 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;
|
||||
};
|
@ -1,9 +0,0 @@
|
||||
'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());
|
||||
}
|
90
src/main/resources/modules/plugin.js
Normal file
90
src/main/resources/modules/plugin.js
Normal file
@ -0,0 +1,90 @@
|
||||
'use strict';
|
||||
|
||||
/*global Java, base, module, exports, require, __FILE__*/
|
||||
var zip = require("core/zip");
|
||||
var fs = require('core/fs');
|
||||
|
||||
/**
|
||||
* 载入插件
|
||||
* @param path
|
||||
*/
|
||||
function loadPlugins(path) {
|
||||
path = fs.file(path);
|
||||
log.i("开始扫描 %s 下的插件...", path);
|
||||
updatePlugins();
|
||||
var files = [];
|
||||
fs.list(path).forEach(function (file) {
|
||||
files.push(file.toFile());
|
||||
});
|
||||
loadZipPlugin(files);
|
||||
loadJsPlugin(files);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新插件
|
||||
* @param path
|
||||
*/
|
||||
function updatePlugins(path) {
|
||||
var dir = fs.file(path, "update");
|
||||
fs.list(dir).forEach(function (file) {
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* ZIP类型插件预加载
|
||||
* @param files
|
||||
*/
|
||||
function loadZipPlugin(files) {
|
||||
// // TODO ZIP类型插件加载
|
||||
// files.filter(function (file) {
|
||||
// return file.name.endsWith(".zip");
|
||||
// }).forEach(function (file) {
|
||||
// zip.unzip(fs.file(plugins_dir, file));
|
||||
// var dir = new File(plugins_dir, file.name.split(".")[0]);
|
||||
// // TODO 添加文件夹类型的插件兼容
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* JS类型插件预加载
|
||||
*/
|
||||
function loadJsPlugin(files) {
|
||||
files.filter(function (file) {
|
||||
return file.name.endsWith(".js")
|
||||
}).forEach(function (file) {
|
||||
var p = require(file);
|
||||
log.d(JSON.stringify(p));
|
||||
if (!p.description || !p.description.name) {
|
||||
log.w("文件 %s 不存在 description 描述信息 无法加载插件!");
|
||||
} else {
|
||||
exports.plugins.push(p);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
exports.$ = undefined;
|
||||
exports.plugins = [];
|
||||
exports.init = function (plugin, path) {
|
||||
if (plugin !== null) {
|
||||
exports.$ = plugin;
|
||||
log.i("Init MiaoScript Engine Version: %s", plugin.description.version);
|
||||
require('./event');
|
||||
}
|
||||
loadPlugins(path);
|
||||
};
|
||||
exports.load = function () {
|
||||
exports.plugins.forEach(function (p) {
|
||||
p.load();
|
||||
})
|
||||
};
|
||||
exports.enable = function () {
|
||||
exports.plugins.forEach(function (p) {
|
||||
p.enable();
|
||||
})
|
||||
};
|
||||
exports.disable = function () {
|
||||
exports.plugins.forEach(function (p) {
|
||||
p.disable();
|
||||
})
|
||||
};
|
@ -1,24 +0,0 @@
|
||||
/**
|
||||
* 基础静态类
|
||||
* 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;
|
Reference in New Issue
Block a user