Skip to content

Commit

Permalink
Refactor item stack default components
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Dec 3, 2024
1 parent 8774cb2 commit a820998
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 213 deletions.
11 changes: 6 additions & 5 deletions demo/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mat4 } from 'gl-matrix'
import type { ItemRendererResources, ItemRenderingContext, NbtTag, Resources, Voxel } from '../src/index.js'
import { BlockDefinition, BlockModel, Identifier, Item, ItemRenderer, ItemStack, NormalNoise, Structure, StructureRenderer, TextureAtlas, VoxelRenderer, XoroshiroRandom, jsonToNbt, upperPowerOfTwo } from '../src/index.js'
import { BlockDefinition, BlockModel, Identifier, ItemRenderer, ItemStack, jsonToNbt, NormalNoise, Structure, StructureRenderer, TextureAtlas, upperPowerOfTwo, VoxelRenderer, XoroshiroRandom } from '../src/index.js'
import { } from '../src/nbt/Util.js'
import { ItemModel } from '../src/render/ItemModel.js'

Expand Down Expand Up @@ -107,13 +107,13 @@ Promise.all([
itemModels['minecraft:' + id] = ItemModel.fromJson(item_models[id].model)
})


const itemComponents: Record<string, Map<string, NbtTag>> = {}
Object.keys(item_components).forEach(id => {
const components = new Map<string, NbtTag>()
Object.keys(item_components[id]).forEach(c_id => {
components.set(c_id, jsonToNbt(item_components[id][c_id]))
})
Item.REGISTRY.register(Identifier.create(id), new Item(components))
itemComponents['minecraft:' + id] = components
})

const atlasCanvas = document.createElement('canvas')
Expand All @@ -140,12 +140,13 @@ Promise.all([
getBlockProperties(id) { return null },
getDefaultBlockProperties(id) { return null },
getItemModel(id) { return itemModels[id.toString()] },
getItemComponents(id) { return itemComponents[id.toString()] },
}

// === Item rendering ===

const context: ItemRenderingContext = {
"bundle/selected_item": 0
'bundle/selected_item': 0,
}

const itemCanvas = document.getElementById('item-display') as HTMLCanvasElement
Expand All @@ -159,7 +160,7 @@ Promise.all([
try {
const id = itemInput.value
const itemStack = ItemStack.fromString(itemInput.value)
itemGl.clear(itemGl.DEPTH_BUFFER_BIT | itemGl.COLOR_BUFFER_BIT);
itemGl.clear(itemGl.DEPTH_BUFFER_BIT | itemGl.COLOR_BUFFER_BIT)
itemRenderer.setItem(itemStack, context)
itemRenderer.drawItem()
itemInput.classList.remove('invalid')
Expand Down
31 changes: 0 additions & 31 deletions src/core/Item.ts

This file was deleted.

49 changes: 31 additions & 18 deletions src/core/ItemStack.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { NbtParser } from '../nbt/NbtParser.js'
import type { NbtTag } from '../nbt/index.js'
import { NbtCompound, NbtInt, NbtString } from '../nbt/index.js'
import { NbtParser } from '../nbt/NbtParser.js'
import { StringReader } from '../util/index.js'
import { Holder } from './Holder.js'
import { Identifier } from './Identifier.js'
import { Item } from './Item.js'

export class ItemStack {
private readonly item: Holder<Item | undefined>
export interface ItemComponentsProvider {
getItemComponents: (id: Identifier) => Map<string, NbtTag>,
}

export class ItemStack {
constructor(
public readonly id: Identifier,
public count: number,
public readonly components: Map<string, NbtTag> = new Map(),
) {
this.item = Holder.reference(Item.REGISTRY, id, false)
}
) {}

public getComponent<T>(key: string | Identifier, reader: (tag: NbtTag) => T, includeDefaultComponents: boolean = true): T | undefined {
public getComponent<T>(key: string | Identifier, baseComponents?: ItemComponentsProvider): NbtTag | undefined {
if (typeof key === 'string') {
key = Identifier.parse(key)
}
Expand All @@ -27,20 +25,28 @@ export class ItemStack {
}
const value = this.components.get(key.toString())
if (value) {
return reader(value)
return value
}
return includeDefaultComponents ? this.item.value()?.getComponent(key, reader) : undefined
if (baseComponents) {
return baseComponents.getItemComponents(this.id)?.get(key.toString())
}
return undefined
}

public hasComponent(key: string | Identifier, includeDefaultComponents: boolean = true): boolean {
public hasComponent(key: string | Identifier, baseComponents?: ItemComponentsProvider): boolean {
if (typeof key === 'string') {
key = Identifier.parse(key)
}
if (this.components.has('!' + key.toString())){
return false
}

return this.components.has(key.toString()) || (includeDefaultComponents && (this.item.value()?.hasComponent(key) ?? false))
if (this.components.has(key.toString())) {
return true
}
if (baseComponents) {
return baseComponents.getItemComponents(this.id)?.has(key.toString())
}
return false
}

public clone(): ItemStack {
Expand Down Expand Up @@ -85,7 +91,9 @@ export class ItemStack {
public toString() {
let result = this.id.toString()
if (this.components.size > 0) {
result += `[${[...this.components.entries()].map(([k, v]) => `${k}=${v.toString()}`).join(',')}]`
result += `[${[...this.components.entries()].map(([k, v]) => {
return k.startsWith('!') ? k : `${k}=${v.toString()}`
}).join(',')}]`
}
if (this.count > 1) {
result += ` ${this.count}`
Expand Down Expand Up @@ -123,12 +131,12 @@ export class ItemStack {
reader.skip()
}
const component = Identifier.parse(reader.getRead(start)).toString()
if (!reader.canRead()) break;
if (!reader.canRead()) break
reader.skip()
const tag = NbtParser.readTag(reader)
components.set(component, tag)
}
if (!reader.canRead()) break;
if (!reader.canRead()) break
if (reader.peek() === ']'){
return new ItemStack(itemId, 1, components)
}
Expand Down Expand Up @@ -156,7 +164,12 @@ export class ItemStack {
const id = Identifier.parse(nbt.getString('id'))
const count = nbt.hasNumber('count') ? nbt.getNumber('count') : 1
const components = new Map(Object.entries(
nbt.getCompound('components').map((key, value) => [Identifier.parse(key).toString(), value])
nbt.getCompound('components').map((key, value) => {
if (key.startsWith('!')) {
return ['!' + Identifier.parse(key).toString(), new NbtCompound()]
}
return [Identifier.parse(key).toString(), value]
})
))
return new ItemStack(id, count, components)
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ export * from './Effects.js'
export * from './Holder.js'
export * from './HolderSet.js'
export * from './Identifier.js'
export * from './Item.js'
export * from './ItemStack.js'
export * from './PalettedContainer.js'
export * from './Registry.js'
export * from './Rotation.js'
export * from './Structure.js'
export * from './StructureProvider.js'

Loading

0 comments on commit a820998

Please sign in to comment.