Skip to content

Commit

Permalink
Merge pull request #47 from flytegg/pr/46
Browse files Browse the repository at this point in the history
Pr/46
  • Loading branch information
joshbker authored Feb 13, 2024
2 parents c11d703 + 0d3f8a1 commit fb3a959
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Maven
<dependency>
<groupId>gg.flyte</groupId>
<artifactId>twilight</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</dependency>
```

Expand All @@ -30,14 +30,14 @@ maven {
url "https://repo.flyte.gg/releases"
}
implementation "gg.flyte:twilight:1.1.1"
implementation "gg.flyte:twilight:1.1.2"
```

Gradle (Kotlin DSL)
```kotlin
maven("https://repo.flyte.gg/releases")

implementation("gg.flyte:twilight:1.1.1")
implementation("gg.flyte:twilight:1.1.2")
```

Certain features of Twilight require configuration, which can be done via the Twilight class. To setup a Twilight class instance, you can use the `twilight` function as shown below:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "gg.flyte"
version = "1.1.1"
version = "1.1.2"

repositories {
mavenLocal()
Expand Down
93 changes: 93 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/gson/ItemStackAdapter.kt
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)
})
}
})

}
}
}
}
1 change: 0 additions & 1 deletion src/main/kotlin/gg/flyte/twilight/inventory/GUIListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ object GUIListener : CustomTwilightListener() {

events += event<InventoryClickEvent>(EventPriority.NORMAL, ignoreCancelled = true) {
if (inventory.isNotCustom() || view.topInventory != inventory) return@event
println("clicked")
(inventory.holder as CustomGUI).onClick(this)
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/string/Symbol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ object Symbol {
const val GIFT = "🎁"
const val JAPANESE_CASTLE = "\uD83C\uDFEF"
const val BELL = "🔔"
const val COFFEE = ""
const val PENCIL = ""
const val TICK = ""
const val CROSS = ""
}
33 changes: 20 additions & 13 deletions src/main/kotlin/gg/flyte/twilight/time/Time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ package gg.flyte.twilight.time
/**
* Returns the English suffix for a given day of the month.
*
* @param day The day of the month for which to retrieve the suffix. Should be a value between 1 and 31.
* @return The English suffix for the given day of the month.
* @throws ArrayIndexOutOfBoundsException if the provided day is not within the valid range (1 to 31).
* @param day The day of the month for which to retrieve the suffix.
* @return The English suffix for the given day.
*/
fun getDaySuffix(day: Int): String {
return arrayOf(
// 0 1 2 3 4 5 6 7 8 9
"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
// 10 11 12 13 14 15 16 17 18 19
"th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
// 20 21 22 23 24 25 26 27 28 29
"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
// 30 31
"th", "st")[day]
}
if (day in 11..13) return "th"

val lastTwoDigits = day % 100
val suffix = when (lastTwoDigits) {
11, 12, 13 -> "th"
else -> {
val lastDigit = day % 10
when (lastDigit) {
1 -> "st"
2 -> "nd"
3 -> "rd"
else -> "th"
}
}
}

return suffix
}

0 comments on commit fb3a959

Please sign in to comment.