fix: add new customName field to potion contents

This commit is contained in:
mworzala 2024-10-22 22:09:59 -04:00 committed by Matt Worzala
parent 14aa2d5d5c
commit 3f1841b669
4 changed files with 31 additions and 30 deletions

View File

@ -147,7 +147,7 @@ public class PlayerInit {
Enchantment.SHARPNESS, 10
)))
.build());
//
player.getInventory().addItemStack(ItemStack.builder(Material.STONE_SWORD)
.build());

View File

@ -95,6 +95,7 @@ public final class ItemComponent {
public static final DataComponent<List<ItemStack>> CONTAINER = register("container", ItemStack.NETWORK_TYPE.list(256), BinaryTagSerializer.ITEM.list());
public static final DataComponent<ItemBlockState> BLOCK_STATE = register("block_state", ItemBlockState.NETWORK_TYPE, ItemBlockState.NBT_TYPE);
public static final DataComponent<List<Bee>> BEES = register("bees", Bee.NETWORK_TYPE.list(Short.MAX_VALUE), Bee.NBT_TYPE.list());
// TODO(1.21.2) Updated NBT format > https://minecraft.wiki/w/Java_Edition_1.21.2#Data_components_3
public static final DataComponent<String> LOCK = register("lock", null, BinaryTagSerializer.STRING);
public static final DataComponent<SeededContainerLoot> CONTAINER_LOOT = register("container_loot", null, SeededContainerLoot.NBT_TYPE);

View File

@ -4,6 +4,7 @@ import net.kyori.adventure.nbt.*;
import net.kyori.adventure.util.RGBLike;
import net.minestom.server.color.Color;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.NetworkBufferTemplate;
import net.minestom.server.potion.CustomPotionEffect;
import net.minestom.server.potion.PotionType;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
@ -16,30 +17,17 @@ import java.util.List;
public record PotionContents(
@Nullable PotionType potion,
@Nullable RGBLike customColor,
@NotNull List<CustomPotionEffect> customEffects
@NotNull List<CustomPotionEffect> customEffects,
@Nullable String customName
) {
public static final int POTION_DRINK_TIME = 32; // 32 ticks, in ms
public static final PotionContents EMPTY = new PotionContents(null, null, List.of());
public static final PotionContents EMPTY = new PotionContents(null, null, List.of(), null);
public static final NetworkBuffer.Type<PotionContents> NETWORK_TYPE = new NetworkBuffer.Type<>() {
@Override
public void write(@NotNull NetworkBuffer buffer, PotionContents value) {
Integer typeId = value.potion == null ? null : value.potion.id();
buffer.write(NetworkBuffer.VAR_INT.optional(), typeId);
buffer.write(Color.NETWORK_TYPE.optional(), value.customColor);
buffer.write(CustomPotionEffect.NETWORK_TYPE.list(), value.customEffects);
}
@Override
public PotionContents read(@NotNull NetworkBuffer buffer) {
Integer typeId = buffer.read(NetworkBuffer.VAR_INT.optional());
return new PotionContents(
typeId == null ? null : PotionType.fromId(typeId),
buffer.read(Color.NETWORK_TYPE.optional()),
buffer.read(CustomPotionEffect.NETWORK_TYPE.list(Short.MAX_VALUE))
);
}
};
public static final NetworkBuffer.Type<PotionContents> NETWORK_TYPE = NetworkBufferTemplate.template(
PotionType.NETWORK_TYPE.optional(), PotionContents::potion,
Color.NETWORK_TYPE.optional(), PotionContents::customColor,
CustomPotionEffect.NETWORK_TYPE.list(Short.MAX_VALUE), PotionContents::customEffects,
NetworkBuffer.STRING.optional(), PotionContents::customName,
PotionContents::new);
public static final BinaryTagSerializer<PotionContents> NBT_TYPE = new BinaryTagSerializer<>() {
@Override
@ -62,6 +50,10 @@ public record PotionContents(
builder.put("custom_effects", effectsBuilder.build());
}
if (value.customName != null) {
builder.putString("custom_name", value.customName);
}
return builder.build();
}
@ -69,7 +61,7 @@ public record PotionContents(
public @NotNull PotionContents read(@NotNull BinaryTag tag) {
// Can be a string with just a potion effect id
if (tag instanceof StringBinaryTag string) {
return new PotionContents(PotionType.fromNamespaceId(string.value()), null, List.of());
return new PotionContents(PotionType.fromNamespaceId(string.value()), null, List.of(), null);
}
// Otherwise must be a compound
@ -95,7 +87,11 @@ public record PotionContents(
customEffects.add(CustomPotionEffect.NBT_TYPE.read(customEffectCompound));
}
return new PotionContents(potion, customColor, customEffects);
String customName = null;
if (compound.get("custom_name") instanceof StringBinaryTag customNameTag)
customName = customNameTag.value();
return new PotionContents(potion, customColor, customEffects, customName);
}
};
@ -104,19 +100,23 @@ public record PotionContents(
}
public PotionContents(@NotNull PotionType potion) {
this(potion, null, List.of());
this(potion, null, List.of(), null);
}
public PotionContents(@NotNull PotionType potion, @NotNull RGBLike customColor) {
this(potion, customColor, List.of());
this(potion, customColor, List.of(), null);
}
public PotionContents(@NotNull List<CustomPotionEffect> customEffects) {
this(null, null, customEffects);
this(null, null, customEffects, null);
}
public PotionContents(@NotNull CustomPotionEffect customEffect) {
this(null, null, List.of(customEffect));
this(null, null, List.of(customEffect), null);
}
public PotionContents(@Nullable PotionType potion, @Nullable RGBLike customColor, @NotNull List<CustomPotionEffect> customEffects) {
this(potion, customColor, customEffects, null);
}
}

View File

@ -43,7 +43,7 @@ public class PotionContentsTest extends AbstractItemComponentTest<PotionContents
@Test
void alternativeNbtSyntax() {
var value = ItemComponent.POTION_CONTENTS.read(BinaryTagSerializer.Context.EMPTY, StringBinaryTag.stringBinaryTag("minecraft:strong_swiftness"));
var expected = new PotionContents(PotionType.STRONG_SWIFTNESS, null, List.of());
var expected = new PotionContents(PotionType.STRONG_SWIFTNESS, null, List.of(), null);
assertEquals(expected, value);
}
}