From 43cdd95a3385537989180de5ab9f08d14cfce5e8 Mon Sep 17 00:00:00 2001 From: gdamms Date: Wed, 18 May 2022 10:05:52 +0200 Subject: [PATCH] feat: exec with velocity --- src/main/java/com/tocard/cam/Camera.java | 1 + .../java/com/tocard/cam/ExecuteTraveling.java | 33 ++++++--- src/main/java/com/tocard/cam/Move.java | 74 +++++++++++++++++++ src/main/resources/plugin.yml | 4 + 4 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/tocard/cam/Move.java diff --git a/src/main/java/com/tocard/cam/Camera.java b/src/main/java/com/tocard/cam/Camera.java index d109b7c..ea2da7e 100644 --- a/src/main/java/com/tocard/cam/Camera.java +++ b/src/main/java/com/tocard/cam/Camera.java @@ -42,6 +42,7 @@ public class Camera extends JavaPlugin implements Listener { this.getCommand("close").setExecutor(new ClosePath()); this.getCommand("load").setExecutor(new LoadPath()); this.getCommand("save").setExecutor(new SavePath()); + this.getCommand("move").setExecutor(new Move()); // Eventhandlers getServer().getPluginManager().registerEvents(this, this); diff --git a/src/main/java/com/tocard/cam/ExecuteTraveling.java b/src/main/java/com/tocard/cam/ExecuteTraveling.java index 4b43874..8be8231 100644 --- a/src/main/java/com/tocard/cam/ExecuteTraveling.java +++ b/src/main/java/com/tocard/cam/ExecuteTraveling.java @@ -5,28 +5,34 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.Bukkit; + import java.util.Iterator; public class ExecuteTraveling implements CommandExecutor { private static int taskID = -1; private static Iterator curveIterator; + private static boolean useVelocity = false; @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (sender instanceof Player) { Player player = (Player) sender; - int fromIndex, toIndex; - try { - fromIndex = Integer.parseInt(args[0]); - } catch (Exception e) { - fromIndex = 0; - } - try { - toIndex = Integer.parseInt(args[1]); - } catch (Exception e) { - toIndex = (Camera.controlPoints.size() - 1) / 3; + int fromIndex = 0; + int toIndex = (Camera.controlPoints.size() - 1) / 3; + useVelocity = false; + if (args.length > 0) { + // Use moveTo intead of teleport + useVelocity = Boolean.parseBoolean(args[0]); + + // Get starting and ending points + int ind = useVelocity ? 1 : 0; + try { + fromIndex = Integer.parseInt(args[ind]); + toIndex = Integer.parseInt(args[ind + 1]); + } catch (Exception e) { + } } if (taskID >= 0) { @@ -39,14 +45,17 @@ public class ExecuteTraveling implements CommandExecutor { curveIterator = Camera.curve.subList(fromIndex * (Camera.nbSubdiv + 1), toIndex * (Camera.nbSubdiv + 1)) .iterator(); + player.teleport(curveIterator.next()); taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Camera.plugin, new Runnable() { @Override public void run() { // Teleport to next point if (curveIterator.hasNext()) { - ExtendedLocation loc = curveIterator.next(); - player.teleport(loc); + if (useVelocity) + Move.moveTo(player, curveIterator.next()); + else + player.teleport(curveIterator.next()); return; } diff --git a/src/main/java/com/tocard/cam/Move.java b/src/main/java/com/tocard/cam/Move.java new file mode 100644 index 0000000..a5ec452 --- /dev/null +++ b/src/main/java/com/tocard/cam/Move.java @@ -0,0 +1,74 @@ +package com.tocard.cam; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +public class Move implements CommandExecutor { + + public static int taskID = -1; + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (sender instanceof Player) { + if (sender instanceof Player) { + Player player = (Player) sender; + + if (taskID >= 0) { + // Cancel show task + Bukkit.getScheduler().cancelTask(taskID); + taskID = -1; + return true; + } + + double x = Float.parseFloat(args[0]); + double y = Float.parseFloat(args[1]); + double z = Float.parseFloat(args[2]); + + // Start showing curve + taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Camera.plugin, + new Runnable() { + @Override + public void run() { + moveTo(player, x, y, z); + } + }, 0, 0); + + // Broadcast TaskID + Bukkit.broadcastMessage("Show curve : " + taskID); + + } + + return true; + } + return true; + } + + public static void moveTo(Player player, double x, double y, double z) { + Location delta = player.getLocation(); + double dx = x - delta.getX(); + double dy = y - delta.getY(); + double dz = z - delta.getZ(); + + player.setVelocity(new Vector(dx, dy, dz).multiply(0.1)); + } + + public static void moveTo(Player player, Location location) { + // rotateTo(player, location); + Location delta = location.clone().subtract(player.getLocation()); + player.setVelocity(delta.toVector().multiply(0.1)); + } + + public static void rotateTo(Player player, Location location) { + Location playerLocation = player.getLocation(); + playerLocation.setYaw(location.getYaw()); + playerLocation.setPitch(location.getPitch()); + player.teleport(playerLocation); + Camera.broadlog(Camera.prettyLocation(playerLocation)); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index f9eb614..e644d49 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -37,3 +37,7 @@ commands: description: Save current path usage: /save permission: com.tocard.cam.savePath + move: + description: Move player to x y z + usage: /move + permission: com.tocard.cam.move