Fixed laser and spot movement

This commit is contained in:
Stijn Bannink 2023-11-09 20:39:32 +01:00
parent 8192d8e0f4
commit b25cd1323f
4 changed files with 41 additions and 58 deletions

View file

@ -15,10 +15,11 @@ import java.util.List;
import java.util.stream.Collectors;
@NoArgsConstructor(force = true)
@TriggerIdentifier(value = "laser", minArgs = 5, argDesc = "<name> <world> <x> <y> <z>")
@TriggerIdentifier(value = "laser", minArgs = 5, argDesc = "<name> <world> <x> <y> <z> [speed]")
public class LaserTrigger extends Trigger {
private final String name;
private final Location newLocation;
private final double speed;
public LaserTrigger(String[] data) throws InvalidArgumentException {
super(data);
@ -43,6 +44,12 @@ public class LaserTrigger extends Trigger {
this.newLocation = new Location(w, x, y, z);
try {
this.speed = data.length >= 6 ? Double.parseDouble(data[5]) : 0.1;
} catch (NumberFormatException ex) {
throw new InvalidArgumentException("Provided speed in LaserTrigger is invalid!");
}
if (!Lasers.exists(name)) {
Lasers.start(name, newLocation);
}
@ -50,7 +57,7 @@ public class LaserTrigger extends Trigger {
@Override
public void trigger() {
Lasers.move(name, newLocation);
Lasers.move(name, newLocation, speed);
}
@Override

View file

@ -15,10 +15,11 @@ import java.util.List;
import java.util.stream.Collectors;
@NoArgsConstructor(force = true)
@TriggerIdentifier(value = "spot", minArgs = 5, argDesc = "<name> <world> <x> <y> <z>")
@TriggerIdentifier(value = "spot", minArgs = 5, argDesc = "<name> <world> <x> <y> <z> [speed]")
public class SpotTrigger extends Trigger {
private final String name;
private final Location newLocation;
private final double speed;
public SpotTrigger(String[] data) throws InvalidArgumentException {
super(data);
@ -43,6 +44,12 @@ public class SpotTrigger extends Trigger {
this.newLocation = new Location(w, x, y, z);
try {
this.speed = data.length >= 6 ? Double.parseDouble(data[5]) : 0.1;
} catch (NumberFormatException ex) {
throw new InvalidArgumentException("Provided speed in SpotTrigger is invalid!");
}
if (!Spots.exists(name)) {
Spots.start(name, newLocation);
}
@ -50,7 +57,7 @@ public class SpotTrigger extends Trigger {
@Override
public void trigger() {
Spots.move(name, newLocation);
Spots.move(name, newLocation, speed);
}
@Override

View file

@ -2,6 +2,7 @@ package tech.sbdevelopment.showcontrol.elements;
import fr.skytasul.guardianbeam.Laser;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.scheduler.BukkitRunnable;
import tech.sbdevelopment.showcontrol.ShowControlPlugin;
@ -38,6 +39,7 @@ public class Lasers {
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
Bukkit.getLogger().info("Spawning laser " + name + " at " + baseLoc);
return false;
}
@ -48,13 +50,11 @@ public class Lasers {
* @param posLoc The new location
* @return true if done, false if it doesn't exists
*/
public static boolean move(String name, Location posLoc) {
public static boolean move(String name, Location posLoc, double speed) {
if (!lasers.containsKey(name)) return false;
LaserRunnable laser = lasers.get(name);
new BukkitRunnable() {
private final double speed = 0.1;
@Override
public void run() {
// Calculate the change in x, y, and z for this tick
@ -67,8 +67,9 @@ public class Lasers {
laser.changePositionLocation(laser.posLoc);
// Check if the laser has reached the target location
if (laser.posLoc.distanceSquared(posLoc) < 0.01) {
// Laser has reached the target, stop the task
double tolerance = 0.05;
if (Math.abs(deltaX) < tolerance && Math.abs(deltaY) < tolerance && Math.abs(deltaZ) < tolerance) {
// Laser movement is very small, stop the task
this.cancel();
}
}
@ -86,16 +87,14 @@ public class Lasers {
private static class LaserRunnable extends BukkitRunnable {
private final Laser laser;
private final String name;
private final Location baseLoc;
private Location posLoc;
public LaserRunnable(String name, Location baseLoc) throws ReflectiveOperationException {
this.name = name;
this.baseLoc = baseLoc;
this.laser = new Laser.GuardianLaser(baseLoc, baseLoc.add(0, 5, 0), -1, 50);
this.laser = new Laser.GuardianLaser(baseLoc, baseLoc.add(0, 1, 0), -1, 50);
this.laser.start(ShowControlPlugin.getInstance());
this.posLoc = baseLoc;
this.posLoc = baseLoc.add(0, 1, 0);
}
@Override
@ -103,7 +102,6 @@ public class Lasers {
if (posLoc == null) return;
try {
laser.moveStart(baseLoc);
laser.moveEnd(posLoc);
} catch (ReflectiveOperationException e) {
e.printStackTrace();

View file

@ -48,55 +48,28 @@ public class Spots {
* @param posLoc The new location
* @return true if done, false if it doesn't exists
*/
public static boolean move(String name, Location posLoc) {
public static boolean move(String name, Location posLoc, double speed) {
if (!spots.containsKey(name)) return false;
SpotRunnable laser = spots.get(name);
new BukkitRunnable() {
boolean fired = false;
Location oldLoc = laser.posLoc;
@Override
public void run() {
if (oldLoc.getBlockX() != posLoc.getBlockX()) {
if (oldLoc.getX() > posLoc.getX()) { //Increase of X
oldLoc = oldLoc.add(0.01, 0, 0);
} else {
oldLoc = oldLoc.add(-0.01, 0, 0);
}
fired = true;
} else {
fired = false;
}
// Calculate the change in x, y, and z for this tick
double deltaX = (posLoc.getX() - laser.posLoc.getX()) * speed;
double deltaY = (posLoc.getY() - laser.posLoc.getY()) * speed;
double deltaZ = (posLoc.getZ() - laser.posLoc.getZ()) * speed;
if (oldLoc.getBlockY() != posLoc.getBlockY()) {
if (oldLoc.getY() > posLoc.getY()) { //Increase of Y
oldLoc = oldLoc.add(0, 0.01, 0);
} else {
oldLoc = oldLoc.add(0, -0.01, 0);
}
fired = true;
} else {
fired = false;
}
// Update the spot's position
laser.posLoc.add(deltaX, deltaY, deltaZ);
laser.changePositionLocation(laser.posLoc);
if (oldLoc.getBlockZ() != posLoc.getBlockZ()) {
if (oldLoc.getZ() > posLoc.getZ()) { //Increase of Z
oldLoc = oldLoc.add(0, 0, 0.01);
} else {
oldLoc = oldLoc.add(0, 0, -0.01);
}
fired = true;
} else {
fired = false;
// Check if the spot has reached the target location
double tolerance = 0.05;
if (Math.abs(deltaX) < tolerance && Math.abs(deltaY) < tolerance && Math.abs(deltaZ) < tolerance) {
// Spot movement is very small, stop the task
this.cancel();
}
if (!fired) {
cancel();
return;
}
laser.changePositionLocation(oldLoc);
}
}.runTaskTimer(ShowControlPlugin.getInstance(), 0L, 1L);
return true;
@ -112,15 +85,14 @@ public class Spots {
private static class SpotRunnable extends BukkitRunnable {
private final Laser spot;
private final String name;
private final Location baseLoc;
private Location posLoc;
public SpotRunnable(String name, Location baseLoc) throws ReflectiveOperationException {
this.name = name;
this.baseLoc = baseLoc;
this.spot = new Laser.CrystalLaser(baseLoc, baseLoc.add(0, 5, 0), -1, 50);
this.spot = new Laser.CrystalLaser(baseLoc.add(0, 1, 0), baseLoc, -1, 50);
this.spot.start(ShowControlPlugin.getInstance());
this.posLoc = baseLoc.add(0, 1, 0);
}
@Override
@ -128,7 +100,6 @@ public class Spots {
if (posLoc == null) return;
try {
spot.moveStart(baseLoc);
spot.moveEnd(posLoc);
} catch (ReflectiveOperationException e) {
e.printStackTrace();