@tendrock/lib
Version:
A lib under the Tendrock system for Minecraft Bedrock Script API
97 lines (96 loc) • 3.97 kB
JavaScript
import { MinecraftBlockTags, MinecraftItemTags } from "@tendrock/vanilla-data";
import { IdUtils } from "../IdUtils";
import { ItemUtils } from "./ItemUtils";
import { TendrockItemTags } from "../../ref";
export class ToolUtils {
static getMatchToolTierByBlockDestructibleTag(destructibleTag) {
return this.ToolTierMap.get(destructibleTag);
}
static isVanillaTool(item, toolTag) {
const itemStack = ItemUtils.getItemStack(item);
if (!itemStack)
return false;
return IdUtils.isVanillaId(itemStack.typeId) && itemStack.getTags().includes(toolTag);
}
static isVanillaAxe(item) {
return this.isVanillaTool(item, MinecraftItemTags.IsAxe);
}
static isVanillaPickaxe(item) {
return this.isVanillaTool(item, MinecraftItemTags.IsPickaxe);
}
static isVanillaShovel(item) {
return this.isVanillaTool(item, MinecraftItemTags.IsShovel);
}
static isVanillaHoe(item) {
return this.isVanillaTool(item, MinecraftItemTags.IsHoe);
}
static isVanillaSword(item) {
return this.isVanillaTool(item, MinecraftItemTags.IsSword);
}
static isTool(item, ...toolTags) {
const itemStack = ItemUtils.getItemStack(item);
if (!itemStack)
return false;
const itemTags = itemStack.getTags();
return toolTags.some((tag) => itemTags.includes(tag));
}
static isAxe(item) {
return this.isTool(item, MinecraftItemTags.IsAxe, TendrockItemTags.IsAxe);
}
static isPickaxe(item) {
return this.isTool(item, MinecraftItemTags.IsPickaxe, TendrockItemTags.IsPickaxe);
}
static isShovel(item) {
return this.isTool(item, MinecraftItemTags.IsShovel, TendrockItemTags.IsShovel);
}
static isHoe(item) {
return this.isTool(item, MinecraftItemTags.IsHoe, TendrockItemTags.IsHoe);
}
static isSword(item) {
return this.isTool(item, MinecraftItemTags.IsSword, TendrockItemTags.IsSword);
}
static matchTool(permutation, tool, ...toolTags) {
const itemTags = tool.getTags();
if (!toolTags.some((tag) => itemTags.includes(tag))) {
return false;
}
const availableToolTier = this.getMatchToolTierByBlockDestructibleTag(permutation.getTags().find(tag => {
return tag.startsWith('minecraft:') && tag.endsWith('_tier_destructible');
}));
if (!availableToolTier || availableToolTier.length <= 0) {
return false;
}
return itemTags.some(tag => {
return availableToolTier.includes(tag);
});
}
static matchPickaxe(permutation, tool) {
return this.matchTool(permutation, tool, MinecraftItemTags.IsPickaxe, TendrockItemTags.IsPickaxe);
}
static matchAxe(permutation, tool) {
return this.matchTool(permutation, tool, MinecraftItemTags.IsAxe, TendrockItemTags.IsAxe);
}
static matchShovel(permutation, tool) {
return this.matchTool(permutation, tool, MinecraftItemTags.IsShovel, TendrockItemTags.IsShovel);
}
static matchHoe(permutation, tool) {
return this.matchTool(permutation, tool, MinecraftItemTags.IsHoe, TendrockItemTags.IsHoe);
}
static matchSword(permutation, tool) {
return this.matchTool(permutation, tool, MinecraftItemTags.IsSword, TendrockItemTags.IsSword);
}
}
ToolUtils.ToolTierMap = new Map([
[
MinecraftBlockTags.StoneTierDestructible,
[MinecraftItemTags.StoneTier, MinecraftItemTags.GoldenTier, MinecraftItemTags.IronTier, MinecraftItemTags.DiamondTier, MinecraftItemTags.NetheriteTier]
],
[
MinecraftBlockTags.IronTierDestructible,
[MinecraftItemTags.GoldenTier, MinecraftItemTags.IronTier, MinecraftItemTags.DiamondTier, MinecraftItemTags.NetheriteTier]
],
[
MinecraftBlockTags.DiamondTierDestructible,
[MinecraftItemTags.DiamondTier, MinecraftItemTags.NetheriteTier]
],
]);