Archiviert
13
0

Report using the correct sender in the update methods.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-09-05 04:03:01 +02:00
Ursprung db2efbe089
Commit 9442e2c36f

Datei anzeigen

@ -1,147 +1,147 @@
/* /*
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2012 Kristian S. Stangeland * Copyright (C) 2012 Kristian S. Stangeland
* *
* This program is free software; you can redistribute it and/or modify it under the terms of the * This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of * GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version. * the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * See the GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License along with this program; * You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol; package com.comphenix.protocol;
import java.io.IOException; import java.io.IOException;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.error.ReportType;
import com.comphenix.protocol.metrics.Updater; import com.comphenix.protocol.metrics.Updater;
import com.comphenix.protocol.metrics.Updater.UpdateResult; import com.comphenix.protocol.metrics.Updater.UpdateResult;
import com.comphenix.protocol.metrics.Updater.UpdateType; import com.comphenix.protocol.metrics.Updater.UpdateType;
import com.comphenix.protocol.utility.WrappedScheduler; import com.comphenix.protocol.utility.WrappedScheduler;
/** /**
* Handles the "protocol" administration command. * Handles the "protocol" administration command.
* *
* @author Kristian * @author Kristian
*/ */
class CommandProtocol extends CommandBase { class CommandProtocol extends CommandBase {
/** /**
* Name of this command. * Name of this command.
*/ */
public static final String NAME = "protocol"; public static final String NAME = "protocol";
public static final ReportType REPORT_HTTP_ERROR = new ReportType("Http error: %s"); public static final ReportType REPORT_HTTP_ERROR = new ReportType("Http error: %s");
public static final ReportType REPORT_CANNOT_CHECK_FOR_UPDATES = new ReportType("Cannot check updates for ProtocolLib."); public static final ReportType REPORT_CANNOT_CHECK_FOR_UPDATES = new ReportType("Cannot check updates for ProtocolLib.");
public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib."); public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib.");
private Plugin plugin; private Plugin plugin;
private Updater updater; private Updater updater;
private ProtocolConfig config; private ProtocolConfig config;
public CommandProtocol(ErrorReporter reporter, Plugin plugin, Updater updater, ProtocolConfig config) { public CommandProtocol(ErrorReporter reporter, Plugin plugin, Updater updater, ProtocolConfig config) {
super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 1); super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 1);
this.plugin = plugin; this.plugin = plugin;
this.updater = updater; this.updater = updater;
this.config = config; this.config = config;
} }
@Override @Override
protected boolean handleCommand(CommandSender sender, String[] args) { protected boolean handleCommand(CommandSender sender, String[] args) {
String subCommand = args[0]; String subCommand = args[0];
// Only return TRUE if we executed the correct command // Only return TRUE if we executed the correct command
if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload"))
reloadConfiguration(sender); reloadConfiguration(sender);
else if (subCommand.equalsIgnoreCase("check")) else if (subCommand.equalsIgnoreCase("check"))
checkVersion(sender); checkVersion(sender);
else if (subCommand.equalsIgnoreCase("update")) else if (subCommand.equalsIgnoreCase("update"))
updateVersion(sender); updateVersion(sender);
else else
return false; return false;
return true; return true;
} }
public void checkVersion(final CommandSender sender) { public void checkVersion(final CommandSender sender) {
// Perform on an async thread // Perform on an async thread
WrappedScheduler.runAsynchronouslyOnce(plugin, new Runnable() { WrappedScheduler.runAsynchronouslyOnce(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true); UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true);
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
} catch (Exception e) { } catch (Exception e) {
if (isHttpError(e)) { if (isHttpError(e)) {
getReporter().reportWarning(this, getReporter().reportWarning(CommandProtocol.this,
Report.newBuilder(REPORT_HTTP_ERROR).messageParam(e.getCause().getMessage()) Report.newBuilder(REPORT_HTTP_ERROR).messageParam(e.getCause().getMessage())
); );
} else { } else {
getReporter().reportDetailed(this, Report.newBuilder(REPORT_CANNOT_CHECK_FOR_UPDATES).error(e).callerParam(sender)); getReporter().reportDetailed(CommandProtocol.this, Report.newBuilder(REPORT_CANNOT_CHECK_FOR_UPDATES).error(e).callerParam(sender));
} }
} }
} }
}, 0L); }, 0L);
updateFinished(); updateFinished();
} }
public void updateVersion(final CommandSender sender) { public void updateVersion(final CommandSender sender) {
// Perform on an async thread // Perform on an async thread
WrappedScheduler.runAsynchronouslyOnce(plugin, new Runnable() { WrappedScheduler.runAsynchronouslyOnce(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
UpdateResult result = updater.update(UpdateType.DEFAULT, true); UpdateResult result = updater.update(UpdateType.DEFAULT, true);
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
} catch (Exception e) { } catch (Exception e) {
if (isHttpError(e)) { if (isHttpError(e)) {
getReporter().reportWarning(this, getReporter().reportWarning(CommandProtocol.this,
Report.newBuilder(REPORT_HTTP_ERROR).messageParam(e.getCause().getMessage()) Report.newBuilder(REPORT_HTTP_ERROR).messageParam(e.getCause().getMessage())
); );
} else { } else {
getReporter().reportDetailed(this,Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(e).callerParam(sender)); getReporter().reportDetailed(CommandProtocol.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(e).callerParam(sender));
} }
} }
} }
}, 0L); }, 0L);
updateFinished(); updateFinished();
} }
private boolean isHttpError(Exception e) { private boolean isHttpError(Exception e) {
Throwable cause = e.getCause(); Throwable cause = e.getCause();
if (cause instanceof IOException) { if (cause instanceof IOException) {
// Thanks for making the message a part of the API ... // Thanks for making the message a part of the API ...
return cause.getMessage().contains("HTTP response"); return cause.getMessage().contains("HTTP response");
} else { } else {
return false; return false;
} }
} }
/** /**
* Prevent further automatic updates until the next delay. * Prevent further automatic updates until the next delay.
*/ */
public void updateFinished() { public void updateFinished() {
long currentTime = System.currentTimeMillis() / ProtocolLibrary.MILLI_PER_SECOND; long currentTime = System.currentTimeMillis() / ProtocolLibrary.MILLI_PER_SECOND;
config.setAutoLastTime(currentTime); config.setAutoLastTime(currentTime);
config.saveAll(); config.saveAll();
} }
public void reloadConfiguration(CommandSender sender) { public void reloadConfiguration(CommandSender sender) {
plugin.reloadConfig(); plugin.reloadConfig();
sender.sendMessage(ChatColor.BLUE + "Reloaded configuration!"); sender.sendMessage(ChatColor.BLUE + "Reloaded configuration!");
} }
} }