Skip to content

Commit

Permalink
feat(code): Update project to 24w34a.
Browse files Browse the repository at this point in the history
Closes #106
  • Loading branch information
Ayfri committed Nov 23, 2024
1 parent faef5ad commit 43250c9
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 71 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kotlin.code.style=official
kotlin.version=2.0.10

# Minecraft version
minecraft.version=24w33a
minecraft.version=24w34a

# Gradle optimization flags
org.gradle.caching=true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.ayfri.kore.arguments.components.consumable

import io.github.ayfri.kore.arguments.components.types.Effect
import kotlinx.serialization.Serializable

@Serializable
data class ApplyEffects(
var effects: List<Effect>,
var probability: Float,
) : ConsumeEffect()

fun ConsumeEffects.applyEffects(probability: Float, vararg effects: Effect) =
apply { this.effects += ApplyEffects(effects.toList(), probability) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.ayfri.kore.arguments.components.consumable

import kotlinx.serialization.Serializable

@Serializable
data object ClearAllEffects : ConsumeEffect()

fun ConsumeEffects.clearAllEffects() = apply { effects += ClearAllEffects }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.ayfri.kore.arguments.components.consumable

import io.github.ayfri.kore.serializers.NamespacedPolymorphicSerializer
import kotlinx.serialization.Serializable

@Serializable(with = ConsumeEffect.Companion.ConsumeEffectSerializer::class)
sealed class ConsumeEffect {
companion object {
data object ConsumeEffectSerializer : NamespacedPolymorphicSerializer<ConsumeEffect>(ConsumeEffect::class, skipOutputName = true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.ayfri.kore.arguments.components.consumable

import kotlinx.serialization.Serializable

@Serializable
data class ConsumeEffects(
var effects: List<ConsumeEffect> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.ayfri.kore.arguments.components.consumable

import io.github.ayfri.kore.arguments.types.resources.SoundEventArgument
import kotlinx.serialization.Serializable

@Serializable
data class PlaySound(
var sound: SoundEventArgument,
) : ConsumeEffect()

fun ConsumeEffects.playSound(sound: SoundEventArgument) = apply { effects += PlaySound(sound) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.ayfri.kore.arguments.components.consumable

import io.github.ayfri.kore.arguments.components.types.Effect
import kotlinx.serialization.Serializable

@Serializable
data class RemoveEffects(
var effects: List<Effect> = emptyList(),
) : ConsumeEffect()

fun ConsumeEffects.removeEffects(vararg effects: Effect) = apply { this.effects += RemoveEffects(effects.toList()) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.ayfri.kore.arguments.components.consumable

import kotlinx.serialization.Serializable

@Serializable
data class TeleportRandomly(
var diameter: Float,
) : ConsumeEffect()

fun ConsumeEffects.teleportRandomly(diameter: Float) = apply { effects += TeleportRandomly(diameter) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.github.ayfri.kore.arguments.components.types

import io.github.ayfri.kore.arguments.components.ComponentsScope
import io.github.ayfri.kore.arguments.components.consumable.ConsumeEffect
import io.github.ayfri.kore.arguments.components.consumable.ConsumeEffects
import io.github.ayfri.kore.arguments.types.resources.SoundEventArgument
import io.github.ayfri.kore.generated.ComponentTypes
import io.github.ayfri.kore.serializers.LowercaseSerializer
import io.github.ayfri.kore.utils.snakeCase
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable(with = ConsumeAnimation.Companion.ConsumeAnimationSerializer::class)
enum class ConsumeAnimation {
NONE,
EAT,
DRINK,
BLOCK,
BOW,
SPEAR,
CROSSBOW,
SPYGLASS,
TOOT_HORN,
BRUSH;

companion object {
data object ConsumeAnimationSerializer : LowercaseSerializer<ConsumeAnimation>(entries)
}
}

@Serializable
data class ConsumableComponent(
@SerialName("consume_seconds")
var consumeSeconds: Float,
var animation: ConsumeAnimation,
var sound: SoundEventArgument,
@SerialName("has_consume_particles")
var hasConsumeParticles: Boolean,
@SerialName("on_consume_effects")
var onConsumeEffects: Map<String, ConsumeEffect>? = null,
) : Component()

fun ComponentsScope.consumable(
consumeSeconds: Float,
animation: ConsumeAnimation,
sound: SoundEventArgument,
hasConsumeParticles: Boolean = true,
block: ConsumableComponent.() -> Unit = {},
) = apply {
this[ComponentTypes.CONSUMABLE] = ConsumableComponent(
consumeSeconds,
animation,
sound,
hasConsumeParticles
).apply(block)
}

fun ConsumableComponent.onConsumeEffects(block: ConsumeEffects.() -> Unit) = apply {
onConsumeEffects = ConsumeEffects().apply(block).effects.associateBy {
"minecraft:${it.javaClass.simpleName.snakeCase()}"
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,23 @@
package io.github.ayfri.kore.arguments.components.types

import io.github.ayfri.kore.arguments.components.ComponentsScope
import io.github.ayfri.kore.arguments.types.resources.EffectArgument
import io.github.ayfri.kore.arguments.types.resources.ItemArgument
import io.github.ayfri.kore.data.item.ItemStack
import io.github.ayfri.kore.generated.ComponentTypes
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class FoodEffect(
var effect: Effect,
var probability: Float,
)

@Serializable
data class FoodComponent(
var nutrition: Int,
var nutrition: Float,
var saturation: Float,
@SerialName("is_meat")
var isMeat: Boolean? = null,
@SerialName("can_always_eat")
var canAlwaysEat: Boolean? = null,
@SerialName("eat_seconds")
var eatSeconds: Float? = null,
var effects: List<FoodEffect>? = null,
@SerialName("using_converts_to")
var usingConvertsTo: ItemStack? = null,
) : Component()

fun ComponentsScope.food(
nutrition: Int,
saturation: Float,
isMeat: Boolean? = null,
canAlwaysEat: Boolean? = null,
eatSeconds: Float? = null,
effects: List<FoodEffect>,
block: FoodComponent.() -> Unit = {},
) = apply {
this[ComponentTypes.FOOD] = FoodComponent(nutrition, saturation, isMeat, canAlwaysEat, eatSeconds, effects).apply(block)
}

fun ComponentsScope.food(
nutrition: Int,
nutrition: Float,
saturation: Float,
isMeat: Boolean? = null,
canAlwaysEat: Boolean? = null,
eatSeconds: Float? = null,
vararg effects: FoodEffect,
block: FoodComponent.() -> Unit = {},
) = food(nutrition, saturation, isMeat, canAlwaysEat, eatSeconds, effects.toList(), block)

fun FoodComponent.effect(probability: Float, effect: Effect) = apply {
effects = (effects ?: mutableListOf()) + FoodEffect(effect, probability)
}

fun FoodComponent.effect(
probability: Float,
id: EffectArgument,
duration: Int,
amplifier: Byte,
ambient: Boolean,
showParticles: Boolean,
showIcon: Boolean,
) = apply {
effects = (effects ?: mutableListOf()) + FoodEffect(Effect(id, duration, amplifier, ambient, showParticles, showIcon), probability)
}

fun FoodComponent.convertsTo(id: ItemArgument, block: ItemStack.() -> Unit = {}) = apply {
usingConvertsTo = ItemStack(id).apply(block)
this[ComponentTypes.FOOD] = FoodComponent(nutrition, saturation, canAlwaysEat).apply(block)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.ayfri.kore.arguments.components.types

import io.github.ayfri.kore.arguments.components.ComponentsScope
import io.github.ayfri.kore.arguments.types.ConsumeCooldownGroupArgument
import io.github.ayfri.kore.generated.ComponentTypes
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class UseCooldownComponent(
var seconds: Float,
@SerialName("cooldown_group")
var cooldownGroup: ConsumeCooldownGroupArgument,
) : Component()

fun ComponentsScope.useCooldown(
seconds: Float,
cooldownGroup: ConsumeCooldownGroupArgument,
block: UseCooldownComponent.() -> Unit = {},
) = apply {
this[ComponentTypes.USE_COOLDOWN] = UseCooldownComponent(seconds, cooldownGroup).apply(block)
}

fun ComponentsScope.useCooldown(
seconds: Float,
cooldownGroup: String,
block: UseCooldownComponent.() -> Unit = {},
) = apply {
this[ComponentTypes.USE_COOLDOWN] = UseCooldownComponent(seconds, ConsumeCooldownGroupArgument(cooldownGroup)).apply(block)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.ayfri.kore.arguments.components.types

import io.github.ayfri.kore.arguments.components.ComponentsScope
import io.github.ayfri.kore.data.item.ItemStack
import io.github.ayfri.kore.generated.ComponentTypes
import kotlinx.serialization.Serializable

@Serializable
data class UseRemainderComponent(
var item: ItemStack,
) : Component()

fun ComponentsScope.useRemainder(item: ItemStack) = apply {
this[ComponentTypes.USE_REMAINDER] = UseRemainderComponent(item)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.ayfri.kore.arguments.types

import io.github.ayfri.kore.arguments.Argument
import kotlinx.serialization.Serializable

@Serializable(with = Argument.ArgumentSerializer::class)
interface ConsumeCooldownGroupArgument : ResourceLocationArgument {
companion object {
operator fun invoke(name: String, namespace: String = "minecraft") = object : ConsumeCooldownGroupArgument {
override val name = name
override val namespace = namespace
}
}
}
62 changes: 44 additions & 18 deletions kore/src/test/kotlin/io/github/ayfri/kore/arguments/Components.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package io.github.ayfri.kore.arguments
import io.github.ayfri.kore.arguments.chatcomponents.text
import io.github.ayfri.kore.arguments.chatcomponents.textComponent
import io.github.ayfri.kore.arguments.colors.Color
import io.github.ayfri.kore.arguments.components.consumable.applyEffects
import io.github.ayfri.kore.arguments.components.consumable.playSound
import io.github.ayfri.kore.arguments.components.consumable.teleportRandomly
import io.github.ayfri.kore.arguments.components.types.*
import io.github.ayfri.kore.arguments.enums.Dimension
import io.github.ayfri.kore.arguments.enums.MapDecoration
Expand Down Expand Up @@ -135,6 +138,30 @@ fun componentsTests() {
chargedProjectilesTest.components!!.chargedProjectiles(Items.ARROW, Items.SPECTRAL_ARROW, Items.TIPPED_ARROW)
chargedProjectilesTest.asString() assertsIs """minecraft:crossbow[charged_projectiles=[{id:"minecraft:arrow"},{id:"minecraft:spectral_arrow"},{id:"minecraft:tipped_arrow"}]]"""

val consumableTest = Items.POTION {
consumable(
consumeSeconds = 1.5f,
animation = ConsumeAnimation.DRINK,
sound = SoundEvents.Item.Bottle.EMPTY,
) {
onConsumeEffects {
applyEffects(
probability = 1.0f, Effect(
id = Effects.POISON,
duration = 100,
amplifier = 1,
ambient = true,
showParticles = true,
showIcon = true
)
)
teleportRandomly(diameter = 2.5f)
playSound(SoundEvents.Entity.Cow.STEP)
}
}
}
consumableTest.asString() assertsIs """minecraft:potion[consumable={consume_seconds:1.5f,animation:"drink",sound:"minecraft:item.bottle.empty",has_consume_particles:1b,on_consume_effects:{"minecraft:apply_effects":{effects:[{id:"minecraft:poison",duration:100,amplifier:1b,ambient:1b,show_particles:1b,show_icon:1b}],probability:1.0f},"minecraft:teleport_randomly":{diameter:2.5f},"minecraft:play_sound":{sound:"minecraft:entity.cow.step"}}}]"""

val containerTest = stone {
container {
slot(0, itemStack(Items.DIAMOND))
Expand Down Expand Up @@ -263,26 +290,12 @@ fun componentsTests() {

val foodTest = Items.COOKED_BEEF {
food(
nutrition = 10,
nutrition = 10f,
saturation = 1.0f,
) {
isMeat = true
eatSeconds = 0.5f

effect(
probability = 1f,
id = Effects.REGENERATION,
duration = 100,
amplifier = 1,
ambient = true,
showParticles = true,
showIcon = true
)

convertsTo(Items.BONE)
}
canAlwaysEat = true
)
}
foodTest.asString() assertsIs """minecraft:cooked_beef[food={nutrition:10,saturation:1.0f,is_meat:1b,eat_seconds:0.5f,effects:[{effect:{id:"minecraft:regeneration",duration:100,amplifier:1b,ambient:1b,show_particles:1b,show_icon:1b},probability:1.0f}],using_converts_to:{id:"minecraft:bone"}}]"""
foodTest.asString() assertsIs """minecraft:cooked_beef[food={nutrition:10.0f,saturation:1.0f,can_always_eat:1b}]"""

val hideAdditionalTooltipTest = stoneSword {
hideAdditionalTooltip()
Expand Down Expand Up @@ -485,6 +498,19 @@ fun componentsTests() {
unbreakableTest.components!!.unbreakable(showInTooltip = true)
unbreakableTest.asString() assertsIs """minecraft:stone_sword[unbreakable={show_in_tooltip:1b}]"""

val useCooldownTest = Items.ENDER_PEARL {
useCooldown(
seconds = 2.0f,
cooldownGroup = "ender_pearl"
)
}
useCooldownTest.asString() assertsIs """minecraft:ender_pearl[use_cooldown={seconds:2.0f,cooldown_group:"minecraft:ender_pearl"}]"""

val useRemainderTest = Items.POTION {
useRemainder(itemStack(Items.GLASS_BOTTLE))
}
useRemainderTest.asString() assertsIs """minecraft:potion[use_remainder={item:{id:"minecraft:glass_bottle"}}]"""

val writableBookTest = Items.WRITABLE_BOOK {
writableBookContent {
page("test")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 43250c9

Please sign in to comment.