Add calculation methods for block id and data (#704)

This commit is contained in:
EnZaXD 2024-03-27 16:49:14 +01:00 committed by GitHub
parent 9d1a5a05f5
commit ed52ef21ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 22 deletions

View File

@ -82,9 +82,9 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
// Include data
short unmappedData = Short.parseShort(key.substring(dataSeparatorIndex + 1));
unmappedId = Integer.parseInt(key.substring(0, dataSeparatorIndex));
unmappedId = (unmappedId << 4) | (unmappedData & 15);
unmappedId = Block.toRawId(unmappedId, unmappedData);
} else {
unmappedId = Integer.parseInt(key) << 4;
unmappedId = Block.rawById(Integer.parseInt(key));
}
mappings.put(unmappedId, new MappedLegacyBlockItem(id, data, name, block));
@ -99,12 +99,12 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
// Special block color handling
if (name.contains("%color%")) {
for (int i = from; i <= to; i++) {
mappings.put(i << 4, new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors.get(i - from)), block));
mappings.put(Block.rawById(i), new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors.get(i - from)), block));
}
} else {
MappedLegacyBlockItem mappedBlockItem = new MappedLegacyBlockItem(id, data, name, block);
for (int i = from; i <= to; i++) {
mappings.put(i << 4, mappedBlockItem);
mappings.put(Block.rawById(i), mappedBlockItem);
}
}
}
@ -118,7 +118,7 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
handler(wrapper -> {
int idx = wrapper.get(Type.VAR_INT, 0);
wrapper.set(Type.VAR_INT, 0, handleBlockID(idx));
wrapper.set(Type.VAR_INT, 0, handleBlockId(idx));
});
}
});
@ -134,7 +134,7 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
handler(wrapper -> {
for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
record.setBlockId(handleBlockID(record.getBlockId()));
record.setBlockId(handleBlockId(record.getBlockId()));
}
});
}
@ -185,14 +185,14 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
return item;
}
public int handleBlockID(int idx) {
int type = idx >> 4;
int meta = idx & 15;
public int handleBlockId(final int rawId) {
final int id = Block.getId(rawId);
final int data = Block.getData(rawId);
Block b = handleBlock(type, meta);
if (b == null) return idx;
final Block mappedBlock = handleBlock(id, data);
if (mappedBlock == null) return rawId;
return (b.getId() << 4 | (b.getData() & 15));
return Block.toRawId(mappedBlock.getId(), mappedBlock.getData());
}
public PacketHandler getFallingBlockHandler() {
@ -222,13 +222,13 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
}
private @Nullable MappedLegacyBlockItem getMappedBlockItem(int id, int data) {
MappedLegacyBlockItem mapping = replacementData.get((id << 4) | (data & 15));
return mapping != null || data == 0 ? mapping : replacementData.get(id << 4);
MappedLegacyBlockItem mapping = replacementData.get(Block.toRawId(id, data));
return mapping != null || data == 0 ? mapping : replacementData.get(Block.rawById(id));
}
private @Nullable MappedLegacyBlockItem getMappedBlockItem(int rawId) {
MappedLegacyBlockItem mapping = replacementData.get(rawId);
return mapping != null ? mapping : replacementData.get(rawId & ~15);
return mapping != null ? mapping : replacementData.get(Block.rawByData(rawId));
}
protected void handleChunk(Chunk chunk) {
@ -278,7 +278,7 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
Block b = handleBlock(btype, meta);
if (b != null) {
palette.setIdByIndex(j, (b.getId() << 4) | (b.getData() & 0xF));
palette.setIdByIndex(j, Block.toRawId(b.getId(), b.getData()));
}
// We already know that is has a handler

View File

@ -24,10 +24,10 @@ import com.viaversion.viabackwards.api.rewriters.LegacyEnchantmentRewriter;
import com.viaversion.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11;
import com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage;
import com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.WindowTracker;
import com.viaversion.viabackwards.utils.Block;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.entity.EntityTracker;
import com.viaversion.viaversion.api.data.entity.StoredEntityData;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_11;
@ -274,7 +274,7 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter<ClientboundPac
@Override
protected void registerRewrites() {
// Handle spawner block entity (map to itself with custom handler)
MappedLegacyBlockItem data = replacementData.computeIfAbsent(52 << 4, s -> new MappedLegacyBlockItem(52, (short) -1, null, false));
MappedLegacyBlockItem data = replacementData.computeIfAbsent(Block.rawById(52), s -> new MappedLegacyBlockItem(52, (short) -1, null, false));
data.setBlockEntityHandler((b, tag) -> {
EntityIdRewriter.toClientSpawner(tag, true);
return tag;

View File

@ -26,6 +26,7 @@ import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.block_entity_ha
import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider;
import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage;
import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.NoteBlockStorage;
import com.viaversion.viabackwards.utils.Block;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
@ -99,11 +100,11 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit
}
if (SpawnEggRewriter.getEntityId(oldId).isPresent()) {
wrapper.write(Type.VAR_INT, 383 << 4);
wrapper.write(Type.VAR_INT, Block.rawById(383));
return;
}
wrapper.write(Type.VAR_INT, oldId >> 4);
wrapper.write(Type.VAR_INT, Block.getId(oldId));
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketHandlers() {
@ -750,7 +751,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit
// Save original id
int originalId = (item.identifier() << 16 | item.data() & 0xFFFF);
int rawId = (item.identifier() << 4 | item.data() & 0xF);
int rawId = Block.toRawId(item.identifier(), item.data());
// NBT Additions
if (isDamageable(item.identifier())) {
@ -831,7 +832,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit
if (item.identifier() == 229) { // purple shulker box
newId = 362; // directly set the new id -> base/colorless shulker box
} else if (item.identifier() == 31 && item.data() == 0) { // Shrub was removed
rawId = 32 << 4; // Dead Bush
rawId = Block.rawById(32); // Dead Bush
} else if (protocol.getMappingData().getItemMappings().inverse().getNewId(rawId & ~0xF) != -1) {
rawId &= ~0xF; // Remove data
} else {

View File

@ -39,6 +39,26 @@ public class Block {
return new Block(this.id, data);
}
public static int getId(final int rawId) {
return rawId >> 4;
}
public static int getData(final int rawId) {
return rawId & 15;
}
public static int rawById(final int id) {
return id << 4;
}
public static int rawByData(final int data) {
return data & ~15;
}
public static int toRawId(final int id, final int data) {
return (id << 4) | (data & 15);
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;