Use VVs IdAndData class for block id conversions (#719)

This commit is contained in:
EnZaXD 2024-04-16 21:05:45 +02:00 committed by GitHub
parent 9cf5f98ed1
commit 09eda34051
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 105 deletions

View File

@ -17,8 +17,8 @@
*/
package com.viaversion.viabackwards.api.data;
import com.viaversion.viabackwards.utils.Block;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.util.IdAndData;
import org.checkerframework.checker.nullness.qual.Nullable;
public class MappedLegacyBlockItem {
@ -26,14 +26,14 @@ public class MappedLegacyBlockItem {
private final int id;
private final short data;
private final String name;
private final Block block;
private final IdAndData block;
private BlockEntityHandler blockEntityHandler;
public MappedLegacyBlockItem(int id, short data, @Nullable String name, boolean block) {
this.id = id;
this.data = data;
this.name = name != null ? "§f" + name : null;
this.block = block ? new Block(id, data) : null;
this.block = block ? new IdAndData(id, data) : null;
}
public int getId() {
@ -52,7 +52,7 @@ public class MappedLegacyBlockItem {
return block != null;
}
public Block getBlock() {
public IdAndData getBlock() {
return block;
}

View File

@ -22,7 +22,6 @@ import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.MappedLegacyBlockItem;
import com.viaversion.viabackwards.api.data.BackwardsMappingDataLoader;
import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.BlockColors;
import com.viaversion.viabackwards.utils.Block;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection;
@ -51,6 +50,7 @@ import com.viaversion.viaversion.util.ComponentUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import com.viaversion.viaversion.util.IdAndData;
import org.checkerframework.checker.nullness.qual.Nullable;
public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
@ -81,9 +81,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 = Block.toRawId(unmappedId, unmappedData);
unmappedId = IdAndData.toRawData(unmappedId, unmappedData);
} else {
unmappedId = Block.rawById(Integer.parseInt(key));
unmappedId = IdAndData.toRawData(Integer.parseInt(key));
}
mappings.put(unmappedId, new MappedLegacyBlockItem(id, data, name, block));
@ -98,12 +98,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(Block.rawById(i), new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors.get(i - from)), block));
mappings.put(IdAndData.toRawData(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(Block.rawById(i), mappedBlockItem);
mappings.put(IdAndData.toRawData(i), mappedBlockItem);
}
}
}
@ -208,7 +208,7 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
if (type.isPresent() && type.get() == EntityTypes1_12.ObjectType.FALLING_BLOCK) {
final int objectData = wrapper.get(Type.INT, 0);
final Block block = handleBlock(objectData & 4095, objectData >> 12 & 15);
final IdAndData block = handleBlock(objectData & 4095, objectData >> 12 & 15);
if (block == null) return;
wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12);
@ -216,11 +216,11 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
};
}
public @Nullable Block handleBlock(int blockId, int data) {
public @Nullable IdAndData handleBlock(int blockId, int data) {
MappedLegacyBlockItem settings = getMappedBlockItem(blockId, data);
if (settings == null || !settings.isBlock()) return null;
Block block = settings.getBlock();
IdAndData block = settings.getBlock();
// For some blocks, the data can still be useful (:
if (block.getData() == -1) {
return block.withData(data);
@ -229,13 +229,13 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
}
public int handleBlockId(final int rawId) {
final int id = Block.getId(rawId);
final int data = Block.getData(rawId);
final int id = IdAndData.getId(rawId);
final int data = IdAndData.getData(rawId);
final Block mappedBlock = handleBlock(id, data);
final IdAndData mappedBlock = handleBlock(id, data);
if (mappedBlock == null) return rawId;
return Block.toRawId(mappedBlock.getId(), mappedBlock.getData());
return IdAndData.toRawData(mappedBlock.getId(), mappedBlock.getData());
}
public void handleChunk(Chunk chunk) {
@ -283,9 +283,9 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
int btype = block >> 4;
int meta = block & 0xF;
Block b = handleBlock(btype, meta);
IdAndData b = handleBlock(btype, meta);
if (b != null) {
palette.setIdByIndex(j, Block.toRawId(b.getId(), b.getData()));
palette.setIdByIndex(j, IdAndData.toRawData(b.getId(), b.getData()));
}
// We already know that is has a handler
@ -336,13 +336,13 @@ public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S
}
private @Nullable MappedLegacyBlockItem getMappedBlockItem(int id, int data) {
MappedLegacyBlockItem mapping = replacementData.get(Block.toRawId(id, data));
return mapping != null || data == 0 ? mapping : replacementData.get(Block.rawById(id));
MappedLegacyBlockItem mapping = replacementData.get(IdAndData.toRawData(id, data));
return mapping != null || data == 0 ? mapping : replacementData.get(IdAndData.toRawData(id));
}
private @Nullable MappedLegacyBlockItem getMappedBlockItem(int rawId) {
MappedLegacyBlockItem mapping = replacementData.get(rawId);
return mapping != null ? mapping : replacementData.get(Block.rawByData(rawId));
return mapping != null ? mapping : replacementData.get(IdAndData.removeData(rawId));
}
protected JsonObject readMappingsFile(final String name) {

View File

@ -24,7 +24,6 @@ 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;
@ -41,6 +40,7 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.protocols.protocol1_11to1_10.rewriter.EntityIdRewriter;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;
import com.viaversion.viaversion.util.IdAndData;
import java.util.Arrays;
import java.util.Optional;
@ -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(Block.rawById(52), s -> new MappedLegacyBlockItem(52, (short) -1, null, false));
MappedLegacyBlockItem data = replacementData.computeIfAbsent(IdAndData.toRawData(52), s -> new MappedLegacyBlockItem(52, (short) -1, null, false));
data.setBlockEntityHandler((b, tag) -> {
EntityIdRewriter.toClientSpawner(tag, true);
return tag;

View File

@ -26,7 +26,6 @@ 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;
@ -56,6 +55,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.BlockIdData;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.IdAndData;
import com.viaversion.viaversion.util.Key;
import com.viaversion.viaversion.util.Pair;
import java.util.ArrayList;
@ -100,11 +100,11 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit
}
if (SpawnEggRewriter.getEntityId(oldId).isPresent()) {
wrapper.write(Type.VAR_INT, Block.rawById(383));
wrapper.write(Type.VAR_INT, IdAndData.toRawData(383));
return;
}
wrapper.write(Type.VAR_INT, Block.getId(oldId));
wrapper.write(Type.VAR_INT, IdAndData.getId(oldId));
});
protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketHandlers() {
@ -751,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 = Block.toRawId(item.identifier(), item.data());
int rawId = IdAndData.toRawData(item.identifier(), item.data());
// NBT Additions
if (isDamageable(item.identifier())) {
@ -832,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 = Block.rawById(32); // Dead Bush
rawId = IdAndData.toRawData(32); // Dead Bush
} else if (protocol.getMappingData().getItemMappings().inverse().getNewId(rawId & ~0xF) != -1) {
rawId &= ~0xF; // Remove data
} else {

View File

@ -1,77 +0,0 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2024 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.utils;
public class Block {
private final int id;
private final short data;
public Block(int id, int data) {
this.id = id;
this.data = (short) data;
}
public int getId() {
return id;
}
public int getData() {
return data;
}
public Block withData(int data) {
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;
if (o == null || getClass() != o.getClass()) return false;
Block block = (Block) o;
if (id != block.id) return false;
return data == block.data;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + data;
return result;
}
}