Skip to content

Commit 2e890f9

Browse files
committed
Added command '/pc addcommand <cmd>' to execute command while displaying
a camera path
1 parent 4366518 commit 2e890f9

9 files changed

Lines changed: 145 additions & 14 deletions

File tree

PowerCamera/src/nl/svenar/powercamera/CameraHandler.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package nl.svenar.powercamera;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.List;
56

7+
import org.bukkit.Bukkit;
68
import org.bukkit.ChatColor;
79
import org.bukkit.GameMode;
810
import org.bukkit.Location;
@@ -23,6 +25,7 @@ public class CameraHandler extends BukkitRunnable {
2325
private String camera_name;
2426

2527
private ArrayList<Location> camera_path_points = new ArrayList<Location>();
28+
private HashMap<Integer, ArrayList<String>> camera_path_commands = new HashMap<Integer, ArrayList<String>>();
2629

2730
private GameMode previous_gamemode;
2831
private Location previous_player_location;
@@ -38,23 +41,55 @@ public CameraHandler generatePath() {
3841
int max_points = (this.plugin.getConfigCameras().getDuration(this.camera_name) * 1000) / this.single_frame_duration_ms;
3942

4043
List<String> raw_camera_points = this.plugin.getConfigCameras().getPoints(this.camera_name);
44+
List<String> raw_camera_move_points = getMovementPoints(raw_camera_points);
4145

42-
for (int i = 0; i < raw_camera_points.size() - 1; i++) {
43-
String raw_point = raw_camera_points.get(i);
44-
String raw_point_next = raw_camera_points.get(i + 1);
46+
for (int i = 0; i < raw_camera_move_points.size() - 1; i++) {
47+
String raw_point = raw_camera_move_points.get(i);
48+
String raw_point_next = raw_camera_move_points.get(i + 1);
4549

4650
Location point = Util.deserializeLocation(raw_point);
4751
Location point_next = Util.deserializeLocation(raw_point_next);
52+
4853

4954
this.camera_path_points.add(point);
50-
for (int j = 0; j < max_points / (raw_camera_points.size() - 1) - 1; j++) {
51-
this.camera_path_points.add(translateLinear(point, point_next, j, max_points / (raw_camera_points.size() - 1) - 1));
55+
for (int j = 0; j < max_points / (raw_camera_move_points.size() - 1) - 1; j++) {
56+
this.camera_path_points.add(translateLinear(point, point_next, j, max_points / (raw_camera_move_points.size() - 1) - 1));
57+
}
58+
}
59+
60+
int command_index = 0;
61+
for (String raw_point : raw_camera_points) {
62+
String type = raw_point.split(":", 2)[0];
63+
String data = raw_point.split(":", 2)[1];
64+
65+
if (type.equalsIgnoreCase("location")) {
66+
command_index += 1;
67+
}
68+
69+
if (type.equalsIgnoreCase("command")) {
70+
int index = ((command_index) * max_points / (raw_camera_move_points.size()) - 1);
71+
index = command_index == 0 ? 0 : index - 1;
72+
index = index < 0 ? 0 : index;
73+
if (!this.camera_path_commands.containsKey(index)) this.camera_path_commands.put(index, new ArrayList<String>());
74+
this.camera_path_commands.get(index).add(data);
75+
// this.camera_path_commands.put(index, raw_camera_points.get(0));
5276
}
5377
}
5478

5579
return this;
5680
}
5781

82+
private List<String> getMovementPoints(List<String> raw_camera_points) {
83+
List<String> output = new ArrayList<String>();
84+
for (String raw_point : raw_camera_points) {
85+
String[] point_data = raw_point.split(":", 2);
86+
if (point_data[0].equalsIgnoreCase("location")) {
87+
output.add(point_data[1]);
88+
}
89+
}
90+
return output;
91+
}
92+
5893
private Location translateLinear(Location point, Location point_next, int progress, int progress_max) {
5994
if (!point.getWorld().getUID().toString().equals(point_next.getWorld().getUID().toString())) {
6095
return point_next;
@@ -128,6 +163,13 @@ public void run() {
128163
Location next_point = camera_path_points.get(this.ticks + 1);
129164

130165
player.teleport(camera_path_points.get(this.ticks));
166+
167+
if (camera_path_commands.containsKey(this.ticks)) {
168+
for (String cmd : camera_path_commands.get(this.ticks)) {
169+
String command = cmd.replaceAll("%player%", player.getName());
170+
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
171+
}
172+
}
131173

132174
player.setVelocity(calculateVelocity(current_pos, next_point));
133175

@@ -154,13 +196,18 @@ public CameraHandler preview(Player player, int num, int preview_time) {
154196

155197
if (num > camera_points.size() - 1)
156198
num = camera_points.size() - 1;
199+
200+
if (!camera_points.get(num).split(":", 2)[0].equalsIgnoreCase("location")) {
201+
player.sendMessage(plugin.getPluginChatPrefix() + ChatColor.RED + "Point " + (num + 1) + " is not a location!");
202+
return this;
203+
}
157204

158205
player.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "Preview started of point " + (num + 1) + "!");
159206
player.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "Ending in " + preview_time + " seconds.");
160207

161208
previous_gamemode = player.getGameMode();
162209
previous_player_location = player.getLocation();
163-
Location point = Util.deserializeLocation(camera_points.get(num));
210+
Location point = Util.deserializeLocation(camera_points.get(num).split(":", 2)[1]);
164211
previous_invisible = player.isInvisible();
165212

166213
plugin.player_camera_mode.put(player.getUniqueId(), PowerCamera.CAMERA_MODE.PREVIEW);

PowerCamera/src/nl/svenar/powercamera/PowerCamera.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.Arrays;
66
import java.util.HashMap;
7+
import java.util.List;
78
import java.util.UUID;
89

910
import org.bukkit.Bukkit;
@@ -93,6 +94,19 @@ private void setupConfig() {
9394
config_plugin.getConfig().set("on-join.show-once", true);
9495
config_plugin.getConfig().set("on-new-player-join-camera-path", null);
9596
}
97+
98+
for (String camera_name : config_cameras.getCameras()) {
99+
List<String> points = config_cameras.getPoints(camera_name);
100+
List<String> new_points = new ArrayList<String>();
101+
for (String point : points) {
102+
if (point.contains(":")) {
103+
new_points.add(point);
104+
} else {
105+
new_points.add("location:" + point);
106+
}
107+
}
108+
config_cameras.getConfig().set("cameras." + camera_name + ".points", new_points);
109+
}
96110

97111
config_plugin.getConfig().set("version", getPluginDescriptionFile().getVersion());
98112
config_plugin.saveConfig();

PowerCamera/src/nl/svenar/powercamera/commands/MainCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public MainCommand(PowerCamera plugin) {
2626
new cmd_create(plugin, "create");
2727
new cmd_remove(plugin, "remove");
2828
new cmd_addpoint(plugin, "addpoint");
29+
new cmd_addcommand(plugin, "addcommand");
2930
new cmd_delpoint(plugin, "delpoint");
3031
new cmd_select(plugin, "select");
3132
new cmd_preview(plugin, "preview");
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package nl.svenar.powercamera.commands;
2+
3+
import org.bukkit.ChatColor;
4+
import org.bukkit.command.Command;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Player;
7+
8+
import nl.svenar.powercamera.PowerCamera;
9+
10+
public class cmd_addcommand extends PowerCameraCommand {
11+
12+
public cmd_addcommand(PowerCamera plugin, String command_name) {
13+
super(plugin, command_name, COMMAND_EXECUTOR.PLAYER);
14+
}
15+
16+
@Override
17+
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
18+
if (sender.hasPermission("powercamera.cmd.addpoint")) {
19+
if (args.length > 0) {
20+
String camera_name = plugin.player_selected_camera.get(((Player) sender).getUniqueId());
21+
if (camera_name != null) {
22+
String command = String.join(" ", args);
23+
plugin.getConfigCameras().camera_addcommand(command, camera_name);
24+
sender.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "Command added to camera '" + camera_name + "'!");
25+
} else {
26+
sender.sendMessage(plugin.getPluginChatPrefix() + ChatColor.RED + "No camera selected!");
27+
sender.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "Select a camera by doing: /" + commandLabel + " select <name>");
28+
}
29+
30+
} else {
31+
sender.sendMessage(plugin.getPluginChatPrefix() + ChatColor.DARK_RED + "Usage: /" + commandLabel + " addcommand <command>");
32+
}
33+
34+
} else {
35+
sender.sendMessage(plugin.getPluginChatPrefix() + ChatColor.DARK_RED + "You do not have permission to execute this command");
36+
}
37+
38+
return false;
39+
}
40+
}

PowerCamera/src/nl/svenar/powercamera/commands/cmd_create.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class cmd_create extends PowerCameraCommand {
1111

1212
public cmd_create(PowerCamera plugin, String command_name) {
13-
super(plugin, command_name, COMMAND_EXECUTOR.ALL);
13+
super(plugin, command_name, COMMAND_EXECUTOR.PLAYER);
1414
}
1515

1616
@Override

PowerCamera/src/nl/svenar/powercamera/commands/cmd_info.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,27 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
3030
sender.sendMessage(ChatColor.DARK_GREEN + "Camera name: " + ChatColor.GREEN + camera_name);
3131
sender.sendMessage(ChatColor.DARK_GREEN + "Path duration: " + ChatColor.GREEN + camera_duration + " seconds");
3232
sender.sendMessage(ChatColor.DARK_GREEN + "Camera points (" + ChatColor.GREEN + camera_points.size() + ChatColor.DARK_GREEN + "):");
33-
for (String point : camera_points) {
33+
34+
int index = 0;
35+
for (String raw_point : camera_points) {
36+
index++;
37+
38+
String type = raw_point.split(":", 2)[0];
39+
String point = raw_point.split(":", 2)[1];
40+
3441
String point_info = "";
35-
Location point_location = Util.deserializeLocation(point);
42+
point_info += "#" + index + " ";
43+
point_info += type + ": ";
3644

37-
point_info += point_location.getWorld().getName();
38-
point_info += ", (X: " + point_location.getBlockX() + ", Y: " + point_location.getBlockY() + ", Z: " + point_location.getBlockZ() + ")";
39-
point_info += ", (Yaw: " + Math.round(point_location.getYaw()) + ", Pitch: " + Math.round(point_location.getPitch()) + ")";
45+
if (type.equalsIgnoreCase("location")) {
46+
Location point_location = Util.deserializeLocation(point);
47+
48+
point_info += point_location.getWorld().getName();
49+
point_info += ", (X: " + point_location.getBlockX() + ", Y: " + point_location.getBlockY() + ", Z: " + point_location.getBlockZ() + ")";
50+
point_info += ", (Yaw: " + Math.round(point_location.getYaw()) + ", Pitch: " + Math.round(point_location.getPitch()) + ")";
51+
} else {
52+
point_info += point;
53+
}
4054

4155
sender.sendMessage(ChatColor.DARK_GREEN + "- " + ChatColor.GREEN + point_info);
4256
}

PowerCamera/src/nl/svenar/powercamera/commands/cmd_remove.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class cmd_remove extends PowerCameraCommand {
1010

1111
public cmd_remove(PowerCamera plugin, String command_name) {
12-
super(plugin, command_name, COMMAND_EXECUTOR.ALL);
12+
super(plugin, command_name, COMMAND_EXECUTOR.PLAYER);
1313
}
1414

1515
@Override

PowerCamera/src/nl/svenar/powercamera/config/CameraStorage.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,20 @@ public void camera_addpoint(Location location, String camera_name) {
9999
if (!camera_exists(camera_name))
100100
return;
101101

102-
String new_point = Util.serializeLocation(location);
102+
String new_point = "location:" + Util.serializeLocation(location);
103+
104+
List<String> camera_points = getConfig().getStringList("cameras." + get_camera_name_ignorecase(camera_name) + ".points");
105+
camera_points.add(new_point);
106+
107+
getConfig().set("cameras." + get_camera_name_ignorecase(camera_name) + ".points", camera_points);
108+
saveConfig();
109+
}
110+
111+
public void camera_addcommand(String command, String camera_name) {
112+
if (!camera_exists(camera_name))
113+
return;
114+
115+
String new_point = "command:" + command;
103116

104117
List<String> camera_points = getConfig().getStringList("cameras." + get_camera_name_ignorecase(camera_name) + ".points");
105118
camera_points.add(new_point);

PowerCamera/src/nl/svenar/powercamera/events/ChatTabExecutor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String alia
2727
commands_list.add("create");
2828
commands_list.add("remove");
2929
commands_list.add("addpoint");
30+
commands_list.add("addcommand");
3031
commands_list.add("delpoint");
3132
commands_list.add("select");
3233
commands_list.add("preview");
3334
commands_list.add("info");
3435
commands_list.add("setduration");
3536
commands_list.add("start");
37+
commands_list.add("stop");
3638
commands_list.add("stats");
3739

3840
for (String command : commands_list) {

0 commit comments

Comments
 (0)