-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97397ae
commit bd53ce8
Showing
8 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...n/cc/modlabs/kpaper/utils/TermpStorage.kt → ...in/cc/modlabs/kpaper/file/TermpStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cc.modlabs.kpaper.utils | ||
package cc.modlabs.kpaper.file | ||
|
||
import java.io.File | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...modlabs/kpaper/utils/config/FileConfig.kt → .../modlabs/kpaper/file/config/FileConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dlabs/kpaper/utils/config/PluginConfig.kt → ...odlabs/kpaper/file/config/PluginConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...odlabs/kpaper/utils/config/WorldConfig.kt → ...modlabs/kpaper/file/config/WorldConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cc.modlabs.kpaper.utils.config | ||
package cc.modlabs.kpaper.file.config | ||
|
||
import org.bukkit.Bukkit | ||
|
||
|
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/cc/modlabs/kpaper/file/gson/ItemStackTypeAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cc.modlabs.kpaper.file.gson | ||
|
||
import com.google.gson.* | ||
import org.bukkit.Bukkit | ||
import org.bukkit.Material | ||
import org.bukkit.inventory.ItemFactory | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.configuration.serialization.ConfigurationSerializable | ||
import java.lang.reflect.Type | ||
|
||
class ItemStackTypeAdapter : JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> { | ||
override fun serialize(src: ItemStack, typeOfSrc: Type, context: JsonSerializationContext): JsonElement { | ||
val jsonObject = JsonObject() | ||
jsonObject.addProperty("item", src.type.key.toString()) | ||
jsonObject.addProperty("count", src.amount) | ||
|
||
if (src.hasItemMeta()) { | ||
val metaMap = src.itemMeta?.serialize() ?: emptyMap() | ||
val metaJson = context.serialize(metaMap) | ||
jsonObject.add("meta", metaJson) | ||
} | ||
|
||
return jsonObject | ||
} | ||
|
||
@Throws(JsonParseException::class) | ||
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): ItemStack { | ||
val jsonObject = json.asJsonObject | ||
val item = Material.matchMaterial(jsonObject["item"].asString) ?: throw JsonParseException("Invalid material") | ||
val count = jsonObject["count"].asInt | ||
|
||
val stack = ItemStack(item, count) | ||
|
||
if (jsonObject.has("meta")) { | ||
val metaJson = jsonObject["meta"] | ||
val metaMap = context.deserialize<Map<String, Any>>(metaJson, Map::class.java) | ||
|
||
val itemFactory: ItemFactory = Bukkit.getItemFactory() | ||
val meta = itemFactory.getItemMeta(item) | ||
if (meta is ConfigurationSerializable) { | ||
metaMap.forEach { (key, value) -> meta.javaClass.getMethod("set$key", value::class.java)?.invoke(meta, value) } | ||
} | ||
stack.itemMeta = meta | ||
} | ||
|
||
return stack | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/cc/modlabs/kpaper/file/gson/LocalDataTypeAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cc.modlabs.kpaper.file.gson | ||
|
||
import com.google.gson.* | ||
import java.lang.reflect.Type | ||
import java.time.LocalDate | ||
|
||
class LocalDateAdapter : JsonSerializer<LocalDate>, JsonDeserializer<LocalDate> { | ||
|
||
override fun serialize( | ||
src: LocalDate?, | ||
typeOfSrc: Type?, | ||
context: JsonSerializationContext? | ||
): JsonElement { | ||
// If you don't want null to appear at all, you can handle it differently | ||
return if (src == null) JsonNull.INSTANCE else JsonPrimitive(src.toString()) | ||
} | ||
|
||
override fun deserialize( | ||
json: JsonElement?, | ||
typeOfT: Type?, | ||
context: JsonDeserializationContext? | ||
): LocalDate? { | ||
if (json == null || json.isJsonNull) return null | ||
return LocalDate.parse(json.asString) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/cc/modlabs/kpaper/file/gson/MaterialTypeAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cc.modlabs.kpaper.file.gson | ||
|
||
import com.google.gson.TypeAdapter | ||
import com.google.gson.stream.JsonReader | ||
import com.google.gson.stream.JsonToken | ||
import com.google.gson.stream.JsonWriter | ||
import org.bukkit.Material | ||
|
||
class MaterialTypeAdapter : TypeAdapter<Material>() { | ||
override fun write(out: JsonWriter?, value: Material?) { | ||
if (value == null) { | ||
out?.nullValue() | ||
return | ||
} | ||
out?.value(value.name.lowercase()) | ||
} | ||
|
||
override fun read(`in`: JsonReader?): Material { | ||
if (`in`?.peek() == JsonToken.NULL) return Material.STONE | ||
|
||
return Material.valueOf(`in`!!.nextString().uppercase()) | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
src/main/kotlin/cc/modlabs/kpaper/translation/TranslationManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters