mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-24 03:25:29 +01:00
Add exception handlers to all the commands.
This commit is contained in:
parent
5b279660e0
commit
0108c3390e
@ -5,6 +5,8 @@ import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.comphenix.protocol.error.ErrorReporter;
|
||||
|
||||
/**
|
||||
* Base class for all our commands.
|
||||
*
|
||||
@ -17,12 +19,14 @@ abstract class CommandBase implements CommandExecutor {
|
||||
private String permission;
|
||||
private String name;
|
||||
private int minimumArgumentCount;
|
||||
|
||||
public CommandBase(String permission, String name) {
|
||||
this(permission, name, 0);
|
||||
private ErrorReporter reporter;
|
||||
|
||||
public CommandBase(ErrorReporter reporter, String permission, String name) {
|
||||
this(reporter, permission, name, 0);
|
||||
}
|
||||
|
||||
public CommandBase(String permission, String name, int minimumArgumentCount) {
|
||||
public CommandBase(ErrorReporter reporter, String permission, String name, int minimumArgumentCount) {
|
||||
this.reporter = reporter;
|
||||
this.name = name;
|
||||
this.permission = permission;
|
||||
this.minimumArgumentCount = minimumArgumentCount;
|
||||
@ -30,22 +34,58 @@ abstract class CommandBase implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public final boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
// Make sure we're dealing with the correct command
|
||||
if (!command.getName().equalsIgnoreCase(name)) {
|
||||
return false;
|
||||
}
|
||||
if (!sender.hasPermission(permission)) {
|
||||
sender.sendMessage(ChatColor.RED + "You haven't got permission to run this command.");
|
||||
try {
|
||||
// Make sure we're dealing with the correct command
|
||||
if (!command.getName().equalsIgnoreCase(name)) {
|
||||
return false;
|
||||
}
|
||||
if (permission != null && !sender.hasPermission(permission)) {
|
||||
sender.sendMessage(ChatColor.RED + "You haven't got permission to run this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check argument length
|
||||
if (args != null && args.length >= minimumArgumentCount) {
|
||||
return handleCommand(sender, args);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
reporter.reportDetailed(this, "Cannot execute command " + name, e, sender, label, args);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check argument length
|
||||
if (args != null && args.length >= minimumArgumentCount) {
|
||||
return handleCommand(sender, args);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the permission necessary to execute this command.
|
||||
* @return The permission, or NULL if not needed.
|
||||
*/
|
||||
public String getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the primary name of this command.
|
||||
* @return Primary name.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the error reporter.
|
||||
* @return Error reporter.
|
||||
*/
|
||||
protected ErrorReporter getReporter() {
|
||||
return reporter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main implementation of this command.
|
||||
* @param sender - command sender.
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
protected abstract boolean handleCommand(CommandSender sender, String[] args);
|
||||
}
|
||||
|
@ -76,11 +76,10 @@ class CommandPacket extends CommandBase {
|
||||
private AbstractIntervalTree<Integer, DetailedPacketListener> clientListeners = createTree(ConnectionSide.CLIENT_SIDE);
|
||||
private AbstractIntervalTree<Integer, DetailedPacketListener> serverListeners = createTree(ConnectionSide.SERVER_SIDE);
|
||||
|
||||
public CommandPacket(Plugin plugin, Logger logger, ErrorReporter reporter, ProtocolManager manager) {
|
||||
super(CommandBase.PERMISSION_ADMIN, NAME, 2);
|
||||
public CommandPacket(ErrorReporter reporter, Plugin plugin, Logger logger, ProtocolManager manager) {
|
||||
super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 2);
|
||||
this.plugin = plugin;
|
||||
this.logger = logger;
|
||||
this.reporter = reporter;
|
||||
this.manager = manager;
|
||||
this.chatter = new ChatExtensions(manager);
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.comphenix.protocol.error.ErrorReporter;
|
||||
import com.comphenix.protocol.metrics.Updater;
|
||||
import com.comphenix.protocol.metrics.Updater.UpdateResult;
|
||||
import com.comphenix.protocol.metrics.Updater.UpdateType;
|
||||
@ -22,9 +23,9 @@ class CommandProtocol extends CommandBase {
|
||||
private Plugin plugin;
|
||||
private Updater updater;
|
||||
private ProtocolConfig config;
|
||||
|
||||
public CommandProtocol(Plugin plugin, Updater updater, ProtocolConfig config) {
|
||||
super(CommandBase.PERMISSION_ADMIN, NAME, 1);
|
||||
|
||||
public CommandProtocol(ErrorReporter reporter, Plugin plugin, Updater updater, ProtocolConfig config) {
|
||||
super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 1);
|
||||
this.plugin = plugin;
|
||||
this.updater = updater;
|
||||
this.config = config;
|
||||
@ -51,8 +52,12 @@ class CommandProtocol extends CommandBase {
|
||||
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true);
|
||||
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
|
||||
try {
|
||||
UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true);
|
||||
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
|
||||
} catch (Exception e) {
|
||||
getReporter().reportDetailed(this, "Cannot check updates for ProtocolLib.", e, sender);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -64,8 +69,12 @@ class CommandProtocol extends CommandBase {
|
||||
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
UpdateResult result = updater.update(UpdateType.DEFAULT, true);
|
||||
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
|
||||
try {
|
||||
UpdateResult result = updater.update(UpdateType.DEFAULT, true);
|
||||
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
|
||||
} catch (Exception e) {
|
||||
getReporter().reportDetailed(this, "Cannot update ProtocolLib.", e, sender);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -75,6 +75,7 @@ public class ProtocolLibrary extends JavaPlugin {
|
||||
|
||||
// Updater
|
||||
private Updater updater;
|
||||
private boolean updateDisabled;
|
||||
|
||||
// Logger
|
||||
private Logger logger;
|
||||
@ -108,8 +109,8 @@ public class ProtocolLibrary extends JavaPlugin {
|
||||
reporter.addGlobalParameter("manager", protocolManager);
|
||||
|
||||
// Initialize command handlers
|
||||
commandProtocol = new CommandProtocol(this, updater, config);
|
||||
commandPacket = new CommandPacket(this, logger, reporter, protocolManager);
|
||||
commandProtocol = new CommandProtocol(reporter, this, updater, config);
|
||||
commandPacket = new CommandPacket(reporter, this, logger, protocolManager);
|
||||
|
||||
// Send logging information to player listeners too
|
||||
broadcastUsers(PERMISSION_INFO);
|
||||
@ -223,7 +224,9 @@ public class ProtocolLibrary extends JavaPlugin {
|
||||
manager.sendProcessedPackets(tickCounter++, true);
|
||||
|
||||
// Check for updates too
|
||||
checkUpdates();
|
||||
if (!updateDisabled) {
|
||||
checkUpdates();
|
||||
}
|
||||
}
|
||||
}, ASYNC_PACKET_DELAY, ASYNC_PACKET_DELAY);
|
||||
|
||||
@ -238,15 +241,20 @@ public class ProtocolLibrary extends JavaPlugin {
|
||||
// Ignore milliseconds - it's pointless
|
||||
long currentTime = System.currentTimeMillis() / MILLI_PER_SECOND;
|
||||
|
||||
// Should we update?
|
||||
if (currentTime > config.getAutoLastTime() + config.getAutoDelay()) {
|
||||
// Initiate the update as if it came from the console
|
||||
if (config.isAutoDownload())
|
||||
commandProtocol.updateVersion(getServer().getConsoleSender());
|
||||
else if (config.isAutoNotify())
|
||||
commandProtocol.checkVersion(getServer().getConsoleSender());
|
||||
else
|
||||
commandProtocol.updateFinished();
|
||||
try {
|
||||
// Should we update?
|
||||
if (currentTime > config.getAutoLastTime() + config.getAutoDelay()) {
|
||||
// Initiate the update as if it came from the console
|
||||
if (config.isAutoDownload())
|
||||
commandProtocol.updateVersion(getServer().getConsoleSender());
|
||||
else if (config.isAutoNotify())
|
||||
commandProtocol.checkVersion(getServer().getConsoleSender());
|
||||
else
|
||||
commandProtocol.updateFinished();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
reporter.reportDetailed(this, "Cannot perform automatic updates.", e);
|
||||
updateDisabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user