net.minestom.server.item.component.CustomData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.item.component;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagReadable;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnknownNullability;
public record CustomData(@NotNull CompoundBinaryTag nbt) implements TagReadable {
public static final CustomData EMPTY = new CustomData(CompoundBinaryTag.empty());
public static final NetworkBuffer.Type NETWORK_TYPE = new NetworkBuffer.Type<>() {
@Override
public void write(@NotNull NetworkBuffer buffer, CustomData value) {
buffer.write(NetworkBuffer.NBT, value.nbt);
}
@Override
public CustomData read(@NotNull NetworkBuffer buffer) {
return new CustomData((CompoundBinaryTag) buffer.read(NetworkBuffer.NBT));
}
};
public static final BinaryTagSerializer NBT_TYPE = BinaryTagSerializer.COMPOUND.map(CustomData::new, CustomData::nbt);
@Override
public @UnknownNullability T getTag(@NotNull Tag tag) {
return tag.read(nbt);
}
public @NotNull CustomData withTag(@NotNull Tag tag, T value) {
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
builder.put(nbt);
tag.write(builder, value);
return new CustomData(builder.build());
}
}