From 8192d8e0f404510d1d47f5078b5ab80be99353a9 Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Mon, 6 Nov 2023 20:44:59 +0100 Subject: [PATCH] Implemented light trigger, started implementing fix on laser / spot movement --- pom.xml | 2 +- .../api/triggers/impl/LightTrigger.java | 86 +++++++++++++++++++ .../showcontrol/elements/Lasers.java | 51 +++-------- 3 files changed, 100 insertions(+), 39 deletions(-) create mode 100644 src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LightTrigger.java diff --git a/pom.xml b/pom.xml index 7cfaea4..2b15c22 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tech.sbdevelopment ShowControl - 1.4.1 + 1.5 jar ShowControl https://sbdevelopment.tech diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LightTrigger.java b/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LightTrigger.java new file mode 100644 index 0000000..a49a7ca --- /dev/null +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/triggers/impl/LightTrigger.java @@ -0,0 +1,86 @@ +package tech.sbdevelopment.showcontrol.api.triggers.impl; + +import lombok.NoArgsConstructor; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.data.Lightable; +import org.bukkit.entity.Player; +import tech.sbdevelopment.showcontrol.api.exceptions.InvalidArgumentException; +import tech.sbdevelopment.showcontrol.api.triggers.Trigger; +import tech.sbdevelopment.showcontrol.api.triggers.TriggerIdentifier; + +import java.util.List; +import java.util.stream.Collectors; + +@NoArgsConstructor(force = true) +@TriggerIdentifier(value = "light", minArgs = 5, argDesc = " ", item = Material.REDSTONE_LAMP) +public class LightTrigger extends Trigger { + private final Location location; + private final boolean state; + + public LightTrigger(String[] data) throws InvalidArgumentException { + super(data); + + World w = Bukkit.getWorld(data[0]); + if (w == null) { + throw new InvalidArgumentException("Provided World in LightTrigger is null!"); + } + + int x; + int y; + int z; + try { + x = Integer.parseInt(data[1]); + y = Integer.parseInt(data[2]); + z = Integer.parseInt(data[3]); + } catch (NumberFormatException ex) { + throw new InvalidArgumentException("Provided position in LightTrigger is invalid!"); + } + + this.location = new Location(w, x, y, z); + this.location.getBlock().setType(Material.REDSTONE_LAMP); + + this.state = parseBoolean(data[4]); + } + + @Override + public void trigger() { + setLightState(location.getBlock(), state); + } + + @Override + public List getArgumentTabComplete(Player player, int index, String arg) { + if (index == 0) { + return player != null ? List.of(player.getWorld().getName()) : Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList()); + } else if (index == 1) { + return player != null ? List.of(String.valueOf(player.getLocation().getBlockX())) : List.of(); + } else if (index == 2) { + return player != null ? List.of(String.valueOf(player.getLocation().getBlockY())) : List.of(); + } else if (index == 3) { + return player != null ? List.of(String.valueOf(player.getLocation().getBlockZ())) : List.of(); + } else if (index == 4) { + return List.of("on", "off"); + } + return List.of(); + } + + private static boolean parseBoolean(String value) { + return value.equalsIgnoreCase("true") + || value.equalsIgnoreCase("on") + || value.equalsIgnoreCase("yes") + || value.equalsIgnoreCase("1"); + } + + private static void setLightState(Block b, boolean state) { + if (!(b.getBlockData() instanceof Lightable)) { + b.setType(Material.REDSTONE_LAMP); + } + + Lightable light = (Lightable) b.getBlockData(); + light.setLit(state); + b.setBlockData(light); + } +} diff --git a/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java b/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java index c31e757..046c190 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/elements/Lasers.java @@ -53,50 +53,24 @@ public class Lasers { LaserRunnable laser = lasers.get(name); new BukkitRunnable() { - boolean fired = false; - Location oldLoc = laser.posLoc; + private final double speed = 0.1; @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 laser'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 laser has reached the target location + if (laser.posLoc.distanceSquared(posLoc) < 0.01) { + // Laser has reached the target, stop the task + this.cancel(); } - - if (!fired) { - cancel(); - return; - } - - laser.changePositionLocation(oldLoc); } }.runTaskTimer(ShowControlPlugin.getInstance(), 0L, 1L); return true; @@ -121,6 +95,7 @@ public class Lasers { this.baseLoc = baseLoc; this.laser = new Laser.GuardianLaser(baseLoc, baseLoc.add(0, 5, 0), -1, 50); this.laser.start(ShowControlPlugin.getInstance()); + this.posLoc = baseLoc; } @Override