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);
}
}
}
}
Aggregations