mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 08:27:43 +01:00
Item metadata fixes (#935)
This commit is contained in:
parent
0d93faf0db
commit
ff7098a083
@ -11,8 +11,8 @@ import org.jetbrains.annotations.UnknownNullability;
|
||||
import java.util.List;
|
||||
|
||||
public record CrossbowMeta(TagReadable readable) implements ItemMetaView<CrossbowMeta.Builder> {
|
||||
private static final Tag<List<ItemStack>> PROJECTILES = Tag.ItemStack("ChargedProjectiles").list();
|
||||
private static final Tag<Boolean> CHARGED = Tag.Boolean("Charged");
|
||||
private static final Tag<List<ItemStack>> PROJECTILES = Tag.ItemStack("ChargedProjectiles").list().defaultValue(List.of());
|
||||
private static final Tag<Boolean> CHARGED = Tag.Boolean("Charged").defaultValue(false);
|
||||
|
||||
public @NotNull List<ItemStack> getProjectiles() {
|
||||
return getTag(PROJECTILES);
|
||||
|
@ -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<Firewor
|
||||
.path("Fireworks").list().defaultValue(List.of());
|
||||
private static final Tag<Byte> FLIGHT_DURATION = Tag.Byte("Flight").path("Fireworks");
|
||||
|
||||
public List<FireworkEffect> getEffects() {
|
||||
public @NotNull List<FireworkEffect> getEffects() {
|
||||
return getTag(EFFECTS);
|
||||
}
|
||||
|
||||
public byte getFlightDuration() {
|
||||
public @Nullable Byte getFlightDuration() {
|
||||
return getTag(FLIGHT_DURATION);
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ public record PlayerHeadMeta(TagReadable readable) implements ItemMetaView<Playe
|
||||
}
|
||||
}).path("SkullOwner");
|
||||
|
||||
public UUID getSkullOwner() {
|
||||
public @Nullable UUID getSkullOwner() {
|
||||
return getTag(SKULL_OWNER);
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ import org.jetbrains.annotations.UnknownNullability;
|
||||
import java.util.List;
|
||||
|
||||
public record PotionMeta(TagReadable readable) implements ItemMetaView<PotionMeta.Builder> {
|
||||
private static final Tag<PotionType> POTION_TYPE = Tag.String("Potion").map(PotionType::fromNamespaceId, ProtocolObject::name);
|
||||
private static final Tag<PotionType> POTION_TYPE = Tag.String("Potion").map(PotionType::fromNamespaceId, ProtocolObject::name).defaultValue(PotionType.EMPTY);
|
||||
private static final Tag<List<CustomPotionEffect>> CUSTOM_POTION_EFFECTS = Tag.Structure("CustomPotionEffects", new TagSerializer<CustomPotionEffect>() {
|
||||
@Override
|
||||
public @Nullable CustomPotionEffect read(@NotNull TagReadable reader) {
|
||||
@ -41,15 +41,15 @@ public record PotionMeta(TagReadable readable) implements ItemMetaView<PotionMet
|
||||
}).list().defaultValue(List.of());
|
||||
private static final Tag<Color> 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<CustomPotionEffect> getCustomPotionEffects() {
|
||||
public @NotNull List<CustomPotionEffect> getCustomPotionEffects() {
|
||||
return getTag(CUSTOM_POTION_EFFECTS);
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
public @Nullable Color getColor() {
|
||||
return getTag(CUSTOM_POTION_COLOR);
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public record WrittenBookMeta(TagReadable readable) implements ItemMetaView<WrittenBookMeta.Builder> {
|
||||
private static final Tag<Boolean> RESOLVED = Tag.Boolean("resolved");
|
||||
private static final Tag<Boolean> RESOLVED = Tag.Boolean("resolved").defaultValue(false);
|
||||
private static final Tag<WrittenBookGeneration> GENERATION = Tag.Integer("resolved").map(integer -> WrittenBookGeneration.values()[integer], Enum::ordinal);
|
||||
private static final Tag<String> AUTHOR = Tag.String("author");
|
||||
private static final Tag<String> TITLE = Tag.String("title");
|
||||
|
@ -106,7 +106,11 @@ public class Tag<T> {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user