Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/samples/plugin/CommandVideoMap.java
8641 views
1
package plugin;
2
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileReader;
6
import java.io.IOException;
7
import java.util.ArrayList;
8
import java.util.List;
9
10
import org.bukkit.ChatColor;
11
import org.bukkit.Location;
12
import org.bukkit.command.Command;
13
import org.bukkit.command.CommandExecutor;
14
import org.bukkit.command.CommandSender;
15
import org.bukkit.entity.Player;
16
17
import ayunami2000.VideoMapPacketCodecBukkit;
18
19
public class CommandVideoMap implements CommandExecutor {
20
21
private VideoMapPacketCodecBukkit currentCodecInstance = null;
22
23
@Override
24
public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
25
if(!(arg0 instanceof Player)) {
26
arg0.sendMessage(ChatColor.RED + "Internal Error: " + ChatColor.WHITE + "CommmandSender must be a Player");
27
return false;
28
}
29
if(arg3.length == 3 && arg3[0].equalsIgnoreCase("begin")) {
30
try {
31
List<int[]> mapRows = new ArrayList();
32
double x = 0.0d;
33
double y = 0.0d;
34
double z = 0.0d;
35
float volume = -1.0f;
36
BufferedReader r = new BufferedReader(new FileReader(new File("video_map_config.txt")));
37
String str;
38
while((str = r.readLine()) != null) {
39
str = str.trim();
40
if(str.startsWith("#")) {
41
continue;
42
}else if(str.startsWith("x")) {
43
int i = str.indexOf('=');
44
if(i > 0) {
45
x = Double.parseDouble(str.substring(i + 1).trim());
46
}
47
}else if(str.startsWith("y")) {
48
int i = str.indexOf('=');
49
if(i > 0) {
50
y = Double.parseDouble(str.substring(i + 1).trim());
51
}
52
}else if(str.startsWith("z")) {
53
int i = str.indexOf('=');
54
if(i > 0) {
55
z = Double.parseDouble(str.substring(i + 1).trim());
56
}
57
}else if(str.startsWith("volume")) {
58
int i = str.indexOf('=');
59
if(i > 0) {
60
volume = Float.parseFloat(str.substring(i + 1).trim());
61
}
62
}else {
63
try {
64
String[] digits = str.split(",");
65
int firstInt = Integer.parseInt(digits[0].trim());
66
int[] newRow = new int[digits.length];
67
newRow[0] = firstInt;
68
for(int i = 1; i < digits.length; ++i) {
69
newRow[i] = Integer.parseInt(digits[i].trim());
70
}
71
if(mapRows.size() > 0 && mapRows.get(0).length != newRow.length) {
72
throw new IOException("All rows in map list must be the same length (" + mapRows.get(0).length + " != " + newRow.length + ")");
73
}
74
mapRows.add(newRow);
75
}catch(NumberFormatException t) {
76
}
77
}
78
}
79
r.close();
80
if(mapRows.size() > 0) {
81
if(currentCodecInstance != null) {
82
currentCodecInstance.disableVideoBukkit().send((Player)arg0);
83
currentCodecInstance = null;
84
}
85
int[][] matrix = new int[mapRows.size()][mapRows.get(0).length];
86
for(int i = 0, l = mapRows.size(); i < l; ++i) {
87
for(int j = 0; j < matrix[i].length; ++j) {
88
matrix[i][j] = mapRows.get(i)[j];
89
}
90
}
91
currentCodecInstance = new VideoMapPacketCodecBukkit(matrix, x, y, z, volume);
92
currentCodecInstance.beginPlaybackBukkit(arg3[1], true, arg3[2].indexOf('.') > 0 ? Float.parseFloat(arg3[2]) : Float.parseFloat(arg3[2] + ".0")).send((Player)arg0);
93
arg0.sendMessage(ChatColor.GREEN + "Enabled video map, URL:" + ChatColor.WHITE + " " + arg3[1]);
94
return true;
95
}else {
96
throw new IOException("No map rows were defined");
97
}
98
}catch(IOException ex) {
99
arg0.sendMessage(ChatColor.RED + "Internal Error while reading \'video_map_config.txt\': " + ChatColor.WHITE + ex.toString());
100
}
101
}else if((arg3.length == 2 || arg3.length == 3) && arg3[0].equalsIgnoreCase("preload")) {
102
int ttl = arg3.length == 3 ? Integer.parseInt(arg3[2]) * 1000 : 180 * 1000;
103
VideoMapPacketCodecBukkit.bufferVideoBukkit(arg3[1], ttl).send((Player)arg0);
104
arg0.sendMessage(ChatColor.GREEN + "Buffered video URL:" + ChatColor.WHITE + " " + arg3[1] + " " + ChatColor.GREEN + "for " + ChatColor.WHITE + (ttl / 1000) + ChatColor.GREEN + " seconds");
105
return true;
106
}else {
107
if(arg3.length == 1 && arg3[0].equalsIgnoreCase("stop")) {
108
if(currentCodecInstance != null) {
109
currentCodecInstance.disableVideoBukkit().send((Player)arg0);
110
currentCodecInstance = null;
111
arg0.sendMessage(ChatColor.GREEN + "Disabled video map");
112
return true;
113
}else {
114
arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");
115
}
116
}else if(arg3.length == 1 && arg3[0].equalsIgnoreCase("pause")) {
117
if(currentCodecInstance != null) {
118
currentCodecInstance.setPausedBukkit(true).send((Player)arg0);
119
arg0.sendMessage(ChatColor.GREEN + "Paused video map");
120
return true;
121
}else {
122
arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");
123
}
124
}else if(arg3.length == 1 && arg3[0].equalsIgnoreCase("resume")) {
125
if(currentCodecInstance != null) {
126
currentCodecInstance.setPausedBukkit(false).send((Player)arg0);
127
arg0.sendMessage(ChatColor.GREEN + "Resumed video map");
128
return true;
129
}else {
130
arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");
131
}
132
}else if((arg3.length == 1 || arg3.length == 2) && arg3[0].equalsIgnoreCase("loop")) {
133
if(currentCodecInstance != null) {
134
boolean gottaLoop = arg3.length == 1 || arg3[1].equalsIgnoreCase("true");
135
currentCodecInstance.setLoopEnableBukkit(gottaLoop).send((Player)arg0);
136
arg0.sendMessage(ChatColor.GREEN + (gottaLoop ? "Enabled video map loop" : "Disabled video map loop"));
137
return true;
138
}else {
139
arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");
140
}
141
}else if(arg3.length == 1 && arg3[0].equalsIgnoreCase("move")) {
142
if(currentCodecInstance != null) {
143
Location l = ((Player)arg0).getLocation();
144
currentCodecInstance.moveAudioSourceBukkit(l.getX(), l.getY(), l.getZ(), currentCodecInstance.getVolume()).send((Player)arg0);
145
arg0.sendMessage(ChatColor.GREEN + "Repositioned audio source to " + l.getBlockX() + ", " + l.getBlockY() + ", " + l.getBlockZ());
146
return true;
147
}else {
148
arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");
149
}
150
}
151
}
152
return false;
153
}
154
155
}
156
157