| 1 | package net.minecraft.world.effect;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import java.util.Set;
|
| 5 | import java.util.function.ToIntFunction;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.particles.ParticleTypes;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.level.block.Blocks;
|
| 15 | import net.minecraft.world.level.gamerules.GameRules;
|
| 16 |
|
| 17 | class WeavingMobEffect extends MobEffect {
|
| 18 | private final ToIntFunction<RandomSource> maxCobwebs;
|
| 19 |
|
| 20 | protected WeavingMobEffect(final MobEffectCategory category, final int color, final ToIntFunction<RandomSource> maxCobwebs) {
|
| 21 | super(category, color, ParticleTypes.ITEM_COBWEB);
|
| 22 | this.maxCobwebs = maxCobwebs;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public void onMobRemoved(final ServerLevel level, final LivingEntity mob, final int amplifier, final Entity.RemovalReason reason) {
|
| 27 | if (reason == Entity.RemovalReason.KILLED && (mob instanceof Player || level.getGameRules().get(GameRules.MOB_GRIEFING))) {
|
| 28 | this.spawnCobwebsRandomlyAround(level, mob.getRandom(), mob.blockPosition());
|
| 29 | }
|
| 30 | }
|
| 31 |
|
| 32 | private void spawnCobwebsRandomlyAround(final ServerLevel level, final RandomSource random, final BlockPos pos) {
|
| 33 | Set<BlockPos> positionsToTransform = Sets.newHashSet();
|
| 34 | int cobwebCount = this.maxCobwebs.applyAsInt(random);
|
| 35 |
|
| 36 | for (BlockPos blockPos : BlockPos.randomInCube(random, 15, pos, 1)) {
|
| 37 | BlockPos below = blockPos.below();
|
| 38 | if (!positionsToTransform.contains(blockPos)
|
| 39 | && level.getBlockState(blockPos).canBeReplaced()
|
| 40 | && level.getBlockState(below).isFaceSturdy(level, below, Direction.UP)) {
|
| 41 | positionsToTransform.add(blockPos.immutable());
|
| 42 | if (positionsToTransform.size() >= cobwebCount) {
|
| 43 | break;
|
| 44 | }
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | for (BlockPos blockPos : positionsToTransform) {
|
| 49 | level.setBlockAndUpdate(blockPos, Blocks.COBWEB.defaultBlockState());
|
| 50 | level.levelEvent(3018, blockPos, 0);
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 |
|