diff --git a/index.d.ts b/index.d.ts index fb7ce9d..6707a18 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,7 +2,7 @@ import { Tags, TagType } from 'prismarine-nbt' -type ItemLike = Item | null +type ItemLike = Item | BedrockItem | null declare class Item { constructor(type: number, count: number, metadata?: number, nbt?: object); @@ -26,6 +26,11 @@ declare class Item { static anvil (itemOne: ItemLike, itemTwo: ItemLike, creative: boolean, rename: string | undefined): { xpCost: number, item: ItemLike } } +declare class BedrockItem extends Item { + static loadItemStates(itemStates: ItemState[]): void; + constructor(type: number, count: number, metadata?: number, nbt?: object); +} + declare interface NotchItem { // 1.8 - 1.12 blockId?: number; @@ -43,4 +48,11 @@ declare interface NormalizedEnchant { lvl: number } -export default function loader(mcVersion: string): typeof Item; +// For bedrock edition +type ItemState = { + name: string + runtime_id: number + component_based: boolean +} + +export default function loader(mcVersion: string): typeof Item | BedrockItem; diff --git a/index.js b/index.js index a00e139..2a0989f 100644 --- a/index.js +++ b/index.js @@ -66,6 +66,24 @@ function loader (registryOrVersion) { } if (item.nbt && item.nbt.length !== 0) { notchItem.nbtData = item.nbt } return notchItem + } else if (registry.supportFeature('itemSerializationUsesNetworkId')) { + if (item == null) return { network_id: -1 } + const notchItem = { + network_id: item.type, + count: item.count, + metadata: item.metadata, + has_stack_id: 0, + block_runtime_id: 0, + extra: { + has_nbt: 0, + can_place_on: [], + can_destroy: [] + } + } + if (item.nbt && item.nbt.length !== 0) { + notchItem.extra.nbt = item.nbt + } + return notchItem } throw new Error("Don't know how to serialize for this mc version ") } @@ -80,6 +98,9 @@ function loader (registryOrVersion) { } else if (registry.supportFeature('itemSerializationUsesBlockId')) { if (item.blockId === -1) return null return new Item(item.blockId, item.itemCount, item.itemDamage, item.nbtData) + } else if (registry.supportFeature('itemSerializationUsesNetworkId')) { + if (!item.metadata && item.metadata !== 0) return null + return new Item(item.network_id, item.count || 0, item.metadata, item.extra?.nbt?.nbt) } throw new Error("Don't know how to deserialize for this mc version ") } @@ -206,8 +227,15 @@ function loader (registryOrVersion) { } } + class BedrockItem extends Item { + static loadItemStates (itemStates) { + registry.loadItemStates(itemStates) + } + } + Item.anvil = require('./lib/anvil.js')(registry, Item) - return Item + BedrockItem.anvil = require('./lib/anvil.js')(registry, BedrockItem) + return registry.version.type === 'bedrock' ? BedrockItem : Item } module.exports = loader