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)
*/
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)
switch (argType) {
case "[object Number]":
@ -34,39 +54,34 @@ item.create = function() {
idOrType = Material[idOrType] || Material[Material['LEGACY_PREFIX'] + idOrType];
break;
case "[object Array]":
idOrType.forEach(function(type) {
var temp = Material[type];
idOrType.some(function(type) {
var temp = item.type(type);
if (temp) {
idOrType = temp;
return;
return true;
}
})
});
case "[object Undefined]":
case "[object Null]":
return idOrType;
default:
throw Error("Unsupport argument type " + argType + " value " + idOrType)
}
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]);
}
return idOrType;
};
/**
* 创建一个头颅
* @constructor (玩家名称)
*/
item.head = function(name) {
var head = item.create(397, 1, 3);
var skullMeta = head.getItemMeta();
var skullMeta = head.itemMeta;
skullMeta.setOwner(name);
head.setItemMeta(skullMeta);
return head;
};
/**
* 给玩家添加物品
* @param player 玩家
@ -74,13 +89,14 @@ item.head = function(name) {
* @param 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) {
drops.forEach(function(itemStack) {
item.drop(player.getLocation(), itemStack);
item.drop(player.location, itemStack);
});
}
};
/**
* 指定地点掉落物品
* @param loc 地点
@ -88,9 +104,10 @@ item.add = function(player, items, drop) {
*/
item.drop = function(loc, item) {
setTimeout(function() {
loc.getWorld().dropItem(loc, item);
loc.world.dropItem(loc, item);
}, 1);
};
/**
* 设置物品名称
* @param item 物品
@ -98,13 +115,14 @@ item.drop = function(loc, item) {
* @returns {*}
*/
item.setName = function(item, name) {
if (item.getType().name() !== "AIR") {
var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType());
if (item.type && item.type.name() !== "AIR") {
var meta = item.meta(item);
meta.setDisplayName(name);
item.setItemMeta(meta);
}
return item;
};
/**
* 设置物品Lore
* @param item 物品
@ -112,15 +130,30 @@ item.setName = function(item, name) {
* @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")
var argType = toString.call(lores);
switch (argType) {
case "[object String]":
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);
item.setItemMeta(meta);
}
return item;
};
item.meta = function(item) {
return item.hasItemMeta() ? item.itemMeta : Bukkit.itemFactory.getItemMeta(item.type);
}
module.exports = item;

View File

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