From a86523c86bf9fe23e2e18e7ad4c3603c12b6f40f Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Thu, 12 Jan 2023 16:11:58 +0200 Subject: [PATCH] [plugin] add LocationSerializer --- .../commands/commands/DebugCommand.kt | 22 ++++----------- .../adventure/TextComponentSerializer.kt | 14 ++++++++++ .../serializers/bukkit/LocationSerializer.kt | 28 +++++++++++++++++++ .../io/serializers/java/PatternSerializer.kt | 16 +++++++++++ .../common/variable/Variable.kt | 2 +- 5 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/adventure/TextComponentSerializer.kt create mode 100644 plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/bukkit/LocationSerializer.kt create mode 100644 plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/java/PatternSerializer.kt diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt index 7c91016..16a3636 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/commands/commands/DebugCommand.kt @@ -6,6 +6,9 @@ import me.gabytm.minecraft.arcanevouchers.ArcaneVouchers import me.gabytm.minecraft.arcanevouchers.Constant import me.gabytm.minecraft.arcanevouchers.commands.ArcaneCommand import me.gabytm.minecraft.arcanevouchers.functions.exception +import me.gabytm.minecraft.arcanevouchers.io.serializers.java.PatternSerializer +import me.gabytm.minecraft.arcanevouchers.io.serializers.adventure.TextComponentSerializer +import me.gabytm.minecraft.arcanevouchers.io.serializers.bukkit.LocationSerializer import me.gabytm.minecraft.arcanevouchers.message.implementations.ActionBarMessage import me.gabytm.minecraft.arcanevouchers.message.implementations.ChatMessage import me.gabytm.minecraft.arcanevouchers.message.implementations.TitleMessage @@ -14,8 +17,8 @@ import me.gabytm.minecraft.arcanevouchers.voucher.settings.VoucherSettings import me.mattstudios.mf.annotations.Permission import me.mattstudios.mf.annotations.SubCommand import net.kyori.adventure.text.TextComponent -import net.kyori.adventure.text.minimessage.MiniMessage import org.bukkit.Bukkit +import org.bukkit.Location import org.bukkit.command.CommandSender import java.io.IOException import java.io.InputStreamReader @@ -42,6 +45,7 @@ class DebugCommand(plugin: ArcaneVouchers) : ArcaneCommand(plugin) { .registerTypeAdapter(TextComponent::class.java, TextComponentSerializer()) .registerTypeAdapter(Voucher::class.java, VoucherSerializer.INSTANCE) .registerTypeAdapter(Pattern::class.java, PatternSerializer()) + .registerTypeAdapter(Location::class.java, LocationSerializer.INSTANCE) // Messages .registerTypeAdapter(ActionBarMessage::class.java, ActionBarMessage.Serializer.INSTANCE) .registerTypeAdapter(ChatMessage::class.java, ChatMessage.Serializer.INSTANCE) @@ -156,20 +160,4 @@ class DebugCommand(plugin: ArcaneVouchers) : ArcaneCommand(plugin) { } - internal class TextComponentSerializer : JsonSerializer { - - override fun serialize(src: TextComponent?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { - return if (src == null) JsonNull.INSTANCE else JsonPrimitive(MiniMessage.miniMessage().serialize(src)) - } - - } - - internal class PatternSerializer : JsonSerializer { - - override fun serialize(src: Pattern?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { - return JsonPrimitive(src?.pattern()) - } - - } - } \ No newline at end of file diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/adventure/TextComponentSerializer.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/adventure/TextComponentSerializer.kt new file mode 100644 index 0000000..ec3c424 --- /dev/null +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/adventure/TextComponentSerializer.kt @@ -0,0 +1,14 @@ +package me.gabytm.minecraft.arcanevouchers.io.serializers.adventure + +import com.google.gson.* +import net.kyori.adventure.text.TextComponent +import net.kyori.adventure.text.minimessage.MiniMessage +import java.lang.reflect.Type + +class TextComponentSerializer : JsonSerializer { + + override fun serialize(src: TextComponent?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { + return if (src == null) JsonNull.INSTANCE else JsonPrimitive(MiniMessage.miniMessage().serialize(src)) + } + +} \ No newline at end of file diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/bukkit/LocationSerializer.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/bukkit/LocationSerializer.kt new file mode 100644 index 0000000..31212b6 --- /dev/null +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/bukkit/LocationSerializer.kt @@ -0,0 +1,28 @@ +package me.gabytm.minecraft.arcanevouchers.io.serializers.bukkit + +import com.google.gson.* +import org.bukkit.Location +import java.lang.reflect.Type + +class LocationSerializer : JsonSerializer { + + override fun serialize(src: Location?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { + if (src == null) { + return JsonNull.INSTANCE + } + + val element = JsonObject() + element.addProperty("world", src.world?.name) + element.addProperty("x", src.x) + element.addProperty("y", src.y) + element.addProperty("z", src.z) + return element + } + + companion object { + + val INSTANCE = LocationSerializer() + + } + +} \ No newline at end of file diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/java/PatternSerializer.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/java/PatternSerializer.kt new file mode 100644 index 0000000..fc31a15 --- /dev/null +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/java/PatternSerializer.kt @@ -0,0 +1,16 @@ +package me.gabytm.minecraft.arcanevouchers.io.serializers.java + +import com.google.gson.JsonElement +import com.google.gson.JsonPrimitive +import com.google.gson.JsonSerializationContext +import com.google.gson.JsonSerializer +import java.lang.reflect.Type +import java.util.regex.Pattern + +class PatternSerializer : JsonSerializer { + + override fun serialize(src: Pattern?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { + return JsonPrimitive(src?.pattern()) + } + +} \ No newline at end of file diff --git a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/common/variable/Variable.kt b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/common/variable/Variable.kt index a354672..88d0fc7 100644 --- a/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/common/variable/Variable.kt +++ b/plugin/src/main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/implementations/common/variable/Variable.kt @@ -3,7 +3,7 @@ package me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations. import org.bukkit.entity.Player abstract class Variable( - private val transformer: (player: Player?) -> T? + @Transient private val transformer: (player: Player?) -> T? ) { @Suppress("unused")