Last active
June 28, 2024 07:45
-
-
Save Alemiz112/504d0f79feac7ef57eda174b668dd345 to your computer and use it in GitHub Desktop.
Example implementation of Minecraft: Bedrock block state network hash computation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PaletteHashTest { | |
private static final int FNV1_32_INIT = 0x811c9dc5; | |
private static final int FNV1_PRIME_32 = 0x01000193; | |
public static void main(String[] args) throws Exception { | |
NbtMap blockState = NbtMap.builder() | |
.putString("name", "minecraft:bamboo_hanging_sign") | |
.putCompound("states", NbtMap.builder() | |
.putInt("facing_direction", 3) | |
.putInt("ground_sign_direction", 9) | |
.putByte("attached_bit", (byte) 1) | |
.putByte("hanging", (byte) 0) | |
.build()) | |
.build(); | |
System.out.printf("hash: %s%n", createHash(blockState)); | |
} | |
public static int createHash(NbtMap block) { | |
if (block.getString("name").equals("minecraft:unknown")) { | |
return -2; // This is special case | |
} | |
NbtMap tag = NbtMap.builder() | |
.putString("name", block.getString("name")) | |
.putCompound("states", NbtMap.fromMap( | |
new TreeMap<>(block.getCompound("states")))) | |
.build(); | |
byte[] bytes; | |
try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
NBTOutputStream outputStream = NbtUtils.createWriterLE(stream)) { | |
outputStream.writeTag(tag); | |
bytes = stream.toByteArray(); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
return fnv1a_32(bytes); | |
} | |
public static int fnv1a_32(byte[] data) { | |
int hash = FNV1_32_INIT; | |
for (byte datum : data) { | |
hash ^= (datum & 0xff); | |
hash *= FNV1_PRIME_32; | |
} | |
return hash; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment