fix: item lib error

Signed-off-by: MiaoWoo <admin@yumc.pw>
merge/3/MERGE
MiaoWoo 2019-04-28 19:32:24 +08:00
parent 565419a292
commit 4653bbc091
1 changed files with 28 additions and 19 deletions

View File

@ -5,7 +5,6 @@
*/ */
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
var Bukkit = MServer; var Bukkit = MServer;
var item = {};
var ItemStack = Java.type("org.bukkit.inventory.ItemStack"); var ItemStack = Java.type("org.bukkit.inventory.ItemStack");
var Material = Java.type('org.bukkit.Material'); var Material = Java.type('org.bukkit.Material');
var ItemIDMaterial = require('./item-id-material.json') var ItemIDMaterial = require('./item-id-material.json')
@ -17,8 +16,8 @@ var ItemIDRegex = /^[0-9]*$/
* @constructor (ID,数量) * @constructor (ID,数量)
* @constructor (ID,数量,子ID) * @constructor (ID,数量,子ID)
*/ */
item.create = function() { function create() {
var idOrType = item.type(arguments[0]); var idOrType = type(arguments[0]);
if (!idOrType) { if (!idOrType) {
throw Error('无效的物品ID或枚举 ' + arguments[0] + ' => ' + idOrType) throw Error('无效的物品ID或枚举 ' + arguments[0] + ' => ' + idOrType)
} }
@ -35,7 +34,7 @@ item.create = function() {
/** /**
* 获得物品枚举(兼容的方式) * 获得物品枚举(兼容的方式)
*/ */
item.type = function(idOrType) { function type(idOrType) {
if (arguments.length > 1) { if (arguments.length > 1) {
idOrType = Array.prototype.slice.apply(arguments); idOrType = Array.prototype.slice.apply(arguments);
} }
@ -74,8 +73,8 @@ item.type = function(idOrType) {
* 创建一个头颅 * 创建一个头颅
* @constructor (玩家名称) * @constructor (玩家名称)
*/ */
item.head = function(name) { function head(name) {
var head = item.create(397, 1, 3); var head = create(397, 1, 3);
var skullMeta = head.itemMeta; var skullMeta = head.itemMeta;
skullMeta.setOwner(name); skullMeta.setOwner(name);
head.setItemMeta(skullMeta); head.setItemMeta(skullMeta);
@ -88,11 +87,11 @@ item.head = function(name) {
* @param items 物品数组 * @param items 物品数组
* @param drop 满背包是否掉落 * @param drop 满背包是否掉落
*/ */
item.add = function(player, items, drop) { function add(player, items, drop) {
var drops = player.inventory.addItem(items).values(); var drops = player.inventory.addItem(items).values();
if (drops.size() !== 0 && drop) { if (drops.size() !== 0 && drop) {
drops.forEach(function(itemStack) { drops.forEach(function(itemStack) {
item.drop(player.location, itemStack); drop(player.location, itemStack);
}); });
} }
}; };
@ -102,7 +101,7 @@ item.add = function(player, items, drop) {
* @param loc 地点 * @param loc 地点
* @param item 物品 * @param item 物品
*/ */
item.drop = function(loc, item) { function drop(loc, item) {
setTimeout(function() { setTimeout(function() {
loc.world.dropItem(loc, item); loc.world.dropItem(loc, item);
}, 1); }, 1);
@ -114,11 +113,11 @@ item.drop = function(loc, item) {
* @param name * @param name
* @returns {*} * @returns {*}
*/ */
item.setName = function(item, name) { function setName(item, name) {
if (item.type && item.type.name() !== "AIR") { if (item.type && item.type.name() !== "AIR") {
var meta = item.meta(item); var itemMeta = meta(item);
meta.setDisplayName(name); itemMeta.setDisplayName(name);
item.setItemMeta(meta); item.setItemMeta(itemMeta);
} }
return item; return item;
}; };
@ -129,7 +128,7 @@ item.setName = function(item, name) {
* @param lores Lore * @param lores Lore
* @returns {*} 物品 * @returns {*} 物品
*/ */
item.setLore = item.setLores = function(item, lores) { function setLore(item, lores) {
var argType = toString.call(lores); var argType = toString.call(lores);
switch (argType) { switch (argType) {
case "[object String]": case "[object String]":
@ -145,15 +144,25 @@ item.setLore = item.setLores = function(item, lores) {
throw Error("Unsupport argument type " + argType + " value " + lores) throw Error("Unsupport argument type " + argType + " value " + lores)
} }
if (item.type && item.type.name() !== "AIR") { if (item.type && item.type.name() !== "AIR") {
var meta = item.meta(item); var itemMeta = meta(item);
meta.setLore(lores); itemMeta.setLore(lores);
item.setItemMeta(meta); item.setItemMeta(itemMeta);
} }
return item; return item;
}; };
item.meta = function(item) { function meta(item) {
return item.hasItemMeta() ? item.itemMeta : Bukkit.itemFactory.getItemMeta(item.type); return item.hasItemMeta() ? item.itemMeta : Bukkit.itemFactory.getItemMeta(item.type);
} }
module.exports = item; module.exports = {
create: create,
type: type,
head: head,
add: add,
drop: drop,
meta: meta,
setName: setName,
setLore: setLore,
setLores: setLore
};