| 1 | package net.minecraft.world.entity.projectile.throwableitemprojectile;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.core.particles.ItemParticleOption;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.world.entity.EntityDimensions;
|
| 8 | import net.minecraft.world.entity.EntityEvent;
|
| 9 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 10 | import net.minecraft.world.entity.EntityType;
|
| 11 | import net.minecraft.world.entity.EntityTypes;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.animal.chicken.Chicken;
|
| 14 | import net.minecraft.world.item.Item;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.ItemStackTemplate;
|
| 17 | import net.minecraft.world.item.Items;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.phys.EntityHitResult;
|
| 20 | import net.minecraft.world.phys.HitResult;
|
| 21 |
|
| 22 | public class ThrownEgg extends ThrowableItemProjectile {
|
| 23 | private static final EntityDimensions ZERO_SIZED_DIMENSIONS = EntityDimensions.fixed(0.0F, 0.0F);
|
| 24 |
|
| 25 | public ThrownEgg(final EntityType<? extends ThrownEgg> type, final Level level) {
|
| 26 | super(type, level);
|
| 27 | }
|
| 28 |
|
| 29 | public ThrownEgg(final Level level, final LivingEntity mob, final ItemStack itemStack) {
|
| 30 | super(EntityTypes.EGG, mob, level, itemStack);
|
| 31 | }
|
| 32 |
|
| 33 | public ThrownEgg(final Level level, final double x, final double y, final double z, final ItemStack itemStack) {
|
| 34 | super(EntityTypes.EGG, x, y, z, level, itemStack);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 39 | if (id == 3) {
|
| 40 | ItemStack item = this.getItem();
|
| 41 | if (!item.isEmpty()) {
|
| 42 | ItemParticleOption breakParticle = new ItemParticleOption(ParticleTypes.ITEM, ItemStackTemplate.fromNonEmptyStack(item));
|
| 43 |
|
| 44 | for (int i = 0; i < 8; i++) {
|
| 45 | this.level()
|
| 46 | .addParticle(
|
| 47 | breakParticle,
|
| 48 | this.getX(),
|
| 49 | this.getY(),
|
| 50 | this.getZ(),
|
| 51 | (this.random.nextFloat() - 0.5) * 0.08,
|
| 52 | (this.random.nextFloat() - 0.5) * 0.08,
|
| 53 | (this.random.nextFloat() - 0.5) * 0.08
|
| 54 | );
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected void onHitEntity(final EntityHitResult hitResult) {
|
| 62 | super.onHitEntity(hitResult);
|
| 63 | hitResult.getEntity().hurt(this.damageSources().thrown(this, this.getOwner()), 0.0F);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void onHit(final HitResult hitResult) {
|
| 68 | super.onHit(hitResult);
|
| 69 | if (!this.level().isClientSide()) {
|
| 70 | if (this.random.nextInt(8) == 0) {
|
| 71 | int count = 1;
|
| 72 | if (this.random.nextInt(32) == 0) {
|
| 73 | count = 4;
|
| 74 | }
|
| 75 |
|
| 76 | for (int i = 0; i < count; i++) {
|
| 77 | Chicken chicken = EntityTypes.CHICKEN.create(this.level(), EntitySpawnReason.TRIGGERED);
|
| 78 | if (chicken != null) {
|
| 79 | chicken.setAge(-24000);
|
| 80 | chicken.snapTo(this.getX(), this.getY(), this.getZ(), this.getYRot(), 0.0F);
|
| 81 | Optional.ofNullable(this.getItem().get(DataComponents.CHICKEN_VARIANT)).ifPresent(chicken::setVariant);
|
| 82 | if (!chicken.fudgePositionAfterSizeChange(ZERO_SIZED_DIMENSIONS)) {
|
| 83 | break;
|
| 84 | }
|
| 85 |
|
| 86 | this.level().addFreshEntity(chicken);
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | this.level().broadcastEntityEvent(this, (byte)3);
|
| 92 | this.discard();
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | protected Item getDefaultItem() {
|
| 98 | return Items.EGG;
|
| 99 | }
|
| 100 | }
|
| 101 |
|