| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.function.Function;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.particles.GeyserParticleOptions;
|
| 9 | import net.minecraft.core.particles.ParticleTypes;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.sounds.SoundSource;
|
| 13 | import net.minecraft.tags.EntityTypeTags;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.world.effect.MobEffectInstance;
|
| 16 | import net.minecraft.world.effect.MobEffects;
|
| 17 | import net.minecraft.world.entity.Entity;
|
| 18 | import net.minecraft.world.entity.EntitySelector;
|
| 19 | import net.minecraft.world.entity.LivingEntity;
|
| 20 | import net.minecraft.world.entity.player.Player;
|
| 21 | import net.minecraft.world.level.ClipContext;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.block.Blocks;
|
| 24 | import net.minecraft.world.level.block.PotentSulfurBlock;
|
| 25 | import net.minecraft.world.level.block.state.BlockState;
|
| 26 | import net.minecraft.world.level.block.state.properties.PotentSulfurState;
|
| 27 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 28 | import net.minecraft.world.level.levelgen.XoroshiroRandomSource;
|
| 29 | import net.minecraft.world.level.material.Fluids;
|
| 30 | import net.minecraft.world.level.storage.ValueInput;
|
| 31 | import net.minecraft.world.level.storage.ValueOutput;
|
| 32 | import net.minecraft.world.phys.AABB;
|
| 33 | import net.minecraft.world.phys.HitResult;
|
| 34 | import net.minecraft.world.phys.Vec3;
|
| 35 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | public class PotentSulfurBlockEntity extends BlockEntity {
|
| 39 | private static final int EFFECT_APPLICATION_FREQUENCY_TICKS = 10;
|
| 40 | private static final float EFFECT_DURATION_IN_SECONDS = 4.0F;
|
| 41 | private static final int EFFECT_DURATION_IN_TICKS = 80;
|
| 42 | public static final float EFFECT_RANGE = 3.0F;
|
| 43 | private static final Predicate<Entity> EFFECT_PREDICATE = EntitySelector.NO_SPECTATORS.and(EntitySelector.ENTITY_STILL_ALIVE);
|
| 44 | public static final int PARTICLE_FREQUENCY_TICKS = 20;
|
| 45 | public static final int SOUND_FREQUENCY_TICKS = 40;
|
| 46 | private static final float GEYSER_BASE_LAUNCH_SPEED = 0.3F;
|
| 47 | private static final float GEYSER_LAUNCH_FORCE = 0.2F;
|
| 48 | public int waitingCountdown = -1;
|
| 49 | public long eruptionTick = -1L;
|
| 50 | public static BlockEntityTicker<PotentSulfurBlockEntity> SERVER_NAUSEA_EFFECT_TICKER = (level, pos, state, potentSulfur) -> {
|
| 51 | if (level.getGameTime() % 10L == 0L) {
|
| 52 | BlockPos sourceBlock = findNoxiousGasSourceBlock(level, pos);
|
| 53 | if (sourceBlock != null) {
|
| 54 | for (LivingEntity entity : getNearbyLivingEntities(level, sourceBlock)) {
|
| 55 | if (canBeReachedByNoxiousGas(level, sourceBlock, entity.getEyePosition())) {
|
| 56 | applyNauseaEffect(entity);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 | };
|
| 62 | public static BlockEntityTicker<PotentSulfurBlockEntity> CLIENT_NOXIOUS_GAS_TICKER = (level, pos, state, entity) -> {
|
| 63 | if (level.getGameTime() % 20L == 0L) {
|
| 64 | BlockPos sourceBlock = findNoxiousGasSourceBlock(level, pos);
|
| 65 | if (sourceBlock != null) {
|
| 66 | spawnNoxiousGasCloudParticle(level, Vec3.atCenterOf(sourceBlock));
|
| 67 | }
|
| 68 | }
|
| 69 | };
|
| 70 | public static Function<SoundEvent, BlockEntityTicker<PotentSulfurBlockEntity>> CLIENT_GEYSER_PLUME_TICKER = sound -> (level, pos, state, entity) -> {
|
| 71 | BlockPos sourceBlock = findNoxiousGasSourceBlock(level, pos);
|
| 72 | if (sourceBlock != null) {
|
| 73 | long eruptionTime = level.getGameTime() - entity.eruptionTick;
|
| 74 | if (eruptionTime % 20L == 0L) {
|
| 75 | spawnGeyserParticle(level, pos, sourceBlock);
|
| 76 | }
|
| 77 |
|
| 78 | if (eruptionTime % 40L == 0L) {
|
| 79 | level.playLocalSound(sourceBlock.getX() + 0.5, sourceBlock.getY() + 0.5, sourceBlock.getZ() + 0.5, sound, SoundSource.BLOCKS, 1.0F, 1.0F, false);
|
| 80 | }
|
| 81 | }
|
| 82 | };
|
| 83 | public static BlockEntityTicker<PotentSulfurBlockEntity> SERVER_WAITING_COUNTDOWN_TICKER = (level, pos, state, entity) -> {
|
| 84 | if (level.getGameTime() % 20L == 0L) {
|
| 85 | BlockPos sourceBlock = findNoxiousGasSourceBlock(level, pos);
|
| 86 | if (sourceBlock != null) {
|
| 87 | if (entity.waitingCountdown <= 0) {
|
| 88 | int waterBlocks = sourceBlock.getY() - pos.getY() - 1;
|
| 89 | RandomSource geyserPositional = geyserPositional((ServerLevel)level, pos);
|
| 90 | if (state.getValue(PotentSulfurBlock.STATE) == PotentSulfurState.DORMANT) {
|
| 91 | entity.waitingCountdown = 10 * (waterBlocks - 1) + geyserPositional.nextIntBetweenInclusive(15, 30);
|
| 92 | } else {
|
| 93 | geyserPositional.nextInt();
|
| 94 | entity.waitingCountdown = waterBlocks - 1 + geyserPositional.nextIntBetweenInclusive(1, 2);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | if (entity.waitingCountdown > 0) {
|
| 99 | entity.waitingCountdown--;
|
| 100 | }
|
| 101 |
|
| 102 | if (entity.waitingCountdown == 0) {
|
| 103 | PotentSulfurState stateToSet = state.getValue(PotentSulfurBlock.STATE) == PotentSulfurState.DORMANT
|
| 104 | ? PotentSulfurState.ERUPTING
|
| 105 | : PotentSulfurState.DORMANT;
|
| 106 | level.setBlockAndUpdate(pos, state.setValue(PotentSulfurBlock.STATE, stateToSet));
|
| 107 | if (stateToSet == PotentSulfurState.DORMANT) {
|
| 108 | level.gameEvent(GameEvent.BLOCK_DEACTIVATE, pos, GameEvent.Context.of(state));
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 | };
|
| 114 | public static final long GEYSER_SALT = -904011478L;
|
| 115 | public static BlockEntityTicker<PotentSulfurBlockEntity> LAUNCH_ENTITY_TICKER = (level, pos, state, entity) -> {
|
| 116 | BlockPos sourceBlock = findNoxiousGasSourceBlock(level, pos);
|
| 117 | if (sourceBlock != null) {
|
| 118 | int waterBlocks = sourceBlock.getY() - pos.getY() - 1;
|
| 119 | int geyserForceHeight = getUnobstructedBlockCount(level, pos.above(), waterBlocks);
|
| 120 | AABB aabb = new AABB(pos.above()).expandTowards(0.0, geyserForceHeight - 1, 0.0);
|
| 121 |
|
| 122 | for (Entity entityToBeLaunched : level.getEntitiesOfClass(Entity.class, aabb, EFFECT_PREDICATE)) {
|
| 123 | Vec3 entityVelocity = entityToBeLaunched.getDeltaMovement();
|
| 124 | entityToBeLaunched.checkFallDistanceAccumulation();
|
| 125 | if (entityToBeLaunched.canSimulateMovement()
|
| 126 | && !(entityToBeLaunched instanceof Player player && player.getAbilities().flying)
|
| 127 | && !entityToBeLaunched.isPassenger()
|
| 128 | && !entityToBeLaunched.is(EntityTypeTags.NOT_AFFECTED_BY_GEYSERS)
|
| 129 | && entityVelocity.y < 0.3F + waterBlocks * 0.1) {
|
| 130 | entityToBeLaunched.addDeltaMovement(new Vec3(0.0, 0.2F, 0.0));
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 | };
|
| 135 |
|
| 136 | public PotentSulfurBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 137 | super(BlockEntityTypes.POTENT_SULFUR, worldPosition, blockState);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | protected void saveAdditional(final ValueOutput output) {
|
| 142 | super.saveAdditional(output);
|
| 143 | output.putInt("countdown", this.waitingCountdown);
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | protected void loadAdditional(final ValueInput input) {
|
| 148 | super.loadAdditional(input);
|
| 149 | input.getInt("countdown").ifPresent(value -> this.waitingCountdown = value);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public void setLevel(final Level level) {
|
| 154 | super.setLevel(level);
|
| 155 | if (this.eruptionTick == -1L) {
|
| 156 | this.eruptionTick = level.getGameTime();
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | public void resetCountdown() {
|
| 161 | this.waitingCountdown = -1;
|
| 162 | }
|
| 163 |
|
| 164 | private static void applyNauseaEffect(final LivingEntity entity) {
|
| 165 | entity.addEffect(new MobEffectInstance(MobEffects.NAUSEA, 80, 0, true, true));
|
| 166 | }
|
| 167 |
|
| 168 | private static List<LivingEntity> getNearbyLivingEntities(final Level level, final BlockPos pos) {
|
| 169 | AABB aabb = new AABB(pos).inflate(2.5, 0.0, 2.5);
|
| 170 | return level.getEntitiesOfClass(LivingEntity.class, aabb, EFFECT_PREDICATE);
|
| 171 | }
|
| 172 |
|
| 173 | public static RandomSource geyserPositional(final ServerLevel level, final BlockPos pos) {
|
| 174 | return new XoroshiroRandomSource(level.getSeed() ^ -904011478L).forkPositional().at(pos);
|
| 175 | }
|
| 176 |
|
| 177 | private static void spawnGeyserParticle(final Level level, final BlockPos sulfurPos, final BlockPos sourcePos) {
|
| 178 | int waterBlocks = sourcePos.getY() - sulfurPos.getY() - 1;
|
| 179 | level.addParticle(
|
| 180 | new GeyserParticleOptions(ParticleTypes.GEYSER, waterBlocks), sourcePos.getX() + 0.5, sourcePos.getY(), sourcePos.getZ() + 0.5, 0.0, 0.0, 0.0
|
| 181 | );
|
| 182 | }
|
| 183 |
|
| 184 | private static void spawnNoxiousGasCloudParticle(final Level level, final Vec3 pos) {
|
| 185 | level.addParticle(ParticleTypes.NOXIOUS_GAS_CLOUD, pos.x, pos.y, pos.z, 0.0, 0.0, 0.0);
|
| 186 | }
|
| 187 |
|
| 188 | private static int getUnobstructedBlockCount(final Level level, final BlockPos pos, final int waterBlocks) {
|
| 189 | int geyserForceHeight = 6 * waterBlocks;
|
| 190 | CollisionContext geyserPositionContext = CollisionContext.positionContext(pos.below().getY());
|
| 191 |
|
| 192 | for (int i = 0; i < geyserForceHeight; i++) {
|
| 193 | BlockPos currentPos = pos.above(i);
|
| 194 | BlockState state = level.getBlockState(currentPos);
|
| 195 | if (!isGeyserPassableBlock(state, level, currentPos, geyserPositionContext)) {
|
| 196 | return i;
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | return geyserForceHeight;
|
| 201 | }
|
| 202 |
|
| 203 | private static boolean isGeyserPassableBlock(final BlockState state, final Level level, final BlockPos pos, final CollisionContext context) {
|
| 204 | return !state.isAir() && !state.is(Blocks.WATER) ? state.getCollisionShape(level, pos, context).isEmpty() : true;
|
| 205 | }
|
| 206 |
|
| 207 | private static @Nullable BlockPos findNoxiousGasSourceBlock(final Level level, final BlockPos origin) {
|
| 208 | int maxY = origin.getY() + 4 + 1;
|
| 209 | CollisionContext geyserPositionContext = CollisionContext.positionContext(origin.getY());
|
| 210 | BlockPos.MutableBlockPos pos = origin.above(1).mutable();
|
| 211 |
|
| 212 | while (pos.getY() <= maxY) {
|
| 213 | BlockState state = level.getBlockState(pos);
|
| 214 | boolean isWaterLogged = level.getFluidState(pos).isSourceOfType(Fluids.WATER);
|
| 215 | if (!isWaterLogged || !state.is(Blocks.WATER) && !isGeyserPassableBlock(state, level, pos, geyserPositionContext)) {
|
| 216 | if (state.isAir() || isGeyserPassableBlock(state, level, pos, geyserPositionContext)) {
|
| 217 | return pos.immutable();
|
| 218 | }
|
| 219 | break;
|
| 220 | }
|
| 221 |
|
| 222 | pos.move(Direction.UP);
|
| 223 | }
|
| 224 |
|
| 225 | return null;
|
| 226 | }
|
| 227 |
|
| 228 | public static boolean canBeReachedByNoxiousGas(final Level level, final BlockPos sourceBlock, final Vec3 pos) {
|
| 229 | BlockPos blockPos = BlockPos.containing(pos);
|
| 230 | CollisionContext geyserPositionContext = CollisionContext.positionContext(blockPos.below().getY());
|
| 231 | if (!isGeyserPassableBlock(level.getBlockState(blockPos), level, blockPos, geyserPositionContext)) {
|
| 232 | return false;
|
| 233 | }
|
| 234 |
|
| 235 | if (pos.distanceToSqr(Vec3.atCenterOf(sourceBlock)) > 9.0) {
|
| 236 | return false;
|
| 237 | }
|
| 238 |
|
| 239 | Vec3 belowSource = Vec3.atCenterOf(sourceBlock.below());
|
| 240 | Vec3 belowPos = pos.with(Direction.Axis.Y, pos.y - 1.0);
|
| 241 | return isWater(level, belowPos) && haveLineOfSight(level, belowSource, belowPos);
|
| 242 | }
|
| 243 |
|
| 244 | private static boolean isWater(final Level level, final Vec3 pos) {
|
| 245 | return level.getFluidState(BlockPos.containing(pos)).isSourceOfType(Fluids.WATER);
|
| 246 | }
|
| 247 |
|
| 248 | private static boolean haveLineOfSight(final Level level, final Vec3 a, final Vec3 b) {
|
| 249 | HitResult hitResult = level.clip(new ClipContext(a, b, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, CollisionContext.empty()));
|
| 250 | return hitResult.getType() != HitResult.Type.BLOCK;
|
| 251 | }
|
| 252 | }
|
| 253 |
|