From a6dc65027d76677d3c7e2c26ae51dd3623cf61a7 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Thu, 16 Jul 2020 21:42:15 -0400 Subject: [PATCH] Improve --- .../entity/DefensiveLaserBlockEntity.java | 85 ++++++++++--------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/thebrokenrail/energonrelics/block/entity/DefensiveLaserBlockEntity.java b/src/main/java/com/thebrokenrail/energonrelics/block/entity/DefensiveLaserBlockEntity.java index 1dc1afb..cf0ef4d 100644 --- a/src/main/java/com/thebrokenrail/energonrelics/block/entity/DefensiveLaserBlockEntity.java +++ b/src/main/java/com/thebrokenrail/energonrelics/block/entity/DefensiveLaserBlockEntity.java @@ -15,10 +15,12 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.particle.ParticleTypes; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.hit.HitResult; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Box; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; import net.minecraft.world.RayTraceContext; +import net.minecraft.world.World; import net.minecraft.world.explosion.Explosion; import java.util.Comparator; @@ -32,8 +34,12 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity { public final Rotation rotation = new Rotation(0, 0, "Rotation"); + private static Vec3d getPosVec(BlockPos pos) { + return new Vec3d(pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d); + } + private Vec3d getPosVec() { - return new Vec3d(getPos().getX() + 0.5d, getPos().getY() + 0.5d, getPos().getZ() + 0.5d); + return getPosVec(getPos()); } @Override @@ -176,6 +182,45 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity { return new ItemEntity(getWorld(), posVec.getX(), posVec.getY(), posVec.getZ()); } + private void fire(World world, BlockPos pos) { + List entities = getEntities(entity -> true); + Vec3d posVec = getPosVec(pos); + Vec3d collision = null; + for (LivingEntity entity : entities) { + Optional optional = entity.getBoundingBox().rayTrace(posVec, posVec.add(rotation.getRayVector().multiply(Config.DEFENSIVE_LASER_RANGE))); + if (optional.isPresent()) { + Vec3d vec = optional.get(); + if (collision == null || vec.distanceTo(posVec) < collision.distanceTo(posVec)) { + collision = optional.get(); + } + } + } + + Vec3d rotationVec = rotation.getRayVector(); + Vec3d start = rotationVec.add(posVec); + Vec3d end = rotationVec.multiply(Config.DEFENSIVE_LASER_RANGE).add(posVec); + HitResult result = world.rayTrace(new RayTraceContext(start, end, RayTraceContext.ShapeType.COLLIDER, RayTraceContext.FluidHandling.ANY, getDummyEntity())); + if (result.getType() != HitResult.Type.MISS && (collision == null || result.getPos().distanceTo(posVec) < collision.distanceTo(posVec))) { + collision = result.getPos(); + } + + double distance; + if (collision != null) { + world.createExplosion(null, collision.getX(), collision.getY(), collision.getZ(), 3, Explosion.DestructionType.NONE); + + distance = posVec.distanceTo(collision); + } else { + distance = Config.DEFENSIVE_LASER_RANGE; + } + + double multiplier = 6d; + distance = distance * multiplier; + for (int i = 0; i < distance; i++) { + Vec3d vec = rotationVec.multiply(i / multiplier).add(posVec); + ((ServerWorld) world).spawnParticles(ParticleTypes.END_ROD, vec.getX(), vec.getY(), vec.getZ(), 1, 0, 0, 0, 0.25f); + } + } + @Override protected void tickEnergy() { assert getWorld() != null; @@ -187,43 +232,7 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity { if (countdown == 1) { addAction(new Action(Config.DEFENSIVE_LASER_FIRE_ENERGY_REQUIRED, (world, pos, state) -> { firing = false; - - List entities = getEntities(entity -> true); - Vec3d posVec = getPosVec(); - Vec3d collision = null; - for (LivingEntity entity : entities) { - Optional optional = entity.getBoundingBox().rayTrace(getPosVec(), getPosVec().add(rotation.getRayVector().multiply(Config.DEFENSIVE_LASER_RANGE))); - if (optional.isPresent()) { - Vec3d vec = optional.get(); - if (collision == null || vec.distanceTo(posVec) < collision.distanceTo(posVec)) { - collision = optional.get(); - } - } - } - - Vec3d rotationVec = rotation.getRayVector(); - Vec3d start = rotationVec.add(posVec); - Vec3d end = rotationVec.multiply(Config.DEFENSIVE_LASER_RANGE).add(posVec); - HitResult result = world.rayTrace(new RayTraceContext(start, end, RayTraceContext.ShapeType.COLLIDER, RayTraceContext.FluidHandling.ANY, getDummyEntity())); - if (result.getType() != HitResult.Type.MISS && (collision == null || result.getPos().distanceTo(posVec) < collision.distanceTo(posVec))) { - collision = result.getPos(); - } - - double distance; - if (collision != null) { - world.createExplosion(null, collision.getX(), collision.getY(), collision.getZ(), 4, Explosion.DestructionType.NONE); - - distance = posVec.distanceTo(collision); - } else { - distance = Config.DEFENSIVE_LASER_RANGE; - } - - double multiplier = 6d; - distance = distance * multiplier; - for (int i = 0; i < distance; i++) { - Vec3d vec = rotationVec.multiply(i / multiplier).add(posVec); - ((ServerWorld) getWorld()).spawnParticles(ParticleTypes.END_ROD, vec.getX(), vec.getY(), vec.getZ(), 1, 0, 0, 0, 1); - } + fire(world, pos); }, (world, pos, state) -> firing = false)); target = null; firing = true;