-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add all new 1.20.6 item-properties
- Loading branch information
Showing
4 changed files
with
195 additions
and
91 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...alizers/src/main/kotlin/com/mineinabyss/idofront/serialization/FoodComponentSerializer.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,61 @@ | ||
package com.mineinabyss.idofront.serialization | ||
|
||
import com.mineinabyss.idofront.time.inWholeTicks | ||
import com.mineinabyss.idofront.time.ticks | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import org.bukkit.Material | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.meta.components.FoodComponent | ||
import org.bukkit.inventory.meta.components.FoodComponent.FoodEffect | ||
import org.bukkit.potion.PotionEffect | ||
import org.bukkit.potion.PotionEffectType | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
import kotlin.time.DurationUnit | ||
|
||
@Serializable | ||
@SerialName("FoodComponent") | ||
private class FoodComponentSurrogate( | ||
val nutrition: Int, | ||
val saturation: Float, | ||
val eatSeconds: Float, | ||
val canAlwaysEat: Boolean, | ||
val effects: List<FoodEffectWrapper> | ||
) { | ||
init { | ||
require(nutrition >= 0) { "FoodComponent must have a positive nutrition" } | ||
require(saturation >= 0) { "FoodComponent must have a positive saturation" } | ||
require(eatSeconds >= 0) { "FoodComponent must have a positive eatSeconds" } | ||
require(effects.all { it.probability in 0.0..1.0 }) { "FoodEffect-probability must be between 0.0..1.0" } | ||
} | ||
} | ||
|
||
object FoodComponentSerializer : KSerializer<FoodComponent> { | ||
override val descriptor: SerialDescriptor = FoodComponentSurrogate.serializer().descriptor | ||
override fun serialize(encoder: Encoder, value: FoodComponent) { | ||
val surrogate = FoodComponentSurrogate( | ||
value.nutrition, | ||
value.saturation, | ||
value.eatSeconds, | ||
value.canAlwaysEat(), | ||
value.effects.map { FoodEffectWrapper(it.effect, it.probability) } | ||
) | ||
encoder.encodeSerializableValue(FoodComponentSurrogate.serializer(), surrogate) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): FoodComponent { | ||
return ItemStack(Material.PAPER).itemMeta.food.apply { | ||
val surrogate = decoder.decodeSerializableValue(FoodComponentSurrogate.serializer()) | ||
nutrition = surrogate.nutrition | ||
saturation = surrogate.saturation | ||
setCanAlwaysEat(surrogate.canAlwaysEat) | ||
eatSeconds = surrogate.eatSeconds | ||
surrogate.effects.forEach { addEffect(it.effect, it.probability) } | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...t-serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/FoodEffectWrapper.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,7 @@ | ||
package com.mineinabyss.idofront.serialization | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.bukkit.potion.PotionEffect | ||
|
||
@Serializable | ||
class FoodEffectWrapper(val effect: @Serializable(PotionEffectSerializer::class) PotionEffect, val probability: Float) |
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
Oops, something went wrong.