-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from flytegg/pr/46
Pr/46
- Loading branch information
Showing
6 changed files
with
121 additions
and
18 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ plugins { | |
} | ||
|
||
group = "gg.flyte" | ||
version = "1.1.1" | ||
version = "1.1.2" | ||
|
||
repositories { | ||
mavenLocal() | ||
|
93 changes: 93 additions & 0 deletions
93
src/main/kotlin/gg/flyte/twilight/gson/ItemStackAdapter.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,93 @@ | ||
package gg.flyte.twilight.gson | ||
|
||
import com.google.gson.* | ||
import org.bukkit.Material | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.meta.MapMeta | ||
import org.bukkit.inventory.meta.SkullMeta | ||
import java.lang.reflect.Type | ||
import gg.flyte.twilight.extension.enumValue; | ||
import gg.flyte.twilight.itembuilder.ItemBuilder | ||
import net.kyori.adventure.text.Component | ||
import org.bukkit.enchantments.Enchantment | ||
|
||
object ItemStackAdapter: JsonDeserializer<ItemStack>, JsonSerializer<ItemStack> { | ||
override fun deserialize(json: JsonElement?, type: Type?, context: JsonDeserializationContext?): ItemStack { | ||
if (json == null) throw JsonParseException("JSON cannot be null.") | ||
if (json !is JsonObject) throw JsonParseException("Not a valid JSON Object.") | ||
|
||
val materialType = json.get("type") | ||
val amount = json.get("amount").asInt | ||
val meta = json.getAsJsonObject("meta") | ||
|
||
if(materialType == null) throw JsonParseException("Invalid JSON format, some required values are null.") | ||
|
||
|
||
if (!materialType.isJsonPrimitive && !(materialType as JsonPrimitive).isString) throw JsonParseException("\"type\" not of type string.") | ||
val material = enumValue<Material>(materialType.asString) ?: throw JsonParseException("Invalid JSON, Invalid Material Provided."); | ||
val builder = ItemBuilder(material, amount) | ||
|
||
|
||
// Meta stuff | ||
if(meta != null) { | ||
val displayName = meta.get("displayName") | ||
val lore = meta.getAsJsonArray("lore") | ||
val enchants = meta.getAsJsonObject("enchants") | ||
val flags = meta.getAsJsonArray("flags") | ||
val unbreakable = meta.get("unbreakable") | ||
|
||
if(unbreakable != null && unbreakable.isJsonPrimitive && (unbreakable as JsonPrimitive).isBoolean) { | ||
builder.unbreakable = unbreakable.asBoolean; | ||
} | ||
|
||
if(displayName != null && displayName.isJsonPrimitive && (displayName as JsonPrimitive).isString) { | ||
builder.name = Component.text(displayName.asString); | ||
} | ||
|
||
if(lore != null) { | ||
builder.lore = lore.map { Component.text(it.asString) }.toMutableList(); | ||
} | ||
|
||
if(enchants != null && !enchants.isEmpty) { | ||
enchants.asMap().forEach{(enchant, level) -> | ||
builder.enchantments[Enchantment.getByName(enchant)!!] = level.asInt | ||
} | ||
} | ||
|
||
if(flags != null) { | ||
// ... | ||
} | ||
} | ||
|
||
return builder.build() | ||
} | ||
|
||
override fun serialize(itemStack: ItemStack?,type: Type?, context: JsonSerializationContext?): JsonElement { | ||
if(itemStack == null) throw JsonParseException("ItemStack cannot be null") | ||
return JsonObject().apply { | ||
addProperty("type", itemStack.type.name) | ||
addProperty("amount", itemStack.amount) | ||
// Meta | ||
if(itemStack.hasItemMeta()) { | ||
add("meta", JsonObject().apply { | ||
val meta = itemStack.itemMeta | ||
addProperty("displayName", meta.displayName) | ||
addProperty("unbreakable", meta.isUnbreakable) | ||
add("lore", GSON.toJsonTree(meta.lore()) as JsonArray) | ||
add("enchants", JsonObject().apply { | ||
meta.enchants.forEach { enchant -> | ||
addProperty(enchant.key.key.key, enchant.value) | ||
} | ||
}) | ||
add("flags", GSON.toJsonTree(meta.itemFlags) as JsonArray) | ||
if(meta is SkullMeta) { | ||
add("skullData", JsonObject().apply { | ||
addProperty("owner", meta.owner) | ||
}) | ||
} | ||
}) | ||
|
||
} | ||
} | ||
} | ||
} |
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
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
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