Skip to content

Commit

Permalink
Add MaterialSerializer, update ComponentSerializer and NamespacedKeyS…
Browse files Browse the repository at this point in the history
…erializer, apply spotless
  • Loading branch information
DenaryDev committed Apr 28, 2024
1 parent 72b1747 commit a8137a6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +28,7 @@ public static TypeSerializerCollection serializers() {
.register(LocationSerializer.TYPE, new LocationSerializer())
.register(new ComponentSerializer())
.register(new NamespacedKeySerializer())
.register(new MaterialSerializer())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,34 @@

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<Component> {

private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();

public ComponentSerializer() {
super(Component.class);
}

@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<Class<?>> typeSupported) {
@Nullable
protected Object serialize(@Nullable final Component item, final Predicate<Class<?>> typeSupported) {
if (item == null) return null;

return MINI_MESSAGE.serialize(item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Objects;
import java.util.UUID;

public class ItemStackSerializer implements TypeSerializer<ItemStack> {
public final class ItemStackSerializer implements TypeSerializer<ItemStack> {
public static final TypeToken<ItemStack> TYPE = TypeToken.get(ItemStack.class);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import java.lang.reflect.Type;

public class LocationSerializer implements TypeSerializer<Location> {

public final class LocationSerializer implements TypeSerializer<Location> {
public static final TypeToken<Location> TYPE = TypeToken.get(Location.class);

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Material> {
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<Class<?>> typeSupported) {
if (item == null) return null;

return item.name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,20 +21,26 @@
* @since 0:00 18.02.2024
*/
@ApiStatus.AvailableSince("2.1.1")
public class NamespacedKeySerializer extends ScalarSerializer<NamespacedKey> {
public final class NamespacedKeySerializer extends ScalarSerializer<NamespacedKey> {
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<Class<?>> typeSupported) {
protected Object serialize(@Nullable final NamespacedKey item, final Predicate<Class<?>> typeSupported) {
if (item == null) return null;

return item.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class ItemUtils {
* создавать {@link ItemStack} с нужными параметрами.
* <p>
* <b><u>Будет удалено в 2.3.0</u></b>
*
*
* @deprecated используйте {@link ItemBuilder#empty()} или {@link ItemBuilder#fromStack(ItemStack)}
*/
@Deprecated(forRemoval = true)
Expand Down

0 comments on commit a8137a6

Please sign in to comment.