Skip to content

Commit

Permalink
Fix compressed id overlap in legacy block item rewriter
Browse files Browse the repository at this point in the history
Fixes an edge case in ViaRewind where different data ints with the same item id are compressing to the same data int;
(383 << 4) | (68 & 15) = 6132
(383 << 4) | (100 & 15) = 6132
  • Loading branch information
FlorianMichael committed Aug 20, 2024
1 parent 0b3a792 commit 2f6c325
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ private void addMapping(String key, JsonObject object, MappedLegacyBlockItem.Typ
// Include data
short unmappedData = Short.parseShort(key.substring(dataSeparatorIndex + 1));
unmappedId = Integer.parseInt(key.substring(0, dataSeparatorIndex));
unmappedId = IdAndData.toRawData(unmappedId, unmappedData);
unmappedId = compress(unmappedId, unmappedData);
} else {
unmappedId = IdAndData.toRawData(Integer.parseInt(key), -1);
unmappedId = compress(Integer.parseInt(key), -1);
}

mappings.put(unmappedId, new MappedLegacyBlockItem(id, data, name, type));
Expand All @@ -120,12 +120,12 @@ private void addMapping(String key, JsonObject object, MappedLegacyBlockItem.Typ
// Special block color handling
if (name != null && name.contains("%color%")) {
for (int i = from; i <= to; i++) {
mappings.put(IdAndData.toRawData(i, -1), new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors1_11_1.get(i - from)), type));
mappings.put(compress(i, -1), new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors1_11_1.get(i - from)), type));
}
} else {
MappedLegacyBlockItem mappedBlockItem = new MappedLegacyBlockItem(id, data, name, type);
for (int i = from; i <= to; i++) {
mappings.put(IdAndData.toRawData(i, -1), mappedBlockItem);
mappings.put(compress(i, -1), mappedBlockItem);
}
}
}
Expand Down Expand Up @@ -360,13 +360,13 @@ protected CompoundTag getNamedTag(String text) {
}

private @Nullable MappedLegacyBlockItem getMappedBlock(int id, int data) {
MappedLegacyBlockItem mapping = blockReplacements.get(IdAndData.toRawData(id, data));
return mapping != null ? mapping : blockReplacements.get(IdAndData.toRawData(id, -1));
MappedLegacyBlockItem mapping = blockReplacements.get(compress(id, data));
return mapping != null ? mapping : blockReplacements.get(compress(id, -1));
}

private @Nullable MappedLegacyBlockItem getMappedItem(int id, int data) {
MappedLegacyBlockItem mapping = itemReplacements.get(IdAndData.toRawData(id, data));
return mapping != null ? mapping : itemReplacements.get(IdAndData.toRawData(id, -1));
MappedLegacyBlockItem mapping = itemReplacements.get(compress(id, data));
return mapping != null ? mapping : itemReplacements.get(compress(id, -1));
}

private @Nullable MappedLegacyBlockItem getMappedBlock(int rawId) {
Expand All @@ -379,6 +379,11 @@ protected JsonObject readMappingsFile(final String name) {
return BackwardsMappingDataLoader.INSTANCE.loadFromDataDir(name);
}

protected int compress(final int id, final int data) {
// Using IdAndData for the internal storage can cause id overlaps in edge cases and would lead to wrong data
return (id << 16) | (data & 0xFFFF);
}

private record Pos(int x, short y, int z) {

public Pos(int x, int y, int z) {
Expand Down

0 comments on commit 2f6c325

Please sign in to comment.