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; import java.util.stream.Collectors;
@NoArgsConstructor(force = true) @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 { public class LaserTrigger extends Trigger {
private final String name; private final String name;
private final Location newLocation; private final Location newLocation;
private final double speed;
public LaserTrigger(String[] data) throws InvalidArgumentException { public LaserTrigger(String[] data) throws InvalidArgumentException {
super(data); super(data);
@ -43,6 +44,12 @@ public class LaserTrigger extends Trigger {
this.newLocation = new Location(w, x, y, z); 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)) { if (!Lasers.exists(name)) {
Lasers.start(name, newLocation); Lasers.start(name, newLocation);
} }
@ -50,7 +57,7 @@ public class LaserTrigger extends Trigger {
@Override @Override
public void trigger() { public void trigger() {
Lasers.move(name, newLocation); Lasers.move(name, newLocation, speed);
} }
@Override @Override

View file

@ -15,10 +15,11 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@NoArgsConstructor(force = true) @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 { public class SpotTrigger extends Trigger {
private final String name; private final String name;
private final Location newLocation; private final Location newLocation;
private final double speed;
public SpotTrigger(String[] data) throws InvalidArgumentException { public SpotTrigger(String[] data) throws InvalidArgumentException {
super(data); super(data);
@ -43,6 +44,12 @@ public class SpotTrigger extends Trigger {
this.newLocation = new Location(w, x, y, z); 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)) { if (!Spots.exists(name)) {
Spots.start(name, newLocation); Spots.start(name, newLocation);
} }
@ -50,7 +57,7 @@ public class SpotTrigger extends Trigger {
@Override @Override
public void trigger() { public void trigger() {
Spots.move(name, newLocation); Spots.move(name, newLocation, speed);
} }
@Override @Override

View file

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

View file

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