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 #97

Merged
merged 25 commits into from
Jan 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ function provider (registry, { Biome, version }) {
return 0
}

function getHashValue (states, name) {
const tag = nbt.comp({
name: { type: 'string', value: name },
states: nbt.comp(states)
})
const s = nbt.writeUncompressed(tag, 'little').toString()
return fnv1a32(s)
}
Copy link
Member

@extremeheat extremeheat Mar 22, 2024

Choose a reason for hiding this comment

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

As a static method:

static getHash (name, states) {
  if (registry.supportFeature('blockHashes')) {
    return ... compute hash ...
  }
}

inside the Block instance constructor:

if (registry.supportFeature('blockHashes')) {
  this.hash = Block.getHash(this.name, this._properties)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where should first edit be made?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think i get it now, ill try

Copy link
Contributor Author

@FreezeEngine FreezeEngine Mar 22, 2024

Choose a reason for hiding this comment

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

As a static method:

static getHash (name, states) {

  if (registry.supportFeature('blockHashes')) {

    return ... compute hash ...

  }

}

inside the Block instance constructor:

if (registry.supportFeature('blockHashes')) {

  this.hash = Block.getHash(this.name, this._properties)

}

Should supportFeature be used as that "variable" that sets on start_game? Or other variable is still needed and supportFeature("blockHashes") will be true for mcbe >1.19.80?

Copy link
Member

Choose a reason for hiding this comment

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

No, supportFeatures data should be inside minecraft-data features.json. It is used for handling features available in different minecraft versions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it


function propValue (state, value) {
if (state.type === 'enum') return state.values[value]
if (state.type === 'bool') return !value
Expand All @@ -391,3 +400,15 @@ function provider (registry, { Biome, version }) {
function mergeObject (to, from) {
Object.defineProperties(to, Object.getOwnPropertyDescriptors(from))
}

function fnv1a32 (s) {
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters
const FNV1_OFFSET_32 = 0x811c9dc5
let h = FNV1_OFFSET_32
for (let i = 0; i < s.length; i++) {
h ^= s.charCodeAt(i) & 0xff
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24)
}

return h & h
Copy link
Member

Choose a reason for hiding this comment

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

Why this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hash only matched for that one block tested, other ones I tested didnt, not sure why this works either, I changed the test to block that didnt match. Also checked hashing online "hello" and with h & h it matched

Copy link
Member

Choose a reason for hiding this comment

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

If you're not sure then please revert. The implication here is that that has a substantive effect and if it does it's not obvious to me (bitwise AND on two numbers yields the number itself, it does not turn signed to unsigned or anything like that). The only effect I can think would be to turn a floating point into an integer, but getting a float is not possible in the operation here.

If it breaks without it then that needs some investigation because it doesn't seem to make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you're not sure then please revert. The implication here is that that has a substantive effect and if it does it's not obvious to me (bitwise AND on two numbers yields the number itself, it does not turn signed to unsigned or anything like that). The only effect I can think would be to turn a floating point into an integer, but getting a float is not possible in the operation here.

If it breaks without it then that needs some investigation because it doesn't seem to make sense.

I tested with my bot now and just returning "h" only works for some blocks, others hash collide
image

Copy link
Member

@extremeheat extremeheat Mar 24, 2024

Choose a reason for hiding this comment

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

Which ones? Can you be more specific with the inputs to trigger?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The one block I changed in tests fails without "h & h", could be used as an example (i think its soul_soil or other soul_...)

Copy link
Member

@extremeheat extremeheat Mar 24, 2024

Choose a reason for hiding this comment

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

Please add tests that are failing or that show the collision. This duplication comment doesn't make any sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added new tests that illustrate difference in outputted numbers

Copy link
Member

@extremeheat extremeheat Mar 24, 2024

Choose a reason for hiding this comment

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

Ah, this is because of dumb JavaScript behavior. The hash is exceeding the 32 bit boundary and because JS turns all Numbers into 32 bit integers when doing bitwise operations, h & h was effectively truncating the bits. This is very unclear so it should be moved to explicitly mask the number to 0xFF FF FF FF

}
Loading