Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): Begin bedrock support #85

Closed
wants to merge 8 commits into from
Closed
16 changes: 14 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
30 changes: 29 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")
}
Expand All @@ -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 ")
}
Expand Down Expand Up @@ -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