Update adventure to v4.22.0

This commit is contained in:
booky10 committed 2025-06-17 18:06:52 +02:00
1 parent 781d4a7c82
commit c1324fe7fe
24 files changed
+397 -119

No files matched your search

@@ -72,7 +72,12 @@ public class ChatTypeDecoration {
wrapper.writeStyle(decoration.style);
}
@Deprecated
public static ChatTypeDecoration decode(NBT nbt, ClientVersion version) {
return decode(nbt, PacketWrapper.createDummyWrapper(version));
}
public static ChatTypeDecoration decode(NBT nbt, PacketWrapper<?> wrapper) {
NBTCompound compound = (NBTCompound) nbt;
String translationKey = compound.getStringTagValueOrThrow("translation_key");
NBT paramsTag = compound.getTagOrThrow("parameters");
@@ -90,11 +95,16 @@ public class ChatTypeDecoration {
}
NBTCompound styleTag = compound.getCompoundTagOrNull("style");
Style style = styleTag == null ? empty() :
AdventureSerializer.serializer(version).nbt().deserializeStyle(styleTag);
wrapper.getSerializers().nbt().deserializeStyle(styleTag, wrapper);
return new ChatTypeDecoration(translationKey, params, style);
}
@Deprecated
public static NBT encode(ChatTypeDecoration decoration, ClientVersion version) {
return encode(decoration, PacketWrapper.createDummyWrapper(version));
}
public static NBT encode(ChatTypeDecoration decoration, PacketWrapper<?> wrapper) {
NBTList<NBTString> paramsTag = NBTList.createStringList();
for (Parameter param : decoration.parameters) {
paramsTag.addTag(new NBTString(param.getId()));
@@ -104,8 +114,8 @@ public class ChatTypeDecoration {
compound.setTag("translation_key", new NBTString(decoration.translationKey));
compound.setTag("parameters", paramsTag);
if (!decoration.style.isEmpty()) {
compound.setTag("style", AdventureSerializer.serializer(version)
.nbt().serializeStyle(decoration.style));
compound.setTag("style", wrapper.getSerializers()
.nbt().serializeStyle(decoration.style, wrapper));
}
return compound;
}
@@ -18,9 +18,11 @@
package com.github.retrooper.packetevents.protocol.chat.clickevent;
import com.github.retrooper.packetevents.protocol.dialog.Dialog;
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.text.event.ClickEvent.Payload;
import org.jspecify.annotations.NullMarked;
@NullMarked
@@ -54,6 +56,14 @@ public interface ClickEvent {
return new ChangePageClickEvent(clickEvent.value());
case COPY_TO_CLIPBOARD:
return new CopyToClipboardClickEvent(clickEvent.value());
case SHOW_DIALOG:
return new ShowDialogClickEvent((Dialog) ((Payload.Dialog) clickEvent.payload()).dialog());
case CUSTOM:
Payload.Custom payload = (Payload.Custom) clickEvent.payload();
return new CustomClickEvent(
new ResourceLocation(payload.key()),
null // TODO parse snbt
);
default:
throw new UnsupportedOperationException("Unsupported clickevent: " + clickEvent);
}
@@ -56,7 +56,8 @@ public class CustomClickEvent implements ClickEvent {
@Override
public net.kyori.adventure.text.event.ClickEvent asAdventure() {
throw new UnsupportedOperationException();
// TODO convert payload to snbt
return net.kyori.adventure.text.event.ClickEvent.custom(this.id.key(), null);
}
public ResourceLocation getId() {
@@ -48,7 +48,7 @@ public class ShowDialogClickEvent implements ClickEvent {
@Override
public net.kyori.adventure.text.event.ClickEvent asAdventure() {
throw new UnsupportedOperationException();
return net.kyori.adventure.text.event.ClickEvent.showDialog(this.dialog);
}
public Dialog getDialog() {
@@ -22,14 +22,22 @@ import com.github.retrooper.packetevents.protocol.nbt.NBT;
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.util.mappings.IRegistry;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NullMarked;
import java.util.Map;
import java.util.Optional;
@NullMarked
public interface IComponentMap {
static StaticComponentMap decode(NBT nbt, PacketWrapper<?> wrapper, IRegistry<? extends ComponentType<?>> registry) {
return decode(nbt, wrapper.getServerVersion().toClientVersion(), registry);
}
@Deprecated
@SuppressWarnings("unchecked") // safe in this case
static StaticComponentMap decode(NBT nbt, ClientVersion version, IRegistry<? extends ComponentType<?>> registry) {
NBTCompound compound = (NBTCompound) nbt;
@@ -45,6 +53,11 @@ public interface IComponentMap {
return components.build();
}
static NBT encode(PacketWrapper<?> wrapper, StaticComponentMap components) {
return encode(components, wrapper.getServerVersion().toClientVersion());
}
@Deprecated
@SuppressWarnings("unchecked") // safe in this case
static NBT encode(StaticComponentMap components, ClientVersion version) {
NBTCompound compound = new NBTCompound();
@@ -26,12 +26,13 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.dialog.DialogLike;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NullMarked;
@NullMarked
public interface Dialog extends MappedEntity, DeepComparableEntity, CopyableEntity<Dialog> {
public interface Dialog extends MappedEntity, DeepComparableEntity, CopyableEntity<Dialog>, DialogLike {
static Dialog read(PacketWrapper<?> wrapper) {
return wrapper.readMappedEntityOrDirect(Dialogs.getRegistry(), Dialog::readDirect);
@@ -87,14 +87,19 @@ public interface WolfVariant extends MappedEntity, CopyableEntity<WolfVariant>,
MappedEntitySet.write(wrapper, variant.getBiomes());
}
@Deprecated
static WolfVariant decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
return decode(nbt, PacketWrapper.createDummyWrapper(version), data);
}
static WolfVariant decode(NBT nbt, PacketWrapper<?> wrapper, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
if (version.isOlderThan(ClientVersion.V_1_21_5)) {
if (wrapper.getServerVersion().isOlderThan(ServerVersion.V_1_21_5)) {
ResourceLocation wildTexture = new ResourceLocation(compound.getStringTagValueOrThrow("wild_texture"));
ResourceLocation tameTexture = new ResourceLocation(compound.getStringTagValueOrThrow("tame_texture"));
ResourceLocation angryTexture = new ResourceLocation(compound.getStringTagValueOrThrow("angry_texture"));
MappedEntitySet<Biome> biomes = MappedEntitySet.decode(
compound.getTagOrThrow("biomes"), version, Biomes.getRegistry());
MappedEntitySet<Biome> biomes = compound.getOrThrow("biomes", (tag, ew) ->
MappedEntitySet.decode(tag, ew, Biomes.getRegistry()), wrapper);
return new StaticWolfVariant(data, wildTexture, tameTexture, angryTexture, biomes);
}
NBTCompound assets = compound.getCompoundTagOrThrow("assets");
@@ -105,13 +110,18 @@ public interface WolfVariant extends MappedEntity, CopyableEntity<WolfVariant>,
angryTexture, MappedEntitySet.createEmpty());
}
@Deprecated
static NBT encode(WolfVariant variant, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), variant);
}
static NBT encode(PacketWrapper<?> wrapper, WolfVariant variant) {
NBTCompound compound = new NBTCompound();
if (version.isOlderThan(ClientVersion.V_1_21_5)) {
if (wrapper.getServerVersion().isOlderThan(ServerVersion.V_1_21_5)) {
compound.setTag("wild_texture", new NBTString(variant.getWildTexture().toString()));
compound.setTag("tame_texture", new NBTString(variant.getTameTexture().toString()));
compound.setTag("angry_texture", new NBTString(variant.getAngryTexture().toString()));
compound.setTag("biomes", MappedEntitySet.encode(variant.getBiomes(), version));
compound.set("biomes", variant.getBiomes(), MappedEntitySet::encode, wrapper);
} else {
NBTCompound assets = new NBTCompound();
assets.setTag("wild", new NBTString(variant.getWildTexture().toString()));
@@ -22,6 +22,7 @@ import com.github.retrooper.packetevents.protocol.nbt.NBT;
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.nbt.NBTInt;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import java.util.Objects;
@@ -35,14 +36,24 @@ public final class EnchantmentCost {
this.perLevelAboveFirst = perLevelAboveFirst;
}
@Deprecated
public static EnchantmentCost decode(NBT nbt, ClientVersion version) {
return decode(nbt, PacketWrapper.createDummyWrapper(version));
}
public static EnchantmentCost decode(NBT nbt, PacketWrapper<?> wrapper) {
NBTCompound compound = (NBTCompound) nbt;
int base = compound.getNumberTagOrThrow("base").getAsInt();
int perLevelAboveFirst = compound.getNumberTagOrThrow("per_level_above_first").getAsInt();
return new EnchantmentCost(base, perLevelAboveFirst);
}
@Deprecated
public static NBT encode(EnchantmentCost cost, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), cost);
}
public static NBT encode(PacketWrapper<?> wrapper, EnchantmentCost cost) {
NBTCompound compound = new NBTCompound();
compound.setTag("base", new NBTInt(cost.base));
compound.setTag("per_level_above_first", new NBTInt(cost.perLevelAboveFirst));
@@ -28,6 +28,8 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTInt;
import com.github.retrooper.packetevents.protocol.nbt.NBTList;
import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import org.jspecify.annotations.NullMarked;
import java.util.ArrayList;
import java.util.Collections;
@@ -37,6 +39,7 @@ import java.util.Optional;
import static com.github.retrooper.packetevents.util.adventure.AdventureIndexUtil.indexValueOrThrow;
@NullMarked
public final class EnchantmentDefinition {
private final MappedEntitySet<ItemType> supportedItems;
@@ -65,16 +68,21 @@ public final class EnchantmentDefinition {
this.slots = slots;
}
@Deprecated
public static EnchantmentDefinition decode(NBT nbt, ClientVersion version) {
return decode(nbt, PacketWrapper.createDummyWrapper(version));
}
public static EnchantmentDefinition decode(NBT nbt, PacketWrapper<?> wrapper) {
NBTCompound compound = (NBTCompound) nbt;
MappedEntitySet<ItemType> supportedItems = MappedEntitySet.decode(
compound.getTagOrThrow("supported_items"), version, ItemTypes.getRegistry());
Optional<MappedEntitySet<ItemType>> primaryItems = Optional.ofNullable(compound.getTagOrNull("primary_items"))
.map(items -> MappedEntitySet.decode(items, version, ItemTypes.getRegistry()));
MappedEntitySet<ItemType> supportedItems = compound.getOrThrow("supported_items", (tag, ew) ->
MappedEntitySet.decode(tag, ew, ItemTypes.getRegistry()), wrapper);
Optional<MappedEntitySet<ItemType>> primaryItems = Optional.ofNullable(compound.getOrNull("primary_items",
(tag, ew) -> MappedEntitySet.decode(tag, ew, ItemTypes.getRegistry()), wrapper));
int weight = compound.getNumberTagOrThrow("weight").getAsInt();
int maxLevel = compound.getNumberTagOrThrow("max_level").getAsInt();
EnchantmentCost minCost = EnchantmentCost.decode(compound.getTagOrThrow("min_cost"), version);
EnchantmentCost maxCost = EnchantmentCost.decode(compound.getTagOrThrow("max_cost"), version);
EnchantmentCost minCost = compound.getOrThrow("min_cost", EnchantmentCost::decode, wrapper);
EnchantmentCost maxCost = compound.getOrThrow("max_cost", EnchantmentCost::decode, wrapper);
int anvilCost = compound.getNumberTagOrThrow("anvil_cost").getAsInt();
NBT slotsTag = compound.getTagOrThrow("slots");
@@ -96,24 +104,32 @@ public final class EnchantmentDefinition {
maxLevel, minCost, maxCost, anvilCost, slots);
}
@Deprecated
public static NBT encode(EnchantmentDefinition definition, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), definition);
}
public static NBT encode(PacketWrapper<?> wrapper, EnchantmentDefinition definition) {
NBTCompound compound = new NBTCompound();
encode(compound, wrapper, definition);
return compound;
}
public static void encode(NBTCompound compound, PacketWrapper<?> wrapper, EnchantmentDefinition definition) {
NBTList<NBTString> slotsTag = NBTList.createStringList();
for (EquipmentSlotGroup slot : definition.slots) {
slotsTag.addTag(new NBTString(slot.getId()));
}
NBTCompound compound = new NBTCompound();
compound.setTag("supported_items", MappedEntitySet.encode(
definition.supportedItems, version));
definition.primaryItems.ifPresent(set -> compound.setTag(
"primary_items", MappedEntitySet.encode(set, version)));
compound.set("supported_items", definition.supportedItems, MappedEntitySet::encode, wrapper);
definition.primaryItems.ifPresent(set -> compound.set(
"primary_items", set, MappedEntitySet::encode, wrapper));
compound.setTag("weight", new NBTInt(definition.weight));
compound.setTag("max_level", new NBTInt(definition.maxLevel));
compound.setTag("min_cost", EnchantmentCost.encode(definition.minCost, version));
compound.setTag("max_cost", EnchantmentCost.encode(definition.maxCost, version));
compound.set("min_cost", definition.minCost, EnchantmentCost::encode, wrapper);
compound.set("max_cost", definition.maxCost, EnchantmentCost::encode, wrapper);
compound.setTag("anvil_cost", new NBTInt(definition.anvilCost));
compound.setTag("slots", slotsTag);
return compound;
}
public MappedEntitySet<ItemType> getSupportedItems() {
@@ -30,8 +30,8 @@ import com.github.retrooper.packetevents.protocol.mapper.MappedEntitySet;
import com.github.retrooper.packetevents.protocol.nbt.NBT;
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NullMarked;
@@ -51,28 +51,39 @@ public interface EnchantmentType extends MappedEntity, CopyableEntity<Enchantmen
StaticComponentMap getEffects();
@Deprecated
static EnchantmentType decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
return decode(nbt, PacketWrapper.createDummyWrapper(version), data);
}
static EnchantmentType decode(NBT nbt, PacketWrapper<?> wrapper, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
Component description = AdventureSerializer.serializer(version).fromNbtTag(compound.getTagOrThrow("description"));
EnchantmentDefinition definition = EnchantmentDefinition.decode(compound, version);
Component description = compound.getOrThrow("description", wrapper.getSerializers(), wrapper);
EnchantmentDefinition definition = EnchantmentDefinition.decode(compound, wrapper);
MappedEntityRefSet<EnchantmentType> exclusiveSet = Optional.ofNullable(compound.getTagOrNull("exclusive_set"))
.map(tag -> MappedEntitySet.<EnchantmentType>decodeRefSet(tag, version))
.map(tag -> MappedEntitySet.<EnchantmentType>decodeRefSet(tag, wrapper))
.orElseGet(MappedEntitySet::createEmpty);
StaticComponentMap effects = Optional.ofNullable(compound.getTagOrNull("effects"))
.map(tag -> IComponentMap.decode(tag, version,
.map(tag -> IComponentMap.decode(tag, wrapper,
EnchantEffectComponentTypes.getRegistry()))
.orElse(StaticComponentMap.EMPTY);
return new StaticEnchantmentType(data, description, definition, exclusiveSet, effects);
}
@Deprecated
static NBT encode(EnchantmentType type, ClientVersion version) {
NBTCompound compound = (NBTCompound) EnchantmentDefinition.encode(type.getDefinition(), version);
compound.setTag("description", AdventureSerializer.serializer(version).asNbtTag(type.getDescription()));
return encode(type, PacketWrapper.createDummyWrapper(version));
}
static NBT encode(EnchantmentType type, PacketWrapper<?> wrapper) {
NBTCompound compound = new NBTCompound();
EnchantmentDefinition.encode(compound, wrapper, type.getDefinition());
compound.set("description", type.getDescription(), wrapper.getSerializers(), wrapper);
if (!type.getExclusiveRefSet().isEmpty()) {
compound.setTag("exclusive_set", MappedEntitySet.encodeRefSet(type.getExclusiveRefSet(), version));
compound.set("exclusive_set", type.getExclusiveRefSet(), MappedEntitySet::encodeRefSet, wrapper);
}
if (!type.getEffects().isEmpty()) {
compound.setTag("effects", IComponentMap.encode(type.getEffects(), version));
compound.set("effects", type.getEffects(), IComponentMap::encode, wrapper);
}
return compound;
}
@@ -25,6 +25,7 @@ import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.util.mappings.MappingHelper;
import com.github.retrooper.packetevents.util.mappings.VersionedRegistry;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
@@ -64,12 +65,13 @@ public final class EnchantmentTypes {
@ApiStatus.Internal
public static EnchantmentType define(String key) {
PacketWrapper<?> wrapper = PacketWrapper.createDummyWrapper(ClientVersion.getLatest());
return REGISTRY.define(key, data -> {
NBTCompound dataTag = ENCHANTMENT_DATA.get(data.getName());
if (dataTag == null) {
throw new IllegalArgumentException("Can't define enchantment " + data.getName() + ", no data found");
}
return EnchantmentType.decode(dataTag, ClientVersion.getLatest(), data);
return EnchantmentType.decode(dataTag, wrapper, data);
});
}
@@ -28,7 +28,6 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTFloat;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.sound.Sound;
import com.github.retrooper.packetevents.util.MathUtil;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.text.Component;
@@ -82,21 +81,31 @@ public interface Instrument extends MappedEntity, CopyableEntity<Instrument>, De
}
}
@Deprecated
static Instrument decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
return decode(nbt, PacketWrapper.createDummyWrapper(version), data);
}
static Instrument decode(NBT nbt, PacketWrapper<?> wrapper, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
Sound sound = Sound.decode(compound.getTagOrThrow("sound_event"), version);
Sound sound = compound.getOrThrow("sound_event", Sound::decode, wrapper);
float useSeconds = compound.getNumberTagOrThrow("use_duration").getAsFloat();
float range = compound.getNumberTagOrThrow("range").getAsFloat();
Component description = AdventureSerializer.serializer(version).fromNbtTag(compound.getTagOrThrow("description"));
Component description = compound.getOrThrow("description", wrapper.getSerializers(), wrapper);
return new StaticInstrument(data, sound, useSeconds, range, description);
}
@Deprecated
static NBT encode(Instrument instrument, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), instrument);
}
static NBT encode(PacketWrapper<?> wrapper, Instrument instrument) {
NBTCompound compound = new NBTCompound();
compound.setTag("sound_event", Sound.encode(instrument.getSound(), version));
compound.set("sound_event", instrument.getSound(), Sound::encode, wrapper);
compound.setTag("use_duration", new NBTFloat(instrument.getUseSeconds()));
compound.setTag("range", new NBTFloat(instrument.getRange()));
compound.setTag("description", AdventureSerializer.serializer(version).asNbtTag(instrument.getDescription()));
compound.set("description", instrument.getDescription(), wrapper.getSerializers(), wrapper);
return compound;
}
}
@@ -27,7 +27,6 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTFloat;
import com.github.retrooper.packetevents.protocol.nbt.NBTInt;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.sound.Sound;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.text.Component;
@@ -43,23 +42,31 @@ public interface IJukeboxSong extends MappedEntity, CopyableEntity<IJukeboxSong>
int getComparatorOutput();
@Deprecated
static IJukeboxSong decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
return decode(nbt, PacketWrapper.createDummyWrapper(version), data);
}
static IJukeboxSong decode(NBT nbt, PacketWrapper<?> wrapper, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
Sound sound = Sound.decode(compound.getTagOrThrow("sound_event"), version);
Component description = AdventureSerializer.serializer(version).fromNbtTag(compound.getTagOrThrow("description"));
Sound sound = compound.getOrThrow("sound_event", Sound::decode, wrapper);
Component description = compound.getOrThrow("description", wrapper.getSerializers(), wrapper);
float length = compound.getNumberTagOrThrow("length_in_seconds").getAsFloat();
int comparator_output = compound.getNumberTagOrThrow("comparator_output").getAsInt();
return new JukeboxSong(data, sound, description, length, comparator_output);
}
@Deprecated
static NBT encode(IJukeboxSong jukeboxSong, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), jukeboxSong);
}
static NBT encode(PacketWrapper<?> wrapper, IJukeboxSong song) {
NBTCompound compound = new NBTCompound();
compound.setTag("sound_event", Sound.encode(jukeboxSong.getSound(), version));
compound.setTag("description", AdventureSerializer.serializer(version).asNbtTag(jukeboxSong.getDescription()));
compound.setTag("length_in_seconds", new NBTFloat(jukeboxSong.getLengthInSeconds()));
compound.setTag("comparator_output", new NBTInt(jukeboxSong.getComparatorOutput()));
compound.set("sound_event", song.getSound(), Sound::encode, wrapper);
compound.set("description", song.getDescription(), wrapper.getSerializers(), wrapper);
compound.setTag("length_in_seconds", new NBTFloat(song.getLengthInSeconds()));
compound.setTag("comparator_output", new NBTInt(song.getComparatorOutput()));
return compound;
}
@@ -31,7 +31,6 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.nbt.NBTFloat;
import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.text.Component;
@@ -102,12 +101,17 @@ public interface TrimMaterial extends MappedEntity, CopyableEntity<TrimMaterial>
wrapper.writeComponent(material.getDescription());
}
@Deprecated
static TrimMaterial decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
return decode(nbt, PacketWrapper.createDummyWrapper(version), data);
}
static TrimMaterial decode(NBT nbt, PacketWrapper<?> wrapper, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
String assetName = compound.getStringTagValueOrThrow("asset_name");
ItemType ingredient = version.isNewerThanOrEquals(ClientVersion.V_1_21_5)
ItemType ingredient = wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21_5)
? null : ItemTypes.getByName(compound.getStringTagValueOrThrow("ingredient"));
float itemModelIndex = version.isNewerThanOrEquals(ClientVersion.V_1_21_4)
float itemModelIndex = wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21_4)
? FALLBACK_ITEM_MODEL_INDEX : compound.getNumberTagOrThrow("item_model_index").getAsFloat();
NBTCompound overrideArmorMaterialsTag = compound.getCompoundTagOrNull("override_armor_materials");
Map<ArmorMaterial, String> overrideArmorMaterials;
@@ -121,11 +125,16 @@ public interface TrimMaterial extends MappedEntity, CopyableEntity<TrimMaterial>
} else {
overrideArmorMaterials = Collections.emptyMap();
}
Component description = AdventureSerializer.serializer(version).fromNbtTag(((NBTCompound) nbt).getTagOrThrow("description"));
Component description = ((NBTCompound) nbt).getOrThrow("description", wrapper.getSerializers(), wrapper);
return new StaticTrimMaterial(data, assetName, ingredient, itemModelIndex, overrideArmorMaterials, description);
}
@Deprecated
static NBT encode(TrimMaterial material, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), material);
}
static NBT encode(PacketWrapper<?> wrapper, TrimMaterial material) {
NBTCompound overrideArmorMaterialsTag;
if (!material.getOverrideArmorMaterials().isEmpty()) {
overrideArmorMaterialsTag = new NBTCompound();
@@ -140,16 +149,16 @@ public interface TrimMaterial extends MappedEntity, CopyableEntity<TrimMaterial>
NBTCompound compound = new NBTCompound();
compound.setTag("asset_name", new NBTString(material.getAssetName()));
if (version.isOlderThan(ClientVersion.V_1_21_5)) {
if (wrapper.getServerVersion().isOlderThan(ServerVersion.V_1_21_5)) {
compound.setTag("ingredient", new NBTString(material.getIngredient().getName().toString()));
}
if (version.isOlderThan(ClientVersion.V_1_21_4)) {
if (wrapper.getServerVersion().isOlderThan(ServerVersion.V_1_21_4)) {
compound.setTag("item_model_index", new NBTFloat(material.getItemModelIndex()));
}
if (overrideArmorMaterialsTag != null) {
compound.setTag("override_armor_materials", overrideArmorMaterialsTag);
}
compound.setTag("description", AdventureSerializer.serializer(version).asNbtTag(material.getDescription()));
compound.set("description", material.getDescription(), wrapper.getSerializers(), wrapper);
return compound;
}
}
@@ -30,7 +30,6 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.text.Component;
@@ -77,24 +76,34 @@ public interface TrimPattern extends MappedEntity, CopyableEntity<TrimPattern>,
wrapper.writeBoolean(pattern.isDecal());
}
@Deprecated
static TrimPattern decode(NBT nbt, ClientVersion version, @Nullable TypesBuilderData data) {
return decode(nbt, PacketWrapper.createDummyWrapper(version), data);
}
static TrimPattern decode(NBT nbt, PacketWrapper<?> wrapper, @Nullable TypesBuilderData data) {
NBTCompound compound = (NBTCompound) nbt;
ResourceLocation assetId = new ResourceLocation(compound.getStringTagValueOrThrow("asset_id"));
ItemType templateItem = version.isNewerThanOrEquals(ClientVersion.V_1_21_5) ? null :
ItemType templateItem = wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21_5) ? null :
ItemTypes.getByName(compound.getStringTagValueOrThrow("template_item"));
Component description = AdventureSerializer.serializer(version).fromNbtTag(compound.getTagOrThrow("description"));
boolean decal = version.isNewerThanOrEquals(ClientVersion.V_1_20_2) && compound.getBoolean("decal");
Component description = compound.getOrThrow("description", wrapper.getSerializers(), wrapper);
boolean decal = wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2) && compound.getBoolean("decal");
return new StaticTrimPattern(data, assetId, templateItem, description, decal);
}
@Deprecated
static NBT encode(TrimPattern pattern, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), pattern);
}
static NBT encode(PacketWrapper<?> wrapper, TrimPattern pattern) {
NBTCompound compound = new NBTCompound();
compound.setTag("asset_id", new NBTString(pattern.getAssetId().toString()));
if (version.isOlderThan(ClientVersion.V_1_21_5)) {
if (wrapper.getServerVersion().isOlderThan(ServerVersion.V_1_21_5)) {
compound.setTag("template_item", new NBTString(pattern.getTemplateItem().getName().toString()));
}
compound.setTag("description", AdventureSerializer.serializer(version).asNbtTag(pattern.getDescription()));
if (version.isNewerThanOrEquals(ClientVersion.V_1_20_2)) {
compound.set("description", pattern.getDescription(), wrapper.getSerializers(), wrapper);
if (wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2)) {
compound.setTag("decal", new NBTByte(pattern.isDecal()));
}
return compound;
@@ -116,8 +116,14 @@ public class MappedEntitySet<T extends MappedEntity> implements MappedEntityRefS
}
}
@Deprecated
public static <Z extends MappedEntity> MappedEntitySet<Z> decode(
NBT nbt, ClientVersion version, IRegistry<Z> registry) {
return decode(nbt, PacketWrapper.createDummyWrapper(version), registry);
}
public static <Z extends MappedEntity> MappedEntitySet<Z> decode(
NBT nbt, PacketWrapper<?> wrapper, IRegistry<Z> registry) {
List<Z> list;
if (nbt instanceof NBTString) {
String singleEntry = ((NBTString) nbt).getValue();
@@ -143,7 +149,12 @@ public class MappedEntitySet<T extends MappedEntity> implements MappedEntityRefS
return new MappedEntitySet<>(list);
}
@Deprecated
public static <Z extends MappedEntity> NBT encode(MappedEntitySet<Z> set, ClientVersion version) {
return encodeRefSet(PacketWrapper.createDummyWrapper(version), set);
}
public static <Z extends MappedEntity> NBT encode(PacketWrapper<?> wrapper, MappedEntitySet<Z> set) {
if (set.tagKey != null) {
return new NBTString("#" + set.tagKey);
}
@@ -57,7 +57,12 @@ public interface Sound extends MappedEntity {
wrapper.writeOptional(sound.getRange(), PacketWrapper::writeFloat);
}
@Deprecated
static Sound decode(NBT nbt, ClientVersion version) {
return decode(nbt, PacketWrapper.createDummyWrapper(version));
}
static Sound decode(NBT nbt, PacketWrapper<?> wrapper) {
if (nbt instanceof NBTString) {
return Sounds.getByNameOrCreate(((NBTString) nbt).getValue());
}
@@ -68,7 +73,12 @@ public interface Sound extends MappedEntity {
return new StaticSound(soundId, range);
}
@Deprecated
static NBT encode(Sound sound, ClientVersion version) {
return encode(PacketWrapper.createDummyWrapper(version), sound);
}
static NBT encode(PacketWrapper<?> wrapper, Sound sound) {
if (sound.isRegistered()) {
return new NBTString(sound.getName().toString());
}
@@ -21,18 +21,27 @@ package com.github.retrooper.packetevents.resources;
import com.github.retrooper.packetevents.protocol.nbt.NBT;
import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NullMarked;
import java.util.Objects;
public class ResourceLocation {
@NullMarked
public class ResourceLocation implements Keyed {
public static final String VANILLA_NAMESPACE = "minecraft";
protected final String namespace;
protected final String key;
public ResourceLocation(Key key) {
this(key.namespace(), key.value());
}
public ResourceLocation(String namespace, String key) {
this.namespace = namespace;
this.key = key;
@@ -100,6 +109,11 @@ public class ResourceLocation {
}
}
@Override
public Key key() {
return Key.key(this.namespace, this.key);
}
public String getNamespace() {
return namespace;
}
@@ -19,6 +19,7 @@
package com.github.retrooper.packetevents.util.adventure;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.dialog.Dialog;
import com.github.retrooper.packetevents.protocol.nbt.NBT;
import com.github.retrooper.packetevents.protocol.nbt.NBTByte;
import com.github.retrooper.packetevents.protocol.nbt.NBTByteArray;
@@ -36,6 +37,7 @@ import com.github.retrooper.packetevents.protocol.nbt.NBTString;
import com.github.retrooper.packetevents.protocol.nbt.NBTType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.util.UniqueIdUtil;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.BlockNBTComponent;
@@ -61,6 +63,7 @@ import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.text.serializer.gson.BackwardCompatUtil;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -91,8 +94,61 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
this(PacketEvents.getAPI().getServerManager().getVersion().toClientVersion(), downsampleColor);
}
@Contract(value = "!null -> !null")
@Deprecated
@Override
public @Nullable Component deserializeOrNull(@Nullable NBT input) {
return this.deserializeOrNull(input, PacketWrapper.createDummyWrapper(this.version));
}
@Contract(value = "!null, _ -> !null")
public @Nullable Component deserializeOrNull(@Nullable NBT input, PacketWrapper<?> wrapper) {
return input != null ? this.deserialize(input, wrapper) : null;
}
@Contract(value = "_, !null -> !null")
@Deprecated
@Override
public @Nullable Component deserializeOr(@Nullable NBT input, @Nullable Component fallback) {
return this.deserializeOr(input, fallback, PacketWrapper.createDummyWrapper(this.version));
}
@Contract(value = "_, !null, _ -> !null")
public @Nullable Component deserializeOr(@Nullable NBT input, @Nullable Component fallback, PacketWrapper<?> wrapper) {
return input != null ? this.deserialize(input, wrapper) : fallback;
}
@Contract(value = "!null -> !null")
@Deprecated
@Override
public @Nullable NBT serializeOrNull(@Nullable Component component) {
return this.serializeOrNull(component, PacketWrapper.createDummyWrapper(this.version));
}
@Contract(value = "!null, _ -> !null")
public @Nullable NBT serializeOrNull(@Nullable Component component, PacketWrapper<?> wrapper) {
return component != null ? this.serialize(component, wrapper) : null;
}
@Contract(value = "_, !null -> !null")
@Deprecated
@Override
public @Nullable NBT serializeOr(@Nullable Component component, @Nullable NBT fallback) {
return this.serializeOr(component, fallback, PacketWrapper.createDummyWrapper(this.version));
}
@Contract(value = "_, !null, _ -> !null")
public @Nullable NBT serializeOr(@Nullable Component component, @Nullable NBT fallback, PacketWrapper<?> wrapper) {
return component != null ? this.serialize(component, wrapper) : fallback;
}
@Deprecated
@Override
public @NotNull Component deserialize(@NotNull NBT input) {
return this.deserialize(input, PacketWrapper.createDummyWrapper(this.version));
}
public @NotNull Component deserialize(@NotNull NBT input, PacketWrapper<?> wrapper) {
if (input.getType() == NBTType.STRING) { // Serialized as string
return Component.text(((NBTString) input).getValue());
}
@@ -154,10 +210,10 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
return args;
});
} else {
translateWith = reader.readList("with", this::deserializeTranslationArgumentList);
translateWith = reader.readList("with", tag -> this.deserializeTranslationArgumentList(tag, wrapper));
}
} else {
translateWith = reader.readList("with", this::deserializeComponentList);
translateWith = reader.readList("with", tag -> this.deserializeComponentList(tag, wrapper));
}
NBTReader score = reader.child("score");
String selector = reader.readUTF("selector", Function.identity());
@@ -167,9 +223,9 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
BlockNBTComponent.Pos nbtBlock = reader.readUTF("block", BlockNBTComponent.Pos::fromString);
String nbtEntity = reader.readUTF("entity", Function.identity());
Key nbtStorage = reader.readUTF("storage", Key::key);
List<Component> extra = reader.readList("extra", this::deserializeComponentList);
Component separator = reader.read("separator", this::deserialize);
Style style = this.deserializeStyle(compound);
List<Component> extra = reader.readList("extra", tag -> this.deserializeComponentList(tag, wrapper));
Component separator = reader.read("separator", tag -> this.deserialize(tag, wrapper));
Style style = this.deserializeStyle(compound, wrapper);
// build component from read values
ComponentBuilder<?, ?> builder;
@@ -225,16 +281,21 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
return builder.build();
}
@Deprecated
@Override
public @NotNull NBT serialize(@NotNull Component component) {
return this.serialize(component, PacketWrapper.createDummyWrapper(this.version));
}
public @NotNull NBT serialize(@NotNull Component component, PacketWrapper<?> wrapper) {
if (component instanceof TextComponent && !component.hasStyling() && component.children().isEmpty()) {
return new NBTString(((TextComponent) component).content());
}
return serializeComponent(component);
return this.serializeComponent(component, wrapper);
}
private @NotNull NBTCompound serializeComponent(Component component) {
private @NotNull NBTCompound serializeComponent(Component component, PacketWrapper<?> wrapper) {
NBTWriter writer = new NBTWriter(new NBTCompound());
// component parts
@@ -257,9 +318,10 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
List<Component> args = ((TranslatableComponent) component).args();
if (!args.isEmpty()) {
if (BackwardCompatUtil.IS_4_15_0_OR_NEWER) {
writer.writeList("with", NBTType.COMPOUND, serializeTranslationArgumentList(((TranslatableComponent) component).arguments()));
writer.writeList("with", NBTType.COMPOUND, this.serializeTranslationArgumentList(
((TranslatableComponent) component).arguments(), wrapper));
} else {
writer.writeList("with", NBTType.COMPOUND, serializeComponentList(args));
writer.writeList("with", NBTType.COMPOUND, this.serializeComponentList(args, wrapper));
}
}
} else if (component instanceof ScoreComponent) {
@@ -279,7 +341,7 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
// separator
Component separator = ((SelectorComponent) component).separator();
if (separator != null) writer.write("separator", this.serialize(separator));
if (separator != null) writer.write("separator", this.serialize(separator, wrapper));
} else if (component instanceof KeybindComponent) {
// keybind
writer.writeUTF("keybind", ((KeybindComponent) component).keybind());
@@ -296,7 +358,7 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
// separator
Component separator = ((NBTComponent<?, ?>) component).separator();
if (separator != null) writer.write("separator", this.serialize(separator));
if (separator != null) writer.write("separator", this.serialize(separator, wrapper));
if (component instanceof BlockNBTComponent) {
// nbt block
@@ -314,21 +376,26 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
}
if (component.hasStyling()) {
serializeStyle(component.style()).getTags().forEach(writer::write);
this.serializeStyle(component.style(), wrapper).getTags().forEach(writer::write);
}
// component children
List<Component> children = component.children();
if (!children.isEmpty()) {
writer.writeList("extra", NBTType.COMPOUND, serializeComponentList(children));
writer.writeList("extra", NBTType.COMPOUND, this.serializeComponentList(children, wrapper));
}
return writer.compound;
}
// -------------------- Style --------------------
@SuppressWarnings({"PatternValidation", "rawtypes"})
@Deprecated
public @NotNull Style deserializeStyle(NBTCompound input) {
return this.deserializeStyle(input, PacketWrapper.createDummyWrapper(this.version));
}
@SuppressWarnings({"PatternValidation", "rawtypes"})
public @NotNull Style deserializeStyle(NBTCompound input, PacketWrapper<?> wrapper) {
if (input.isEmpty()) return Style.empty();
Style.Builder style = Style.style();
@@ -355,31 +422,43 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
NBTReader clickEvent = reader.child(modernEvents ? "click_event" : "clickEvent");
if (clickEvent != null) {
ClickEvent.Action action = clickEvent.readUTF("action", ClickEvent.Action.NAMES::value);
String value;
ClickEvent value;
if (!modernEvents) {
value = clickEvent.readUTF("value", Function.identity());
value = ClickEvent.clickEvent(action, clickEvent.readUTF("value", Function.identity()));
} else {
switch (action) {
case OPEN_URL:
value = clickEvent.readUTF("url", Function.identity());
value = ClickEvent.openUrl(clickEvent.readUTF("url", Function.identity()));
break;
case OPEN_FILE:
value = clickEvent.readUTF("path", Function.identity());
value = ClickEvent.openFile(clickEvent.readUTF("path", Function.identity()));
break;
case RUN_COMMAND:
value = ClickEvent.runCommand(clickEvent.readUTF("command", Function.identity()));
break;
case SUGGEST_COMMAND:
value = clickEvent.readUTF("command", Function.identity());
value = ClickEvent.suggestCommand(clickEvent.readUTF("command", Function.identity()));
break;
case CHANGE_PAGE:
value = clickEvent.readNumber("page", String::valueOf);
value = ClickEvent.changePage(clickEvent.readNumber("page", Number::intValue));
break;
case COPY_TO_CLIPBOARD:
default:
value = clickEvent.readUTF("value", Function.identity());
value = ClickEvent.copyToClipboard(clickEvent.readUTF("value", Function.identity()));
break;
case SHOW_DIALOG:
NBT dialogTag = clickEvent.read("dialog", Function.identity());
value = ClickEvent.showDialog(Dialog.decode(dialogTag, wrapper));
break;
case CUSTOM:
Key key = clickEvent.readUTF("id", Key::key);
NBT payload = clickEvent.read("payload", Function.identity());
value = ClickEvent.custom(key, "0b"); // TODO snbt serialization
break;
default:
throw new UnsupportedOperationException("Unsupported clickevent: " + action);
}
}
style.clickEvent(ClickEvent.clickEvent(action, value));
style.clickEvent(value);
}
NBTReader hoverEvent = reader.child(modernEvents ? "hover_event" : "hoverEvent");
@@ -387,7 +466,8 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
HoverEvent.Action action = hoverEvent.readUTF("action", HoverEvent.Action.NAMES::value);
switch (action.toString()) {
case "show_text":
style.hoverEvent(HoverEvent.showText(hoverEvent.read(modernEvents ? "value" : "contents", this::deserialize)));
style.hoverEvent(HoverEvent.showText(hoverEvent.read(modernEvents ? "value" : "contents",
tag -> this.deserialize(tag, wrapper))));
break;
case "show_item":
if (!modernEvents && hoverEvent.type("contents") == NBTType.STRING) {
@@ -426,7 +506,7 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
style.hoverEvent(HoverEvent.showEntity(
entity.readUTF(modernEvents ? "id" : "type", Key::key),
entity.readIntArray(modernEvents ? "uuid" : "id", UniqueIdUtil::fromIntArray),
entity.read("name", this::deserialize)));
entity.read("name", name -> this.deserialize(name, wrapper))));
break;
}
}
@@ -434,7 +514,12 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
return style.build();
}
@Deprecated
public @NotNull NBTCompound serializeStyle(Style style) {
return this.serializeStyle(style, PacketWrapper.createDummyWrapper(this.version));
}
public @NotNull NBTCompound serializeStyle(Style style, PacketWrapper<?> wrapper) {
if (style.isEmpty()) return new NBTCompound();
NBTWriter writer = new NBTWriter(new NBTCompound());
@@ -480,12 +565,26 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
child.writeUTF("command", clickEvent.value());
break;
case CHANGE_PAGE:
child.writeInt("page", Integer.parseInt(clickEvent.value()));
if (BackwardCompatUtil.IS_4_22_0_OR_NEWER) {
child.writeInt("page", ((ClickEvent.Payload.Int) clickEvent.payload()).integer());
} else {
child.writeInt("page", Integer.parseInt(clickEvent.value()));
}
break;
case COPY_TO_CLIPBOARD:
default:
child.writeUTF("value", clickEvent.value());
break;
case SHOW_DIALOG:
Dialog dialog = (Dialog) ((ClickEvent.Payload.Dialog) clickEvent.payload()).dialog();
child.write("dialog", Dialog.encode(wrapper, dialog));
break;
case CUSTOM:
child.writeUTF("id", ((ClickEvent.Payload.Custom) clickEvent.payload()).key().asString());
// TODO snbt deserialization
// child.write("payload", ((ClickEvent.Payload.Custom) clickEvent.payload()).data());
break;
default:
throw new UnsupportedOperationException("Unsupported clickevent: " + clickEvent);
}
}
}
@@ -497,7 +596,7 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
child.writeUTF("action", hoverEvent.action().toString());
switch (hoverEvent.action().toString()) {
case "show_text":
child.write(modern ? "value" : "contents", this.serialize((Component) hoverEvent.value()));
child.write(modern ? "value" : "contents", this.serialize((Component) hoverEvent.value(), wrapper));
break;
case "show_item":
HoverEvent.ShowItem item = (HoverEvent.ShowItem) hoverEvent.value();
@@ -541,7 +640,7 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
entity.writeUTF(modern ? "id" : "type", showEntity.type().asString());
entity.writeIntArray(modern ? "uuid" : "id", UniqueIdUtil.toIntArray(showEntity.id()));
if (showEntity.name() != null) {
entity.write("name", this.serialize(showEntity.name()));
entity.write("name", this.serialize(showEntity.name(), wrapper));
}
break;
}
@@ -578,28 +677,28 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
// -------------------------------------------------
// ---------------- Component List -----------------
private @NotNull List<Component> deserializeComponentList(List<?> value) {
private @NotNull List<Component> deserializeComponentList(List<?> value, PacketWrapper<?> wrapper) {
if (value.isEmpty()) return Collections.emptyList();
List<Component> components = new ArrayList<>(value.size());
for (Object nbt : value) {
components.add(deserialize((NBT) nbt));
components.add(this.deserialize((NBT) nbt, wrapper));
}
return components;
}
private List<NBTCompound> serializeComponentList(List<Component> value) {
private List<NBTCompound> serializeComponentList(List<Component> value, PacketWrapper<?> wrapper) {
List<NBTCompound> components = new ArrayList<>(value.size());
for (Component component : value) {
components.add(serializeComponent(component));
components.add(this.serializeComponent(component, wrapper));
}
return components;
}
// -------------------------------------------------
// ------------ TranslationArgument List ------------
private @NotNull List<TranslationArgument> deserializeTranslationArgumentList(List<?> value) {
private @NotNull List<TranslationArgument> deserializeTranslationArgumentList(List<?> value, PacketWrapper<?> wrapper) {
if (value.isEmpty()) return Collections.emptyList();
List<TranslationArgument> arguments = new ArrayList<>(value.size());
@@ -611,17 +710,17 @@ public class AdventureNBTSerializer implements ComponentSerializer<Component, Co
} else if (nbt instanceof NBTString) {
arguments.add(TranslationArgument.component(Component.text(((NBTString) nbt).getValue())));
} else {
arguments.add(TranslationArgument.component(deserialize(requireType((NBT) nbt, NBTType.COMPOUND))));
arguments.add(TranslationArgument.component(this.deserialize(requireType((NBT) nbt, NBTType.COMPOUND), wrapper)));
}
}
return arguments;
}
private List<NBTCompound> serializeTranslationArgumentList(List<TranslationArgument> value) {
private List<NBTCompound> serializeTranslationArgumentList(List<TranslationArgument> value, PacketWrapper<?> wrapper) {
List<NBTCompound> arguments = new ArrayList<>(value.size());
for (TranslationArgument argument : value) {
arguments.add(serializeComponent(argument.asComponent()));
arguments.add(this.serializeComponent(argument.asComponent(), wrapper));
}
return arguments;
}
@@ -163,7 +163,7 @@ public final class AdventureSerializer implements NbtEncoder<Component>, NbtDeco
}
/**
* @deprecated use {@link #fromNbtTag(NBT)} instead
* @deprecated use {@link #fromNbtTag(NBT, PacketWrapper)} instead
*/
@Deprecated
public static Component fromNbt(NBT tag) {
@@ -171,7 +171,7 @@ public final class AdventureSerializer implements NbtEncoder<Component>, NbtDeco
}
/**
* @deprecated use {@link #asNbtTag(Component)} instead
* @deprecated use {@link #asNbtTag(Component, PacketWrapper)} instead
*/
@Deprecated
public static NBT toNbt(Component component) {
@@ -204,12 +204,22 @@ public final class AdventureSerializer implements NbtEncoder<Component>, NbtDeco
return component != null ? this.gson().serializeToTree(component) : null;
}
@Deprecated
public Component fromNbtTag(NBT tag) {
return this.nbt().deserializeOrNull(tag);
return this.fromNbtTag(tag, PacketWrapper.createDummyWrapper(this.version));
}
public Component fromNbtTag(NBT tag, PacketWrapper<?> wrapper) {
return this.nbt().deserializeOrNull(tag, wrapper);
}
@Deprecated
public NBT asNbtTag(Component component) {
return this.nbt().serializeOrNull(component);
return this.asNbtTag(component, PacketWrapper.createDummyWrapper(this.version));
}
public NBT asNbtTag(Component component, PacketWrapper<?> wrapper) {
return this.nbt().serializeOrNull(component, wrapper);
}
public GsonComponentSerializer gson() {
@@ -264,11 +274,11 @@ public final class AdventureSerializer implements NbtEncoder<Component>, NbtDeco
@Override
public Component decode(NBT nbt, PacketWrapper<?> wrapper) {
return this.nbt().deserialize(nbt);
return this.nbt().deserialize(nbt, wrapper);
}
@Override
public NBT encode(PacketWrapper<?> wrapper, Component value) {
return this.nbt().serialize(value);
return this.nbt().serialize(value, wrapper);
}
}
@@ -91,9 +91,9 @@ public final class SynchronizedRegistriesHandler {
Stream.of(
new RegistryEntry<>(Biomes.getRegistry(), Biome::decode),
new RegistryEntry<>(ChatTypes.getRegistry(), ChatType::decode),
new RegistryEntry<>(TrimPatterns.getRegistry(), TrimPattern::decode),
new RegistryEntry<>(TrimMaterials.getRegistry(), TrimMaterial::decode),
new RegistryEntry<>(WolfVariants.getRegistry(), WolfVariant::decode),
new RegistryEntry<>(TrimPatterns.getRegistry(), (NbtEntryDecoder<TrimPattern>) TrimPattern::decode),
new RegistryEntry<>(TrimMaterials.getRegistry(), (NbtEntryDecoder<TrimMaterial>) TrimMaterial::decode),
new RegistryEntry<>(WolfVariants.getRegistry(), (NbtEntryDecoder<WolfVariant>) WolfVariant::decode),
new RegistryEntry<>(WolfSoundVariants.getRegistry(), WolfSoundVariant::decode),
new RegistryEntry<>(PigVariants.getRegistry(), PigVariant::decode),
new RegistryEntry<>(FrogVariants.getRegistry(), FrogVariant::decode),
@@ -104,9 +104,9 @@ public final class SynchronizedRegistriesHandler {
new RegistryEntry<>(DimensionTypes.getRegistry(), DimensionType::decode),
new RegistryEntry<>(DamageTypes.getRegistry(), DamageType::decode),
new RegistryEntry<>(BannerPatterns.getRegistry(), BannerPattern::decode),
new RegistryEntry<>(EnchantmentTypes.getRegistry(), EnchantmentType::decode),
new RegistryEntry<>(JukeboxSongs.getRegistry(), IJukeboxSong::decode),
new RegistryEntry<>(Instruments.getRegistry(), Instrument::decode),
new RegistryEntry<>(EnchantmentTypes.getRegistry(), (NbtEntryDecoder<EnchantmentType>) EnchantmentType::decode),
new RegistryEntry<>(JukeboxSongs.getRegistry(), (NbtEntryDecoder<IJukeboxSong>) IJukeboxSong::decode),
new RegistryEntry<>(Instruments.getRegistry(), (NbtEntryDecoder<Instrument>) Instrument::decode),
new RegistryEntry<>(Dialogs.getRegistry(), Dialog::decodeDirect)
).forEach(entry -> REGISTRY_KEYS.put(entry.getRegistryKey(), entry));
}
@@ -206,6 +206,10 @@ public class PacketWrapper<T extends PacketWrapper<T>> {
this.packetTypeData = new PacketTypeData(packetType, id);
}
public static PacketWrapper<?> createDummyWrapper(ClientVersion version) {
return new PacketWrapper<>(version, version.toServerVersion(), -2);
}
public static PacketWrapper<?> createUniversalPacketWrapper(Object byteBuf) {
return createUniversalPacketWrapper(byteBuf, PacketEvents.getAPI().getServerManager().getVersion());
}
@@ -579,7 +583,7 @@ public class PacketWrapper<T extends PacketWrapper<T>> {
}
public AdventureSerializer getSerializers() {
return AdventureSerializer.serializer(this.serverVersion.toClientVersion());
return AdventureSerializer.serializer(this);
}
@Deprecated
@@ -594,7 +598,7 @@ public class PacketWrapper<T extends PacketWrapper<T>> {
}
public Component readComponentAsNBT() {
return this.getSerializers().fromNbtTag(this.readNBTRaw());
return this.getSerializers().fromNbtTag(this.readNBTRaw(), this);
}
public Component readComponentAsJSON() {
@@ -611,7 +615,7 @@ public class PacketWrapper<T extends PacketWrapper<T>> {
}
public void writeComponentAsNBT(Component component) {
this.writeNBTRaw(this.getSerializers().asNbtTag(component));
this.writeNBTRaw(this.getSerializers().asNbtTag(component, this));
}
public void writeComponentAsJSON(Component component) {
@@ -620,11 +624,11 @@ public class PacketWrapper<T extends PacketWrapper<T>> {
}
public Style readStyle() {
return this.getSerializers().nbt().deserializeStyle(this.readNBT());
return this.getSerializers().nbt().deserializeStyle(this.readNBT(), this);
}
public void writeStyle(Style style) {
this.writeNBT(this.getSerializers().nbt().serializeStyle(style));
this.writeNBT(this.getSerializers().nbt().serializeStyle(style, this));
}
public ResourceLocation readIdentifier(int maxLen) {
+1 -1
View File
@@ -1,5 +1,5 @@
[versions]
adventure = "4.21.0"
adventure = "4.22.0"
examination = "1.3.0"
netty = "4.1.72.Final"
jetbrains-annotations = "23.0.0"
@@ -20,6 +20,7 @@ package net.kyori.adventure.text.serializer.gson;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.util.Codec;
@@ -37,6 +38,7 @@ public final class BackwardCompatUtil {
public static final boolean IS_4_15_0_OR_NEWER;
public static final boolean IS_4_17_0_OR_NEWER;
public static final boolean IS_4_18_0_OR_NEWER;
public static final boolean IS_4_22_0_OR_NEWER;
static {
boolean is4_10_0OrNewer = false;
@@ -82,6 +84,15 @@ public final class BackwardCompatUtil {
} catch (Throwable ignored) {
}
IS_4_18_0_OR_NEWER = is4_18_0OrNewer;
boolean is4_22_0OrNewer = false;
try {
// support for 1.21.6+ clickevent payloads was added in 4.22.0
ClickEvent.custom(Key.key("test"), "{test:true}");
is4_22_0OrNewer = true;
} catch (Throwable ignored) {
}
IS_4_22_0_OR_NEWER = is4_22_0OrNewer;
}
private BackwardCompatUtil() {