feat: exec with velocity

This commit is contained in:
gdamms 2022-05-18 10:05:52 +02:00
parent e9824aa78c
commit 43cdd95a33
4 changed files with 100 additions and 12 deletions

View file

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

View file

@ -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<ExtendedLocation> 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;
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[0]);
fromIndex = Integer.parseInt(args[ind]);
toIndex = Integer.parseInt(args[ind + 1]);
} catch (Exception e) {
fromIndex = 0;
}
try {
toIndex = Integer.parseInt(args[1]);
} catch (Exception e) {
toIndex = (Camera.controlPoints.size() - 1) / 3;
}
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;
}

View file

@ -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));
}
}

View file

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