diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/ActionEffectTimeline.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/ActionEffectTimeline.kt index caabe009e2..3b8042f78f 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/ActionEffectTimeline.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/ActionEffectTimeline.kt @@ -10,8 +10,12 @@ package com.cobblemon.mod.common.api.moves.animations import com.bedrockk.molang.runtime.MoLangRuntime import com.cobblemon.mod.common.api.molang.ExpressionLike +import com.cobblemon.mod.common.api.molang.MoLangFunctions.addStandardFunctions import com.cobblemon.mod.common.api.moves.animations.keyframes.ActionEffectKeyframe +import com.cobblemon.mod.common.api.scheduling.Schedulable +import com.cobblemon.mod.common.api.scheduling.ServerTaskTracker import com.cobblemon.mod.common.entity.npc.NPCEntity +import com.cobblemon.mod.common.entity.pokemon.PokemonEntity import com.cobblemon.mod.common.util.asExpressionLike import com.cobblemon.mod.common.util.resolveBoolean import java.util.concurrent.CompletableFuture @@ -76,8 +80,28 @@ class ActionEffectContext( var interrupted: Boolean = false, var currentKeyframes: MutableList = mutableListOf(), var level: Level? = null, - var holdRemovalCallbacks: MutableMap Unit> = mutableMapOf() + var holdRemovalCallbacks: MutableMap Unit> = mutableMapOf(), + var tracker: Schedulable = ServerTaskTracker ) { + companion object { + fun fromPokemonEntity( + actionEffect: ActionEffectTimeline, + pokemonEntity: PokemonEntity, + runtime: MoLangRuntime = MoLangRuntime().also { it.environment.query.addStandardFunctions() }, + holds: Collection = emptySet(), + holdRemovalCallbacks: Map Unit> = emptyMap() + ): ActionEffectContext { + return ActionEffectContext( + actionEffect = actionEffect, + providers = mutableListOf(UsersProvider(listOf(pokemonEntity))), + holdRemovalCallbacks = holdRemovalCallbacks.toMutableMap(), + holds = holds.toMutableSet(), + runtime = runtime, + level = pokemonEntity.level() + ) + } + } + lateinit var future: CompletableFuture inline fun findOneProvider() = providers.filterIsInstance().firstOrNull() diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/AnimationActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/AnimationActionEffectKeyframe.kt index a263e3e459..8ec7a2b5d0 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/AnimationActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/AnimationActionEffectKeyframe.kt @@ -49,6 +49,6 @@ class AnimationActionEffectKeyframe : ConditionalActionEffectKeyframe(), EntityC players.forEach { it.sendPacket(pkt) } } - return delayedFuture(seconds = delay.resolveFloat(context.runtime)) + return delayedFuture(seconds = delay.resolveFloat(context.runtime), tracker = context.tracker) } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityMoLangActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityMoLangActionEffectKeyframe.kt index 49b8707497..841535add2 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityMoLangActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityMoLangActionEffectKeyframe.kt @@ -46,6 +46,6 @@ class EntityMoLangActionEffectKeyframe : ConditionalActionEffectKeyframe(), Enti players.forEach { it.sendPacket(pkt) } } - return delayedFuture(seconds = delay.resolveFloat(context.runtime)) + return delayedFuture(seconds = delay.resolveFloat(context.runtime), context.tracker) } } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityParticlesActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityParticlesActionEffectKeyframe.kt index 3766b70b99..301e8ca997 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityParticlesActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntityParticlesActionEffectKeyframe.kt @@ -18,7 +18,6 @@ import com.cobblemon.mod.common.entity.PosableEntity import com.cobblemon.mod.common.net.messages.client.effect.SpawnSnowstormEntityParticlePacket import com.cobblemon.mod.common.util.asExpressionLike import com.cobblemon.mod.common.util.asIdentifierDefaultingNamespace -import net.minecraft.commands.arguments.EntityArgument.getPlayers import java.util.concurrent.CompletableFuture import net.minecraft.server.level.ServerLevel @@ -79,6 +78,6 @@ class EntityParticlesActionEffectKeyframe : ConditionalActionEffectKeyframe(), E } } - return delayedFuture(seconds = delay.resolveFloat(context.runtime)) + return delayedFuture(seconds = delay.resolveFloat(context.runtime), context.tracker) } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntitySoundActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntitySoundActionEffectKeyframe.kt index 134e05a98a..3067fa2dfe 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntitySoundActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/EntitySoundActionEffectKeyframe.kt @@ -15,8 +15,8 @@ import com.cobblemon.mod.common.api.moves.animations.UsersProvider import com.cobblemon.mod.common.api.scheduling.delayedFuture import com.cobblemon.mod.common.util.asExpressionLike import com.cobblemon.mod.common.util.asIdentifierDefaultingNamespace -import net.minecraft.core.registries.Registries import java.util.concurrent.CompletableFuture +import net.minecraft.core.registries.Registries /** * An action effect keyframe that plays a positioned sound for all matching entities. @@ -45,6 +45,6 @@ class EntitySoundActionEffectKeyframe : ConditionalActionEffectKeyframe(), Entit entity.playSound(soundEvent, 1f, 1f) } - return delayedFuture(seconds = delay.resolveFloat(context.runtime)) + return delayedFuture(seconds = delay.resolveFloat(context.runtime), context.tracker) } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoLangActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoLangActionEffectKeyframe.kt index 5cfcd8e805..6c14bd35cc 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoLangActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoLangActionEffectKeyframe.kt @@ -8,8 +8,6 @@ package com.cobblemon.mod.common.api.moves.animations.keyframes -import com.bedrockk.molang.runtime.struct.QueryStruct -import com.cobblemon.mod.common.Cobblemon import com.cobblemon.mod.common.api.molang.ExpressionLike import com.cobblemon.mod.common.api.moves.animations.ActionEffectContext import com.cobblemon.mod.common.api.scheduling.delayedFuture @@ -27,6 +25,6 @@ class MoLangActionEffectKeyframe : ActionEffectKeyframe { val delay: ExpressionLike = "0".asExpressionLike() override fun play(context: ActionEffectContext): CompletableFuture { expressions.resolve(context.runtime) - return delayedFuture(seconds = delay.resolveFloat(context.runtime)) + return delayedFuture(seconds = delay.resolveFloat(context.runtime), context.tracker) } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoveToTargetActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoveToTargetActionEffectKeyframe.kt index 9003a1f168..49f7bdb433 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoveToTargetActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/MoveToTargetActionEffectKeyframe.kt @@ -13,7 +13,6 @@ import com.cobblemon.mod.common.api.moves.animations.ActionEffectTimeline import com.cobblemon.mod.common.api.moves.animations.ActionEffects import com.cobblemon.mod.common.api.moves.animations.TargetsProvider import com.cobblemon.mod.common.api.moves.animations.UsersProvider -import com.cobblemon.mod.common.api.scheduling.afterOnServer import com.cobblemon.mod.common.entity.ai.OmniPathNavigation.NavigationContext import com.cobblemon.mod.common.entity.pokemon.PokemonEntity import com.cobblemon.mod.common.util.asExpressionLike @@ -61,7 +60,7 @@ class MoveToTargetActionEffectKeyframe : ActionEffectKeyframe { } ) - afterOnServer(seconds = timeout.resolveFloat(context.runtime)) { + context.tracker.after(seconds = timeout.resolveFloat(context.runtime)) { if (!future.isDone && !timedOut) { timedOut = true timeoutEffect.run(context).thenApply { future.complete(Unit) } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/PauseActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/PauseActionEffectKeyframe.kt index aa01469911..09580dc5e2 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/PauseActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/PauseActionEffectKeyframe.kt @@ -14,5 +14,5 @@ import com.cobblemon.mod.common.util.asExpressionLike class PauseActionEffectKeyframe : ConditionalActionEffectKeyframe() { val pause = "1".asExpressionLike() - override fun playWhenTrue(context: ActionEffectContext) = delayedFuture(seconds = pause.resolveFloat(context.runtime)) + override fun playWhenTrue(context: ActionEffectContext) = delayedFuture(seconds = pause.resolveFloat(context.runtime), context.tracker) } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/ReturnToPositionActionEffectKeyframe.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/ReturnToPositionActionEffectKeyframe.kt index 3557814643..fac50fab62 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/ReturnToPositionActionEffectKeyframe.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/moves/animations/keyframes/ReturnToPositionActionEffectKeyframe.kt @@ -13,13 +13,12 @@ import com.cobblemon.mod.common.api.moves.animations.ActionEffectContext import com.cobblemon.mod.common.api.moves.animations.ActionEffectTimeline import com.cobblemon.mod.common.api.moves.animations.ActionEffects import com.cobblemon.mod.common.api.moves.animations.UsersProvider -import com.cobblemon.mod.common.api.scheduling.after import com.cobblemon.mod.common.entity.ai.OmniPathNavigation.NavigationContext import com.cobblemon.mod.common.entity.pokemon.PokemonEntity import com.cobblemon.mod.common.util.asExpressionLike +import java.util.concurrent.CompletableFuture import net.minecraft.resources.ResourceLocation import net.minecraft.world.phys.Vec3 -import java.util.concurrent.CompletableFuture class ReturnToPositionActionEffectKeyframe : ActionEffectKeyframe { val speed = 1F @@ -56,7 +55,7 @@ class ReturnToPositionActionEffectKeyframe : ActionEffectKeyframe { } ) - after(seconds = timeout.resolveFloat(context.runtime)) { + context.tracker.after(seconds = timeout.resolveFloat(context.runtime)) { if (!future.isDone && !timedOut) { timedOut = true timeoutEffect.run(context).thenApply { future.complete(Unit) } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/transformation/controller/TransformationController.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/transformation/controller/TransformationController.kt index ae2ccf6874..ebe0cc6c95 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/transformation/controller/TransformationController.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/pokemon/transformation/controller/TransformationController.kt @@ -39,14 +39,6 @@ interface TransformationController : MutableSet { */ fun canStart(): Boolean - /** - * Validates the [TransformationLike] currently active for this controller's [pokemon] and determines whether it can be - * reverted and if the pokemon is capable of reverting right now. - * - * @return whether the transformation can be reverted. - */ - fun canRevert(): Boolean - /** * Retrieves the [TransformationLike] in this controller that matches the provided id. * diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/scheduling/SchedulingFunctions.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/scheduling/SchedulingFunctions.kt index 2a1c18e37e..bf85286f2d 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/scheduling/SchedulingFunctions.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/scheduling/SchedulingFunctions.kt @@ -9,8 +9,6 @@ package com.cobblemon.mod.common.api.scheduling import java.util.concurrent.CompletableFuture -import com.cobblemon.mod.common.util.runOnServer -import net.minecraft.world.TickRateManager import net.minecraft.world.level.Level @Deprecated("Use afterOnServer or afterOnClient; ambiguous side is not good for your health") @@ -52,9 +50,9 @@ fun lerpOnServer(seconds: Float = 0F, action: (Float) -> Unit) = ServerTaskTrack fun lerpOnClient(seconds: Float = 0F, action: (Float) -> Unit) = ClientTaskTracker.lerp(seconds = seconds, action = action) fun taskBuilder() = ScheduledTask.Builder() -fun delayedFuture(seconds: Float): CompletableFuture { +fun delayedFuture(seconds: Float, tracker: Schedulable = ServerTaskTracker): CompletableFuture { val future = CompletableFuture() - afterOnServer(seconds = seconds) { + tracker.after(seconds = seconds) { future.complete(Unit) } return future diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/api/storage/party/PlayerPartyStore.kt b/common/src/main/kotlin/com/cobblemon/mod/common/api/storage/party/PlayerPartyStore.kt index 4087cdad90..d0df5c28cd 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/api/storage/party/PlayerPartyStore.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/api/storage/party/PlayerPartyStore.kt @@ -12,6 +12,7 @@ import com.cobblemon.mod.common.Cobblemon import com.cobblemon.mod.common.advancement.CobblemonCriteria import com.cobblemon.mod.common.api.events.CobblemonEvents import com.cobblemon.mod.common.api.events.pokemon.PokemonGainedEvent +import com.cobblemon.mod.common.api.moves.animations.ActionEffectContext import com.cobblemon.mod.common.api.pokemon.feature.TickingSpeciesFeature import com.cobblemon.mod.common.api.pokemon.transformation.trigger.PassiveTrigger import com.cobblemon.mod.common.api.storage.pc.PCStore @@ -22,6 +23,7 @@ import com.cobblemon.mod.common.pokemon.activestate.ShoulderedState import com.cobblemon.mod.common.pokemon.transformation.form.TemporaryForm import com.cobblemon.mod.common.util.DataKeys import com.cobblemon.mod.common.util.getPlayer +import com.cobblemon.mod.common.util.isBusy import com.cobblemon.mod.common.util.isPokemonEntity import com.cobblemon.mod.common.util.lang import com.cobblemon.mod.common.util.server @@ -152,9 +154,20 @@ open class PlayerPartyStore( // Reevaluate active form val form = pokemon.form - if (form is TemporaryForm && !form.reevaluate(pokemon)) { - form.revert(pokemon) - } // TODO check locks? + if (form is TemporaryForm && !form.reevaluate(pokemon) && !player.isBusy() && pokemon.entity?.isBusy != true) { + val entity = pokemon.entity + if (entity == null) { + form.revert(pokemon) + } else { + form.performRevert( + pokemonEntity = entity, + context =ActionEffectContext.fromPokemonEntity( + actionEffect = form.revertActionEffect, + pokemonEntity = entity + ) + ) + } + } // Reevaluate transformation requirements and remove invalid transformations from the controller pokemon.formProxy.server().reevaluate() diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FormChangeInstruction.kt b/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FormChangeInstruction.kt index 7347e372ef..ada1f24a31 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FormChangeInstruction.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/battles/interpreter/instructions/FormChangeInstruction.kt @@ -14,7 +14,6 @@ import com.cobblemon.mod.common.api.battles.model.PokemonBattle import com.cobblemon.mod.common.api.events.CobblemonEvents import com.cobblemon.mod.common.api.events.battles.instruction.FormeChangeEvent import com.cobblemon.mod.common.api.moves.animations.ActionEffectContext -import com.cobblemon.mod.common.api.moves.animations.UsersProvider import com.cobblemon.mod.common.battles.ShowdownInterpreter import com.cobblemon.mod.common.battles.dispatch.ActionEffectInstruction import com.cobblemon.mod.common.battles.dispatch.GO @@ -64,26 +63,36 @@ class FormChangeInstruction(val message: BattleMessage): ActionEffectInstruction override fun runActionEffect(battle: PokemonBattle, runtime: MoLangRuntime) { battle.dispatch { - if (form is TemporaryForm) { - val actionEffect = (form as TemporaryForm).actionEffect - val providers = mutableListOf(battle) - val pokemon = message.battlePokemon(0, battle) ?: return@dispatch GO - pokemon.effectedPokemon.entity?.let { UsersProvider(it) }?.let(providers::add) - val context = ActionEffectContext( - actionEffect = actionEffect, - runtime = runtime, - providers = providers, - level = battle.players.firstOrNull()?.level() - ) - (form as TemporaryForm).perform(battlePokemon.effectedPokemon.entity!!, context) - UntilDispatch { context.future.isDone } - } else { - val form = battlePokemon.effectedPokemon.form - if (form is TemporaryForm) { - form.revert(battlePokemon.effectedPokemon) - } + val entity = battlePokemon.effectedPokemon.entity + val currentForm = battlePokemon.effectedPokemon.form + if (entity == null) { + battlePokemon.effectedPokemon.form = form + return@dispatch GO } - return@dispatch GO + + val form = form + val actionEffect = if (form is TemporaryForm) { + form.actionEffect + } else if (currentForm is TemporaryForm) { + currentForm.revertActionEffect + } else { + battlePokemon.effectedPokemon.form = form + return@dispatch GO // This should never happen tbh + } + + val context = ActionEffectContext.fromPokemonEntity( + actionEffect = actionEffect, + runtime = runtime, + pokemonEntity = entity + ) + + if (form is TemporaryForm) { + form.perform(entity, context) + } else { + (currentForm as TemporaryForm).performRevert(entity, context) + } + + return@dispatch UntilDispatch { context.future.isDone } } } diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/summary/widgets/screens/transformations/EvolutionSelectScreen.kt b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/summary/widgets/screens/transformations/EvolutionSelectScreen.kt index 7007d5f6fd..6526c68d2a 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/summary/widgets/screens/transformations/EvolutionSelectScreen.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/client/gui/summary/widgets/screens/transformations/EvolutionSelectScreen.kt @@ -23,7 +23,6 @@ import com.cobblemon.mod.common.client.render.drawScaledText import com.cobblemon.mod.common.client.render.models.blockbench.FloatingState import com.cobblemon.mod.common.pokemon.FormData import com.cobblemon.mod.common.pokemon.Pokemon -import com.cobblemon.mod.common.pokemon.transformation.controller.ClientTransformationController import com.cobblemon.mod.common.pokemon.transformation.proxies.TransformationProxy import com.cobblemon.mod.common.util.cobblemonResource import com.cobblemon.mod.common.util.math.fromEulerXYZDegrees @@ -87,7 +86,7 @@ abstract class EvolutionSelectScreen { override fun handle(packet: RevertFormPacket, server: MinecraftServer, player: ServerPlayer) { - val pokemon = player.party().get(packet.pokemonUUID) ?: return + val pokemon = player.party()[packet.pokemonUUID] ?: return val form = pokemon.form - if (form is TemporaryForm) { - form.revert(pokemon) + if (form is TemporaryForm && form.toggleable && !pokemon.isBusy() && !player.isBusy()) { + val entity = pokemon.entity + if (entity != null) { + form.performRevert(pokemonEntity = entity, context = ActionEffectContext.fromPokemonEntity(actionEffect = form.revertActionEffect, pokemonEntity = entity)) + } else { + form.revert(pokemon) + } } } } \ No newline at end of file diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ClientTransformationController.kt b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ClientTransformationController.kt index 204ef8a9cf..1c97fe38e5 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ClientTransformationController.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ClientTransformationController.kt @@ -10,6 +10,7 @@ package com.cobblemon.mod.common.pokemon.transformation.controller import com.cobblemon.mod.common.api.net.NetworkPacket import com.cobblemon.mod.common.api.pokemon.transformation.TransformationDisplay +import com.cobblemon.mod.common.api.pokemon.transformation.TransformationLike import com.cobblemon.mod.common.api.pokemon.transformation.controller.TransformationController import com.cobblemon.mod.common.client.CobblemonClient import com.cobblemon.mod.common.pokemon.Pokemon @@ -44,6 +45,15 @@ abstract class ClientTransformationController( this.canStart() && this.any { it === transformation } + + /** + * Validates the [TransformationLike] currently active for this controller's [pokemon] and determines whether it can be + * reverted and if the pokemon is capable of reverting right now. + * + * @return whether the transformation can be reverted. + */ + abstract fun canRevert(): Boolean + // FROM MutableSet ------------------------------------------------------------------------------------------------ override fun add(element: T): Boolean { diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerEvolutionController.kt b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerEvolutionController.kt index d3da975e44..3b0e5645fb 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerEvolutionController.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerEvolutionController.kt @@ -37,7 +37,6 @@ class ServerEvolutionController( override fun addPacket(transformation: Evolution) = AddEvolutionPacket(this.pokemon, transformation) override fun clearPacket() = ClearEvolutionsPacket { this.pokemon } override fun removePacket(transformation: Evolution) = RemoveEvolutionPacket(this.pokemon, transformation) - override fun canRevert() = false // TODO evolutions are currently one-way, but modders may appreciate reversion support override fun isDuplicate(element: Evolution) = this.find { element.result.matches(it.result) } != null override fun start(transformation: Evolution) { diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerFormController.kt b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerFormController.kt index 006a805a15..77eecc8a61 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerFormController.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/controller/ServerFormController.kt @@ -38,7 +38,6 @@ class ServerFormController( override fun clearPacket() = ClearFormsPacket { this.pokemon } override fun removePacket(transformation: TemporaryForm) = RemoveFormPacket(this.pokemon, transformation) override fun isDuplicate(element: TemporaryForm) = this.find { it.name == element.name } != null - override fun canRevert(): Boolean = pokemon.form is TemporaryForm && (pokemon.form as TemporaryForm).toggleable override fun start(transformation: TemporaryForm) { if (!canStart(transformation)) return diff --git a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/form/TemporaryForm.kt b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/form/TemporaryForm.kt index 92994e0407..100a67d293 100644 --- a/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/form/TemporaryForm.kt +++ b/common/src/main/kotlin/com/cobblemon/mod/common/pokemon/transformation/form/TemporaryForm.kt @@ -8,16 +8,13 @@ package com.cobblemon.mod.common.pokemon.transformation.form -import com.bedrockk.molang.runtime.MoLangRuntime import com.cobblemon.mod.common.api.events.CobblemonEvents import com.cobblemon.mod.common.api.events.pokemon.form.FormCompleteEvent import com.cobblemon.mod.common.api.events.pokemon.form.FormDisplayEvent import com.cobblemon.mod.common.api.events.pokemon.form.FormTestedEvent -import com.cobblemon.mod.common.api.molang.MoLangFunctions.addStandardFunctions import com.cobblemon.mod.common.api.moves.animations.ActionEffectContext import com.cobblemon.mod.common.api.moves.animations.ActionEffectTimeline import com.cobblemon.mod.common.api.moves.animations.ActionEffects -import com.cobblemon.mod.common.api.moves.animations.UsersProvider import com.cobblemon.mod.common.api.pokemon.PokemonSpecies import com.cobblemon.mod.common.api.pokemon.transformation.Transformation import com.cobblemon.mod.common.api.pokemon.transformation.evolution.PreEvolution @@ -72,7 +69,10 @@ open class TemporaryForm ( ?: ActionEffects.actionEffects["generic_form".asIdentifierDefaultingNamespace()] ?: throw Exception() - + val revertActionEffect: ActionEffectTimeline + get() = ActionEffects.actionEffects[actionEffectIdentifier?.withPath { "revert" } ?: cobblemonResource("form_${name.lowercase()}_revert")] + ?: ActionEffects.actionEffects["generic_form_revert".asIdentifierDefaultingNamespace()] + ?: throw Exception() /* Temporary forms DO NOT have their own preevo/evolutions/forms and have to fallback on the parent. Because parentForm is not initialized until AFTER species registration, none of these properties are safe to access until [PokemonSpecies.observable] */ @@ -138,42 +138,48 @@ open class TemporaryForm ( pokemon.recall() } - val pokemonEntity = pokemon.entity - if (pokemonEntity != null) { - val runtime = MoLangRuntime() - runtime.environment.query.addStandardFunctions() - val providers: MutableList = mutableListOf(UsersProvider(pokemonEntity)) - val context = ActionEffectContext( - actionEffect = actionEffect, - runtime = runtime, - providers = providers, - level = pokemonEntity.level() - ) - this.perform(pokemonEntity, context) - } else { + val pokemonEntity = pokemon.entity ?: run { + pokemon.form = this fulfill(pokemon) + return } + + val context = ActionEffectContext.fromPokemonEntity( + actionEffect = actionEffect, + pokemonEntity = pokemonEntity + ) + + this.perform(pokemonEntity, context) } override fun fulfill(pokemon: Pokemon) { super.fulfill(pokemon) pokemon.form = this - pokemon.entity?.entityData?.set(PokemonEntity.TRANSFORMATION_STARTED, false) CobblemonEvents.FORM_COMPLETE.post(FormCompleteEvent(pokemon, this)) } - fun perform(pokemonEntity: PokemonEntity, context: ActionEffectContext) { + private fun doActionEffect(pokemonEntity: PokemonEntity, context: ActionEffectContext, afterAction: () -> Unit) { + context.tracker = pokemonEntity // Bind all of this to an entity so recalling the entity just kills all of this pokemonEntity.entityData.set(PokemonEntity.TRANSFORMATION_STARTED, true) val finishTransformation: () -> Unit = { if (pokemonEntity.isTransforming) { - fulfill(pokemonEntity.pokemon) + afterAction() + pokemonEntity.entityData.set(PokemonEntity.TRANSFORMATION_STARTED, false) } } context.holdRemovalCallbacks["transformation"] = finishTransformation - val future = actionEffect.run(context) + val future = context.actionEffect.run(context) future.thenRun(finishTransformation) } + fun perform(pokemonEntity: PokemonEntity, context: ActionEffectContext) { + doActionEffect(pokemonEntity, context) { fulfill(pokemonEntity.pokemon) } + } + + fun performRevert(pokemonEntity: PokemonEntity, context: ActionEffectContext) { + doActionEffect(pokemonEntity, context) { revert(pokemonEntity.pokemon) } + } + fun revert(pokemon: Pokemon) { if (toggleable) { pokemon.form = parentForm diff --git a/common/src/main/resources/data/cobblemon/action_effects/forms/generic_form_revert.json b/common/src/main/resources/data/cobblemon/action_effects/forms/generic_form_revert.json new file mode 100644 index 0000000000..631156e5c1 --- /dev/null +++ b/common/src/main/resources/data/cobblemon/action_effects/forms/generic_form_revert.json @@ -0,0 +1,26 @@ +{ + "timeline": [ + { + "type": "add_holds", + "holds": ["transformation"] + }, + { + "type": "entity_particles", + "effect": "cobblemon:statdown_actor", + "delay": 0.1, + "locators": ["root"] + }, + { + "type": "pause", + "pause": 1.0 + }, + { + "type": "remove_holds", + "holds": ["transformation"] + }, + { + "type": "pause", + "pause": 2.0 + } + ] +} \ No newline at end of file diff --git a/common/src/main/resources/data/cobblemon/action_effects/forms/instant_form.json b/common/src/main/resources/data/cobblemon/action_effects/forms/instant_form.json new file mode 100644 index 0000000000..2973967ad6 --- /dev/null +++ b/common/src/main/resources/data/cobblemon/action_effects/forms/instant_form.json @@ -0,0 +1,8 @@ +{ + "timeline": [ + { + "type": "remove_holds", + "holds": ["transformation"] + } + ] +} \ No newline at end of file