From 0272518f72c31f52c2bd95547737b367b15bdd2d Mon Sep 17 00:00:00 2001 From: coding Date: Thu, 2 Nov 2017 17:35:51 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E7=B1=BB=E5=BA=93=20=E6=96=B0=E5=A2=9E=20bukkit=20=E7=9A=84?= =?UTF-8?q?=E7=89=A9=E5=93=81=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/api/item.js | 2 + src/main/resources/api/mserver.js | 3 +- src/main/resources/core/init.js | 4 +- src/main/resources/core/patch.js | 25 +++++ src/main/resources/core/require.js | 7 +- src/main/resources/internal/bukkit/item.js | 97 +++++++++++++++++++ src/main/resources/internal/bukkit/server.js | 3 - src/main/resources/internal/sponge/command.js | 28 +++++- 8 files changed, 158 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/api/item.js create mode 100644 src/main/resources/internal/bukkit/item.js diff --git a/src/main/resources/api/item.js b/src/main/resources/api/item.js new file mode 100644 index 0000000..c2fdbac --- /dev/null +++ b/src/main/resources/api/item.js @@ -0,0 +1,2 @@ +/*global Java, base, module, exports, require*/ +module.exports = require('./mserver').item; \ No newline at end of file diff --git a/src/main/resources/api/mserver.js b/src/main/resources/api/mserver.js index bddbd10..ff3d90b 100644 --- a/src/main/resources/api/mserver.js +++ b/src/main/resources/api/mserver.js @@ -9,5 +9,6 @@ module.exports = { permission: impl('permission'), plugin: impl('plugin'), server: impl('server'), - task: impl('task') + task: impl('task'), + item: impl('item') }; \ No newline at end of file diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index 474d31f..81e8f45 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -4,8 +4,8 @@ var global = this; // noinspection JSUnusedLocalSymbols function init(root) { - log.info('Init MiaoScript System...'); global.root = root; + global.noop = function () {}; loadCore(); loadRequire(); loadPatch(); @@ -17,8 +17,6 @@ function init(root) { * 初始化核心 */ function loadCore() { - global.noop = function () { - }; // 加载基础模块 load(root + '/core/ext.js'); // 探测服务器类型 diff --git a/src/main/resources/core/patch.js b/src/main/resources/core/patch.js index e4689c4..7cd0183 100644 --- a/src/main/resources/core/patch.js +++ b/src/main/resources/core/patch.js @@ -19,4 +19,29 @@ Object.prototype.toYaml = function () { return yaml.safeDump(this); } + + /** + * 日期格式化 + * 例: new Date().format('yyyy-MM-dd hh:mm:ss.s') => "2017-08-24 16:15:40.693" + * @param fmt 格式化字符串 + * @returns {*} + */ + Date.prototype.format = function (fmt) { //author: meizz + var o = { + "M+": this.getMonth() + 1, //月份 + "d+": this.getDate(), //日 + "h+": this.getHours(), //小时 + "m+": this.getMinutes(), //分 + "s+": this.getSeconds(), //秒 + "q+": Math.floor((this.getMonth() + 3) / 3), //季度 + "S": this.getMilliseconds() //毫秒 + }; + if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "") .substr(4 - RegExp.$1.length)); + for (var k in o) { + if (new RegExp("(" + k + ")").test(fmt)) { + fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? o[k] : (("00" + o[k]).substr(("" + o[k]).length))); + } + } + return fmt; + }; })(); diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index efd6949..63c17a7 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -148,6 +148,10 @@ return module; } + function _isFile(file) { + return file.isFile && file.isFile(); + } + /** * 获得文件规范路径 * @param file @@ -172,7 +176,8 @@ * @private */ function _require(name, path, optional) { - var file = name.isFile && name.isFile() ? name : resolve(name, path); + var file = new File(name); + file = _isFile(file) ? file : resolve(name, path); if (file === undefined) { console.console("§c模块 §a%s §c加载失败! §4未找到该模块!".format(name)); return {exports: {}}; diff --git a/src/main/resources/internal/bukkit/item.js b/src/main/resources/internal/bukkit/item.js new file mode 100644 index 0000000..d520c0d --- /dev/null +++ b/src/main/resources/internal/bukkit/item.js @@ -0,0 +1,97 @@ +'use strict'; +/** + * 物品快速生成类 + * Created by 蒋天蓓 on 2017/2/9 0009. + */ +/*global Java, base, module, exports, require, __FILE__*/ +var Bukkit = MServer; +var item = {}; +var ItemStack = Java.type("org.bukkit.inventory.ItemStack"); +var Material = Java.type('org.bukkit.Material'); + +/** + * 创建一个物品 + * @constructor (ID) + * @constructor (ID,数量) + * @constructor (ID,数量,子ID) + */ +item.create = function () { + var idOrType = arguments[0]; + if (isNaN(new Number(idOrType))) { + idOrType = Material[idOrType]; + } + switch (arguments.length) { + case 1: + return new ItemStack(idOrType); + case 2: + return new ItemStack(idOrType, arguments[1]); + case 3: + return new ItemStack(idOrType, arguments[1], arguments[2]); + } +}; +/** + * 创建一个头颅 + * @constructor (玩家名称) + */ +item.head = function (name) { + var head = item.create(397, 1, 3); + var skullMeta = head.getItemMeta(); + skullMeta.setOwner(name); + head.setItemMeta(skullMeta); + return head; +}; +/** + * 给玩家添加物品 + * @param player 玩家 + * @param items 物品数组 + * @param drop 满背包是否掉落 + */ +item.add = function (player, items, drop) { + var drops = player.getInventory().addItem(items).values(); + if (drops.size() !== 0 && drop) { + drops.forEach(function (itemStack) { + item.drop(player.getLocation(), itemStack); + }); + } +}; +/** + * 指定地点掉落物品 + * @param loc 地点 + * @param item 物品 + */ +item.drop = function (loc, item) { + setTimeout(function () { + loc.getWorld().dropItem(loc, item); + }, 1); +}; +/** + * 设置物品名称 + * @param item 物品 + * @param name + * @returns {*} + */ +item.setName = function (item, name) { + if (item.getType().name() !== "AIR") { + var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); + meta.setDisplayName(name); + item.setItemMeta(meta); + } + return item; +}; +/** + * 设置物品Lore + * @param item 物品 + * @param lores Lore + * @returns {*} 物品 + */ +item.setLore = item.setLores = function (item, lores) { + if (item.getType().name() !== "AIR") { + var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); + if (typeof(lores) === 'string') { lores = lores.split("\n") }; + meta.setLore(lores); + item.setItemMeta(meta); + } + return item; +}; + +module.exports = item; \ No newline at end of file diff --git a/src/main/resources/internal/bukkit/server.js b/src/main/resources/internal/bukkit/server.js index 23b5a6e..c709008 100644 --- a/src/main/resources/internal/bukkit/server.js +++ b/src/main/resources/internal/bukkit/server.js @@ -19,9 +19,6 @@ exports.nmsVersion = Bukkit.server.class.name.split('.')[3]; exports.nmsCls = function (name) { return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.')); }; -exports.command = function (name) { - return Server.getPluginCommand(name); -}; /** * 获取玩家 */ diff --git a/src/main/resources/internal/sponge/command.js b/src/main/resources/internal/sponge/command.js index 741292c..1f40fd3 100644 --- a/src/main/resources/internal/sponge/command.js +++ b/src/main/resources/internal/sponge/command.js @@ -10,11 +10,33 @@ var plugin = server.plugin.self; var CommandManager = server.CommandManager; var CommandSpec = Java.type('org.spongepowered.api.command.spec.CommandSpec'); +var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable'); + var Text = Java.type('org.spongepowered.api.text.Text'); +var Optional = Java.type('java.util.Optional'); + var ArrayList = Java.type('java.util.ArrayList'); var Arrays = Java.type('java.util.Arrays'); +var SimpleCommandCallable = function () { + this.process = function (source, arguments) { + + }, + this.getSuggestions = function (source, arguments, targetPosition) { + return Arrays.asList(''); + }, + this.testPermission = function (source) { + return true; + }, + this.getShortDescription = function (source) { + return Optional.ofNullable(''); + }, + this.getHelp = function (source) { + + } +} + function enable(jsp) { var commands = jsp.description.commands; if (commands) { @@ -22,9 +44,9 @@ function enable(jsp) { for (var name in commands) { var command = commands[name]; if (typeof command !== 'object') continue; - var newCmd = create(jsp, name); - if (command.description) newCmd.setDescription(command.description); - if (command.usage) newCmd.setUsage(command.usage); + var newCmd = CommandSpec.builder(); + if (command.description) newCmd.description(Text.of(command.description)); + // if (command.usage) newCmd.setUsage(command.usage); if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases)); if (command.permission) newCmd.setPermission(command.permission); if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']);