-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathTerastalMechanic.java
358 lines (332 loc) · 14.5 KB
/
TerastalMechanic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package pokecube.gimmicks.terastal;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;
import pokecube.api.PokecubeAPI;
import pokecube.api.entity.pokemob.ICanEvolve;
import pokecube.api.entity.pokemob.IPokemob;
import pokecube.api.entity.pokemob.PokemobCaps;
import pokecube.api.entity.pokemob.ai.GeneralStates;
import pokecube.api.entity.pokemob.commandhandlers.ChangeFormHandler;
import pokecube.api.entity.pokemob.commandhandlers.ChangeFormHandler.IChangeHandler;
import pokecube.api.events.init.PokemakeArgumentEvent;
import pokecube.api.events.pokemobs.ChangeForm;
import pokecube.api.events.pokemobs.HealEvent;
import pokecube.api.events.pokemobs.RecallEvent;
import pokecube.api.events.pokemobs.combat.MoveUse;
import pokecube.api.moves.utils.MoveApplication;
import pokecube.api.raids.RaidManager;
import pokecube.api.utils.PokeType;
import pokecube.core.PokecubeCore;
import pokecube.core.PokecubeItems;
import pokecube.core.entity.genetics.GeneticsManager;
import pokecube.core.eventhandlers.PokemobEventsHandler.MegaEvoTicker;
import pokecube.core.handlers.PokecubePlayerDataHandler;
import pokecube.core.network.pokemobs.PacketSyncGene;
import pokecube.gimmicks.terastal.TeraTypeGene.TeraType;
import pokecube.mixin.accessors.AttributeMaxAccessor;
import thut.api.ThutCaps;
import thut.api.entity.genetics.Alleles;
import thut.api.entity.genetics.Gene;
import thut.api.entity.genetics.GeneRegistry;
import thut.api.entity.genetics.IMobGenetics;
import thut.lib.TComponent;
/**
* Implementation of the Terastallizing mechanic. This is tracked per pokemob
* via genes, and this class contains the required code for attaching the genes,
* managing the types, etc. This is all arranged via calling
* {@link TerastalMechanic#init(FMLLoadCompleteEvent)}, and via the
* EventBusSubscriber
*
*/
@Mod.EventBusSubscriber(bus = Bus.MOD, modid = PokecubeCore.MODID)
public class TerastalMechanic
{
/**
* Setup and register tera type stuff.
*/
@SubscribeEvent
public static void init(FMLLoadCompleteEvent event)
{
// Register the genes
GeneRegistry.register(TeraTypeGene.class);
// Add listener for adding the STAB bonus when we are used.
PokecubeAPI.MOVE_BUS.addListener(EventPriority.LOW, false, TerastalMechanic::duringPreMoveUse);
// Add listener for removing tera when recalled
PokecubeAPI.POKEMOB_BUS.addListener(TerastalMechanic::onRecall);
// Add listener for removing tera cooldown when healed at a pokecenter
PokecubeAPI.POKEMOB_BUS.addListener(TerastalMechanic::onHeal);
// Add listener for removing tera cooldown when healed at a pokecenter
PokecubeAPI.POKEMOB_BUS.addListener(TerastalMechanic::onPokemake);
// Register a change handler for terastallizing
ChangeFormHandler.addChangeHandler(new Terastallizer());
// Register a raid type
RaidManager.registerBossType(new TerastalRaid());
var attr = Attributes.MAX_HEALTH;
if (attr instanceof AttributeMaxAccessor acc && acc.maxValue() < 1e8) acc.setMaxValue(1e8);
}
private static class Terastallizer implements IChangeHandler
{
@Override
public boolean handleChange(IPokemob pokemob)
{
return tryToggleTera(pokemob);
}
@Override
public int getPriority()
{
return 200;
}
@Override
public void onFail(IPokemob pokemob)
{
final LivingEntity owner = pokemob.getOwner();
if (owner instanceof ServerPlayer player)
{
CompoundTag data = PokecubePlayerDataHandler.getCustomDataTag(pokemob.getOwnerId());
int teraCooldown = data.getInt("pokecube:tera_cooldown");
if (teraCooldown == 0) thut.lib.ChatHelper.sendSystemMessage(player,
TComponent.translatable("pokecube.mega.noring", pokemob.getDisplayName()));
else if (teraCooldown > 0) thut.lib.ChatHelper.sendSystemMessage(player,
TComponent.translatable("pokemob.terastal.on_cooldown"));
else thut.lib.ChatHelper.sendSystemMessage(player,
TComponent.translatable("pokemob.terastal.not_yet", pokemob.getDisplayName()));
}
}
@Override
public String changeKey()
{
return "terastal";
}
}
/**
* @param mob - to get TeraType for
* @return the TeraType for the mob, null if not present.
*/
@Nullable
public static TeraType getTera(Entity mob)
{
Alleles<TeraType, Gene<TeraType>> genes = getTeraGenes(mob);
if (genes == null) return null;
return genes.getExpressed().getValue();
}
/**
*
* @param entity - to get tera genes for
* @return the tera genes for the entity, null if not present
*/
@Nullable
public static Alleles<TeraType, Gene<TeraType>> getTeraGenes(Entity entity)
{
final IMobGenetics genes = ThutCaps.getGenetics(entity);
if (genes == null) return null;
if (!genes.getKeys().contains(GeneticsManager.TERAGENE))
{
// Initialise it for the mob here.
Alleles<TeraType, Gene<TeraType>> alleles = new Alleles<>(genes);
Gene<TeraType> gene1 = new TeraTypeGene().mutate();
Gene<TeraType> gene2 = new TeraTypeGene().mutate();
IPokemob pokemob = PokemobCaps.getPokemobFor(entity);
if (pokemob != null)
{
gene1.getValue().teraType = pokemob.getType1();
gene2.getValue().teraType = pokemob.getType2();
if (gene2.getValue().teraType == PokeType.unknown) gene2.getValue().teraType = pokemob.getType1();
}
alleles.setAllele(0, gene1);
alleles.setAllele(1, gene2);
alleles.getExpressed();
genes.getAlleles().put(GeneticsManager.TERAGENE, alleles);
if (entity.getLevel() instanceof ServerLevel) PacketSyncGene.syncGeneToTracking(entity, alleles);
}
try
{
Alleles<TeraType, Gene<TeraType>> alleles = genes.getAlleles(GeneticsManager.TERAGENE);
if (alleles == null) return null;
return alleles;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static void doTera(IPokemob pokemob)
{
Alleles<TeraType, Gene<TeraType>> genes = getTeraGenes(pokemob.getEntity());
if (pokemob.isPlayerOwned())
{
CompoundTag data = PokecubePlayerDataHandler.getCustomDataTag(pokemob.getOwnerId());
data.putInt("pokecube:tera_cooldown", 1);
PokecubePlayerDataHandler.saveCustomData(pokemob.getOwnerId().toString());
}
Component mess = TComponent.translatable("pokemob.terastal.command.transform", pokemob.getDisplayName());
pokemob.displayMessageToOwner(mess);
mess = TComponent.translatable("pokemob.terastal.success", pokemob.getDisplayName());
MegaEvoTicker.scheduleChange(PokecubeCore.getConfig().evolutionTicks, pokemob.getPokedexEntry(), pokemob, mess,
() ->
{
// Flag as evolving for animation effects
pokemob.setGeneralState(GeneralStates.EVOLVING, true);
pokemob.setGeneralState(GeneralStates.EXITINGCUBE, false);
pokemob.setEvolutionTicks(PokecubeCore.getConfig().evolutionTicks + 50);
pokemob.setEvolutionStack(PokecubeItems.getStack(ICanEvolve.EVERSTONE));
PokecubeAPI.POKEMOB_BUS.post(new ChangeForm.Pre(pokemob));
if (pokemob.isPlayerOwned())
{
CompoundTag data = PokecubePlayerDataHandler.getCustomDataTag(pokemob.getOwnerId());
data.putInt("pokecube:tera_cooldown", 1);
PokecubePlayerDataHandler.saveCustomData(pokemob.getOwnerId().toString());
}
genes.getExpressed().getValue().isTera = true;
PacketSyncGene.syncGeneToTracking(pokemob.getEntity(), genes);
}, () -> {
PokecubeAPI.POKEMOB_BUS.post(new ChangeForm.Post(pokemob));
pokemob.setGeneralState(GeneralStates.EVOLVING, false);
pokemob.setEvolutionStack(ItemStack.EMPTY);
});
}
/**
* Marks the use of terastallization. This sets the pokemob's owner's
* cooldown for using tera to 1, meaning they need to reset it somehow, say
* via healing at a pokecenter, sleeping in a bed, or whatever else we set
* to reset it.
*
* @param pokemob - the pokemob trying to terastallize
* @return whether we did terastallize
*/
public static boolean tryToggleTera(IPokemob pokemob)
{
if (!(pokemob.getEntity().getLevel() instanceof ServerLevel)) return false;
Alleles<TeraType, Gene<TeraType>> genes = getTeraGenes(pokemob.getEntity());
if (genes == null) return false;
if (genes.getExpressed().getValue().isTera)
{
Component mess = TComponent.translatable("pokemob.terastal.command.revert", pokemob.getDisplayName());
pokemob.displayMessageToOwner(mess);
mess = TComponent.translatable("pokemob.terastal.revert", pokemob.getDisplayName());
MegaEvoTicker.scheduleChange(PokecubeCore.getConfig().evolutionTicks, pokemob.getPokedexEntry(), pokemob,
mess, () ->
{
// Flag as evolving for animation effects
pokemob.setGeneralState(GeneralStates.EVOLVING, true);
pokemob.setGeneralState(GeneralStates.EXITINGCUBE, false);
pokemob.setEvolutionTicks(PokecubeCore.getConfig().evolutionTicks + 50);
pokemob.setEvolutionStack(PokecubeItems.getStack(ICanEvolve.EVERSTONE));
PokecubeAPI.POKEMOB_BUS.post(new ChangeForm.Pre(pokemob));
}, () -> {
genes.getExpressed().getValue().isTera = false;
PacketSyncGene.syncGeneToTracking(pokemob.getEntity(), genes);
PokecubeAPI.POKEMOB_BUS.post(new ChangeForm.Post(pokemob));
pokemob.setGeneralState(GeneralStates.EVOLVING, false);
pokemob.setEvolutionStack(ItemStack.EMPTY);
});
return true;
}
boolean canTera = !pokemob.isPlayerOwned();
if (!canTera)
{
CompoundTag data = PokecubePlayerDataHandler.getCustomDataTag(pokemob.getOwnerId());
canTera = data.getInt("pokecube:tera_cooldown") == 0;
}
if (canTera)
{
doTera(pokemob);
}
return canTera;
}
/**
* This handles processing the tera type from the nbt in the pokemake
* arguments.
*
* @param event
*/
private static final void onPokemake(PokemakeArgumentEvent event)
{
String type = event.getNbt().getString("tera_type");
var tera = getTera(event.getPokemob().getEntity());
if (!type.isBlank())
{
try
{
PokeType tera_type = PokeType.getType(type);
if (tera != null) tera.teraType = tera_type;
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (event.getNbt().getBoolean("is_tera"))
{
tera.isTera = true;
}
}
/**
* This clears the isTera state for the pokemob, ie breaks the
* terastallization when recalled.
*/
private static final void onHeal(HealEvent.Post event)
{
if (event.getContext().owner() instanceof ServerPlayer player && event.getContext().fromHealer())
{
CompoundTag data = PokecubePlayerDataHandler.getCustomDataTag(player);
data.putInt("pokecube:tera_cooldown", 0);
PokecubePlayerDataHandler.saveCustomData(player);
}
}
/**
* This clears the isTera state for the pokemob, ie breaks the
* terastallization when recalled.
*/
private static final void onRecall(RecallEvent.Post event)
{
TeraType type = getTera(event.recalled.getEntity());
if (type != null && type.isTera)
{
type.isTera = false;
}
}
/**
* This applies the bonus STAB, and the damage boost for low powered moves.
* It also increments a counter for the owner (if present), which is used to
* determine if the owner can terastallize their pokemob.
*/
private static final void duringPreMoveUse(MoveUse.DuringUse.Pre evt)
{
final MoveApplication move = evt.getPacket();
final IPokemob attacker = move.getUser();
TeraType tera = getTera(attacker.getEntity());
// Here we apply the effects to the move when terastallized.
if (tera != null && tera.isTera)
{
boolean originalType = move.type == attacker.originalType1() || move.type == attacker.originalType2();
if (originalType) move.stab = true;
if (attacker.isType(move.type)) move.stabFactor = 2.0f;
if (move.pwr > 0 && move.pwr < 60) move.pwr = 60;
}
// Here we increment the tera counter if it is negative. Raids can set
// it negative to require a delay before use.
if (move.pwr > 0 && attacker.isPlayerOwned())
{
CompoundTag data = PokecubePlayerDataHandler.getCustomDataTag(attacker.getOwnerId());
int c;
if ((c = data.getInt("pokecube:tera_cooldown")) < 0)
{
data.putInt("pokecube:tera_cooldown", c + 1);
PokecubePlayerDataHandler.saveCustomData(attacker.getOwnerId().toString());
}
}
}
}