/
ButterflyEgg.java
488 lines (440 loc) · 16.5 KB
/
ButterflyEgg.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package com.bokmcdok.butterflies.world.entity.animal;
import com.bokmcdok.butterflies.ButterfliesMod;
import com.bokmcdok.butterflies.world.ButterflyData;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
public class ButterflyEgg extends DirectionalCreature {
// The unique IDs that are used to reference a butterfly egg entity.
public static final String ADMIRAL_NAME = "admiral_egg";
public static final String BUCKEYE_NAME = "buckeye_egg";
public static final String CABBAGE_NAME = "cabbage_egg";
public static final String CHALKHILL_NAME = "chalkhill_egg";
public static final String CLIPPER_NAME = "clipper_egg";
public static final String COMMON_NAME = "common_egg";
public static final String EMPEROR_NAME = "emperor_egg";
public static final String FORESTER_NAME = "forester_egg";
public static final String GLASSWING_NAME = "glasswing_egg";
public static final String HAIRSTREAK_NAME = "hairstreak_egg";
public static final String HEATH_NAME = "heath_egg";
public static final String LONGWING_NAME = "longwing_egg";
public static final String MONARCH_NAME = "monarch_egg";
public static final String MORPHO_NAME = "morpho_egg";
public static final String RAINBOW_NAME = "rainbow_egg";
public static final String SWALLOWTAIL_NAME = "swallowtail_egg";
// The size of the butterfly egg.
private final ButterflyData.Size size;
// The butterfly egg item location.
private final ResourceLocation butterflyEggItem;
/**
* Create a Admiral butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createAdmiral(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("admiral", entityType, level);
}
/**
* Create a Buckeye butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createBuckeye(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("buckeye", entityType, level);
}
/**
* Create a Cabbage butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createCabbage(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("cabbage", entityType, level);
}
/**
* Create a Chalkhill butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createChalkhill(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("chalkhill", entityType, level);
}
/**
* Create a Clipper butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createClipper(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("clipper", entityType, level);
}
/**
* Create a Common butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createCommon(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("common", entityType, level);
}
/**
* Create an Emperor butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createEmperor(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("emperor", entityType, level);
}
/**
* Create a Forester butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createForester(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("forester", entityType, level);
}
/**
* Create a Glasswing butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createGlasswing(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("glasswing", entityType, level);
}
/**
* Create a Hairstreak butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createHairstreak(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("hairstreak", entityType, level);
}
/**
* Create a Heath butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createHeath(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("heath", entityType, level);
}
/**
* Create a Longwing butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createLongwing(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("longwing", entityType, level);
}
/**
* Create a Monarch butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createMonarch(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("monarch", entityType, level);
}
/**
* Create a Morpho butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createMorpho(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("morpho", entityType, level);
}
/**
* Create a Rainbow butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createRainbow(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("rainbow", entityType, level);
}
/**
* Create a Swallowtail butterfly egg.
*
* @param entityType The type of the entity.
* @param level The current level.
* @return A newly constructed entity.
*/
@NotNull
public static ButterflyEgg createSwallowtail(EntityType<? extends ButterflyEgg> entityType,
Level level) {
return new ButterflyEgg("swallowtail", entityType, level);
}
/**
* Spawns a butterfly egg into the world.
* @param level The level to spawn the entity.
* @param location The resource location of the entity to spawn.
* @param spawnBlock The block to spawn the entity on.
* @param surfaceDirection The direction the entity is facing.
*/
public static void spawn(ServerLevel level,
ResourceLocation location,
BlockPos spawnBlock,
Direction surfaceDirection) {
EntityType<?> entityType = ForgeRegistries.ENTITY_TYPES.getValue(location);
if (entityType != null) {
Entity entity = entityType.create(level);
if (entity instanceof ButterflyEgg egg) {
double x = spawnBlock.getX() + level.random.nextDouble();
double y = spawnBlock.getY() + level.random.nextDouble();
double z = spawnBlock.getZ() + level.random.nextDouble();
switch (surfaceDirection) {
case WEST -> x = Math.floor(spawnBlock.getX());
case EAST -> x = Math.floor(spawnBlock.getX()) + 1.0d;
case DOWN -> y = Math.floor(spawnBlock.getY());
case UP -> y = Math.floor(spawnBlock.getY()) + 1.0d;
case NORTH -> z = Math.floor(spawnBlock.getZ());
case SOUTH -> z = Math.floor(spawnBlock.getZ()) + 1.0d;
}
egg.moveTo(x, y, z, 0.0F, 0.0F);
egg.setSurfaceDirection(surfaceDirection);
egg.finalizeSpawn(level,
level.getCurrentDifficultyAt(spawnBlock),
MobSpawnType.NATURAL,
null,
null);
level.addFreshEntity(egg);
}
}
}
/**
* Reduce the size of the butterfly egg - they are small!
* @return The new size of the caterpillar.
*/
@Override
public float getScale() {
float scale = 0.05f;
switch (this.size) {
case SMALL -> { return 0.7f * scale; }
case LARGE ->{ return 1.28f * scale; }
default -> { return scale; }
}
}
/**
* If a player hits a butterfly egg it will be converted to an item in their inventory.
* @param damageSource The source of the damage.
* @param damage The amount of damage.
* @return The result from hurting the caterpillar.
*/
@Override
public boolean hurt(@NotNull DamageSource damageSource,
float damage) {
if (damageSource.getEntity() instanceof Player player) {
if (this.level().isClientSide) {
player.playSound(SoundEvents.PLAYER_ATTACK_SWEEP, 1F, 1F);
} else {
this.remove(RemovalReason.DISCARDED);
Item itemToAdd = ForgeRegistries.ITEMS.getValue(this.butterflyEggItem);
if (itemToAdd != null) {
ItemStack itemStack = new ItemStack(itemToAdd);
player.addItem(itemStack);
}
}
return true;
}
return super.hurt(damageSource, damage);
}
/**
* Overrides how an entity handles triggers such as tripwires and pressure
* plates. Butterfly eggs aren't heavy enough to trigger either.
* @return Always TRUE, so butterfly eggs ignore block triggers.
*/
@Override
public boolean isIgnoringBlockTriggers() {
return true;
}
/**
* Butterfly eggs ignore gravity.
*/
@Override
public boolean isNoGravity() {
return true;
}
/**
* Override this to control if an entity can be pushed or not. Butterfly eggs
* can't be pushed by other entities.
*
* @return Always FALSE.
*/
@Override
public boolean isPushable() {
return false;
}
/**
* Override so that the bounding box isn't recalculated for "babies".
* @param age The age of the entity.
*/
@Override
public void setAge(int age) {
this.age = age;
}
/**
* Overridden so that butterfly entities will render at a decent distance.
* @param distance The distance to check.
* @return TRUE if we should render the entity.
*/
@Override
public boolean shouldRenderAtSqrDistance(double distance) {
double d0 = this.getBoundingBox().getSize() * 10.0D;
if (Double.isNaN(d0)) {
d0 = 1.0D;
}
d0 *= 64.0D * getViewScale();
return distance < d0 * d0;
}
/**
* Construction
* @param species The species of the butterfly
* @param entityType The type of the entity.
* @param level The current level.
*/
protected ButterflyEgg(String species,
EntityType<? extends ButterflyEgg> entityType,
Level level) {
super("textures/item/butterfly_egg/" + species + "_egg.png", entityType, level);
ResourceLocation location = new ResourceLocation(ButterfliesMod.MODID, species);
ButterflyData data = ButterflyData.getEntry(location);
this.size = data.size;
setAge(-data.eggLifespan);
this.butterflyEggItem = ButterflyData.indexToButterflyEggItem(data.butterflyIndex);
}
/**
* A custom step for the AI update loop.
*/
@Override
protected void customServerAiStep() {
super.customServerAiStep();
// If the surface block is destroyed then the butterfly egg dies.
if (this.level().isEmptyBlock(getSurfaceBlockPos())) {
kill();
}
// Spawn Butterfly.
if (this.getAge() >= 0 && this.random.nextInt(0, 15) == 0) {
ResourceLocation location = EntityType.getKey(this.getType());
int index = ButterflyData.getButterflyIndex(location);
ResourceLocation newLocation = ButterflyData.indexToCaterpillarEntity(index);
if (newLocation != null) {
Caterpillar.spawn((ServerLevel)this.level(), newLocation, this.blockPosition(), this.getDirection(), false);
this.remove(RemovalReason.DISCARDED);
}
}
}
/**
* Override to change how pushing other entities affects them. Butterfly eggs
* don't push other entities.
* @param otherEntity The other entity pushing/being pushed.
*/
@Override
protected void doPush(@NotNull Entity otherEntity) {
// No-op
}
/**
* Override to control what kind of movement events the entity will emit.
* Butterfly eggs will not emit sounds.
* @return Movement events only.
*/
@NotNull
@Override
protected MovementEmission getMovementEmission() {
return MovementEmission.NONE;
}
/**
* Override to control an entity's relative volume. Butterfly eggs are silent.
* @return Always zero, so caterpillars are silent.
*/
@Override
protected float getSoundVolume() {
return 0.0f;
}
/**
* Override to set the entity's eye height.
* @param pose The current pose of the entity.
* @param dimensions The dimensions of the entity.
* @return The height of the entity's eyes.
*/
@Override
protected float getStandingEyeHeight(@NotNull Pose pose,
EntityDimensions dimensions) {
return dimensions.height / 2.0f;
}
/**
* Override to change how pushing other entities affects them. Butterfly eggs
* don't push other entities.
*/
@Override
protected void pushEntities() {
// No-op
}
}