| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.particles.TrailParticleOption;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.sounds.SoundEvent;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.Difficulty;
|
| 12 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 13 | import net.minecraft.world.effect.MobEffect;
|
| 14 | import net.minecraft.world.effect.MobEffectInstance;
|
| 15 | import net.minecraft.world.effect.MobEffects;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 18 | import net.minecraft.world.entity.animal.bee.Bee;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 |
|
| 25 | public class EyeblossomBlock extends FlowerBlock {
|
| 26 | private static final int EYEBLOSSOM_XZ_RANGE = 3;
|
| 27 | private static final int EYEBLOSSOM_Y_RANGE = 2;
|
| 28 | private final EyeblossomBlock.Type type;
|
| 29 |
|
| 30 | public EyeblossomBlock(final EyeblossomBlock.Type type, final BlockBehaviour.Properties properties) {
|
| 31 | super(type.effect, type.effectDuration, properties);
|
| 32 | this.type = type;
|
| 33 | }
|
| 34 |
|
| 35 | public EyeblossomBlock(final boolean open, final BlockBehaviour.Properties properties) {
|
| 36 | super(EyeblossomBlock.Type.fromBoolean(open).effect, EyeblossomBlock.Type.fromBoolean(open).effectDuration, properties);
|
| 37 | this.type = EyeblossomBlock.Type.fromBoolean(open);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 42 | if (this.type.emitSounds() && random.nextInt(700) == 0) {
|
| 43 | BlockState below = level.getBlockState(pos.below());
|
| 44 | if (below.is(Blocks.PALE_MOSS_BLOCK)) {
|
| 45 | level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.EYEBLOSSOM_IDLE, SoundSource.AMBIENT, 1.0F, 1.0F, false);
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 52 | if (this.tryChangingState(state, level, pos, random)) {
|
| 53 | level.playSound(null, pos, this.type.transform().longSwitchSound, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 54 | }
|
| 55 |
|
| 56 | super.randomTick(state, level, pos, random);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 61 | if (this.tryChangingState(state, level, pos, random)) {
|
| 62 | level.playSound(null, pos, this.type.transform().shortSwitchSound, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 63 | }
|
| 64 |
|
| 65 | super.tick(state, level, pos, random);
|
| 66 | }
|
| 67 |
|
| 68 | private boolean tryChangingState(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 69 | boolean shouldBeOpen = level.environmentAttributes().getValue(EnvironmentAttributes.EYEBLOSSOM_OPEN, pos).toBoolean(this.type.open);
|
| 70 | if (shouldBeOpen == this.type.open) {
|
| 71 | return false;
|
| 72 | }
|
| 73 |
|
| 74 | EyeblossomBlock.Type newType = this.type.transform();
|
| 75 | level.setBlockAndUpdate(pos, newType.state());
|
| 76 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(state));
|
| 77 | newType.spawnTransformParticle(level, pos, random);
|
| 78 | BlockPos minPos = pos.offset(-3, -2, -3);
|
| 79 | BlockPos maxPos = pos.offset(3, 2, 3);
|
| 80 | level.findBlocksIn(minPos, maxPos).filterState(nearbyState -> nearbyState == state).forEach((nearbyPos, var5x) -> {
|
| 81 | double distance = Math.sqrt(pos.distSqr(nearbyPos));
|
| 82 | int delay = random.nextIntBetweenInclusive((int)(distance * 5.0), (int)(distance * 10.0));
|
| 83 | level.scheduleTick(nearbyPos, state.getBlock(), delay);
|
| 84 | });
|
| 85 | return true;
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void entityInside(
|
| 90 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 91 | ) {
|
| 92 | if (!level.isClientSide()
|
| 93 | && level.getDifficulty() != Difficulty.PEACEFUL
|
| 94 | && entity instanceof Bee bee
|
| 95 | && Bee.attractsBees(state)
|
| 96 | && !bee.hasEffect(MobEffects.POISON)) {
|
| 97 | bee.addEffect(this.getBeeInteractionEffect());
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public MobEffectInstance getBeeInteractionEffect() {
|
| 103 | return new MobEffectInstance(MobEffects.POISON, 25);
|
| 104 | }
|
| 105 |
|
| 106 | public enum Type {
|
| 107 | OPEN(true, MobEffects.BLINDNESS, 11.0F, SoundEvents.EYEBLOSSOM_OPEN_LONG, SoundEvents.EYEBLOSSOM_OPEN, 16545810),
|
| 108 | CLOSED(false, MobEffects.NAUSEA, 7.0F, SoundEvents.EYEBLOSSOM_CLOSE_LONG, SoundEvents.EYEBLOSSOM_CLOSE, 6250335);
|
| 109 |
|
| 110 | private final boolean open;
|
| 111 | private final Holder<MobEffect> effect;
|
| 112 | private final float effectDuration;
|
| 113 | private final SoundEvent longSwitchSound;
|
| 114 | private final SoundEvent shortSwitchSound;
|
| 115 | private final int particleColor;
|
| 116 |
|
| 117 | Type(
|
| 118 | final boolean open,
|
| 119 | final Holder<MobEffect> effect,
|
| 120 | final float duration,
|
| 121 | final SoundEvent longSwitchSound,
|
| 122 | final SoundEvent shortSwitchSound,
|
| 123 | final int particleColor
|
| 124 | ) {
|
| 125 | this.open = open;
|
| 126 | this.effect = effect;
|
| 127 | this.effectDuration = duration;
|
| 128 | this.longSwitchSound = longSwitchSound;
|
| 129 | this.shortSwitchSound = shortSwitchSound;
|
| 130 | this.particleColor = particleColor;
|
| 131 | }
|
| 132 |
|
| 133 | public Block block() {
|
| 134 | return this.open ? Blocks.OPEN_EYEBLOSSOM : Blocks.CLOSED_EYEBLOSSOM;
|
| 135 | }
|
| 136 |
|
| 137 | public BlockState state() {
|
| 138 | return this.block().defaultBlockState();
|
| 139 | }
|
| 140 |
|
| 141 | public EyeblossomBlock.Type transform() {
|
| 142 | return fromBoolean(!this.open);
|
| 143 | }
|
| 144 |
|
| 145 | public boolean emitSounds() {
|
| 146 | return this.open;
|
| 147 | }
|
| 148 |
|
| 149 | public static EyeblossomBlock.Type fromBoolean(final boolean open) {
|
| 150 | return open ? OPEN : CLOSED;
|
| 151 | }
|
| 152 |
|
| 153 | public void spawnTransformParticle(final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 154 | Vec3 start = Vec3.atCenterOf(pos);
|
| 155 | double lifetime = 0.5 + random.nextDouble();
|
| 156 | Vec3 velocity = new Vec3(random.nextDouble() - 0.5, random.nextDouble() + 1.0, random.nextDouble() - 0.5);
|
| 157 | Vec3 target = start.add(velocity.scale(lifetime));
|
| 158 | TrailParticleOption particle = new TrailParticleOption(target, this.particleColor, (int)(20.0 * lifetime));
|
| 159 | level.sendParticles(particle, start.x, start.y, start.z, 1, 0.0, 0.0, 0.0, 0.0);
|
| 160 | }
|
| 161 |
|
| 162 | public SoundEvent longSwitchSound() {
|
| 163 | return this.longSwitchSound;
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|