Item metadata fixes (#935)

This commit is contained in:
Bloepiloepi 2022-04-17 20:52:29 +02:00 committed by GitHub
parent 0d93faf0db
commit ff7098a083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 11 deletions

View File

@ -11,8 +11,8 @@ import org.jetbrains.annotations.UnknownNullability;
import java.util.List; import java.util.List;
public record CrossbowMeta(TagReadable readable) implements ItemMetaView<CrossbowMeta.Builder> { public record CrossbowMeta(TagReadable readable) implements ItemMetaView<CrossbowMeta.Builder> {
private static final Tag<List<ItemStack>> PROJECTILES = Tag.ItemStack("ChargedProjectiles").list(); private static final Tag<List<ItemStack>> PROJECTILES = Tag.ItemStack("ChargedProjectiles").list().defaultValue(List.of());
private static final Tag<Boolean> CHARGED = Tag.Boolean("Charged"); private static final Tag<Boolean> CHARGED = Tag.Boolean("Charged").defaultValue(false);
public @NotNull List<ItemStack> getProjectiles() { public @NotNull List<ItemStack> getProjectiles() {
return getTag(PROJECTILES); return getTag(PROJECTILES);

View File

@ -7,6 +7,7 @@ import net.minestom.server.tag.TagHandler;
import net.minestom.server.tag.TagReadable; import net.minestom.server.tag.TagReadable;
import net.minestom.server.tag.TagSerializer; import net.minestom.server.tag.TagSerializer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability; import org.jetbrains.annotations.UnknownNullability;
import java.util.List; import java.util.List;
@ -17,11 +18,11 @@ public record FireworkMeta(TagReadable readable) implements ItemMetaView<Firewor
.path("Fireworks").list().defaultValue(List.of()); .path("Fireworks").list().defaultValue(List.of());
private static final Tag<Byte> FLIGHT_DURATION = Tag.Byte("Flight").path("Fireworks"); private static final Tag<Byte> FLIGHT_DURATION = Tag.Byte("Flight").path("Fireworks");
public List<FireworkEffect> getEffects() { public @NotNull List<FireworkEffect> getEffects() {
return getTag(EFFECTS); return getTag(EFFECTS);
} }
public byte getFlightDuration() { public @Nullable Byte getFlightDuration() {
return getTag(FLIGHT_DURATION); return getTag(FLIGHT_DURATION);
} }

View File

@ -42,7 +42,7 @@ public record PlayerHeadMeta(TagReadable readable) implements ItemMetaView<Playe
} }
}).path("SkullOwner"); }).path("SkullOwner");
public UUID getSkullOwner() { public @Nullable UUID getSkullOwner() {
return getTag(SKULL_OWNER); return getTag(SKULL_OWNER);
} }

View File

@ -13,7 +13,7 @@ import org.jetbrains.annotations.UnknownNullability;
import java.util.List; import java.util.List;
public record PotionMeta(TagReadable readable) implements ItemMetaView<PotionMeta.Builder> { 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>() { private static final Tag<List<CustomPotionEffect>> CUSTOM_POTION_EFFECTS = Tag.Structure("CustomPotionEffects", new TagSerializer<CustomPotionEffect>() {
@Override @Override
public @Nullable CustomPotionEffect read(@NotNull TagReadable reader) { public @Nullable CustomPotionEffect read(@NotNull TagReadable reader) {
@ -41,15 +41,15 @@ public record PotionMeta(TagReadable readable) implements ItemMetaView<PotionMet
}).list().defaultValue(List.of()); }).list().defaultValue(List.of());
private static final Tag<Color> CUSTOM_POTION_COLOR = Tag.Integer("CustomPotionColor").path("display").map(Color::new, Color::asRGB); 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); return getTag(POTION_TYPE);
} }
public List<CustomPotionEffect> getCustomPotionEffects() { public @NotNull List<CustomPotionEffect> getCustomPotionEffects() {
return getTag(CUSTOM_POTION_EFFECTS); return getTag(CUSTOM_POTION_EFFECTS);
} }
public Color getColor() { public @Nullable Color getColor() {
return getTag(CUSTOM_POTION_COLOR); return getTag(CUSTOM_POTION_COLOR);
} }

View File

@ -14,7 +14,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
public record WrittenBookMeta(TagReadable readable) implements ItemMetaView<WrittenBookMeta.Builder> { 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<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> AUTHOR = Tag.String("author");
private static final Tag<String> TITLE = Tag.String("title"); private static final Tag<String> TITLE = Tag.String("title");

View File

@ -106,7 +106,11 @@ public class Tag<T> {
return new Tag<>(index, key, readMap, return new Tag<>(index, key, readMap,
new Serializers.Entry<>(readFunction, writeFunction), new Serializers.Entry<>(readFunction, writeFunction),
// Default value // Default value
() -> readMap.apply(createDefault()), () -> {
T defaultValue = createDefault();
if (defaultValue == null) return null;
return readMap.apply(defaultValue);
},
path, null, listScope); path, null, listScope);
} }

View File

@ -3,6 +3,7 @@ package net.minestom.server.tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class TagMapTest { public class TagMapTest {
@ -32,4 +33,11 @@ public class TagMapTest {
assertEquals(2, handler.getTag(intTag)); assertEquals(2, handler.getTag(intTag));
assertEquals(new Entry(2), handler.getTag(tag)); 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));
}
} }