From b25cd1323f635caaaeeb8455333a699120b12e3a Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Thu, 9 Nov 2023 20:39:32 +0100 Subject: [PATCH] Fixed laser and spot movement --- .../api/triggers/impl/LaserTrigger.java | 11 +++- .../api/triggers/impl/SpotTrigger.java | 11 +++- .../showcontrol/elements/Lasers.java | 18 +++--- .../showcontrol/elements/Spots.java | 59 +++++-------------- 4 files changed, 41 insertions(+), 58 deletions(-) diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LaserTrigger.java b/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LaserTrigger.java index 8cac4c7..7805bb4 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LaserTrigger.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LaserTrigger.java @@ -15,10 +15,11 @@ import java.util.List; import java.util.stream.Collectors; @NoArgsConstructor(force = true) -@TriggerIdentifier(value = "laser", minArgs = 5, argDesc = " ") +@TriggerIdentifier(value = "laser", minArgs = 5, argDesc = " [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 diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/SpotTrigger.java b/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/SpotTrigger.java index 0e0c504..ca7e9c1 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/SpotTrigger.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/SpotTrigger.java @@ -15,10 +15,11 @@ import java.util.List; import java.util.stream.Collectors; @NoArgsConstructor(force = true) -@TriggerIdentifier(value = "spot", minArgs = 5, argDesc = " ") +@TriggerIdentifier(value = "spot", minArgs = 5, argDesc = " [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 diff --git a/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java b/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java index 046c190..ff7407f 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java @@ -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(); diff --git a/src/main/java/tech/sbdevelopment/showcontrol/elements/Spots.java b/src/main/java/tech/sbdevelopment/showcontrol/elements/Spots.java index 0430a4e..a3767cc 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/elements/Spots.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/elements/Spots.java @@ -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();