3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-19 14:30:16 +01:00

Add a few structured data -> nbt converters

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-03-12 11:00:07 +01:00
Ursprung 666b204ebb
Commit 658364b643
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
3 geänderte Dateien mit 85 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -64,11 +64,11 @@ public final class Enchantments1_20_3 {
"vanishing_curse"
);
public static @Nullable String enchantment(final int id) {
public static @Nullable String idToKey(final int id) {
return ENCHANTMENTS.idToKey(id);
}
public static int id(final String enchantment) {
public static int keyToId(final String enchantment) {
return ENCHANTMENTS.keyToId(enchantment);
}
}

Datei anzeigen

@ -416,8 +416,8 @@ 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 NumberTag slotTag = modifierTag.getNumberTag("Slot");
if (name == null || attributeName == null || amountTag == null || uuidTag == null || slotTag == null) {
final int slot = modifierTag.getInt("Slot");
if (name == null || attributeName == null || amountTag == null || uuidTag == null) {
return null;
}
@ -439,7 +439,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
amountTag.asDouble(),
operationId
),
slotTag.asInt()
slot
);
}).filter(Objects::nonNull).toArray(AttributeModifier[]::new);
data.set(StructuredDataKey.ATTRIBUTE_MODIFIERS, new AttributeModifiers(modifiers, showInTooltip));
@ -699,7 +699,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
continue;
}
final int intId = Enchantments1_20_3.id(id);
final int intId = Enchantments1_20_3.keyToId(id);
if (intId == -1) {
continue;
}

Datei anzeigen

@ -18,9 +18,17 @@
package com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.minecraft.item.data.AttributeModifier;
import com.viaversion.viaversion.api.minecraft.item.data.Enchantments;
import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.Attributes1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.Enchantments1_20_3;
import com.viaversion.viaversion.util.ComponentUtil;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import java.util.Map;
@ -29,8 +37,75 @@ final class StructuredDataConverter {
private static final Map<StructuredDataKey<?>, Rewriter<?>> REWRITERS = new Reference2ObjectOpenHashMap<>();
static {
// TODO
register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> tag.putString("CustomName", ComponentUtil.tagToJsonString(data.value())));
register(StructuredDataKey.DAMAGE, (data, tag) -> tag.putInt("Damage", data));
register(StructuredDataKey.UNBREAKABLE, (data, tag) -> {
tag.putBoolean("Unbreakable", true);
if (!data.showInTooltip()) {
putHideFlag(tag, 0x04);
}
});
register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> tag.putString("CustomName", ComponentUtil.tagToJsonString(data)));
register(StructuredDataKey.LORE, (data, tag) -> {
final ListTag<StringTag> lore = new ListTag<>(StringTag.class);
for (final Tag loreEntry : data) {
lore.add(new StringTag(ComponentUtil.tagToJsonString(loreEntry)));
}
tag.put("Lore", lore);
});
register(StructuredDataKey.ENCHANTMENTS, StructuredDataConverter::convertEnchantments);
register(StructuredDataKey.STORED_ENCHANTMENTS, StructuredDataConverter::convertEnchantments);
//register(StructuredDataKey.CAN_PLACE_ON, (data, tag) -> ); // TODO
//register(StructuredDataKey.CAN_BREAK, (data, tag) -> ); // TODO
register(StructuredDataKey.ATTRIBUTE_MODIFIERS, (data, tag) -> {
final ListTag<CompoundTag> modifiers = new ListTag<>(CompoundTag.class);
for (final AttributeModifier modifier : data.modifiers()) {
final String identifier = Attributes1_20_3.idToKey(modifier.attribute());
if (identifier == null) {
continue;
}
final CompoundTag modifierTag = new CompoundTag();
modifierTag.putString("AttributeName", identifier);
modifierTag.putString("Name", modifier.modifier().name());
modifierTag.putDouble("Amount", modifier.modifier().amount());
modifierTag.putInt("Slot", modifier.slot());
modifierTag.putInt("Operation", modifier.modifier().operation());
modifiers.add(modifierTag);
}
tag.put("AttributeModifiers", modifiers);
if (!data.showInTooltip()) {
putHideFlag(tag, 0x02);
}
});
register(StructuredDataKey.CUSTOM_MODEL_DATA, (data, tag) -> tag.putInt("CustomModelData", data));
register(StructuredDataKey.HIDE_ADDITIONAL_TOOLTIP, (data, tag) -> putHideFlag(tag, 0x20));
register(StructuredDataKey.REPAIR_COST, (data, tag) -> tag.putInt("RepairCost", data));
}
private static void convertEnchantments(final Enchantments data, final CompoundTag tag) {
final ListTag<CompoundTag> enchantments = new ListTag<>(CompoundTag.class);
for (final Int2IntMap.Entry entry : data.enchantments().int2IntEntrySet()) {
final String identifier = Enchantments1_20_3.idToKey(entry.getIntKey());
if (identifier == null) {
continue;
}
final CompoundTag enchantment = new CompoundTag();
enchantment.putString("id", identifier);
enchantment.putShort("lvl", (short) entry.getIntKey());
enchantments.add(enchantment);
}
tag.put("Enchantments", enchantments);
if (!data.showInTooltip()) {
putHideFlag(tag, 0x01);
}
}
private static void putHideFlag(final CompoundTag tag, final int value) {
tag.putInt("HideFlags", tag.getInt("HideFlags") | value);
}
public static <T> void rewrite(final StructuredData<T> data, final CompoundTag tag) {
@ -41,7 +116,7 @@ final class StructuredDataConverter {
//noinspection unchecked
final Rewriter<T> rewriter = (Rewriter<T>) REWRITERS.get(data.key());
if (rewriter != null) {
rewriter.rewrite(data, tag);
rewriter.rewrite(data.value(), tag);
}
}
@ -52,6 +127,6 @@ final class StructuredDataConverter {
@FunctionalInterface
interface Rewriter<T> {
void rewrite(StructuredData<T> data, CompoundTag tag);
void rewrite(T data, CompoundTag tag);
}
}