feat: optimize item lib

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-04-28 19:18:53 +08:00
parent a28386401c
commit 565419a292
2 changed files with 63 additions and 29 deletions

View File

@ -18,7 +18,27 @@ var ItemIDRegex = /^[0-9]*$/
* @constructor (ID,数量,子ID) * @constructor (ID,数量,子ID)
*/ */
item.create = function() { item.create = function() {
var idOrType = arguments[0]; var idOrType = item.type(arguments[0]);
if (!idOrType) {
throw Error('无效的物品ID或枚举 ' + arguments[0] + ' => ' + 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]);
}
};
/**
* 获得物品枚举(兼容的方式)
*/
item.type = function(idOrType) {
if (arguments.length > 1) {
idOrType = Array.prototype.slice.apply(arguments);
}
var argType = toString.call(idOrType) var argType = toString.call(idOrType)
switch (argType) { switch (argType) {
case "[object Number]": case "[object Number]":
@ -34,39 +54,34 @@ item.create = function() {
idOrType = Material[idOrType] || Material[Material['LEGACY_PREFIX'] + idOrType]; idOrType = Material[idOrType] || Material[Material['LEGACY_PREFIX'] + idOrType];
break; break;
case "[object Array]": case "[object Array]":
idOrType.forEach(function(type) { idOrType.some(function(type) {
var temp = Material[type]; var temp = item.type(type);
if (temp) { if (temp) {
idOrType = temp; idOrType = temp;
return; return true;
} }
}) });
case "[object Undefined]":
case "[object Null]":
return idOrType;
default: default:
throw Error("Unsupport argument type " + argType + " value " + idOrType) throw Error("Unsupport argument type " + argType + " value " + idOrType)
} }
if (!idOrType) { return idOrType;
throw Error('无效的物品ID或枚举 ' + arguments[0] + ' => ' + 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 (玩家名称) * @constructor (玩家名称)
*/ */
item.head = function(name) { item.head = function(name) {
var head = item.create(397, 1, 3); var head = item.create(397, 1, 3);
var skullMeta = head.getItemMeta(); var skullMeta = head.itemMeta;
skullMeta.setOwner(name); skullMeta.setOwner(name);
head.setItemMeta(skullMeta); head.setItemMeta(skullMeta);
return head; return head;
}; };
/** /**
* 给玩家添加物品 * 给玩家添加物品
* @param player 玩家 * @param player 玩家
@ -74,13 +89,14 @@ item.head = function(name) {
* @param drop 满背包是否掉落 * @param drop 满背包是否掉落
*/ */
item.add = function(player, items, drop) { item.add = function(player, items, drop) {
var drops = player.getInventory().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.getLocation(), itemStack); item.drop(player.location, itemStack);
}); });
} }
}; };
/** /**
* 指定地点掉落物品 * 指定地点掉落物品
* @param loc 地点 * @param loc 地点
@ -88,9 +104,10 @@ item.add = function(player, items, drop) {
*/ */
item.drop = function(loc, item) { item.drop = function(loc, item) {
setTimeout(function() { setTimeout(function() {
loc.getWorld().dropItem(loc, item); loc.world.dropItem(loc, item);
}, 1); }, 1);
}; };
/** /**
* 设置物品名称 * 设置物品名称
* @param item 物品 * @param item 物品
@ -98,13 +115,14 @@ item.drop = function(loc, item) {
* @returns {*} * @returns {*}
*/ */
item.setName = function(item, name) { item.setName = function(item, name) {
if (item.getType().name() !== "AIR") { if (item.type && item.type.name() !== "AIR") {
var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); var meta = item.meta(item);
meta.setDisplayName(name); meta.setDisplayName(name);
item.setItemMeta(meta); item.setItemMeta(meta);
} }
return item; return item;
}; };
/** /**
* 设置物品Lore * 设置物品Lore
* @param item 物品 * @param item 物品
@ -112,15 +130,30 @@ item.setName = function(item, name) {
* @returns {*} 物品 * @returns {*} 物品
*/ */
item.setLore = item.setLores = function(item, lores) { item.setLore = item.setLores = function(item, lores) {
if (item.getType().name() !== "AIR") { var argType = toString.call(lores);
var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType()); switch (argType) {
if (typeof (lores) === 'string') { case "[object String]":
lores = lores.split("\n") lores = lores.split("\n");
} break;
case "[object Array]":
var temp = [];
lores.forEach(function(lore) {
temp.push(lore.split("\n"))
});
lores = temp;
default:
throw Error("Unsupport argument type " + argType + " value " + lores)
}
if (item.type && item.type.name() !== "AIR") {
var meta = item.meta(item);
meta.setLore(lores); meta.setLore(lores);
item.setItemMeta(meta); item.setItemMeta(meta);
} }
return item; return item;
}; };
item.meta = function(item) {
return item.hasItemMeta() ? item.itemMeta : Bukkit.itemFactory.getItemMeta(item.type);
}
module.exports = item; module.exports = item;

View File

@ -3,6 +3,7 @@
* WorldEdit 插件 * WorldEdit 插件
*/ */
/*global Java, base, module, exports, require*/ /*global Java, base, module, exports, require*/
var item = require('/api/item');
var command = require('api/command'); var command = require('api/command');
var Material = Java.type("org.bukkit.Material"); var Material = Java.type("org.bukkit.Material");
@ -28,11 +29,11 @@ function enable() {
} }
var player = sender; var player = sender;
var location = player.location; var location = player.location;
var type = Material[args[0]] || Material['STONE']; var type = item.type(args[0], 'STONE');
player.velocity = player.velocity.setY(0.5); player.velocity = player.velocity.setY(0.5);
setTimeout(function() { setTimeout(function() {
location.block.type = type location.block.type = type
}, 8); }, 6);
return true; return true;
}, },
tab: function(sender, command, args) { tab: function(sender, command, args) {