diff --git a/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java b/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java index 47df6d363..cfc525f2f 100644 --- a/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java @@ -11,8 +11,8 @@ import org.jetbrains.annotations.UnknownNullability; import java.util.List; public record CrossbowMeta(TagReadable readable) implements ItemMetaView { - private static final Tag> PROJECTILES = Tag.ItemStack("ChargedProjectiles").list(); - private static final Tag CHARGED = Tag.Boolean("Charged"); + private static final Tag> PROJECTILES = Tag.ItemStack("ChargedProjectiles").list().defaultValue(List.of()); + private static final Tag CHARGED = Tag.Boolean("Charged").defaultValue(false); public @NotNull List getProjectiles() { return getTag(PROJECTILES); diff --git a/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java b/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java index 1654a02cd..722057271 100644 --- a/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/FireworkMeta.java @@ -7,6 +7,7 @@ import net.minestom.server.tag.TagHandler; import net.minestom.server.tag.TagReadable; import net.minestom.server.tag.TagSerializer; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnknownNullability; import java.util.List; @@ -17,11 +18,11 @@ public record FireworkMeta(TagReadable readable) implements ItemMetaView FLIGHT_DURATION = Tag.Byte("Flight").path("Fireworks"); - public List getEffects() { + public @NotNull List getEffects() { return getTag(EFFECTS); } - public byte getFlightDuration() { + public @Nullable Byte getFlightDuration() { return getTag(FLIGHT_DURATION); } diff --git a/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java b/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java index c9b8fc268..6dd857a56 100644 --- a/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PlayerHeadMeta.java @@ -42,7 +42,7 @@ public record PlayerHeadMeta(TagReadable readable) implements ItemMetaView { - private static final Tag POTION_TYPE = Tag.String("Potion").map(PotionType::fromNamespaceId, ProtocolObject::name); + private static final Tag POTION_TYPE = Tag.String("Potion").map(PotionType::fromNamespaceId, ProtocolObject::name).defaultValue(PotionType.EMPTY); private static final Tag> CUSTOM_POTION_EFFECTS = Tag.Structure("CustomPotionEffects", new TagSerializer() { @Override public @Nullable CustomPotionEffect read(@NotNull TagReadable reader) { @@ -41,15 +41,15 @@ public record PotionMeta(TagReadable readable) implements ItemMetaView CUSTOM_POTION_COLOR = Tag.Integer("CustomPotionColor").path("display").map(Color::new, Color::asRGB); - public PotionType getPotionType() { + public @NotNull PotionType getPotionType() { return getTag(POTION_TYPE); } - public List getCustomPotionEffects() { + public @NotNull List getCustomPotionEffects() { return getTag(CUSTOM_POTION_EFFECTS); } - public Color getColor() { + public @Nullable Color getColor() { return getTag(CUSTOM_POTION_COLOR); } diff --git a/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java b/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java index da0adc77f..a295ba70d 100644 --- a/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/WrittenBookMeta.java @@ -14,7 +14,7 @@ import java.util.Arrays; import java.util.List; public record WrittenBookMeta(TagReadable readable) implements ItemMetaView { - private static final Tag RESOLVED = Tag.Boolean("resolved"); + private static final Tag RESOLVED = Tag.Boolean("resolved").defaultValue(false); private static final Tag GENERATION = Tag.Integer("resolved").map(integer -> WrittenBookGeneration.values()[integer], Enum::ordinal); private static final Tag AUTHOR = Tag.String("author"); private static final Tag TITLE = Tag.String("title"); diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index 97c0b89ab..ab3b72c5d 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -106,7 +106,11 @@ public class Tag { return new Tag<>(index, key, readMap, new Serializers.Entry<>(readFunction, writeFunction), // Default value - () -> readMap.apply(createDefault()), + () -> { + T defaultValue = createDefault(); + if (defaultValue == null) return null; + return readMap.apply(defaultValue); + }, path, null, listScope); } diff --git a/src/test/java/net/minestom/server/tag/TagMapTest.java b/src/test/java/net/minestom/server/tag/TagMapTest.java index b06a10974..08595008d 100644 --- a/src/test/java/net/minestom/server/tag/TagMapTest.java +++ b/src/test/java/net/minestom/server/tag/TagMapTest.java @@ -3,6 +3,7 @@ package net.minestom.server.tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; public class TagMapTest { @@ -32,4 +33,11 @@ public class TagMapTest { assertEquals(2, handler.getTag(intTag)); assertEquals(new Entry(2), handler.getTag(tag)); } + + @Test + public void mapDefaultAbsent() { + var handler = TagHandler.newHandler(); + var tag = Tag.Integer("key").map(Entry::new, Entry::value); + assertNull(handler.getTag(tag)); + } }