@tendrock/lib
Version:
A lib under the Tendrock system for Minecraft Bedrock Script API
53 lines (52 loc) • 2.01 kB
JavaScript
import { ItemDurabilityComponent, ItemEnchantableComponent, ItemStack } from '@minecraft/server';
import { MinecraftItemTags } from "@tendrock/vanilla-data";
import { tryCatch } from "../../lib";
export class ItemUtils {
static _checkComponent(item, componentId) {
if (!item.hasComponent(componentId)) {
throw Error(`Item: "${item.typeId}" doesn't has an "${componentId}" component!`);
}
}
static getDurability(itemStack) {
return itemStack.getComponent(ItemDurabilityComponent.componentId);
}
static getEnchants(itemStack) {
return itemStack.getComponent(ItemEnchantableComponent.componentId);
}
/**
* 返回是否应该损耗耐久
* @param item
* @param durabilityComponent
*/
static useUnbreaking(item, durabilityComponent) {
let unbreakingLevel;
const enchantment = this.getEnchants(item);
if (!enchantment) {
return false;
}
durabilityComponent !== null && durabilityComponent !== void 0 ? durabilityComponent : (durabilityComponent = this.getDurability(item));
if (enchantment.hasEnchantment("unbreaking")) {
unbreakingLevel = enchantment.getEnchantment("unbreaking").level;
}
if (!unbreakingLevel)
return true;
const damageChance = durabilityComponent.getDamageChance(unbreakingLevel);
// TODO: damage change range
return Math.random() * 100 <= damageChance;
}
static setDamage(item, amount) {
this._checkComponent(item, ItemDurabilityComponent.componentId);
this.getDurability(item).damage = amount;
}
static isLog(item) {
return item.hasTag(MinecraftItemTags.Logs);
}
static getItemStack(item) {
const { data: itemStack, success } = tryCatch(() => {
return typeof item === 'string' ? new ItemStack(item) : item;
});
if (!success)
return undefined;
return itemStack;
}
}