Search in sources :

Example 1 with LightningRayEntity

use of paulevs.edenring.entities.LightningRayEntity in project EdenRing by paulevsGitch.

the class BrainTreeBlock method hitLighting.

private void hitLighting(ServerLevel world, Random random, BlockPos pos) {
    List<LivingEntity> entities = world.getNearbyEntities(LivingEntity.class, TargetingConditions.forNonCombat(), null, new AABB(pos).inflate(16, 16, 16));
    if (!entities.isEmpty()) {
        LivingEntity entity = entities.get(random.nextInt(entities.size()));
        if (entity instanceof Player && ((Player) entity).isCreative()) {
            return;
        }
        MutableBlockPos mpos = pos.mutable();
        float dx = (float) (entity.getX() - pos.getX() - 0.5);
        float dy = (float) (entity.getY() - pos.getY() - 0.5);
        float dz = (float) (entity.getZ() - pos.getZ() - 0.5);
        float ax = Mth.abs(dx);
        float ay = Mth.abs(dy);
        float az = Mth.abs(dz);
        float max = MHelper.max(ax, ay, az);
        int count = Mth.ceil(max);
        dx /= count;
        dy /= count;
        dz /= count;
        boolean hit = true;
        for (int i = 2; i < count; i++) {
            mpos.set(pos.getX() + dx * i, pos.getY() + dy * i, pos.getZ() + dz * i);
            BlockState blockState = world.getBlockState(mpos);
            if (blockState.getMaterial().blocksMotion() && !(blockState.getBlock() instanceof BrainTreeBlock)) {
                hit = false;
                break;
            }
        }
        if (hit) {
            LightningRayEntity lightningRay = EdenEntities.LIGHTNING_RAY.create(world);
            lightningRay.teleportTo(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
            lightningRay.setEnd(entity.position());
            world.addFreshEntity(lightningRay);
            world.playLocalSound(entity.getX(), entity.getY(), entity.getZ(), SoundEvents.LIGHTNING_BOLT_IMPACT, SoundSource.WEATHER, MHelper.randRange(1.0F, 5.0F, random), MHelper.randRange(0.5F, 1.5F, random), false);
            if (entity.isInvulnerable()) {
                return;
            }
            float resistance = 0;
            Iterator<ItemStack> iterator = entity.getArmorSlots().iterator();
            while (iterator.hasNext()) {
                ItemStack stack = iterator.next();
                if (stack.getItem() instanceof ArmorItem) {
                    ArmorItem item = (ArmorItem) stack.getItem();
                    ArmorMaterial material = item.getMaterial();
                    for (ArmorMaterial m : PROTECTIVE) {
                        if (material == m) {
                            resistance += 0.25F;
                            break;
                        }
                    }
                }
            }
            if (resistance < 1) {
                entity.hurt(DamageSource.LIGHTNING_BOLT, (1 - resistance) * 3F);
            }
        }
    }
}
Also used : Player(net.minecraft.world.entity.player.Player) LivingEntity(net.minecraft.world.entity.LivingEntity) ArmorItem(net.minecraft.world.item.ArmorItem) BlockState(net.minecraft.world.level.block.state.BlockState) LightningRayEntity(paulevs.edenring.entities.LightningRayEntity) ArmorMaterial(net.minecraft.world.item.ArmorMaterial) ItemStack(net.minecraft.world.item.ItemStack) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) AABB(net.minecraft.world.phys.AABB)

Aggregations

MutableBlockPos (net.minecraft.core.BlockPos.MutableBlockPos)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 Player (net.minecraft.world.entity.player.Player)1 ArmorItem (net.minecraft.world.item.ArmorItem)1 ArmorMaterial (net.minecraft.world.item.ArmorMaterial)1 ItemStack (net.minecraft.world.item.ItemStack)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 AABB (net.minecraft.world.phys.AABB)1 LightningRayEntity (paulevs.edenring.entities.LightningRayEntity)1