11package generations.gg.generations.core.generationscore.common.battle
22
3+ import com.cobblemon.mod.common.CobblemonNetwork
4+ import com.cobblemon.mod.common.api.abilities.Abilities
35import com.cobblemon.mod.common.api.abilities.Ability
46import com.cobblemon.mod.common.api.battles.interpreter.BattleMessage
57import com.cobblemon.mod.common.api.battles.model.PokemonBattle
6- import com.cobblemon.mod.common.api.events. battles.instruction.FormeChangeEvent
7- import com.cobblemon.mod.common.api.events.battles.instruction.MegaEvolutionEvent
8+ import com.cobblemon.mod.common.api.battles.model.actor.BattleActor
9+ import com.cobblemon.mod.common.api.events.battles.BattleStartedPreEvent
810import com.cobblemon.mod.common.api.events.battles.instruction.TerastallizationEvent
11+ import com.cobblemon.mod.common.api.moves.Moves
912import com.cobblemon.mod.common.api.pokemon.feature.FlagSpeciesFeature
1013import com.cobblemon.mod.common.api.pokemon.feature.SpeciesFeature
1114import com.cobblemon.mod.common.api.pokemon.feature.StringSpeciesFeature
12- import com.cobblemon.mod.common.entity.pokemon.PokemonEntity
15+ import com.cobblemon.mod.common.battles.pokemon.BattlePokemon
16+ import com.cobblemon.mod.common.net.messages.client.battle.BattleInitializePacket
1317import com.cobblemon.mod.common.net.messages.client.battle.BattleTransformPokemonPacket
1418import com.cobblemon.mod.common.pokemon.Pokemon
15- import com.mojang.datafixers.util.Unit
16- import generations.gg.generations.core.generationscore.common.GenerationsCore
19+ import com.cobblemon.mod.common.util.server
20+ import generations.gg.generations.core.generationscore.common.util.replaceMove
21+ import java.util.UUID
22+
23+ import net.minecraft.server.level.ServerPlayer
1724
1825object GenerationsInstructionProcessor {
19- private var originalAbility: Ability ? = null
2026
2127 @JvmStatic
2228 fun processFormeChangeInstruction (battle : PokemonBattle , message : BattleMessage ) {
@@ -32,7 +38,8 @@ object GenerationsInstructionProcessor {
3238
3339 val name = s3[1 ]
3440
35- originalAbility = battlePokemon.originalPokemon.ability
41+ val ability = battlePokemon.originalPokemon.ability.name
42+ battlePokemon.originalPokemon.persistentData.putString(" original_ability" , ability)
3643
3744 if (effectedPokemon.form.name.equals(" Dusk-Mane" )) {
3845 battlePokemon.originalPokemon.persistentData.putString(" necro_fusion" , " dusk" )
@@ -41,7 +48,7 @@ object GenerationsInstructionProcessor {
4148 }
4249
4350 var pair: Pair <String , Any > = when (name) {
44- " ash" -> " ash" to true
51+ // "ash" -> "ash" to true
4552 " mega" -> " mega" to true
4653 " mega-x" -> " mega_x" to true
4754 " mega-y" -> " mega_y" to true
@@ -71,6 +78,40 @@ object GenerationsInstructionProcessor {
7178 }
7279 }
7380
81+ @JvmStatic
82+ fun processTerastallization (terastallizationEvent : TerastallizationEvent ) {
83+ val battle = terastallizationEvent.battle
84+ val teraCheck = FlagSpeciesFeature (" terastal_active" , true )
85+ val pokemon = terastallizationEvent.pokemon
86+ terastallizationEvent.pokemon.effectedPokemon.applyBattleFeature(teraCheck)
87+
88+ val active = battle.activePokemon.find {
89+ it.battlePokemon?.uuid == pokemon.uuid || it.battlePokemon?.effectedPokemon?.uuid == pokemon.uuid
90+ }
91+ val pnx = active?.getPNX()
92+ val updated = pokemon
93+ if (pnx != null ) {
94+ (battle.playerUUIDs + battle.spectators).forEach { viewer ->
95+ val isAlly = battle.isAllied(viewer, pokemon.actor)
96+ println (" UUID: " + viewer)
97+ val packet = BattleTransformPokemonPacket (pnx, updated, isAlly)
98+ getPlayerFromUUID(viewer)?.let { CobblemonNetwork .sendPacketToPlayer(it, packet) }
99+ }
100+ }
101+
102+ battle.dispatchWaitingToFront(2.5f ) { Unit }
103+ }
104+
105+ @JvmStatic
106+ fun preBattleChanges (battleStartedPreEvent : BattleStartedPreEvent ) {
107+ val battle = battleStartedPreEvent.battle
108+ for (actors in battle.actors) {
109+ for (battlePokemon in actors.pokemonList) {
110+ doggoMoveChanger(battlePokemon)
111+ }
112+ }
113+ }
114+
74115 @JvmStatic
75116 fun processBattleEnd (battle : PokemonBattle ) {
76117 battle.actors.forEach { actor ->
@@ -97,32 +138,48 @@ object GenerationsInstructionProcessor {
97138 battlePokemon.originalPokemon.updateAspects()
98139 }
99140 }
100- battlePokemon.originalPokemon.restoreAbility(tempAbility, originalAbility, name)
141+ doggoMoveChanger(battlePokemon)
142+ battlePokemon.originalPokemon.restoreAbility(tempAbility)
101143 }
102144 }
103145 }
104146}
105147
106148private fun Pokemon.removeBattleFeature () {
107149 val data = this .persistentData
108- val name = if (data.contains(" form_name" )) data.getString(" form_name" ).also { data.remove(" form_name" ) } else return
109- val value = if (data.contains(" form_value" )) data.getString(" form_value" ).also { data.remove(" form_value" ) } else null
110150
111- if (value == null ) {
112- features.removeIf { it.name == name }
113- } else {
114- val feature = features.firstOrNull { it.name == name } ? : return
151+ if (data.contains(" terastal" )) {
152+ val name = data.getString(" terastal" )
153+ features.removeIf {it.name == name}
154+ data.remove(" terastal" )
155+ data.remove(" tera_type" )
156+ }
157+
158+ if (data.contains(" form_name" )) {
159+ val name = data.getString(" form_name" ).also { data.remove(" form_name" ) }
160+ val value = if (data.contains(" form_value" )) data.getString(" form_value" ).also { data.remove(" form_value" ) } else null
115161
116- if (feature is StringSpeciesFeature ) {
117- feature.value = value
162+ if (value == null ) {
163+ features.removeIf { it.name == name }
164+ } else {
165+ val feature = features.firstOrNull { it.name == name } ? : return
166+
167+ if (feature is StringSpeciesFeature ) {
168+ feature.value = value
169+ }
118170 }
119171 }
120172
121173 updateAspects()
122174}
123175
124176private fun Pokemon.applyBattleFeature (feature : SpeciesFeature ) {
125- this .persistentData.putString(" form_name" , feature.name)
177+ if (feature.name.equals(" terastal_active" )) {
178+ this .persistentData.putString(" terastal" , feature.name)
179+ this .persistentData.putString(" tera_type" , this .teraType.id.path)
180+ } else {
181+ this .persistentData.putString(" form_name" , feature.name)
182+ }
126183 if (feature is StringSpeciesFeature ) {
127184 this .persistentData.putString(" form_value" , feature.value)
128185 feature.apply (this )
@@ -131,14 +188,71 @@ private fun Pokemon.applyBattleFeature(feature: SpeciesFeature) {
131188 }
132189}
133190
134- private fun Pokemon.restoreAbility (tempAbility : Ability , originalAbility : Ability ? , name : String ) {
135- if (name == " mega" || name == " mega_x" || name == " mega_y" ) {
136- if (tempAbility != originalAbility) {
137- originalAbility?.let { ability ->
138- this .updateAbility(ability)
139- } ? : run {
140- GenerationsCore .LOGGER .warn(" Original Ability is null" )
191+ private fun Pokemon.restoreAbility (tempAbility : Ability ) {
192+ val abilityId = this .persistentData.getString(" original_ability" )
193+ val template = Abilities .get(abilityId)
194+
195+ if (template != null ) {
196+ val ability = template.create()
197+ if (tempAbility.template != template) {
198+ this .updateAbility(ability)
199+ }
200+ }
201+ }
202+
203+ private fun PokemonBattle.isAllied (uuid : UUID , actor : BattleActor ? ): Boolean {
204+ if (actor == null ) return false
205+ return actors.any {
206+ it.getPlayerUUIDs().contains(uuid) && it.getSide() == actor.getSide()
207+ }
208+ }
209+
210+ private fun getPlayerFromUUID (uuid : UUID ): ServerPlayer ? {
211+ return server()?.playerList?.getPlayer(uuid)
212+ }
213+
214+ private fun doggoMoveChanger (battlePokemon : BattlePokemon ) {
215+ val effectedPokemon = battlePokemon.effectedPokemon
216+ val ironHead = Moves .getByNameOrDummy(" ironhead" )
217+ val behemothBlade = Moves .getByNameOrDummy(" behemothblade" )
218+ val behemothBash = Moves .getByNameOrDummy(" behemothbash" )
219+
220+ val speciesName = battlePokemon.originalPokemon.species.name.lowercase()
221+
222+ val hasIronHead = battlePokemon.moveSet.any {move -> move.template.name.equals(" ironhead" )}
223+ val hasBehemoth = battlePokemon.moveSet.any { move ->
224+ move.template.name.lowercase() in listOf (" behemothblade" , " behemothbash" )
225+ }
226+ if (effectedPokemon.aspects.contains(" crowned" )) {
227+ val benchedMoves = effectedPokemon.benchedMoves
228+
229+ println (" PRE-CLEAR" )
230+ benchedMoves.forEach {
231+ println (" benchedmove: " + it.moveTemplate.name)
232+ }
233+
234+ benchedMoves.clear()
235+
236+ println (" POST-CLEAR" )
237+ benchedMoves.forEach {
238+ println (" benchedmove: " + it.moveTemplate.name)
239+ }
240+ if (hasIronHead) {
241+ when (speciesName) {
242+ " zacian" -> effectedPokemon.replaceMove(" ironhead" , " behemothblade" )
243+ " zamazenta" -> effectedPokemon.replaceMove(" ironhead" , " behemothbash" )
244+ }
245+ } else if (hasBehemoth) {
246+ when (speciesName) {
247+ " zacian" -> {
248+ effectedPokemon.replaceMove(" behemothblade" , " ironhead" )
249+ }
250+ " zamazenta" -> {
251+ effectedPokemon.replaceMove(" behemothbash" , " ironhead" )
252+ }
141253 }
142254 }
143255 }
144256}
257+
258+
0 commit comments