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

Support for hashed block network ID's #33

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
38 changes: 4 additions & 34 deletions lib/bedrock/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
const buildIndexFromArray = require('../indexer')

module.exports = (data) => {
return {

Copy link
Member

@extremeheat extremeheat Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean put the non-API helper functions before this return statement

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if you make a change to the PR that needs review, feel free to comment so we can get notified of it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thank you, will note

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if you make a change to the PR that needs review, feel free to comment so we can get notified of it

Might've done it

handleStartGame (packet) {
this.loadItemStates(packet.itemstates)
const { loadItemStates, loadRuntimeIds, loadHashedRuntimeIds } = require('./loader')(this, data)
loadItemStates(packet.itemstates)
if (this.supportFeature('blockHashes') && packet.block_network_ids_are_hashes) {
this.loadHashedRuntimeIds()
loadHashedRuntimeIds()
} else {
this.loadRuntimeIds()
}
},

loadItemStates (itemStates) {
const items = []
for (const item of itemStates) {
const name = item.name.replace('minecraft:', '')
items.push({ ...data.itemsByName[name], name, id: item.runtime_id })
loadRuntimeIds()
}
data.itemsArray = items
data.items = buildIndexFromArray(data.itemsArray, 'id')
data.itemsByName = buildIndexFromArray(data.itemsArray, 'name')
},

writeItemStates () {
Expand All @@ -37,23 +24,6 @@ module.exports = (data) => {
}

return itemstates
},

loadHashedRuntimeIds () {
const Block = require('prismarine-block')(this)
data.blocksByRuntimeId = {}
for (let i = 0; i < data.blockStates.length; i++) {
const { name, states } = data.blockStates[i]
const hash = Block.getHash(name, states)
data.blocksByRuntimeId[hash] = { stateId: i, ...data.blocksByStateId[i] }
}
},

loadRuntimeIds () {
data.blocksByRuntimeId = {}
for (let i = 0; i < data.blockStates.length; i++) {
data.blocksByRuntimeId[i] = { stateId: i, ...data.blocksByStateId[i] }
}
}
}
}
32 changes: 32 additions & 0 deletions lib/bedrock/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const buildIndexFromArray = require('../indexer')
module.exports = (registry, data) => {
return {
loadItemStates (itemStates) {
const items = []
for (const item of itemStates) {
const name = item.name.replace('minecraft:', '')
items.push({ ...data.itemsByName[name], name, id: item.runtime_id })
}
data.itemsArray = items
data.items = buildIndexFromArray(data.itemsArray, 'id')
data.itemsByName = buildIndexFromArray(data.itemsArray, 'name')
},

loadHashedRuntimeIds () {
const Block = require('prismarine-block')(registry)
data.blocksByRuntimeId = {}
for (let i = 0; i < data.blockStates.length; i++) {
const { name, states } = data.blockStates[i]
const hash = Block.getHash(name, states)
data.blocksByRuntimeId[hash] = { stateId: i, ...data.blocksByStateId[i] }
}
},

loadRuntimeIds () {
data.blocksByRuntimeId = {}
for (let i = 0; i < data.blockStates.length; i++) {
data.blocksByRuntimeId[i] = { stateId: i, ...data.blocksByStateId[i] }
}
}
}
}
Loading