feat: /close

This commit is contained in:
gdamms 2022-05-14 21:44:55 +02:00
parent 1aa618865c
commit e5a28d45a3
7 changed files with 152 additions and 101 deletions

View file

@ -40,6 +40,7 @@ public class Camera extends JavaPlugin implements Listener {
this.getCommand("reset").setExecutor(new ClearPoints());
this.getCommand("exec").setExecutor(new ExecuteTraveling());
this.getCommand("show").setExecutor(new ShowCurve());
this.getCommand("close").setExecutor(new ClosePath());
// Eventhandlers
getServer().getPluginManager().registerEvents(this, this);

View file

@ -26,6 +26,7 @@ public class ClearPoints implements CommandExecutor {
Camera.controlPoints = new ArrayList<>();
Camera.compute();
ClosePath.closed = false;
Camera.broadlog("All cleared !");
for (World world : Bukkit.getWorlds()) {

View file

@ -0,0 +1,40 @@
package com.tocard.cam;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
public class ClosePath implements CommandExecutor {
public static boolean closed = false;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (!closed && Camera.controlPoints.size() >= 4) {
closed = true;
NewPoint.add(null, Camera.controlPoints.get(0), player.getWorld());
NewPoint.set(new String[] { "", "" + (Camera.controlPoints.size() - 2) },
Camera.controlPoints.get(0).clone().multiply(2)
.subtract(Camera.controlPoints.get(1)),
player.getWorld());
} else if (closed) {
closed = false;
NewPoint.rm(new String[] { "", "" + (Camera.controlPoints.size() - 1) }, null, null);
}
Camera.compute();
}
return true;
}
}

View file

@ -10,31 +10,46 @@ import java.util.Iterator;
public class ExecuteTraveling implements CommandExecutor {
private static int taskID = -1;
private static Iterator<Location> curveIterator;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
try {
Bukkit.getScheduler().cancelTask(Integer.parseInt(args[0]));
Player player = (Player) sender;
} catch (Exception e) {
Player player = (Player) sender;
if (taskID >= 0) {
// Cancel show task
Bukkit.getScheduler().cancelTask(taskID);
taskID = -1;
Iterator<Location> curveIterator = Camera.curve.iterator();
int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Camera.plugin, new Runnable() {
@Override
public void run() {
try {
player.teleport(curveIterator.next());
} catch (Exception e) {
Thread.currentThread().interrupt();
}
}
}, 0, 0);
Bukkit.broadcastMessage("" + taskID);
return true;
}
curveIterator = Camera.curve.iterator();
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Camera.plugin, new Runnable() {
@Override
public void run() {
// Teleport to next point
if (curveIterator.hasNext()) {
player.teleport(curveIterator.next());
return;
}
// No more point, end traveling
if (!ClosePath.closed) {
Thread.currentThread().interrupt();
}
// Regenerate traveling and begin
curveIterator = Camera.curve.iterator();
player.teleport(curveIterator.next());
}
}, 0, 0);
Bukkit.broadcastMessage("Traveling : " + taskID);
}
return true;
}
}

View file

