Implemented light trigger, started implementing fix on laser / spot movement

This commit is contained in:
Stijn Bannink 2023-11-06 20:44:59 +01:00
parent d30510d371
commit 8192d8e0f4
3 changed files with 100 additions and 39 deletions

View file

@ -6,7 +6,7 @@
<groupId>tech.sbdevelopment</groupId>
<artifactId>ShowControl</artifactId>
<version>1.4.1</version>
<version>1.5</version>
<packaging>jar</packaging>
<name>ShowControl</name>
<url>https://sbdevelopment.tech</url>

View file

@ -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 = "<world> <x> <y> <z> <state>", 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<String> 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);
}
}

View file

@ -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