Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-04 23:30:24 +01:00
Small cleanup
Dieser Commit ist enthalten in:
Ursprung
8d3492ba30
Commit
bb4a8b73e0
@ -41,19 +41,19 @@ public final class AttributeModifier {
|
|||||||
public void write(final ByteBuf buffer, final AttributeModifier value) throws Exception {
|
public void write(final ByteBuf buffer, final AttributeModifier value) throws Exception {
|
||||||
Type.VAR_INT.writePrimitive(buffer, value.attribute);
|
Type.VAR_INT.writePrimitive(buffer, value.attribute);
|
||||||
ModifierData.TYPE.write(buffer, value.modifier);
|
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);
|
public static final Type<AttributeModifier[]> ARRAY_TYPE = new ArrayType<>(TYPE);
|
||||||
|
|
||||||
private final int attribute;
|
private final int attribute;
|
||||||
private final ModifierData modifier;
|
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.attribute = attribute;
|
||||||
this.modifier = modifier;
|
this.modifier = modifier;
|
||||||
this.slot = slot;
|
this.slotType = slotType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int attribute() {
|
public int attribute() {
|
||||||
@ -64,7 +64,7 @@ public final class AttributeModifier {
|
|||||||
return modifier;
|
return modifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int slot() {
|
public int slotType() {
|
||||||
return slot;
|
return slotType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,19 +22,18 @@
|
|||||||
*/
|
*/
|
||||||
package com.viaversion.viaversion.api.type.types.item;
|
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.data.FullMappings;
|
||||||
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
|
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
|
||||||
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
|
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
|
||||||
import com.viaversion.viaversion.api.protocol.Protocol;
|
import com.viaversion.viaversion.api.protocol.Protocol;
|
||||||
import com.viaversion.viaversion.api.type.Type;
|
import com.viaversion.viaversion.api.type.Type;
|
||||||
import io.netty.buffer.ByteBuf;
|
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;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
public class StructuredDataType extends Type<StructuredData<?>> {
|
public class StructuredDataType extends Type<StructuredData<?>> {
|
||||||
|
|
||||||
private final Int2ObjectMap<StructuredDataKey<?>> types = new Int2ObjectOpenHashMap<>();
|
private StructuredDataKey<?>[] types;
|
||||||
|
|
||||||
public StructuredDataType() {
|
public StructuredDataType() {
|
||||||
super(StructuredData.class);
|
super(StructuredData.class);
|
||||||
@ -48,16 +47,17 @@ public class StructuredDataType extends Type<StructuredData<?>> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StructuredData<?> read(final ByteBuf buffer) throws Exception {
|
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 int id = Type.VAR_INT.readPrimitive(buffer);
|
||||||
final StructuredDataKey<?> key = this.types.get(id);
|
final StructuredDataKey<?> key = this.types[id];
|
||||||
if (key != null) {
|
if (key == null) {
|
||||||
return readData(buffer, key, id);
|
|
||||||
}
|
|
||||||
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) {
|
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 {
|
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) {
|
public DataFiller filler(final Protocol<?, ?, ?, ?> protocol) {
|
||||||
return filler(protocol, true);
|
return new DataFiller(protocol);
|
||||||
}
|
|
||||||
|
|
||||||
public DataFiller filler(final Protocol<?, ?, ?, ?> protocol, final boolean useMappedNames) {
|
|
||||||
return new DataFiller(protocol, useMappedNames);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class DataFiller {
|
public final class DataFiller {
|
||||||
|
|
||||||
private final FullMappings mappings;
|
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.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) {
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 StructuredItem item = new StructuredItem(old.identifier(), (byte) old.amount(), new StructuredDataContainer());
|
||||||
final StructuredDataContainer data = item.structuredData();
|
final StructuredDataContainer data = item.structuredData();
|
||||||
data.setIdLookup(protocol, true);
|
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) {
|
if (tag == null) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rewrite nbt to new data structures
|
// Rewrite nbt to new data structures
|
||||||
final int hideFlagsValue = tag.getInt("HideFlags");
|
final int hideFlagsValue = tag.getInt("HideFlags");
|
||||||
if ((hideFlagsValue & 0x20) != 0) {
|
if ((hideFlagsValue & StructuredDataConverter.HIDE_ADDITIONAL) != 0) {
|
||||||
data.set(StructuredDataKey.HIDE_ADDITIONAL_TOOLTIP);
|
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");
|
final NumberTag unbreakable = tag.getNumberTag("Unbreakable");
|
||||||
if (unbreakable != null && unbreakable.asBoolean()) {
|
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");
|
final CompoundTag trimTag = tag.getCompoundTag("Trim");
|
||||||
if (trimTag != null) {
|
if (trimTag != null) {
|
||||||
updateArmorTrim(data, trimTag, (hideFlagsValue & 0x80) == 0);
|
updateArmorTrim(data, trimTag, (hideFlagsValue & StructuredDataConverter.HIDE_ARMOR_TRIM) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
final CompoundTag explosionTag = tag.getCompoundTag("Explosion");
|
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);
|
final ListTag<CompoundTag> attributeModifiersTag = tag.getListTag("AttributeModifiers", CompoundTag.class);
|
||||||
if (attributeModifiersTag != null) {
|
if (attributeModifiersTag != null) {
|
||||||
updateAttributes(data, attributeModifiersTag, (hideFlagsValue & 0x02) == 0);
|
updateAttributes(data, attributeModifiersTag, (hideFlagsValue & StructuredDataConverter.HIDE_ATTRIBUTES) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
final CompoundTag fireworksTag = tag.getCompoundTag("Fireworks");
|
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);
|
updateItemList(data, tag, "Items", StructuredDataKey.BUNDLE_CONTENTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateEnchantments(data, tag, "Enchantments", StructuredDataKey.ENCHANTMENTS, (hideFlagsValue & 0x01) == 0);
|
updateEnchantments(data, tag, "Enchantments", StructuredDataKey.ENCHANTMENTS, (hideFlagsValue & StructuredDataConverter.HIDE_ENCHANTMENTS) == 0);
|
||||||
updateEnchantments(data, tag, "StoredEnchantments", StructuredDataKey.STORED_ENCHANTMENTS, (hideFlagsValue & 0x20) == 0);
|
updateEnchantments(data, tag, "StoredEnchantments", StructuredDataKey.STORED_ENCHANTMENTS, (hideFlagsValue & StructuredDataConverter.HIDE_ADDITIONAL) == 0);
|
||||||
|
|
||||||
final NumberTag mapId = tag.getNumberTag("map");
|
final NumberTag mapId = tag.getNumberTag("map");
|
||||||
if (mapId != null) {
|
if (mapId != null) {
|
||||||
@ -418,7 +418,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
|
|||||||
final String name = modifierTag.getString("Name");
|
final String name = modifierTag.getString("Name");
|
||||||
final NumberTag amountTag = modifierTag.getNumberTag("Amount");
|
final NumberTag amountTag = modifierTag.getNumberTag("Amount");
|
||||||
final IntArrayTag uuidTag = modifierTag.getIntArrayTag("UUID");
|
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) {
|
if (name == null || attributeName == null || amountTag == null || uuidTag == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -441,7 +441,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
|
|||||||
amountTag.asDouble(),
|
amountTag.asDouble(),
|
||||||
operationId
|
operationId
|
||||||
),
|
),
|
||||||
slot
|
slotType
|
||||||
);
|
);
|
||||||
}).filter(Objects::nonNull).toArray(AttributeModifier[]::new);
|
}).filter(Objects::nonNull).toArray(AttributeModifier[]::new);
|
||||||
data.set(StructuredDataKey.ATTRIBUTE_MODIFIERS, new AttributeModifiers(modifiers, showInTooltip));
|
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");
|
final NumberTag colorTag = displayTag.getNumberTag("color");
|
||||||
if (colorTag != null) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,15 @@ import java.util.Map;
|
|||||||
|
|
||||||
final class StructuredDataConverter {
|
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<>();
|
private static final Map<StructuredDataKey<?>, DataConverter<?>> REWRITERS = new Reference2ObjectOpenHashMap<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@ -50,7 +59,7 @@ final class StructuredDataConverter {
|
|||||||
register(StructuredDataKey.UNBREAKABLE, (data, tag) -> {
|
register(StructuredDataKey.UNBREAKABLE, (data, tag) -> {
|
||||||
tag.putBoolean("Unbreakable", true);
|
tag.putBoolean("Unbreakable", true);
|
||||||
if (!data.showInTooltip()) {
|
if (!data.showInTooltip()) {
|
||||||
putHideFlag(tag, 0x04);
|
putHideFlag(tag, HIDE_UNBREAKABLE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> tag.putString("CustomName", ComponentUtil.tagToJsonString(data)));
|
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("AttributeName", identifier);
|
||||||
modifierTag.putString("Name", modifier.modifier().name());
|
modifierTag.putString("Name", modifier.modifier().name());
|
||||||
modifierTag.putDouble("Amount", modifier.modifier().amount());
|
modifierTag.putDouble("Amount", modifier.modifier().amount());
|
||||||
modifierTag.putInt("Slot", modifier.slot());
|
modifierTag.putInt("Slot", modifier.slotType());
|
||||||
modifierTag.putInt("Operation", modifier.modifier().operation());
|
modifierTag.putInt("Operation", modifier.modifier().operation());
|
||||||
modifiers.add(modifierTag);
|
modifiers.add(modifierTag);
|
||||||
}
|
}
|
||||||
tag.put("AttributeModifiers", modifiers);
|
tag.put("AttributeModifiers", modifiers);
|
||||||
|
|
||||||
if (!data.showInTooltip()) {
|
if (!data.showInTooltip()) {
|
||||||
putHideFlag(tag, 0x02);
|
putHideFlag(tag, HIDE_ATTRIBUTES);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
register(StructuredDataKey.CUSTOM_MODEL_DATA, (data, tag) -> tag.putInt("CustomModelData", data));
|
register(StructuredDataKey.CUSTOM_MODEL_DATA, (data, tag) -> tag.putInt("CustomModelData", data));
|
||||||
@ -91,7 +100,7 @@ final class StructuredDataConverter {
|
|||||||
register(StructuredDataKey.DYED_COLOR, (data, tag) -> {
|
register(StructuredDataKey.DYED_COLOR, (data, tag) -> {
|
||||||
tag.putInt("color", data.rgb());
|
tag.putInt("color", data.rgb());
|
||||||
if (!data.showInTooltip()) {
|
if (!data.showInTooltip()) {
|
||||||
putHideFlag(tag, 0x40);
|
putHideFlag(tag, HIDE_DYE_COLOR);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
register(StructuredDataKey.MAP_COLOR, (data, tag) -> tag.putInt("MapColor", data));
|
register(StructuredDataKey.MAP_COLOR, (data, tag) -> tag.putInt("MapColor", data));
|
||||||
@ -281,7 +290,7 @@ final class StructuredDataConverter {
|
|||||||
tag.put(storedEnchantments ? "StoredEnchantments" : "Enchantments", enchantments);
|
tag.put(storedEnchantments ? "StoredEnchantments" : "Enchantments", enchantments);
|
||||||
|
|
||||||
if (!data.showInTooltip()) {
|
if (!data.showInTooltip()) {
|
||||||
putHideFlag(tag, storedEnchantments ? 0x20 : 0x01);
|
putHideFlag(tag, storedEnchantments ? HIDE_ADDITIONAL : HIDE_ENCHANTMENTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren