Skip to content

Commit

Permalink
[plugin] add LocationSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
iGabyTM committed Jan 12, 2023
1 parent e884aad commit a86523c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -156,20 +160,4 @@ class DebugCommand(plugin: ArcaneVouchers) : ArcaneCommand(plugin) {

}

internal class TextComponentSerializer : JsonSerializer<TextComponent> {

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

override fun serialize(src: Pattern?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
return JsonPrimitive(src?.pattern())
}

}

}
Original file line number Diff line number Diff line change
@@ -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<TextComponent> {

override fun serialize(src: TextComponent?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
return if (src == null) JsonNull.INSTANCE else JsonPrimitive(MiniMessage.miniMessage().serialize(src))
}

}
Original file line number Diff line number Diff line change
@@ -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<Location> {

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()

}

}
Original file line number Diff line number Diff line change
@@ -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<Pattern> {

override fun serialize(src: Pattern?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
return JsonPrimitive(src?.pattern())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.
import org.bukkit.entity.Player

abstract class Variable<T>(
private val transformer: (player: Player?) -> T?
@Transient private val transformer: (player: Player?) -> T?
) {

@Suppress("unused")
Expand Down

0 comments on commit a86523c

Please sign in to comment.