| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.Direction;
|
| 4 | import net.minecraft.core.particles.ParticleTypes;
|
| 5 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 6 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 7 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.entity.item.ItemEntity;
|
| 13 | import net.minecraft.world.entity.projectile.Projectile;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.ProjectileItem;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 18 | import net.minecraft.world.level.material.PushReaction;
|
| 19 | import net.minecraft.world.level.storage.ValueInput;
|
| 20 | import net.minecraft.world.level.storage.ValueOutput;
|
| 21 | import net.minecraft.world.phys.Vec3;
|
| 22 |
|
| 23 | public class OminousItemSpawner extends Entity {
|
| 24 | private static final int SPAWN_ITEM_DELAY_MIN = 60;
|
| 25 | private static final int SPAWN_ITEM_DELAY_MAX = 120;
|
| 26 | private static final String TAG_SPAWN_ITEM_AFTER_TICKS = "spawn_item_after_ticks";
|
| 27 | private static final String TAG_ITEM = "item";
|
| 28 | private static final EntityDataAccessor<ItemStack> DATA_ITEM = SynchedEntityData.defineId(OminousItemSpawner.class, EntityDataSerializers.ITEM_STACK);
|
| 29 | public static final int TICKS_BEFORE_ABOUT_TO_SPAWN_SOUND = 36;
|
| 30 | private long spawnItemAfterTicks;
|
| 31 |
|
| 32 | public OminousItemSpawner(final EntityType<? extends OminousItemSpawner> type, final Level level) {
|
| 33 | super(type, level);
|
| 34 | this.noPhysics = true;
|
| 35 | }
|
| 36 |
|
| 37 | public static OminousItemSpawner create(final Level level, final ItemStack item) {
|
| 38 | OminousItemSpawner itemSpawner = new OminousItemSpawner(EntityTypes.OMINOUS_ITEM_SPAWNER, level);
|
| 39 | itemSpawner.spawnItemAfterTicks = level.getRandom().nextIntBetweenInclusive(60, 120);
|
| 40 | itemSpawner.setItem(item);
|
| 41 | return itemSpawner;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void tick() {
|
| 46 | super.tick();
|
| 47 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 48 | this.tickServer(serverLevel);
|
| 49 | } else {
|
| 50 | this.tickClient();
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | private void tickServer(final ServerLevel level) {
|
| 55 | if (this.tickCount == this.spawnItemAfterTicks - 36L) {
|
| 56 | level.playSound(null, this.blockPosition(), SoundEvents.TRIAL_SPAWNER_ABOUT_TO_SPAWN_ITEM, SoundSource.NEUTRAL);
|
| 57 | }
|
| 58 |
|
| 59 | if (this.tickCount >= this.spawnItemAfterTicks) {
|
| 60 | this.spawnItem();
|
| 61 | this.kill(level);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | private void tickClient() {
|
| 66 | if (this.level().getGameTime() % 5L == 0L) {
|
| 67 | this.addParticles();
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | private void spawnItem() {
|
| 72 | if (this.level() instanceof ServerLevel level) {
|
| 73 | ItemStack item = this.getItem();
|
| 74 | if (!item.isEmpty()) {
|
| 75 | Entity spawnedEntity;
|
| 76 | if (item.getItem() instanceof ProjectileItem projectileItem) {
|
| 77 | spawnedEntity = this.spawnProjectile(level, projectileItem, item);
|
| 78 | } else {
|
| 79 | spawnedEntity = new ItemEntity(level, this.getX(), this.getY(), this.getZ(), item);
|
| 80 | level.addFreshEntity(spawnedEntity);
|
| 81 | }
|
| 82 |
|
| 83 | level.levelEvent(3021, this.blockPosition(), 1);
|
| 84 | level.gameEvent(spawnedEntity, GameEvent.ENTITY_PLACE, this.position());
|
| 85 | this.setItem(ItemStack.EMPTY);
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private Entity spawnProjectile(final ServerLevel level, final ProjectileItem projectileItem, final ItemStack item) {
|
| 91 | ProjectileItem.DispenseConfig dispenseConfig = projectileItem.createDispenseConfig();
|
| 92 | dispenseConfig.overrideDispenseEvent().ifPresent(event -> level.levelEvent(event, this.blockPosition(), 0));
|
| 93 | Direction direction = Direction.DOWN;
|
| 94 | Projectile projectile = Projectile.spawnProjectileUsingShoot(
|
| 95 | projectileItem.asProjectile(level, this.position(), item, direction),
|
| 96 | level,
|
| 97 | item,
|
| 98 | direction.getStepX(),
|
| 99 | direction.getStepY(),
|
| 100 | direction.getStepZ(),
|
| 101 | dispenseConfig.power(),
|
| 102 | dispenseConfig.uncertainty()
|
| 103 | );
|
| 104 | projectile.setOwner(this);
|
| 105 | return projectile;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 110 | entityData.define(DATA_ITEM, ItemStack.EMPTY);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 115 | this.setItem(input.read("item", ItemStack.CODEC).orElse(ItemStack.EMPTY));
|
| 116 | this.spawnItemAfterTicks = input.getLongOr("spawn_item_after_ticks", 0L);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 121 | if (!this.getItem().isEmpty()) {
|
| 122 | output.store("item", ItemStack.CODEC, this.getItem());
|
| 123 | }
|
| 124 |
|
| 125 | output.putLong("spawn_item_after_ticks", this.spawnItemAfterTicks);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected boolean canAddPassenger(final Entity passenger) {
|
| 130 | return false;
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected boolean couldAcceptPassenger() {
|
| 135 | return false;
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected void addPassenger(final Entity passenger) {
|
| 140 | throw new IllegalStateException("Should never addPassenger without checking couldAcceptPassenger()");
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public PushReaction getPistonPushReaction() {
|
| 145 | return PushReaction.IGNORE_ENTITY;
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public boolean isIgnoringBlockTriggers() {
|
| 150 | return true;
|
| 151 | }
|
| 152 |
|
| 153 | public void addParticles() {
|
| 154 | Vec3 flyTowards = this.position();
|
| 155 | int particleCount = this.random.nextIntBetweenInclusive(1, 3);
|
| 156 |
|
| 157 | for (int i = 0; i < particleCount; i++) {
|
| 158 | double radius = 0.4;
|
| 159 | Vec3 flyFrom = new Vec3(
|
| 160 | this.getX() + 0.4 * (this.random.nextGaussian() - this.random.nextGaussian()),
|
| 161 | this.getY() + 0.4 * (this.random.nextGaussian() - this.random.nextGaussian()),
|
| 162 | this.getZ() + 0.4 * (this.random.nextGaussian() - this.random.nextGaussian())
|
| 163 | );
|
| 164 | Vec3 randomDirection = flyTowards.vectorTo(flyFrom);
|
| 165 | this.level()
|
| 166 | .addParticle(ParticleTypes.OMINOUS_SPAWNING, flyTowards.x(), flyTowards.y(), flyTowards.z(), randomDirection.x(), randomDirection.y(), randomDirection.z());
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | public ItemStack getItem() {
|
| 171 | return this.getEntityData().get(DATA_ITEM);
|
| 172 | }
|
| 173 |
|
| 174 | private void setItem(final ItemStack itemStack) {
|
| 175 | this.getEntityData().set(DATA_ITEM, itemStack);
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public final boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 180 | return false;
|
| 181 | }
|
| 182 | }
|
| 183 |
|