Improve
All checks were successful
EnergonRelics/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-07-16 21:42:15 -04:00
parent befb1eae2f
commit a6dc65027d

View File

@ -15,10 +15,12 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.hit.HitResult; import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box; import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RayTraceContext; import net.minecraft.world.RayTraceContext;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion; import net.minecraft.world.explosion.Explosion;
import java.util.Comparator; import java.util.Comparator;
@ -32,8 +34,12 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
public final Rotation rotation = new Rotation(0, 0, "Rotation"); 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() { private Vec3d getPosVec() {
return new Vec3d(getPos().getX() + 0.5d, getPos().getY() + 0.5d, getPos().getZ() + 0.5d); return getPosVec(getPos());
} }
@Override @Override
@ -176,6 +182,45 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
return new ItemEntity(getWorld(), posVec.getX(), posVec.getY(), posVec.getZ()); return new ItemEntity(getWorld(), posVec.getX(), posVec.getY(), posVec.getZ());
} }
private void fire(World world, BlockPos pos) {
List<LivingEntity> entities = getEntities(entity -> true);
Vec3d posVec = getPosVec(pos);
Vec3d collision = null;
for (LivingEntity entity : entities) {
Optional<Vec3d> 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 @Override
protected void tickEnergy() { protected void tickEnergy() {
assert getWorld() != null; assert getWorld() != null;
@ -187,43 +232,7 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
if (countdown == 1) { if (countdown == 1) {
addAction(new Action(Config.DEFENSIVE_LASER_FIRE_ENERGY_REQUIRED, (world, pos, state) -> { addAction(new Action(Config.DEFENSIVE_LASER_FIRE_ENERGY_REQUIRED, (world, pos, state) -> {
firing = false; firing = false;
fire(world, pos);
List<LivingEntity> entities = getEntities(entity -> true);
Vec3d posVec = getPosVec();
Vec3d collision = null;
for (LivingEntity entity : entities) {
Optional<Vec3d> 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);
}
}, (world, pos, state) -> firing = false)); }, (world, pos, state) -> firing = false));
target = null; target = null;
firing = true; firing = true;