-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrackerCommand.java
131 lines (110 loc) · 3.45 KB
/
TrackerCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package edu.whimc.positiontracker;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
/**
* Main handler for the /positiontracker root command.
*/
public class TrackerCommand implements CommandExecutor{
/** The instance of the plugin. */
private Tracker plugin;
/**
* Constructs a TrackerCommand.
*
* @param plugin the instance of the plugin.
*/
public TrackerCommand(Tracker plugin) {
this.plugin = plugin;
}
/**
* {@inheritDoc}
*/
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
// ensure sender is an operator
if (!sender.isOp()) {
sender.sendMessage(ChatColor.RED + "You must be OP to use this command!");
return true;
}
// send valid command list if no arguments provided
if (args.length == 0) {
sendCommands(sender);
return true;
}
String arg = args[0];
// toggling debug mode
if (arg.equalsIgnoreCase("debug")) {
String message = ChatColor.YELLOW + "Console debug messages are now ";
if (plugin.getDebug()) {
message += ChatColor.RED + "off";
} else {
message += ChatColor.GREEN + "on";
}
plugin.setDebug(!plugin.getDebug());
sender.sendMessage(message);
return true;
}
boolean running = plugin.isRunning();
// checking plugin status
if (arg.equalsIgnoreCase("status")) {
String message = ChatColor.YELLOW + "PositionTracker is currently ";
if (running) {
message += ChatColor.GREEN + "Running";
} else {
message += ChatColor.RED + "Stopped";
}
message += ChatColor.YELLOW + ". " + ChatColor.GRAY + "" + ChatColor.ITALIC +
" (debugger " + (plugin.getDebug() ? "on" : "off") + ")";
sender.sendMessage(message);
return true;
}
// starting the tracker
if (arg.equalsIgnoreCase("start")) {
// notify sender if already running
if (running) {
sender.sendMessage(ChatColor.RED + "The tracker is already running!");
return true;
}
boolean success = plugin.startRunner();
if (success) {
sender.sendMessage(ChatColor.GREEN + "Tracker started!");
} else {
sender.sendMessage(ChatColor.RED + "There was an error! Check console.");
}
return true;
}
// stopping the tracker
if (arg.equalsIgnoreCase("stop")) {
// notify sender if already stopped
if (!running) {
sender.sendMessage(ChatColor.RED + "The tracker is already stopped!");
return true;
}
boolean success = plugin.stopRunner();
if (success) {
sender.sendMessage(ChatColor.GREEN + "Tracker stopped!");
} else {
sender.sendMessage(ChatColor.RED + "There was an error! Check console.");
}
return true;
}
sendCommands(sender);
return true;
}
/**
* Sends the list of commands and their usage to the sender.
*
* @param sender the command's sender.
*/
private void sendCommands(CommandSender sender) {
sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Position Tracker");
sender.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC +
" /positiontracker status - get status of tracker");
sender.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC +
" /positiontracker debug - toggle debug messages in console");
sender.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC +
" /positiontracker start - starts tracker");
sender.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC +
" /positiontracker stop - stops tracker");
}
}