Skip to content

Commit

Permalink
implement offhand
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Dec 13, 2024
1 parent 6d71575 commit 7841ba9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
41 changes: 25 additions & 16 deletions prismarine-viewer/viewer/lib/worldDataEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,33 @@ export class WorldDataEmitter extends EventEmitter {
time: () => {
this.emitter.emit('time', bot.time.timeOfDay)
},
heldItemChanged: () => {
if (!this.handDisplay) {
viewer.world.onHandItemSwitch(undefined)
return
}
const newItem = bot.heldItem
if (!newItem) {
viewer.world.onHandItemSwitch(undefined)
return
}
const block = loadedData.blocksByName[newItem.name]
// todo clean types
const blockProperties = block ? new window.PrismarineBlock(block.id, 'void', newItem.metadata).getProperties() : {}
// todo item props
viewer.world.onHandItemSwitch({ name: newItem.name, properties: blockProperties, id: newItem.type, type: block ? 'block' : 'item', })
heldItemChanged () {
handChanged(false)
},
} satisfies Partial<BotEvents>
this.eventListeners.heldItemChanged()
const handChanged = (isLeftHand: boolean) => {
if (!this.handDisplay) {
viewer.world.onHandItemSwitch(undefined, isLeftHand)
return
}
const newItem = isLeftHand ? bot.inventory.slots[45] : bot.heldItem
if (!newItem) {
viewer.world.onHandItemSwitch(undefined, isLeftHand)
return
}
const block = loadedData.blocksByName[newItem.name]
// todo clean types
const blockProperties = block ? new window.PrismarineBlock(block.id, 'void', newItem.metadata).getProperties() : {}
// todo item props
viewer.world.onHandItemSwitch({ name: newItem.name, properties: blockProperties, id: newItem.type, type: block ? 'block' : 'item', }, isLeftHand)
}
bot.inventory.on('updateSlot', (index) => {
if (index === 45) {
handChanged(true)
}
})
handChanged(false)
handChanged(true)


bot._client.on('update_light', ({ chunkX, chunkZ }) => {
Expand Down
4 changes: 2 additions & 2 deletions prismarine-viewer/viewer/lib/worldrendererCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
}
}

onHandItemSwitch (item: HandItemBlock | undefined): void { }
changeHandSwingingState (isAnimationPlaying: boolean): void { }
onHandItemSwitch (item: HandItemBlock | undefined, isLeftHand: boolean): void { }
changeHandSwingingState (isAnimationPlaying: boolean, isLeftHand: boolean): void { }

abstract handleWorkerMessage (data: WorkerReceive): void

Expand Down
30 changes: 21 additions & 9 deletions prismarine-viewer/viewer/lib/worldrendererThree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class WorldRendererThree extends WorldRendererCommon {
starField: StarField
cameraSectionPos: Vec3 = new Vec3(0, 0, 0)
holdingBlock: HoldingBlock
holdingBlockLeft: HoldingBlock
rendererDevice = '...'

get tilesRendered () {
Expand All @@ -38,33 +39,43 @@ export class WorldRendererThree extends WorldRendererCommon {
this.rendererDevice = String(WorldRendererThree.getRendererInfo(this.renderer))
this.starField = new StarField(scene)
this.holdingBlock = new HoldingBlock()
this.holdingBlockLeft = new HoldingBlock()
this.holdingBlockLeft.rightSide = false

this.renderUpdateEmitter.on('itemsTextureDownloaded', () => {
if (this.holdingBlock.toBeRenderedItem || true) {
if (this.holdingBlock.toBeRenderedItem) {
this.onHandItemSwitch(this.holdingBlock.toBeRenderedItem)
this.holdingBlock.toBeRenderedItem = undefined
}
if (this.holdingBlockLeft.toBeRenderedItem) {
this.onHandItemSwitch(this.holdingBlock.toBeRenderedItem, true)
this.holdingBlockLeft.toBeRenderedItem = undefined
}
})

this.addDebugOverlay()
}

onHandItemSwitch (item: HandItemBlock | undefined) {
item ??= {
type: 'hand',
onHandItemSwitch (item: HandItemBlock | undefined, isLeft = false) {
if (!isLeft) {
item ??= {
type: 'hand',
}
}
const holdingBlock = isLeft ? this.holdingBlockLeft : this.holdingBlock
if (!this.currentTextureImage) {
this.holdingBlock.toBeRenderedItem = item
holdingBlock.toBeRenderedItem = item
return
}
void this.holdingBlock.initHandObject(this.material, this.blockstatesModels, this.blocksAtlases, item)
void holdingBlock.initHandObject(this.material, this.blockstatesModels, this.blocksAtlases, item)
}

changeHandSwingingState (isAnimationPlaying: boolean) {
changeHandSwingingState (isAnimationPlaying: boolean, isLeft = false) {
const holdingBlock = isLeft ? this.holdingBlockLeft : this.holdingBlock
if (isAnimationPlaying) {
this.holdingBlock.startSwing()
holdingBlock.startSwing()
} else {
void this.holdingBlock.stopSwing()
void holdingBlock.stopSwing()
}
}

Expand Down Expand Up @@ -230,6 +241,7 @@ export class WorldRendererThree extends WorldRendererCommon {
const cam = this.camera instanceof THREE.Group ? this.camera.children.find(child => child instanceof THREE.PerspectiveCamera) as THREE.PerspectiveCamera : this.camera
this.renderer.render(this.scene, cam)
this.holdingBlock.render(this.camera, this.renderer, viewer.ambientLight, viewer.directionalLight)
this.holdingBlockLeft.render(this.camera, this.renderer, viewer.ambientLight, viewer.directionalLight)
}

renderSign (position: Vec3, rotation: number, isWall: boolean, isHanging: boolean, blockEntity) {
Expand Down

0 comments on commit 7841ba9

Please sign in to comment.