From a8137a6cc021a4f6ed6258448a70154ec30b33ce Mon Sep 17 00:00:00 2001 From: DenaryDev Date: Sun, 28 Apr 2024 15:39:03 +0500 Subject: [PATCH] Add MaterialSerializer, update ComponentSerializer and NamespacedKeySerializer, apply spotless --- .../crystal/config/BukkitConfigs.java | 2 + .../serializers/ComponentSerializer.java | 16 +++++-- .../serializers/ItemStackSerializer.java | 2 +- .../serializers/LocationSerializer.java | 3 +- .../serializers/MaterialSerializer.java | 48 +++++++++++++++++++ .../serializers/NamespacedKeySerializer.java | 19 +++++--- .../me/denarydev/crystal/utils/ItemUtils.java | 2 +- 7 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/MaterialSerializer.java diff --git a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/BukkitConfigs.java b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/BukkitConfigs.java index a946c9a..ddc05a7 100644 --- a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/BukkitConfigs.java +++ b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/BukkitConfigs.java @@ -10,6 +10,7 @@ import me.denarydev.crystal.config.serializers.ComponentSerializer; import me.denarydev.crystal.config.serializers.ItemStackSerializer; import me.denarydev.crystal.config.serializers.LocationSerializer; +import me.denarydev.crystal.config.serializers.MaterialSerializer; import me.denarydev.crystal.config.serializers.NamespacedKeySerializer; import org.spongepowered.configurate.serialize.TypeSerializerCollection; @@ -27,6 +28,7 @@ public static TypeSerializerCollection serializers() { .register(LocationSerializer.TYPE, new LocationSerializer()) .register(new ComponentSerializer()) .register(new NamespacedKeySerializer()) + .register(new MaterialSerializer()) .build(); } } diff --git a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ComponentSerializer.java b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ComponentSerializer.java index aa420f1..abf0df1 100644 --- a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ComponentSerializer.java +++ b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ComponentSerializer.java @@ -9,13 +9,13 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; +import org.jetbrains.annotations.Nullable; import org.spongepowered.configurate.serialize.ScalarSerializer; import java.lang.reflect.Type; import java.util.function.Predicate; public class ComponentSerializer extends ScalarSerializer { - private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage(); public ComponentSerializer() { @@ -23,12 +23,20 @@ public ComponentSerializer() { } @Override - public Component deserialize(Type type, Object obj) { - return MINI_MESSAGE.deserialize(obj.toString()); + @Nullable + public Component deserialize(final Type type, @Nullable final Object obj) { + if (obj instanceof String s) { + return MINI_MESSAGE.deserialize(s); + } + + return null; } @Override - protected Object serialize(Component item, Predicate> typeSupported) { + @Nullable + protected Object serialize(@Nullable final Component item, final Predicate> typeSupported) { + if (item == null) return null; + return MINI_MESSAGE.serialize(item); } } diff --git a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ItemStackSerializer.java b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ItemStackSerializer.java index 0af4d0a..58aa683 100644 --- a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ItemStackSerializer.java +++ b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/ItemStackSerializer.java @@ -30,7 +30,7 @@ import java.util.Objects; import java.util.UUID; -public class ItemStackSerializer implements TypeSerializer { +public final class ItemStackSerializer implements TypeSerializer { public static final TypeToken TYPE = TypeToken.get(ItemStack.class); @Override diff --git a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/LocationSerializer.java b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/LocationSerializer.java index c739e44..e6c30cc 100644 --- a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/LocationSerializer.java +++ b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/LocationSerializer.java @@ -17,8 +17,7 @@ import java.lang.reflect.Type; -public class LocationSerializer implements TypeSerializer { - +public final class LocationSerializer implements TypeSerializer { public static final TypeToken TYPE = TypeToken.get(Location.class); @Override diff --git a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/MaterialSerializer.java b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/MaterialSerializer.java new file mode 100644 index 0000000..cf15fa3 --- /dev/null +++ b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/MaterialSerializer.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2024 DenaryDev + * + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ +package me.denarydev.crystal.config.serializers; + +import org.bukkit.Material; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.configurate.serialize.ScalarSerializer; +import org.spongepowered.configurate.serialize.SerializationException; + +import java.lang.reflect.Type; +import java.util.function.Predicate; + +/** + * @author DenaryDev + * @since 14:31 28.04.2024 + */ +@ApiStatus.AvailableSince("2.2.0") +public final class MaterialSerializer extends ScalarSerializer { + public MaterialSerializer() { + super(Material.class); + } + + @Override + public @NotNull Material deserialize(final Type type, @Nullable final Object obj) throws SerializationException { + if (obj instanceof String s) { + final var material = Material.matchMaterial(s); + if (material == null) throw new SerializationException("Cannot deserialize " + obj + " as a Material"); + return material; + } + + return Material.AIR; + } + + @Override + @Nullable + protected Object serialize(@Nullable final Material item, final Predicate> typeSupported) { + if (item == null) return null; + + return item.name(); + } +} diff --git a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/NamespacedKeySerializer.java b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/NamespacedKeySerializer.java index 2e64eb7..7890a4c 100644 --- a/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/NamespacedKeySerializer.java +++ b/library/paper/serializers/src/main/java/me/denarydev/crystal/config/serializers/NamespacedKeySerializer.java @@ -9,6 +9,7 @@ import org.bukkit.NamespacedKey; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; import org.spongepowered.configurate.serialize.ScalarSerializer; import org.spongepowered.configurate.serialize.SerializationException; @@ -20,20 +21,26 @@ * @since 0:00 18.02.2024 */ @ApiStatus.AvailableSince("2.1.1") -public class NamespacedKeySerializer extends ScalarSerializer { +public final class NamespacedKeySerializer extends ScalarSerializer { public NamespacedKeySerializer() { super(NamespacedKey.class); } @Override - public NamespacedKey deserialize(Type type, Object obj) throws SerializationException { - final var key = NamespacedKey.fromString(obj.toString()); - if (key != null) return key; - else throw new SerializationException("Invalid namespaced key"); + public NamespacedKey deserialize(final Type type, @Nullable final Object obj) throws SerializationException { + if (obj instanceof String s) { + final var key = NamespacedKey.fromString(s); + if (key == null) throw new SerializationException("Cannot deserialize " + obj + " as a NamespacedKey"); + return key; + } + + return null; } @Override - protected Object serialize(NamespacedKey item, Predicate> typeSupported) { + protected Object serialize(@Nullable final NamespacedKey item, final Predicate> typeSupported) { + if (item == null) return null; + return item.toString(); } } diff --git a/library/paper/utils/src/main/java/me/denarydev/crystal/utils/ItemUtils.java b/library/paper/utils/src/main/java/me/denarydev/crystal/utils/ItemUtils.java index 9c30196..2c6ee31 100644 --- a/library/paper/utils/src/main/java/me/denarydev/crystal/utils/ItemUtils.java +++ b/library/paper/utils/src/main/java/me/denarydev/crystal/utils/ItemUtils.java @@ -28,7 +28,7 @@ public final class ItemUtils { * создавать {@link ItemStack} с нужными параметрами. *

* Будет удалено в 2.3.0 - * + * * @deprecated используйте {@link ItemBuilder#empty()} или {@link ItemBuilder#fromStack(ItemStack)} */ @Deprecated(forRemoval = true)