-
Notifications
You must be signed in to change notification settings - Fork 7
/
Dragon.java
414 lines (315 loc) · 14.8 KB
/
Dragon.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package insane96mcp.progressivebosses.events.entities;
import insane96mcp.progressivebosses.events.entities.ai.DragonMinionAttackGoal;
import insane96mcp.progressivebosses.events.entities.ai.DragonMinionAttackNearestGoal;
import insane96mcp.progressivebosses.setup.ModConfig;
import insane96mcp.progressivebosses.utils.MathRandom;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EntityPredicate;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.entity.ai.goal.NearestAttackableTargetGoal;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.entity.boss.dragon.phase.PhaseType;
import net.minecraft.entity.item.ExperienceOrbEntity;
import net.minecraft.entity.monster.EndermiteEntity;
import net.minecraft.entity.monster.ShulkerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.storage.loot.LootTables;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import java.util.ArrayList;
import java.util.List;
public class Dragon {
public static void setStats(EntityJoinWorldEvent event) {
if (event.getWorld().getDimension().getType() != DimensionType.THE_END)
return;
if (!(event.getEntity() instanceof EnderDragonEntity))
return;
EnderDragonEntity dragon = (EnderDragonEntity) event.getEntity();
CompoundNBT tags = dragon.getPersistentData();
boolean alreadyProcessed = tags.getBoolean("progressivebosses:processed");
if (alreadyProcessed)
return;
tags.putBoolean("progressivebosses:processed", true);
int radius = 160;
BlockPos pos1 = new BlockPos(-radius, -radius, -radius);
BlockPos pos2 = new BlockPos(radius, radius, radius);
AxisAlignedBB bb = new AxisAlignedBB(pos1, pos2);
List<ServerPlayerEntity> players = event.getWorld().getEntitiesWithinAABB(ServerPlayerEntity.class, bb);
if (players.size() == 0)
return;
int c;
int eggsToDrop = 0;
float killedCount = 0;
for (ServerPlayerEntity player : players) {
CompoundNBT playerTags = player.getPersistentData();
c = playerTags.getInt("progressivebosses:killed_dragons");
if (c == 0) {
dragon.getFightManager().previouslyKilled = false;
eggsToDrop++;
}
killedCount += c;
}
tags.putInt("progressivebosses:eggs_to_drop", eggsToDrop);
if (killedCount == 0)
return;
if (!ModConfig.Dragon.General.sumKilledDragonsDifficulty.get() && killedCount > 0)
killedCount /= players.size();
setHealth(dragon, killedCount);
tags.putFloat("progressivebosses:difficulty", killedCount);
}
private static void setHealth(EnderDragonEntity dragon, float killedCount) {
IAttributeInstance attribute = dragon.getAttribute(SharedMonsterAttributes.MAX_HEALTH);
attribute.setBaseValue(attribute.getBaseValue() + (killedCount * ModConfig.Dragon.Health.bonusPerDifficulty.get()));
dragon.setHealth((float) attribute.getBaseValue());
}
private static void dropMoreExperience(EnderDragonEntity dragon, World world) {
if (dragon.deathTicks != 150)
return;
CompoundNBT tags = dragon.getPersistentData();
int radius = 160;
BlockPos pos1 = new BlockPos(-radius, -radius, -radius);
BlockPos pos2 = new BlockPos(radius, radius, radius);
AxisAlignedBB bb = new AxisAlignedBB(pos1, pos2);
List<ServerPlayerEntity> players = world.getEntitiesWithinAABB(ServerPlayerEntity.class, bb);
int playersAround = players.size();
int playersFirstTime = tags.getInt("progressivebosses:eggs_to_drop");
boolean previouslyKilledDragon = false;
if (dragon.getFightManager() != null)
previouslyKilledDragon = dragon.getFightManager().hasPreviouslyKilledDragon();
float difficulty = tags.getFloat("progressivebosses:difficulty");
int xp = 0;
if (!previouslyKilledDragon) {
playersFirstTime--;
//Add XP for players that killed the first time
if (playersAround > 1)
xp += 12000 * playersFirstTime;
//Add XP per more than 1 player around
if (playersAround > 1)
xp += (500 * ((ModConfig.Dragon.Rewards.bonusExperience.get() + 1) * difficulty / 100f) * (playersAround - playersFirstTime));
} else {
//Add bonus XP for the normal experience drop
xp += (int) (500 * (ModConfig.Dragon.Rewards.bonusExperience.get() * difficulty / 100f));
//Add XP per more than 1 player around
if (playersAround > 1)
xp += (500 * ((ModConfig.Dragon.Rewards.bonusExperience.get() + 1) * difficulty / 100f) * (playersAround - 1));
}
while (xp > 0) {
int i = ExperienceOrbEntity.getXPSplit(xp);
xp -= i;
world.addEntity(new ExperienceOrbEntity(dragon.world, dragon.posX, dragon.posY, dragon.posZ, i));
}
}
public static void onDeath(LivingDeathEvent event) {
if (!(event.getEntity() instanceof EnderDragonEntity))
return;
EnderDragonEntity dragon = (EnderDragonEntity) event.getEntity();
CompoundNBT tags = dragon.getPersistentData();
if (tags.getBoolean("progressivebosses:has_been_killed"))
return;
tags.putBoolean("progressivebosses:has_been_killed", true);
int radius = 160;
BlockPos pos1 = new BlockPos(-radius, -radius, -radius);
BlockPos pos2 = new BlockPos(radius, radius, radius);
AxisAlignedBB bb = new AxisAlignedBB(pos1, pos2);
List<ServerPlayerEntity> players = dragon.world.getEntitiesWithinAABB(ServerPlayerEntity.class, bb);
if (players.size() == 0)
return;
int c;
for (ServerPlayerEntity player : players) {
CompoundNBT playerTags = player.getPersistentData();
c = playerTags.getInt("progressivebosses:killed_dragons");
if (c >= ModConfig.Dragon.General.maxDifficulty.get())
continue;
playerTags.putInt("progressivebosses:killed_dragons", c + 1);
}
}
public static void onPlayerDamage(LivingHurtEvent event) {
if (!(event.getSource().getImmediateSource() instanceof EnderDragonEntity))
return;
EnderDragonEntity dragon = (EnderDragonEntity) event.getSource().getImmediateSource();
CompoundNBT tags = dragon.getPersistentData();
float difficulty = tags.getFloat("progressivebosses:difficulty");
if (difficulty == 0)
return;
event.setAmount((float) (event.getAmount() * (1 + difficulty * (ModConfig.Dragon.Attack.bonusAttackDamage.get() / 100.0))));
}
private static void dropEgg(EnderDragonEntity dragon, World world) {
if (dragon.deathTicks != 100)
return;
CompoundNBT tags = dragon.getPersistentData();
int eggsToDrop = tags.getInt("progressivebosses:eggs_to_drop");
if (dragon.getFightManager() != null && !dragon.getFightManager().hasPreviouslyKilledDragon()) {
eggsToDrop--;
}
for (int i = 0; i < eggsToDrop; i++) {
world.setBlockState(new BlockPos(0, 255 - i, 0), Blocks.DRAGON_EGG.getDefaultState());
}
}
public static void update(LivingUpdateEvent event) {
if (!(event.getEntity() instanceof EnderDragonEntity))
return;
World world = event.getEntity().world;
EnderDragonEntity dragon = (EnderDragonEntity) event.getEntity();
CompoundNBT tags = dragon.getPersistentData();
chargePlayer(dragon);
spawnEndermites(dragon, world);
spawnShulkers(dragon, world);
heal(dragon, tags);
dropEgg(dragon, world);
dropMoreExperience(dragon, world);
}
private static void chargePlayer(EnderDragonEntity dragon) {
CompoundNBT tags = dragon.getPersistentData();
float difficulty = tags.getFloat("progressivebosses:difficulty");
double chance = (ModConfig.Dragon.Attack.chargePlayerBaseChance.get() / 100.0) / 24;
chance *= difficulty;
int crystalsAlive = dragon.getFightManager().getNumAliveCrystals() + 1;
chance *= (1f / crystalsAlive);
if (Math.random() < chance && dragon.getPhaseManager().getCurrentPhase().getType() == PhaseType.HOLDING_PATTERN) {
AxisAlignedBB axisAlignedBB = new AxisAlignedBB(-128, -128, -128, 128, 128, 128);
ServerPlayerEntity player = dragon.world.getClosestEntityWithinAABB(ServerPlayerEntity.class, new EntityPredicate().setDistance(128.0), dragon, dragon.posX, dragon.posY + (double) dragon.getEyeHeight(), dragon.posZ, axisAlignedBB);
if (player != null) {
dragon.getPhaseManager().setPhase(PhaseType.CHARGING_PLAYER);
(dragon.getPhaseManager().getPhase(PhaseType.CHARGING_PLAYER)).setTarget(new Vec3d(player.posX, player.posY, player.posZ));
}
}
}
private static void heal(EnderDragonEntity dragon, CompoundNBT tags) {
if (ModConfig.Dragon.Health.maximumBonusRegen.get() == 0.0f)
return;
if (dragon.ticksExisted % 20 != 0)
return;
float difficulty = tags.getFloat("progressivebosses:difficulty");
if (difficulty == 0)
return;
double maxHeal = ModConfig.Dragon.Health.maximumBonusRegen.get();
double heal = difficulty * ModConfig.Dragon.Health.bonusRegenPerSpawned.get();
if (heal > maxHeal)
heal = maxHeal;
float health = dragon.getHealth();
if (dragon.getHealth() < dragon.getMaxHealth() && dragon.getHealth() > 0.0f)
dragon.setHealth((float) (health + heal));
}
private static void spawnEndermites(EnderDragonEntity dragon, World world) {
if (ModConfig.Dragon.Larvae.maxSpawned.get() == 0)
return;
CompoundNBT tags = dragon.getPersistentData();
//Mobs Properties Randomness
tags.putBoolean("mobspropertiesrandomness:checked", true);
float difficulty = tags.getFloat("progressivebosses:difficulty");
if (difficulty < ModConfig.Dragon.Larvae.difficultyToSpawnOneMore.get())
return;
int cooldown = tags.getInt("progressivebosses:endermites_cooldown");
if (cooldown > 0) {
tags.putInt("progressivebosses:endermites_cooldown", cooldown - 1);
} else {
int cooldownReduction = (int) (difficulty * ModConfig.Dragon.Larvae.cooldownReduction.get());
cooldown = MathRandom.getInt(world.rand, ModConfig.Dragon.Larvae.minCooldown.get() - cooldownReduction, ModConfig.Dragon.Larvae.maxCooldown.get() - cooldownReduction);
tags.putInt("progressivebosses:endermites_cooldown", cooldown);
for (int i = 1; i <= difficulty; i++) {
if (i / ModConfig.Dragon.Larvae.difficultyToSpawnOneMore.get() > ModConfig.Dragon.Larvae.maxSpawned.get())
break;
if (i % ModConfig.Dragon.Larvae.difficultyToSpawnOneMore.get() == 0) {
EndermiteEntity endermite = new EndermiteEntity(EntityType.ENDERMITE, world);
CompoundNBT endermiteTags = endermite.getPersistentData();
//Scaling Health
endermiteTags.putShort("scalinghealth:difficulty", (short) -1);
float angle = world.rand.nextFloat() * (float) Math.PI * 2f;
float x = (float) (Math.cos(angle) * 3.15f);
float z = (float) (Math.sin(angle) * 3.15f);
int y = world.getHeight(Heightmap.Type.MOTION_BLOCKING, new BlockPos(x, 255, z)).getY();
IAttributeInstance attribute = endermite.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED);
attribute.setBaseValue(attribute.getBaseValue() * 1.5f);
attribute = endermite.getAttribute(SharedMonsterAttributes.FOLLOW_RANGE);
attribute.setBaseValue(96f);
attribute = endermite.getAttribute(SharedMonsterAttributes.MAX_HEALTH);
attribute.setBaseValue(4);
endermite.setHealth((float) attribute.getBaseValue());
endermite.setPosition(x, y, z);
endermite.setCustomName(new TranslationTextComponent("dragon.larva"));
//Stream<PrioritizedGoal> runningGoals = endermite.goalSelector.getRunningGoals();
ArrayList<Goal> toRemove = new ArrayList<>();
endermite.targetSelector.goals.forEach(goal -> {
if (goal.getGoal() instanceof NearestAttackableTargetGoal)
toRemove.add(goal.getGoal());
});
for (Goal goal : toRemove) {
endermite.targetSelector.removeGoal(goal);
}
endermite.targetSelector.addGoal(2, new NearestAttackableTargetGoal(endermite, PlayerEntity.class, false));
endermite.experienceValue = 1;
world.addEntity(endermite);
}
}
}
}
private static void spawnShulkers(EnderDragonEntity dragon, World world) {
if (ModConfig.Dragon.Minion.difficultyToSpawn.get() <= 0)
return;
CompoundNBT tags = dragon.getPersistentData();
//Mobs Properties Randomness
tags.putBoolean("mpr:prevent_processing", true);
float difficulty = tags.getFloat("progressivebosses:difficulty");
if (difficulty < ModConfig.Dragon.Minion.difficultyToSpawn.get())
return;
int cooldown = tags.getInt("progressivebosses:shulkers_cooldown");
if (cooldown > 0) {
tags.putInt("progressivebosses:shulkers_cooldown", cooldown - 1);
} else {
int cooldownReduction = (int) (difficulty * ModConfig.Dragon.Minion.cooldownReduction.get());
cooldown = MathRandom.getInt(world.rand, ModConfig.Dragon.Minion.minCooldown.get() - cooldownReduction, ModConfig.Dragon.Minion.maxCooldown.get() - cooldownReduction);
tags.putInt("progressivebosses:shulkers_cooldown", cooldown);
ShulkerEntity shulker = new ShulkerEntity(EntityType.SHULKER, world);
CompoundNBT shulkerTags = shulker.getPersistentData();
//Scaling Health
shulkerTags.putShort("scalinghealth:difficulty", (short) -1);
float angle = world.rand.nextFloat() * (float) Math.PI * 2f;
float x = (float) (Math.cos(angle) * (MathRandom.getFloat(world.rand, 15f, 40f)));
float z = (float) (Math.sin(angle) * (MathRandom.getFloat(world.rand, 15f, 40f)));
float y = world.getHeight(Heightmap.Type.MOTION_BLOCKING, new BlockPos(x, 255, z)).getY();
IAttributeInstance followRange = shulker.getAttribute(SharedMonsterAttributes.FOLLOW_RANGE);
followRange.setBaseValue(64f);
shulker.setPosition(x, y, z);
CompoundNBT compound = shulker.serializeNBT();
compound.putByte("Color", (byte) 15);
shulker.deserializeNBT(compound);
shulker.setCustomName(new TranslationTextComponent("dragon.minion"));
ArrayList<Goal> toRemove = new ArrayList<>();
shulker.goalSelector.goals.forEach(goal -> {
if (goal.getGoal() instanceof ShulkerEntity.AttackGoal)
toRemove.add(goal.getGoal());
});
for (Goal goal : toRemove) {
shulker.goalSelector.removeGoal(goal);
}
toRemove.clear();
shulker.targetSelector.goals.forEach(goal -> {
if (goal.getGoal() instanceof NearestAttackableTargetGoal)
toRemove.add(goal.getGoal());
});
for (Goal goal : toRemove) {
shulker.goalSelector.removeGoal(goal);
}
toRemove.clear();
shulker.goalSelector.addGoal(1, new DragonMinionAttackGoal(shulker));
shulker.targetSelector.addGoal(2, new DragonMinionAttackNearestGoal(shulker));
shulker.deathLootTable = LootTables.EMPTY;
shulker.experienceValue = 2;
world.addEntity(shulker);
}
}
}