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

Conversation

FreezeEngine
Copy link
Contributor

Aims to add support for hashed block network ids.
WIP so more commits to follow

index.js Outdated
Comment on lines 384 to 391
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

index.js Outdated
@@ -414,5 +414,5 @@ function computeFnv1a32Hash (buf) {
h ^= buf[i] & 0xff
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24)
}
return h
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

@rom1504
Copy link
Member

rom1504 commented Oct 26, 2024

@extremeheat do you think this is ready?

@FreezeEngine
Copy link
Contributor Author

@extremeheat

@extremeheat
Copy link
Member

Please update the doc

@FreezeEngine
Copy link
Contributor Author

@extremeheat I think the whole pack is ready, let me know if any changes required
PrismarineJS/prismarine-chunk#237
PrismarineJS/prismarine-registry#33

@extremeheat
Copy link
Member

I meant update with what you're exposing in the PR

@FreezeEngine
Copy link
Contributor Author

I meant update with what you're exposing in the PR

I think I did, whatever is in index.d.ts now is in docs + some extra that wasnt documented/had it outdated

@FreezeEngine
Copy link
Contributor Author

FreezeEngine commented Jan 3, 2025

Ok I have forgot getHash() and hash fields, indeed

@FreezeEngine
Copy link
Contributor Author

Should be fine now @extremeheat

@extremeheat extremeheat merged commit bc8a7af into PrismarineJS:master Jan 4, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

3 participants