File size: 2,241 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package net.minecraft.world.effect;

import com.google.common.collect.Sets;
import java.util.Set;
import java.util.function.ToIntFunction;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.block.Blocks;

class WeavingMobEffect extends MobEffect {
    private final ToIntFunction<RandomSource> maxCobwebs;

    protected WeavingMobEffect(MobEffectCategory p_331231_, int p_336179_, ToIntFunction<RandomSource> p_328620_) {
        super(p_331231_, p_336179_, ParticleTypes.ITEM_COBWEB);
        this.maxCobwebs = p_328620_;
    }

    @Override
    public void onMobRemoved(ServerLevel p_362050_, LivingEntity p_335117_, int p_333338_, Entity.RemovalReason p_328096_) {
        if (p_328096_ == Entity.RemovalReason.KILLED && (p_335117_ instanceof Player || p_362050_.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING))) {
            this.spawnCobwebsRandomlyAround(p_362050_, p_335117_.getRandom(), p_335117_.blockPosition());
        }
    }

    private void spawnCobwebsRandomlyAround(ServerLevel p_368804_, RandomSource p_332035_, BlockPos p_329542_) {
        Set<BlockPos> set = Sets.newHashSet();
        int i = this.maxCobwebs.applyAsInt(p_332035_);

        for (BlockPos blockpos : BlockPos.randomInCube(p_332035_, 15, p_329542_, 1)) {
            BlockPos blockpos1 = blockpos.below();
            if (!set.contains(blockpos) && p_368804_.getBlockState(blockpos).canBeReplaced() && p_368804_.getBlockState(blockpos1).isFaceSturdy(p_368804_, blockpos1, Direction.UP)
                )
             {
                set.add(blockpos.immutable());
                if (set.size() >= i) {
                    break;
                }
            }
        }

        for (BlockPos blockpos2 : set) {
            p_368804_.setBlock(blockpos2, Blocks.COBWEB.defaultBlockState(), 3);
            p_368804_.levelEvent(3018, blockpos2, 0);
        }
    }
}