@ -9,24 +9,20 @@ import org.bukkit.entity.Player;
public class NewPoint implements CommandExecutor {
private static Location location;
private static Player player;
private static int index;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
try {
if (sender instanceof Player) {
player = (Player) sender;
Player player = (Player) sender;
switch (args[0]) {
case "add":
add(args);
add(args, player.getLocation(), player.getWorld());
break;
case "rm":
rm(args);
rm(args, player.getLocation(), player.getWorld());
break;
case "set":
set(args);
set(args, player.getLocation(), player.getWorld());
break;
default:
break;
@ -55,14 +51,12 @@ public class NewPoint implements CommandExecutor {
}
public static void rmPoint(int index) {
location = Camera.controlPoints.remove(index);
Location location = Camera.controlPoints.remove(index);
ShowCurve.rm(index);
Camera.broadlog("Point deleted: " + Camera.prettyLocation(location));
}
public static void add(String[] args) {
World world = player.getWorld();
location = player.getLocation();
public static void add(String[] args, Location location, World world) {
int n = Camera.controlPoints.size();
if (n < 4) {
@ -80,16 +74,15 @@ public class NewPoint implements CommandExecutor {
}
@Deprecated
public static void ins(String[] args) {
index = Integer.parseInt(args[1]);
location = player.getLocation();
public static void ins(String[] args, Location location, World world) {
int index = Integer.parseInt(args[1]);
Camera.controlPoints.add(index, location);
Camera.broadlog("Point added: " + Camera.prettyLocation(location));
ShowCurve.insert(index, location, player.getWorld());
ShowCurve.insert(index, location, world);
}
public static void rm(String[] args) {
index = Integer.parseInt(args[1]);
public static void rm(String[] args, Location location, World world) {
int index = Integer.parseInt(args[1]);
int n = Camera.controlPoints.size();
if (n <= 4) {
@ -109,13 +102,13 @@ public class NewPoint implements CommandExecutor {
for (int i = 0; i < 3; i++) {
rmPoint(indMin);
}
return;
}
public static void set(String[] args) {
World world = player.getWorld();
public static void set(String[] args, Location location, World world) {
int n = Camera.controlPoints.size();
index = Integer.parseInt(args[1]);
location = player.getLocation();
int index = Integer.parseInt(args[1]);
if (n <= 4) {
setPoint(index, location, world);

View file

@ -22,80 +22,77 @@ public class ShowCurve implements CommandExecutor {
public static List<ArmorStand> controlPointsArmorStands = new ArrayList<>();
private static ItemStack cameraHeadAnchor = new ItemStack(Material.OBSERVER);
private static ItemStack cameraHeadControl = new ItemStack(Material.DISPENSER);
public static int showTaskID = -1;
private static int taskID = -1;
// This method is called, when somebody uses our command
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
try {
Bukkit.getScheduler().cancelTask(Integer.parseInt(args[0]));
Player player = (Player) sender;
} catch (Exception e) {
Player player = (Player) sender;
if (taskID >= 0) {
// Cancel show task
Bukkit.getScheduler().cancelTask(taskID);
taskID = -1;
if (ShowCurve.showTaskID < 0) {
// Show control points
int i = 0;
for (ArmorStand as : controlPointsArmorStands) {
if (i % 3 == 0)
as.getEquipment().setHelmet(cameraHeadAnchor);
else
as.getEquipment().setHelmet(cameraHeadControl);
i++;
}
// Kill control points' armorstand
for (ArmorStand as : controlPointsArmorStands) {
as.getEquipment().setHelmet(new ItemStack(Material.AIR));
}
return true;
}
// Start showing curve
ShowCurve.showTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Camera.plugin,
new Runnable() {
@Override
public void run() {
// Get curve iterator
Iterator<Location> curveIterator = Camera.curve.iterator();
// Show control points
int i = 0;
for (ArmorStand as : controlPointsArmorStands) {
if (i % 3 == 0)
as.getEquipment().setHelmet(cameraHeadAnchor);
else
as.getEquipment().setHelmet(cameraHeadControl);
i++;
}
// Draw path
while (curveIterator.hasNext()) {
player.getWorld().spawnParticle(Particle.ELECTRIC_SPARK,
curveIterator.next().clone().add(0, 1.8, 0), 1,
// Start showing curve
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Camera.plugin,
new Runnable() {
@Override
public void run() {
// Get curve iterator
Iterator<Location> curveIterator = Camera.curve.iterator();
// Draw path
while (curveIterator.hasNext()) {
player.getWorld().spawnParticle(Particle.ELECTRIC_SPARK,
curveIterator.next().clone().add(0, 1.8, 0), 1,
0, 0, 0, 0);
}
// Draw control lines
if (Camera.controlPoints.size() >= 4) {
int nAnchor = (Camera.controlPoints.size() - 1) / 3 + 1;
for (int i = 0; i < nAnchor; i++) {
int indMin = Math.max(3 * i - 1, 0);
int indMax = Math.min(3 * i + 1, Camera.controlPoints.size() - 1);
for (float t = 0; t < 1; t += 0.05) {
player.getWorld().spawnParticle(Particle.COMPOSTER,
Camera.controlPoints.get(indMin).clone().multiply(1 - t)
.add(Camera.controlPoints.get(indMax).clone()
.multiply(t))
.add(0, 1.8, 0),
1,
0, 0, 0, 0);
}
// Draw control lines
if (Camera.controlPoints.size() >= 4) {
int nAnchor = (Camera.controlPoints.size() - 1) / 3 + 1;
for (int i = 0; i < nAnchor; i++) {
int indMin = Math.max(3 * i - 1, 0);
int indMax = Math.min(3 * i + 1, Camera.controlPoints.size() - 1);
for (float t = 0; t < 1; t += 0.05) {
player.getWorld().spawnParticle(Particle.COMPOSTER,
Camera.controlPoints.get(indMin).clone().multiply(1 - t)
.add(Camera.controlPoints.get(indMax).clone()
.multiply(t))
.add(0, 1.8, 0),
1,
0, 0, 0, 0);
}
}
}
}
}, 0, 0);
}
}
}, 0, 0);
// Broadcast TaskID
Bukkit.broadcastMessage("Show curve : " + ShowCurve.showTaskID);
// Broadcast TaskID
Bukkit.broadcastMessage("Show curve : " + taskID);
} else {
// Cancel show task
Bukkit.getScheduler().cancelTask(ShowCurve.showTaskID);
ShowCurve.showTaskID = -1;
// Kill control points' armorstand
for (ArmorStand as : controlPointsArmorStands) {
as.getEquipment().setHelmet(new ItemStack(Material.AIR));
}
}
}
}
return true;
}
@ -105,7 +102,7 @@ public class ShowCurve implements CommandExecutor {
as.setVisible(false);
as.setMarker(true);
as.addScoreboardTag("controlPoint");
if (showTaskID > 0)
if (taskID > 0)
if ((Camera.controlPoints.size() - 1) % 3 == 0)
as.getEquipment().setHelmet(cameraHeadAnchor);
else
@ -119,7 +116,7 @@ public class ShowCurve implements CommandExecutor {
as.setVisible(false);
as.setMarker(true);
as.addScoreboardTag("controlPoint");
if (showTaskID > 0)
if (taskID > 0)
if ((Camera.controlPoints.size() - 1) % 3 == 0)
as.getEquipment().setHelmet(cameraHeadAnchor);
else

View file

@ -24,4 +24,8 @@ commands:
show:
description: Show the traveling path
usage: /show
permission: com.tocard.cam.showCurve
permission: com.tocard.cam.showPath
close:
description: Close path
usage: /close
permission: com.tocard.cam.closeMath