diff options
author | snowleo <schneeleo@gmail.com> | 2011-10-27 00:03:16 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-10-27 00:03:16 +0200 |
commit | 60b9d4fc80c881ce5548f7387c3de0d9fc5a7637 (patch) | |
tree | ca3fb9676c76f8700bda0bd21f9ffcc579d46c42 | |
parent | 7622c5c5108f406d3fbb285de0d2446fdc3127dd (diff) | |
download | Essentials-60b9d4fc80c881ce5548f7387c3de0d9fc5a7637.tar Essentials-60b9d4fc80c881ce5548f7387c3de0d9fc5a7637.tar.gz Essentials-60b9d4fc80c881ce5548f7387c3de0d9fc5a7637.tar.lz Essentials-60b9d4fc80c881ce5548f7387c3de0d9fc5a7637.tar.xz Essentials-60b9d4fc80c881ce5548f7387c3de0d9fc5a7637.zip |
Some cleanup, refactoring of EssentialsHelp class
24 files changed, 600 insertions, 428 deletions
diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/WorkListener.java b/EssentialsUpdate/src/com/earth2me/essentials/update/AbstractWorkListener.java index da6bdb978..487e372c8 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/WorkListener.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/AbstractWorkListener.java @@ -1,12 +1,11 @@ package com.earth2me.essentials.update; -import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -public abstract class WorkListener +public abstract class AbstractWorkListener { - public WorkListener(final Plugin plugin, final VersionInfo newVersionInfo) + public AbstractWorkListener(final Plugin plugin, final VersionInfo newVersionInfo) { this.plugin = plugin; this.newVersionInfo = newVersionInfo; diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java b/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java index 3caa22cc3..ec62566ca 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java @@ -1,16 +1,16 @@ package com.earth2me.essentials.update; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.Charset; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.bukkit.Bukkit; +import com.earth2me.essentials.update.chat.Command; +import com.earth2me.essentials.update.chat.ConfigCommand; +import com.earth2me.essentials.update.chat.ErrorsCommand; +import com.earth2me.essentials.update.chat.HelpCommand; +import com.earth2me.essentials.update.chat.IrcBot; +import com.earth2me.essentials.update.chat.ListCommand; +import com.earth2me.essentials.update.chat.StartupCommand; +import com.earth2me.essentials.update.chat.UsernameUtil; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -21,7 +21,6 @@ import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; -import org.jibble.pircbot.User; public class EssentialsHelp extends PlayerListener @@ -29,14 +28,19 @@ public class EssentialsHelp extends PlayerListener private transient Player chatUser; private final transient Server server; private final transient Plugin plugin; - private final static Charset UTF8 = Charset.forName("utf-8"); private transient IrcBot ircBot; + private final transient Map<String, Command> commands = new HashMap<String, Command>(); public EssentialsHelp(final Plugin plugin) { super(); this.plugin = plugin; this.server = plugin.getServer(); + commands.put("!help", new HelpCommand()); + commands.put("!list", new ListCommand()); + commands.put("!startup", new StartupCommand(plugin)); + commands.put("!errors", new ErrorsCommand(plugin)); + commands.put("!config", new ConfigCommand(plugin)); } public void registerEvents() @@ -46,7 +50,7 @@ public class EssentialsHelp extends PlayerListener pluginManager.registerEvent(Type.PLAYER_CHAT, this, Priority.Low, plugin); } - public void onCommand(CommandSender sender) + public void onCommand(final CommandSender sender) { if (sender instanceof Player && sender.hasPermission("essentials.helpchat")) { @@ -74,11 +78,7 @@ public class EssentialsHelp extends PlayerListener public void onDisable() { - if ( ircBot != null) - { - ircBot.quit(); - ircBot = null; - } + closeConnection(); } private boolean sendChatMessage(final Player player, final String message) @@ -90,35 +90,19 @@ public class EssentialsHelp extends PlayerListener } if (ircBot == null) { - if (messageCleaned.equalsIgnoreCase("yes")) - { - player.sendMessage("Connecting..."); - connectToIRC(player); - return true; - } - if (messageCleaned.equalsIgnoreCase("no") || message.equalsIgnoreCase("!quit")) - { - chatUser = null; - return true; - } - return false; + return handleAnswer(messageCleaned, player); } else { - if (ircBot.isKicked()) { - chatUser = null; - ircBot.quit(); - ircBot = null; + if (ircBot.isKicked()) + { + closeConnection(); return false; } - final String lowMessage = messageCleaned.toLowerCase(); + final String lowMessage = messageCleaned.toLowerCase(Locale.ENGLISH); if (lowMessage.startsWith("!quit")) { - chatUser = null; - if (ircBot != null) { - ircBot.quit(); - ircBot = null; - } + closeConnection(); player.sendMessage("Connection closed."); return true; } @@ -126,52 +110,8 @@ public class EssentialsHelp extends PlayerListener { return false; } - if (lowMessage.startsWith("!list")) - { - final User[] members = ircBot.getUsers(); - final StringBuilder sb = new StringBuilder(); - for (User user : members) - { - if (sb.length() > 0) - { - sb.append("§f, "); - } - if (user.isOp() || user.hasVoice()) - { - sb.append("§6"); - } - else - { - sb.append("§7"); - } - sb.append(user.getPrefix()).append(user.getNick()); - } - player.sendMessage(sb.toString()); - return true; - } - if (lowMessage.startsWith("!help")) + if (handleCommands(lowMessage, player)) { - player.sendMessage("Commands: (Note: Files send to the chat will be public viewable.)"); - player.sendMessage("!errors - Send the last server errors to the chat."); - player.sendMessage("!startup - Send the last startup messages to the chat."); - player.sendMessage("!config - Sends your Essentials config to the chat."); - player.sendMessage("!list - List all players in chat."); - player.sendMessage("!quit - Leave chat."); - return true; - } - if (lowMessage.startsWith("!errors")) - { - sendErrors(); - return true; - } - if (lowMessage.startsWith("!startup")) - { - sendStartup(); - return true; - } - if (lowMessage.startsWith("!config")) - { - sendConfig(); return true; } ircBot.sendMessage(messageCleaned); @@ -180,300 +120,62 @@ public class EssentialsHelp extends PlayerListener } } - private String buildIrcName() + private void closeConnection() { - final StringBuilder nameBuilder = new StringBuilder(); - nameBuilder.append(chatUser.getName()); - - final Matcher versionMatch = Pattern.compile("git-Bukkit-([0-9]+).([0-9]+).([0-9]+)-[0-9]+-[0-9a-z]+-b([0-9]+)jnks.*").matcher(server.getVersion()); - if (versionMatch.matches()) - { - nameBuilder.append(" CB"); - nameBuilder.append(versionMatch.group(4)); - } - - final Plugin essentials = server.getPluginManager().getPlugin("Essentials"); - if (essentials != null) - { - nameBuilder.append(" ESS"); - nameBuilder.append(essentials.getDescription().getVersion()); - } - - final Plugin groupManager = server.getPluginManager().getPlugin("GroupManager"); - if (groupManager != null) - { - nameBuilder.append(" GM"); - if (!groupManager.isEnabled()) - { - nameBuilder.append('!'); - } - } - - final Plugin pex = server.getPluginManager().getPlugin("PermissionsEx"); - if (pex != null) - { - nameBuilder.append(" PEX"); - if (!pex.isEnabled()) - { - nameBuilder.append('!'); - } - nameBuilder.append(pex.getDescription().getVersion()); - } - - final Plugin pb = server.getPluginManager().getPlugin("PermissionsBukkit"); - if (pb != null) - { - nameBuilder.append(" PB"); - if (!pb.isEnabled()) - { - nameBuilder.append('!'); - } - nameBuilder.append(pb.getDescription().getVersion()); - } - - final Plugin bp = server.getPluginManager().getPlugin("bPermissions"); - if (bp != null) - { - nameBuilder.append(" BP"); - if (!bp.isEnabled()) - { - nameBuilder.append('!'); - } - nameBuilder.append(bp.getDescription().getVersion()); - } - - final Plugin perm = server.getPluginManager().getPlugin("Permissions"); - if (perm != null) + chatUser = null; + if (ircBot != null) { - nameBuilder.append(" P"); - if (!perm.isEnabled()) - { - nameBuilder.append('!'); - } - nameBuilder.append(perm.getDescription().getVersion()); + ircBot.quit(); + ircBot = null; } - - return nameBuilder.toString(); } - private void connectToIRC(final Player player) + private boolean handleAnswer(final String message, final Player player) { - ircBot = new IrcBot(player, "Ess_" + player.getName(), buildIrcName()); - } - - private void sendErrors() - { - BufferedReader page = null; - try + if (message.equalsIgnoreCase("yes")) { - File bukkitFolder = plugin.getDataFolder().getAbsoluteFile().getParentFile().getParentFile(); - if (bukkitFolder == null || !bukkitFolder.exists()) - { - chatUser.sendMessage("Bukkit folder not found."); - return; - } - File logFile = new File(bukkitFolder, "server.log"); - if (!logFile.exists()) - { - chatUser.sendMessage("Server log not found."); - return; - } - FileInputStream fis = new FileInputStream(logFile); - if (logFile.length() > 1000000) - { - fis.skip(logFile.length() - 1000000); - } - page = new BufferedReader(new InputStreamReader(fis)); - final StringBuilder input = new StringBuilder(); - String line; - Pattern pattern = Pattern.compile("^[0-9 :-]+\\[INFO\\].*"); - while ((line = page.readLine()) != null) - { - if (!pattern.matcher(line).matches()) - { - input.append(line).append("\n"); - } - } - if (input.length() > 10000) - { - input.delete(0, input.length() - 10000); - } - final PastieUpload pastie = new PastieUpload(); - final String url = pastie.send(input.toString()); - String message = "Errors: " + url; - chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message); - ircBot.sendMessage(message); - } - catch (IOException ex) - { - Bukkit.getLogger().log(Level.SEVERE, null, ex); - chatUser.sendMessage(ex.getMessage()); + player.sendMessage("Connecting..."); + connectToIRC(player); + return true; } - finally + if (message.equalsIgnoreCase("no") || message.equalsIgnoreCase("!quit")) { - try - { - if (page != null) - { - page.close(); - } - } - catch (IOException ex) - { - Logger.getLogger(EssentialsHelp.class.getName()).log(Level.SEVERE, null, ex); - } + chatUser = null; + return true; } + return false; } - private void sendStartup() + private boolean handleCommands(final String lowMessage, final Player player) { - BufferedReader page = null; - try - { - File bukkitFolder = plugin.getDataFolder().getAbsoluteFile().getParentFile().getParentFile(); - if (bukkitFolder == null || !bukkitFolder.exists()) - { - chatUser.sendMessage("Bukkit folder not found."); - return; - } - File logFile = new File(bukkitFolder, "server.log"); - if (!logFile.exists()) - { - chatUser.sendMessage("Server log not found."); - return; - } - FileInputStream fis = new FileInputStream(logFile); - if (logFile.length() > 1000000) - { - fis.skip(logFile.length() - 1000000); - } - page = new BufferedReader(new InputStreamReader(fis)); - final StringBuilder input = new StringBuilder(); - String line; - Pattern patternStart = Pattern.compile("^[0-9 :-]+\\[INFO\\] Starting minecraft server version.*"); - Pattern patternEnd = Pattern.compile("^[0-9 :-]+\\[INFO\\] Done \\([0-9.,]+s\\)! For help, type \"help\".*"); - boolean log = false; - while ((line = page.readLine()) != null) - { - if (patternStart.matcher(line).matches()) - { - if (input.length() > 0) - { - input.delete(0, input.length()); - } - log = true; - } - if (log) - { - input.append(line).append("\n"); - } - if (patternEnd.matcher(line).matches()) - { - log = false; - } - } - if (input.length() > 10000) - { - input.delete(0, input.length() - 10000); - } - final PastieUpload pastie = new PastieUpload(); - final String url = pastie.send(input.toString()); - String message = "Startup: " + url; - chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message); - ircBot.sendMessage(message); - } - catch (IOException ex) - { - Bukkit.getLogger().log(Level.SEVERE, null, ex); - chatUser.sendMessage(ex.getMessage()); - } - finally + final String[] parts = lowMessage.split(" "); + if (commands.containsKey(parts[0])) { - try - { - if (page != null) - { - page.close(); - } - } - catch (IOException ex) - { - Logger.getLogger(EssentialsHelp.class.getName()).log(Level.SEVERE, null, ex); - } + commands.get(parts[0]).run(ircBot, player); + return true; } + return false; } - private void sendConfig() + private void connectToIRC(final Player player) { - BufferedReader page = null; - try - { - File configFolder = new File(plugin.getDataFolder().getParentFile(), "Essentials"); - if (!configFolder.exists()) - { - chatUser.sendMessage("Essentials plugin folder not found."); - return; - } - File configFile = new File(configFolder, "config.yml"); - if (!configFile.exists()) - { - chatUser.sendMessage("Essentials config file not found."); - return; - } - page = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), UTF8)); - final StringBuilder input = new StringBuilder(); - String line; - while ((line = page.readLine()) != null) - { - input.append(line).append("\n"); - } - final PastieUpload pastie = new PastieUpload(); - final String url = pastie.send(input.toString()); - String message = "Essentials config.yml: " + url; - chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message); - ircBot.sendMessage(message); - - } - catch (IOException ex) - { - Bukkit.getLogger().log(Level.SEVERE, null, ex); - chatUser.sendMessage(ex.getMessage()); - } - finally - { - try - { - if (page != null) - { - page.close(); - } - } - catch (IOException ex) - { - Logger.getLogger(EssentialsHelp.class.getName()).log(Level.SEVERE, null, ex); - } - } + ircBot = new IrcBot(player, "Ess_" + player.getName(), UsernameUtil.createUsername(player)); } @Override - public void onPlayerChat(PlayerChatEvent event) + public void onPlayerChat(final PlayerChatEvent event) { if (event.getPlayer() == chatUser) { - boolean success = sendChatMessage(event.getPlayer(), event.getMessage()); + final boolean success = sendChatMessage(event.getPlayer(), event.getMessage()); event.setCancelled(success); return; } } @Override - public void onPlayerQuit(PlayerQuitEvent event) + public void onPlayerQuit(final PlayerQuitEvent event) { - chatUser = null; - if (ircBot != null) { - ircBot.quit(); - ircBot = null; - } - return; + closeConnection(); } } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/GetFile.java b/EssentialsUpdate/src/com/earth2me/essentials/update/GetFile.java index 8727d2f3b..b48d95dd2 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/GetFile.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/GetFile.java @@ -12,7 +12,8 @@ import java.net.URL; import java.net.URLConnection; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.logging.Logger; +import java.util.logging.Level; +import org.bukkit.Bukkit; public class GetFile @@ -53,7 +54,7 @@ public class GetFile } catch (NoSuchAlgorithmException ex) { - // Ignore because the code is never called + throw new RuntimeException(ex); } } @@ -101,7 +102,7 @@ public class GetFile } if (brokenFile && !file.delete()) { - Logger.getLogger("Minecraft").severe("Could not delete file " + file.getPath()); + Bukkit.getLogger().log(Level.SEVERE, "Could not delete file {0}", file.getPath()); } } finally diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java index 685700426..29afc4d3f 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java @@ -3,7 +3,6 @@ package com.earth2me.essentials.update; import java.io.File; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.bukkit.Bukkit; @@ -22,7 +21,7 @@ public class UpdateCheck private final transient Plugin plugin; private transient boolean essentialsInstalled; - public UpdateCheck(Plugin plugin) + public UpdateCheck(final Plugin plugin) { this.plugin = plugin; updateFile = new UpdateFile(plugin); @@ -31,21 +30,20 @@ public class UpdateCheck private void checkForEssentials() { - PluginManager pm = plugin.getServer().getPluginManager(); - Plugin essentials = pm.getPlugin("Essentials"); - if (essentials == null) + final PluginManager pluginManager = plugin.getServer().getPluginManager(); + final Plugin essentials = pluginManager.getPlugin("Essentials"); + essentialsInstalled = essentials != null; + if (essentialsInstalled) + { + currentVersion = new Version(essentials.getDescription().getVersion()); + } + else { - essentialsInstalled = false; if (new File(plugin.getDataFolder().getParentFile(), "Essentials.jar").exists()) { Bukkit.getLogger().severe("Essentials.jar found, but not recognized by Bukkit. Broken download?"); } } - else - { - essentialsInstalled = true; - currentVersion = new Version(essentials.getDescription().getVersion()); - } } public void scheduleUpdateTask() @@ -81,6 +79,7 @@ public class UpdateCheck return updateFile.getVersions().get(newVersion); } + public enum CheckResult { NEW_ESS, NEW_ESS_BUKKIT, NEW_BUKKIT, OK, UNKNOWN diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java index c1dc16702..977cb0ebc 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java @@ -60,7 +60,7 @@ public class UpdateProcess extends PlayerListener currentPlayer.sendMessage("A newer version of EssentialsUpdate is found. Downloading new file and reloading server."); } Bukkit.getLogger().log(Level.INFO, "A newer version of EssentialsUpdate is found. Downloading new file and reloading server."); - new SelfUpdate(new WorkListener(plugin, updateCheck.getNewVersionInfo()) + new SelfUpdate(new AbstractWorkListener(plugin, updateCheck.getNewVersionInfo()) { @Override public void onWorkAbort(final String message) diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/Version.java b/EssentialsUpdate/src/com/earth2me/essentials/update/Version.java index 8e6cbc97f..a82f49abf 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/Version.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/Version.java @@ -147,7 +147,7 @@ public class Version implements Comparable<Version> hash = 71 * hash + this.major; hash = 71 * hash + this.minor; hash = 71 * hash + this.build; - hash = 71 * hash + (this.type != null ? this.type.hashCode() : 0); + hash = 71 * hash + (this.type == null ? 0 : this.type.hashCode()); return hash; } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/AbstractFileCommand.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/AbstractFileCommand.java new file mode 100644 index 000000000..12aa3326e --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/AbstractFileCommand.java @@ -0,0 +1,68 @@ +package com.earth2me.essentials.update.chat; + +import com.earth2me.essentials.update.PastieUpload; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import org.bukkit.plugin.Plugin; + + +public abstract class AbstractFileCommand implements Command +{ + private final transient Plugin plugin; + private final static Charset UTF8 = Charset.forName("utf-8"); + + public AbstractFileCommand(final Plugin plugin) + { + this.plugin = plugin; + } + + protected BufferedReader getServerLogReader() throws IOException + { + final File bukkitFolder = plugin.getDataFolder().getAbsoluteFile().getParentFile().getParentFile(); + if (bukkitFolder == null || !bukkitFolder.exists()) + { + throw new IOException("Bukkit folder not found."); + } + final File logFile = new File(bukkitFolder, "server.log"); + if (!logFile.exists()) + { + throw new IOException("Server log not found."); + } + final FileInputStream fis = new FileInputStream(logFile); + if (logFile.length() > 1000000) + { + fis.skip(logFile.length() - 1000000); + } + return new BufferedReader(new InputStreamReader(fis)); + } + + protected BufferedReader getPluginConfig(final String pluginName, final String fileName) throws IOException + { + final File configFolder = new File(plugin.getDataFolder().getAbsoluteFile().getParentFile(), pluginName); + if (!configFolder.exists()) + { + throw new IOException(pluginName+" plugin folder not found."); + } + final File configFile = new File(configFolder, fileName); + if (!configFile.exists()) + { + throw new IOException(pluginName+" plugin file "+fileName+" not found."); + } + return new BufferedReader(new InputStreamReader(new FileInputStream(configFile), UTF8)); + + } + + protected String uploadToPastie(final StringBuilder input) throws IOException + { + if (input.length() > 10000) + { + input.delete(0, input.length() - 10000); + } + final PastieUpload pastie = new PastieUpload(); + return pastie.send(input.toString()); + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/Command.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/Command.java new file mode 100644 index 000000000..ad4c75e43 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/Command.java @@ -0,0 +1,9 @@ +package com.earth2me.essentials.update.chat; + +import org.bukkit.entity.Player; + + +public interface Command +{ + void run(final IrcBot ircBot, final Player player); +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ConfigCommand.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ConfigCommand.java new file mode 100644 index 000000000..5fda07106 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ConfigCommand.java @@ -0,0 +1,62 @@ +package com.earth2me.essentials.update.chat; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + + +public class ConfigCommand extends AbstractFileCommand implements Command +{ + public ConfigCommand(final Plugin plugin) + { + super(plugin); + } + + @Override + public void run(final IrcBot ircBot, final Player player) + { + BufferedReader page = null; + try + { + page = getPluginConfig("Essentials", "config.yml"); + final StringBuilder input = new StringBuilder(); + do + { + final String line = page.readLine(); + if (line == null) { + break; + } else { + input.append(line).append("\n"); + } + } while (true); + page.close(); + final String message = "Essentials config.yml: " + uploadToPastie(input); + player.sendMessage("§6" + ircBot.getNick() + ": §7" + message); + ircBot.sendMessage(message); + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, null, ex); + player.sendMessage(ex.getMessage()); + } + finally + { + try + { + if (page != null) + { + page.close(); + } + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, null, ex); + player.sendMessage(ex.getMessage()); + } + } + + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ErrorsCommand.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ErrorsCommand.java new file mode 100644 index 000000000..41aa551c4 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ErrorsCommand.java @@ -0,0 +1,71 @@ +package com.earth2me.essentials.update.chat; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.logging.Level; +import java.util.regex.Pattern; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + + +public class ErrorsCommand extends AbstractFileCommand implements Command +{ + private final transient Pattern pattern = Pattern.compile("^[0-9 :-]+\\[INFO\\].*"); + + public ErrorsCommand(final Plugin plugin) + { + super(plugin); + } + + @Override + public void run(final IrcBot ircBot, final Player player) + { + BufferedReader page = null; + try + { + page = getServerLogReader(); + final StringBuilder input = new StringBuilder(); + do + { + final String line = page.readLine(); + if (line == null) + { + break; + } + else + { + if (!pattern.matcher(line).matches()) + { + input.append(line).append("\n"); + } + } + } + while (true); + page.close(); + final String message = "Errors: " + uploadToPastie(input); + player.sendMessage("§6" + ircBot.getNick() + ": §7" + message); + ircBot.sendMessage(message); + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, null, ex); + player.sendMessage(ex.getMessage()); + } + finally + { + try + { + if (page != null) + { + page.close(); + } + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, null, ex); + player.sendMessage(ex.getMessage()); + } + } + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/HelpCommand.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/HelpCommand.java new file mode 100644 index 000000000..a6f76cece --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/HelpCommand.java @@ -0,0 +1,18 @@ +package com.earth2me.essentials.update.chat; + +import org.bukkit.entity.Player; + + +public class HelpCommand implements Command +{ + @Override + public void run(final IrcBot ircBot, final Player player) + { + player.sendMessage("Commands: (Note: Files send to the chat will be public viewable.)"); + player.sendMessage("!errors - Send the last server errors to the chat."); + player.sendMessage("!startup - Send the last startup messages to the chat."); + player.sendMessage("!config - Sends your Essentials config to the chat."); + player.sendMessage("!list - List all players in chat."); + player.sendMessage("!quit - Leave chat."); + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/IrcBot.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/IrcBot.java index a314df15d..31e9384ee 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/IrcBot.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/IrcBot.java @@ -1,4 +1,4 @@ -package com.earth2me.essentials.update; +package com.earth2me.essentials.update.chat; import java.io.IOException; import java.util.logging.Level; @@ -95,7 +95,9 @@ public class IrcBot extends PircBot } @Override - protected void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) + protected void onKick(final String channel, final String kickerNick, + final String kickerLogin, final String kickerHostname, + final String recipientNick, final String reason) { if (recipientNick.equals(getNick())) { @@ -111,25 +113,33 @@ public class IrcBot extends PircBot } @Override - protected void onMessage(String channel, String sender, String login, String hostname, String message) + protected void onMessage(final String channel, final String sender, + final String login, final String hostname, + final String message) { player.sendMessage(formatChatMessage(sender, message, false)); } @Override - protected void onAction(String sender, String login, String hostname, String target, String action) + protected void onAction(final String sender, final String login, + final String hostname, final String target, + final String action) { player.sendMessage(formatChatMessage(sender, action, true)); } @Override - protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice) + protected void onNotice(final String sourceNick, final String sourceLogin, + final String sourceHostname, final String target, + final String notice) { player.sendMessage(formatChatMessage(sourceNick, notice, false)); } @Override - protected void onTopic(String channel, String topic, String setBy, long date, boolean changed) + protected void onTopic(final String channel, final String topic, + final String setBy, final long date, + final boolean changed) { player.sendMessage(formatChatMessage(channel, topic, false)); } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ListCommand.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ListCommand.java new file mode 100644 index 000000000..9aa932efa --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/ListCommand.java @@ -0,0 +1,32 @@ +package com.earth2me.essentials.update.chat; + +import org.bukkit.entity.Player; +import org.jibble.pircbot.User; + + +public class ListCommand implements Command +{ + @Override + public void run(final IrcBot ircBot, final Player player) + { + final User[] members = ircBot.getUsers(); + final StringBuilder message = new StringBuilder(); + for (User user : members) + { + if (message.length() > 0) + { + message.append("§f, "); + } + if (user.isOp() || user.hasVoice()) + { + message.append("§6"); + } + else + { + message.append("§7"); + } + message.append(user.getPrefix()).append(user.getNick()); + } + player.sendMessage(message.toString()); + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/StartupCommand.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/StartupCommand.java new file mode 100644 index 000000000..f244d6e3a --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/StartupCommand.java @@ -0,0 +1,77 @@ +package com.earth2me.essentials.update.chat; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.logging.Level; +import java.util.regex.Pattern; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + + +public class StartupCommand extends AbstractFileCommand implements Command +{ + private final transient Pattern patternStart = Pattern.compile("^[0-9 :-]+\\[INFO\\] Starting minecraft server version.*"); + private final transient Pattern patternEnd = Pattern.compile("^[0-9 :-]+\\[INFO\\] Done \\([0-9.,]+s\\)! For help, type \"help\".*"); + + public StartupCommand(final Plugin plugin) + { + super(plugin); + } + + @Override + public void run(final IrcBot ircBot, final Player player) + { + BufferedReader page = null; + try + { + page = getServerLogReader(); + final StringBuilder input = new StringBuilder(); + String line; + boolean log = false; + while ((line = page.readLine()) != null) + { + if (patternStart.matcher(line).matches()) + { + if (input.length() > 0) + { + input.delete(0, input.length()); + } + log = true; + } + if (log) + { + input.append(line).append("\n"); + } + if (patternEnd.matcher(line).matches()) + { + log = false; + } + } + page.close(); + final String message = "Startup: " + uploadToPastie(input); + player.sendMessage("§6" + ircBot.getNick() + ": §7" + message); + ircBot.sendMessage(message); + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, null, ex); + player.sendMessage(ex.getMessage()); + } + finally + { + try + { + if (page != null) + { + page.close(); + } + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, null, ex); + player.sendMessage(ex.getMessage()); + } + } + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/chat/UsernameUtil.java b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/UsernameUtil.java new file mode 100644 index 000000000..3df615652 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/chat/UsernameUtil.java @@ -0,0 +1,124 @@ +package com.earth2me.essentials.update.chat; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + + +public final class UsernameUtil +{ + private static final Pattern CB_PATTERN = Pattern.compile("git-Bukkit-([0-9]+).([0-9]+).([0-9]+)-[0-9]+-[0-9a-z]+-b([0-9]+)jnks.*"); + + private UsernameUtil() + { + } + + public static String createUsername(final Player player) + { + final StringBuilder nameBuilder = new StringBuilder(); + final Server server = Bukkit.getServer(); + nameBuilder.append(player.getName()); + + addCraftBukkitVersion(server, nameBuilder); + addEssentialsVersion(server, nameBuilder); + addGroupManagerVersion(server, nameBuilder); + addPermissionsExVersion(server, nameBuilder); + addPermissionsBukkitVersion(server, nameBuilder); + addBPermissionsVersion(server, nameBuilder); + addPermissionsVersion(server, nameBuilder); + + return nameBuilder.toString(); + } + + private static void addPermissionsVersion(final Server server, final StringBuilder nameBuilder) + { + final Plugin perm = server.getPluginManager().getPlugin("Permissions"); + if (perm != null) + { + nameBuilder.append(" P"); + if (!perm.isEnabled()) + { + nameBuilder.append('!'); + } + nameBuilder.append(perm.getDescription().getVersion()); + } + } + + private static void addBPermissionsVersion(final Server server, final StringBuilder nameBuilder) + { + final Plugin bperm = server.getPluginManager().getPlugin("bPermissions"); + if (bperm != null) + { + nameBuilder.append(" BP"); + if (!bperm.isEnabled()) + { + nameBuilder.append('!'); + } + nameBuilder.append(bperm.getDescription().getVersion()); + } + } + + private static void addPermissionsBukkitVersion(final Server server, final StringBuilder nameBuilder) + { + final Plugin permb = server.getPluginManager().getPlugin("PermissionsBukkit"); + if (permb != null) + { + nameBuilder.append(" PB"); + if (!permb.isEnabled()) + { + nameBuilder.append('!'); + } + nameBuilder.append(permb.getDescription().getVersion()); + } + } + + private static void addPermissionsExVersion(final Server server, final StringBuilder nameBuilder) + { + final Plugin pex = server.getPluginManager().getPlugin("PermissionsEx"); + if (pex != null) + { + nameBuilder.append(" PEX"); + if (!pex.isEnabled()) + { + nameBuilder.append('!'); + } + nameBuilder.append(pex.getDescription().getVersion()); + } + } + + private static void addGroupManagerVersion(final Server server, final StringBuilder nameBuilder) + { + final Plugin groupManager = server.getPluginManager().getPlugin("GroupManager"); + if (groupManager != null) + { + nameBuilder.append(" GM"); + if (!groupManager.isEnabled()) + { + nameBuilder.append('!'); + } + } + } + + private static void addEssentialsVersion(final Server server, final StringBuilder nameBuilder) + { + final Plugin essentials = server.getPluginManager().getPlugin("Essentials"); + if (essentials != null) + { + nameBuilder.append(" ESS"); + nameBuilder.append(essentials.getDescription().getVersion()); + } + } + + private static void addCraftBukkitVersion(final Server server, final StringBuilder nameBuilder) + { + final Matcher versionMatch = CB_PATTERN.matcher(server.getVersion()); + if (versionMatch.matches()) + { + nameBuilder.append(" CB"); + nameBuilder.append(versionMatch.group(4)); + } + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java index d0ebd7b4c..7478d81b7 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.update.states; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import org.bukkit.entity.Player; @@ -96,7 +96,7 @@ public abstract class AbstractState /** * Do something based on the answer, that the user gave. */ - public void doWork(final WorkListener listener) + public void doWork(final AbstractWorkListener listener) { listener.onWorkDone(); } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java index ef560bdc3..6aaed634a 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.update.states; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import com.earth2me.essentials.update.tasks.InstallModule; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -35,7 +35,7 @@ public class EssentialsChat extends AbstractYesNoState } @Override - public void doWork(final WorkListener listener) + public void doWork(final AbstractWorkListener listener) { if (getAnswer()) { diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsGeoIP.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsGeoIP.java index b96f17c02..a91a44787 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsGeoIP.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsGeoIP.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.update.states; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import com.earth2me.essentials.update.tasks.InstallModule; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -35,7 +35,7 @@ public class EssentialsGeoIP extends AbstractYesNoState } @Override - public void doWork(final WorkListener listener) + public void doWork(final AbstractWorkListener listener) { if (getAnswer()) { diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsProtect.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsProtect.java index 7ffb61268..ca0337d30 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsProtect.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsProtect.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.update.states; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import com.earth2me.essentials.update.tasks.InstallModule; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -35,7 +35,7 @@ public class EssentialsProtect extends AbstractYesNoState } @Override - public void doWork(final WorkListener listener) + public void doWork(final AbstractWorkListener listener) { if (getAnswer()) { diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsSpawn.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsSpawn.java index 95cc6599b..2a87638ba 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsSpawn.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsSpawn.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.update.states; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import com.earth2me.essentials.update.tasks.InstallModule; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -35,7 +35,7 @@ public class EssentialsSpawn extends AbstractYesNoState } @Override - public void doWork(final WorkListener listener) + public void doWork(final AbstractWorkListener listener) { if (getAnswer()) { diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java index 4d8c8e45b..6db8734e7 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java @@ -1,14 +1,14 @@ package com.earth2me.essentials.update.states; import com.earth2me.essentials.update.UpdateCheck; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import java.util.Iterator; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -public class StateMachine extends WorkListener implements Runnable +public class StateMachine extends AbstractWorkListener implements Runnable { public enum MachineResult { diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallModule.java b/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallModule.java index b97991a04..66eeb380d 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallModule.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallModule.java @@ -3,7 +3,7 @@ package com.earth2me.essentials.update.tasks; import com.earth2me.essentials.update.GetFile; import com.earth2me.essentials.update.ModuleInfo; import com.earth2me.essentials.update.VersionInfo; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import java.io.File; import java.net.URL; import java.util.logging.Level; @@ -12,16 +12,16 @@ import org.bukkit.Bukkit; public class InstallModule implements Runnable, Task { - protected final transient WorkListener listener; + protected final transient AbstractWorkListener listener; private final transient String moduleName; private final transient String fileName; - public InstallModule(final WorkListener listener, final String moduleName) + public InstallModule(final AbstractWorkListener listener, final String moduleName) { this(listener, moduleName, moduleName + ".jar"); } - public InstallModule(final WorkListener listener, final String moduleName, final String fileName) + public InstallModule(final AbstractWorkListener listener, final String moduleName, final String fileName) { this.listener = listener; this.moduleName = moduleName; diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/SelfUpdate.java b/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/SelfUpdate.java index e9581b2cd..777e42517 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/SelfUpdate.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/SelfUpdate.java @@ -1,14 +1,14 @@ package com.earth2me.essentials.update.tasks; -import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.AbstractWorkListener; import org.bukkit.Bukkit; -public class SelfUpdate extends WorkListener implements Task, Runnable +public class SelfUpdate extends AbstractWorkListener implements Task, Runnable { - private final transient WorkListener listener; + private final transient AbstractWorkListener listener; - public SelfUpdate(final WorkListener listener) + public SelfUpdate(final AbstractWorkListener listener) { super(listener.getPlugin(), listener.getNewVersionInfo()); this.listener = listener; diff --git a/EssentialsUpdate/test/com/earth2me/essentials/update/VersionTest.java b/EssentialsUpdate/test/com/earth2me/essentials/update/VersionTest.java index 901a8f9dc..92faade26 100644 --- a/EssentialsUpdate/test/com/earth2me/essentials/update/VersionTest.java +++ b/EssentialsUpdate/test/com/earth2me/essentials/update/VersionTest.java @@ -49,39 +49,39 @@ public class VersionTest extends TestCase @Test public void testCompareTo() { - Version a = new Version("1.1.1"); - Version b = new Version("Dev1.1.2"); - Version c = new Version("1.1.2"); - Version d = new Version("1.2.0"); - Version e = new Version("2.0.0"); - Version f = new Version("Pre1.1.1.1"); - Version g = new Version("Dev1.2.2"); - assertTrue("Testing dev", a.compareTo(b) < 0); - assertTrue("Testing dev", b.compareTo(a) > 0); - assertTrue("Testing build", a.compareTo(c) < 0); - assertTrue("Testing build", c.compareTo(a) > 0); - assertTrue("Testing minor", a.compareTo(d) < 0); - assertTrue("Testing minor", d.compareTo(a) > 0); - assertTrue("Testing major", a.compareTo(e) < 0); - assertTrue("Testing major", e.compareTo(a) > 0); - assertTrue("Testing pre", f.compareTo(a) < 0); - assertTrue("Testing pre", a.compareTo(f) > 0); - assertTrue("Testing dev vs dev", b.compareTo(g) < 0); - assertTrue("Testing dev vs dev", g.compareTo(b) > 0); + final Version verA = new Version("1.1.1"); + final Version verB = new Version("Dev1.1.2"); + final Version verC = new Version("1.1.2"); + final Version verD = new Version("1.2.0"); + final Version verE = new Version("2.0.0"); + final Version verF = new Version("Pre1.1.1.1"); + final Version verG = new Version("Dev1.2.2"); + assertTrue("Testing dev", verA.compareTo(verB) < 0); + assertTrue("Testing dev", verB.compareTo(verA) > 0); + assertTrue("Testing build", verA.compareTo(verC) < 0); + assertTrue("Testing build", verC.compareTo(verA) > 0); + assertTrue("Testing minor", verA.compareTo(verD) < 0); + assertTrue("Testing minor", verD.compareTo(verA) > 0); + assertTrue("Testing major", verA.compareTo(verE) < 0); + assertTrue("Testing major", verE.compareTo(verA) > 0); + assertTrue("Testing pre", verF.compareTo(verA) < 0); + assertTrue("Testing pre", verA.compareTo(verF) > 0); + assertTrue("Testing dev vs dev", verB.compareTo(verG) < 0); + assertTrue("Testing dev vs dev", verG.compareTo(verB) > 0); final TreeSet<Version> set = new TreeSet<Version>(); - set.add(a); - set.add(b); - set.add(c); - set.add(d); - set.add(e); - set.add(f); - set.add(g); - assertEquals("Testing sorting", f, set.pollFirst()); - assertEquals("Testing sorting", a, set.pollFirst()); - assertEquals("Testing sorting", c, set.pollFirst()); - assertEquals("Testing sorting", d, set.pollFirst()); - assertEquals("Testing sorting", e, set.pollFirst()); - assertEquals("Testing sorting", b, set.pollFirst()); - assertEquals("Testing sorting", g, set.pollFirst()); + set.add(verA); + set.add(verB); + set.add(verC); + set.add(verD); + set.add(verE); + set.add(verF); + set.add(verG); + assertEquals("Testing sorting", verF, set.pollFirst()); + assertEquals("Testing sorting", verA, set.pollFirst()); + assertEquals("Testing sorting", verC, set.pollFirst()); + assertEquals("Testing sorting", verD, set.pollFirst()); + assertEquals("Testing sorting", verE, set.pollFirst()); + assertEquals("Testing sorting", verB, set.pollFirst()); + assertEquals("Testing sorting", verG, set.pollFirst()); } } |