Small cleanup

This commit is contained in:
Nassim Jahnke 2024-03-14 10:54:18 +01:00
parent 8d3492ba30
commit bb4a8b73e0
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 46 additions and 38 deletions

View File

@ -41,19 +41,19 @@ public final class AttributeModifier {
public void write(final ByteBuf buffer, final AttributeModifier value) throws Exception {
Type.VAR_INT.writePrimitive(buffer, value.attribute);
ModifierData.TYPE.write(buffer, value.modifier);
Type.VAR_INT.writePrimitive(buffer, value.slot);
Type.VAR_INT.writePrimitive(buffer, value.slotType);
}
};
public static final Type<AttributeModifier[]> ARRAY_TYPE = new ArrayType<>(TYPE);
private final int attribute;
private final ModifierData modifier;
private final int slot;
private final int slotType;
public AttributeModifier(final int attribute, final ModifierData modifier, final int slot) {
public AttributeModifier(final int attribute, final ModifierData modifier, final int slotType) {
this.attribute = attribute;
this.modifier = modifier;
this.slot = slot;
this.slotType = slotType;
}
public int attribute() {
@ -64,7 +64,7 @@ public final class AttributeModifier {
return modifier;
}
public int slot() {
return slot;
public int slotType() {
return slotType;
}
}

View File

@ -22,19 +22,18 @@
*/
package com.viaversion.viaversion.api.type.types.item;
import com.google.common.base.Preconditions;
import com.viaversion.viaversion.api.data.FullMappings;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.type.Type;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable;
public class StructuredDataType extends Type<StructuredData<?>> {
private final Int2ObjectMap<StructuredDataKey<?>> types = new Int2ObjectOpenHashMap<>();
private StructuredDataKey<?>[] types;
public StructuredDataType() {
super(StructuredData.class);
@ -48,16 +47,17 @@ public class StructuredDataType extends Type<StructuredData<?>> {
@Override
public StructuredData<?> read(final ByteBuf buffer) throws Exception {
Preconditions.checkNotNull(types, "StructuredDataType has not been initialized");
final int id = Type.VAR_INT.readPrimitive(buffer);
final StructuredDataKey<?> key = this.types.get(id);
if (key != null) {
return readData(buffer, key, id);
final StructuredDataKey<?> key = this.types[id];
if (key == null) {
throw new IllegalArgumentException("No data component serializer found for id " + id);
}
throw new IllegalArgumentException("No data component serializer found for id " + id);
return readData(buffer, key, id);
}
public @Nullable StructuredDataKey<?> key(final int id) {
return types.get(id);
return id >= 0 && id < types.length ? types[id] : null;
}
private <T> StructuredData<T> readData(final ByteBuf buffer, final StructuredDataKey<T> key, final int id) throws Exception {
@ -65,25 +65,24 @@ public class StructuredDataType extends Type<StructuredData<?>> {
}
public DataFiller filler(final Protocol<?, ?, ?, ?> protocol) {
return filler(protocol, true);
}
public DataFiller filler(final Protocol<?, ?, ?, ?> protocol, final boolean useMappedNames) {
return new DataFiller(protocol, useMappedNames);
return new DataFiller(protocol);
}
public final class DataFiller {
private final FullMappings mappings;
private final boolean useMappedNames;
private DataFiller(final Protocol<?, ?, ?, ?> protocol, final boolean useMappedNames) {
private DataFiller(final Protocol<?, ?, ?, ?> protocol) {
this.mappings = protocol.getMappingData().getDataComponentSerializerMappings();
this.useMappedNames = useMappedNames;
Preconditions.checkArgument(mappings != null, "No mappings found for protocol %s", protocol.getClass());
Preconditions.checkArgument(types == null, "StructuredDataType has already been initialized");
types = new StructuredDataKey[mappings.mappedSize()];
}
public DataFiller add(final StructuredDataKey<?> reader) {
types.put(useMappedNames ? mappings.mappedId(reader.identifier()) : mappings.id(reader.identifier()), reader);
final int id = mappings.mappedId(reader.identifier());
Preconditions.checkArgument(id != -1, "No mapped id found for %s", reader.identifier());
types[id] = reader;
return this;
}
}

View File

@ -261,14 +261,14 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final StructuredItem item = new StructuredItem(old.identifier(), (byte) old.amount(), new StructuredDataContainer());
final StructuredDataContainer data = item.structuredData();
data.setIdLookup(protocol, true);
// TODO add default data :>
// TODO add default data if needed (e.g. when getting a goat horn via the give command) :>
if (tag == null) {
return item;
}
// Rewrite nbt to new data structures
final int hideFlagsValue = tag.getInt("HideFlags");
if ((hideFlagsValue & 0x20) != 0) {
if ((hideFlagsValue & StructuredDataConverter.HIDE_ADDITIONAL) != 0) {
data.set(StructuredDataKey.HIDE_ADDITIONAL_TOOLTIP);
}
@ -313,12 +313,12 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final NumberTag unbreakable = tag.getNumberTag("Unbreakable");
if (unbreakable != null && unbreakable.asBoolean()) {
data.set(StructuredDataKey.UNBREAKABLE, new Unbreakable((hideFlagsValue & 0x04) == 0));
data.set(StructuredDataKey.UNBREAKABLE, new Unbreakable((hideFlagsValue & StructuredDataConverter.HIDE_UNBREAKABLE) == 0));
}
final CompoundTag trimTag = tag.getCompoundTag("Trim");
if (trimTag != null) {
updateArmorTrim(data, trimTag, (hideFlagsValue & 0x80) == 0);
updateArmorTrim(data, trimTag, (hideFlagsValue & StructuredDataConverter.HIDE_ARMOR_TRIM) == 0);
}
final CompoundTag explosionTag = tag.getCompoundTag("Explosion");
@ -352,7 +352,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final ListTag<CompoundTag> attributeModifiersTag = tag.getListTag("AttributeModifiers", CompoundTag.class);
if (attributeModifiersTag != null) {
updateAttributes(data, attributeModifiersTag, (hideFlagsValue & 0x02) == 0);
updateAttributes(data, attributeModifiersTag, (hideFlagsValue & StructuredDataConverter.HIDE_ATTRIBUTES) == 0);
}
final CompoundTag fireworksTag = tag.getCompoundTag("Fireworks");
@ -378,8 +378,8 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
updateItemList(data, tag, "Items", StructuredDataKey.BUNDLE_CONTENTS);
}
updateEnchantments(data, tag, "Enchantments", StructuredDataKey.ENCHANTMENTS, (hideFlagsValue & 0x01) == 0);
updateEnchantments(data, tag, "StoredEnchantments", StructuredDataKey.STORED_ENCHANTMENTS, (hideFlagsValue & 0x20) == 0);
updateEnchantments(data, tag, "Enchantments", StructuredDataKey.ENCHANTMENTS, (hideFlagsValue & StructuredDataConverter.HIDE_ENCHANTMENTS) == 0);
updateEnchantments(data, tag, "StoredEnchantments", StructuredDataKey.STORED_ENCHANTMENTS, (hideFlagsValue & StructuredDataConverter.HIDE_ADDITIONAL) == 0);
final NumberTag mapId = tag.getNumberTag("map");
if (mapId != null) {
@ -418,7 +418,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final String name = modifierTag.getString("Name");
final NumberTag amountTag = modifierTag.getNumberTag("Amount");
final IntArrayTag uuidTag = modifierTag.getIntArrayTag("UUID");
final int slot = modifierTag.getInt("Slot");
final int slotType = modifierTag.getInt("Slot");
if (name == null || attributeName == null || amountTag == null || uuidTag == null) {
return null;
}
@ -441,7 +441,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
amountTag.asDouble(),
operationId
),
slot
slotType
);
}).filter(Objects::nonNull).toArray(AttributeModifier[]::new);
data.set(StructuredDataKey.ATTRIBUTE_MODIFIERS, new AttributeModifiers(modifiers, showInTooltip));
@ -801,7 +801,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final NumberTag colorTag = displayTag.getNumberTag("color");
if (colorTag != null) {
data.set(StructuredDataKey.DYED_COLOR, new DyedColor(colorTag.asInt(), (hideFlags & 0x40) == 0));
data.set(StructuredDataKey.DYED_COLOR, new DyedColor(colorTag.asInt(), (hideFlags & StructuredDataConverter.HIDE_DYE_COLOR) == 0));
}
}

View File

@ -43,6 +43,15 @@ import java.util.Map;
final class StructuredDataConverter {
static final int HIDE_ENCHANTMENTS = 1;
static final int HIDE_ATTRIBUTES = 1 << 1;
static final int HIDE_UNBREAKABLE = 1 << 2;
static final int HIDE_CAN_BREAK = 1 << 3;
static final int HIDE_CAN_PLACE_ON = 1 << 4;
static final int HIDE_ADDITIONAL = 1 << 5;
static final int HIDE_DYE_COLOR = 1 << 6;
static final int HIDE_ARMOR_TRIM = 1 << 7;
private static final Map<StructuredDataKey<?>, DataConverter<?>> REWRITERS = new Reference2ObjectOpenHashMap<>();
static {
@ -50,7 +59,7 @@ final class StructuredDataConverter {
register(StructuredDataKey.UNBREAKABLE, (data, tag) -> {
tag.putBoolean("Unbreakable", true);
if (!data.showInTooltip()) {
putHideFlag(tag, 0x04);
putHideFlag(tag, HIDE_UNBREAKABLE);
}
});
register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> tag.putString("CustomName", ComponentUtil.tagToJsonString(data)));
@ -75,14 +84,14 @@ final class StructuredDataConverter {
modifierTag.putString("AttributeName", identifier);
modifierTag.putString("Name", modifier.modifier().name());
modifierTag.putDouble("Amount", modifier.modifier().amount());
modifierTag.putInt("Slot", modifier.slot());
modifierTag.putInt("Slot", modifier.slotType());
modifierTag.putInt("Operation", modifier.modifier().operation());
modifiers.add(modifierTag);
}
tag.put("AttributeModifiers", modifiers);
if (!data.showInTooltip()) {
putHideFlag(tag, 0x02);
putHideFlag(tag, HIDE_ATTRIBUTES);
}
});
register(StructuredDataKey.CUSTOM_MODEL_DATA, (data, tag) -> tag.putInt("CustomModelData", data));
@ -91,7 +100,7 @@ final class StructuredDataConverter {
register(StructuredDataKey.DYED_COLOR, (data, tag) -> {
tag.putInt("color", data.rgb());
if (!data.showInTooltip()) {
putHideFlag(tag, 0x40);
putHideFlag(tag, HIDE_DYE_COLOR);
}
});
register(StructuredDataKey.MAP_COLOR, (data, tag) -> tag.putInt("MapColor", data));
@ -281,7 +290,7 @@ final class StructuredDataConverter {
tag.put(storedEnchantments ? "StoredEnchantments" : "Enchantments", enchantments);
if (!data.showInTooltip()) {
putHideFlag(tag, storedEnchantments ? 0x20 : 0x01);
putHideFlag(tag, storedEnchantments ? HIDE_ADDITIONAL : HIDE_ENCHANTMENTS);
}
}