diff options
author | ElgarL <ElgarL@palmergames.com> | 2012-04-10 20:40:04 +0100 |
---|---|---|
committer | ElgarL <ElgarL@palmergames.com> | 2012-04-10 20:40:04 +0100 |
commit | 90dc7b628727cb976bb05dbd692d52c752df068d (patch) | |
tree | 5196dd668710ea4627e15a356bfbe3984b9fb458 /EssentialsGroupManager | |
parent | 74534d7781850f4cbb854c084911503c12f13cdf (diff) | |
download | Essentials-90dc7b628727cb976bb05dbd692d52c752df068d.tar Essentials-90dc7b628727cb976bb05dbd692d52c752df068d.tar.gz Essentials-90dc7b628727cb976bb05dbd692d52c752df068d.tar.lz Essentials-90dc7b628727cb976bb05dbd692d52c752df068d.tar.xz Essentials-90dc7b628727cb976bb05dbd692d52c752df068d.zip |
Stop the error on shutdown if GM failed to load at startup.
GroupManager will now generate it's own log (in the GM folder) to
keep things tidy, but also to account of those players unable to
find/access their server.log.
Startup errors will now lock out ALL commands other than '/manload'
Diffstat (limited to 'EssentialsGroupManager')
3 files changed, 173 insertions, 43 deletions
diff --git a/EssentialsGroupManager/src/Changelog.txt b/EssentialsGroupManager/src/Changelog.txt index 0a9708773..2c38acf1b 100644 --- a/EssentialsGroupManager/src/Changelog.txt +++ b/EssentialsGroupManager/src/Changelog.txt @@ -169,4 +169,7 @@ v 2.0: - Expand 'manwhois' to also list a users subgroups.
- Fix a concurrent modification error when removing all attachments.
- Better handling of errors in user and group yml's.
- - Added missing confirmation message on '/manload'.
\ No newline at end of file + - Added missing confirmation message on '/manload'.
+ - Stop the error on shutdown if GM failed to load at startup.
+ - GroupManager will now generate it's own log (in the GM folder) to keep things tidy, but also to account of those players unable to find/access their server.log.
+ - Startup errors will now lock out ALL commands other than '/manload'
\ No newline at end of file diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GroupManager.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GroupManager.java index e10675e8d..b39b640a4 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/GroupManager.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/GroupManager.java @@ -14,6 +14,7 @@ import org.anjocaido.groupmanager.data.Group; import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder; import org.anjocaido.groupmanager.dataholder.WorldDataHolder; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; @@ -60,6 +61,9 @@ public class GroupManager extends JavaPlugin { private Map<CommandSender, String> selectedWorlds = new HashMap<CommandSender, String>(); private WorldsHolder worldsHolder; private boolean validateOnlinePlayer = true; + + private String lastError = ""; + /** * @return the validateOnlinePlayer */ @@ -108,8 +112,10 @@ public class GroupManager extends JavaPlugin { WorldEvents = null; // Remove all attachments before clearing - BukkitPermissions.removeAllAttachments(); - BukkitPermissions = null; + if (BukkitPermissions != null) { + BukkitPermissions.removeAllAttachments(); + BukkitPermissions = null; + } // EXAMPLE: Custom code, here we just output some info so we can check that // all is well @@ -118,53 +124,108 @@ public class GroupManager extends JavaPlugin { GroupManager.logger.removeHandler(ch); } - @Override + //@Override public void onEnable() { - GroupManager.logger.setUseParentHandlers(false); - ch = new GMLoggerHandler(); - GroupManager.logger.addHandler(ch); - logger.setLevel(Level.ALL); - - // Create the backup folder, if it doesn't exist. - prepareFileFields(); - // Load the config.yml - prepareConfig(); - // Load the global groups - globalGroups = new GlobalGroups(this); - worldsHolder = new WorldsHolder(this); + + try { + lastError = ""; + + GroupManager.logger.setUseParentHandlers(false); + ch = new GMLoggerHandler(); + GroupManager.logger.addHandler(ch); + logger.setLevel(Level.ALL); + + // Create the backup folder, if it doesn't exist. + prepareFileFields(); + // Load the config.yml + prepareConfig(); + // Load the global groups + globalGroups = new GlobalGroups(this); + worldsHolder = new WorldsHolder(this); + + + PluginDescriptionFile pdfFile = this.getDescription(); + if (worldsHolder == null) { + GroupManager.logger.severe("Can't enable " + pdfFile.getName() + " version " + pdfFile.getVersion() + ", bad loading!"); + this.getServer().getPluginManager().disablePlugin(this); + throw new IllegalStateException("An error ocurred while loading GroupManager"); + } + + // Set a few defaults (reloads) + setLoaded(false); + + // Initialize the world listener and bukkit permissions to handle + // events. + WorldEvents = new GMWorldListener(this); + BukkitPermissions = new BukkitPermissions(this); + + enableScheduler(); + + /* + * Schedule a Bukiit Permissions update for 1 tick later. All plugins + * will be loaded by then + */ + + if (getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitPermsUpdateTask(), 1) == -1) { + GroupManager.logger.severe("Could not schedule superperms Update."); + setLoaded(true); + } + + System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!"); + + // Register as a service + this.getServer().getServicesManager().register(WorldsHolder.class, this.worldsHolder, this, ServicePriority.Lowest); + } catch (Exception ex) { + + /* + * Store the error and write to the log. + */ + saveErrorLog(ex); + /* + * Throw an error so Bukkit knows about it. + */ + throw new IllegalArgumentException(ex.getMessage(),ex); - PluginDescriptionFile pdfFile = this.getDescription(); - if (worldsHolder == null) { - GroupManager.logger.severe("Can't enable " + pdfFile.getName() + " version " + pdfFile.getVersion() + ", bad loading!"); - this.getServer().getPluginManager().disablePlugin(this); - throw new IllegalStateException("An error ocurred while loading GroupManager"); } - - // Set a few defaults (reloads) - setLoaded(false); + } + + /** + * Write an error.log + * + * @param ex + */ + private void saveErrorLog(Exception ex) { - // Initialize the world listener and bukkit permissions to handle - // events. - WorldEvents = new GMWorldListener(this); - BukkitPermissions = new BukkitPermissions(this); - - enableScheduler(); - - /* - * Schedule a Bukiit Permissions update for 1 tick later. All plugins - * will be loaded by then - */ - - if (getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitPermsUpdateTask(), 1) == -1) { - GroupManager.logger.severe("Could not schedule superperms Update."); - setLoaded(true); + if (!getDataFolder().exists()) { + getDataFolder().mkdirs(); + } + + lastError = ex.getMessage(); + + GroupManager.logger.severe("==================================================="); + GroupManager.logger.severe("= ERROR REPORT START ="); + GroupManager.logger.severe("==================================================="); + GroupManager.logger.severe("=== PLEASE COPY AND PASTE THE ERROR.LOG FROM THE =="); + GroupManager.logger.severe("= GROUPMANAGER FOLDER TO AN ESSENTIALS DEVELOPER ="); + GroupManager.logger.severe("==================================================="); + GroupManager.logger.severe(lastError); + GroupManager.logger.severe("==================================================="); + GroupManager.logger.severe("= ERROR REPORT ENDED ="); + GroupManager.logger.severe("==================================================="); + + // Append this error to the error log. + try { + String error = "=============================== GM ERROR LOG ===============================\n\n"; + error += Tasks.getStackTraceAsString(ex); + error += "\n============================================================================\n"; + + Tasks.appendStringToFile(error, (getDataFolder() + System.getProperty("file.separator") + "ERROR.LOG")); + } catch (IOException e) { + // Failed to write file. + e.printStackTrace(); } - System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!"); - - // Register as a service - this.getServer().getServicesManager().register(WorldsHolder.class, this.worldsHolder, this, ServicePriority.Lowest); } public static boolean isLoaded() { @@ -300,10 +361,17 @@ public class GroupManager extends JavaPlugin { Group senderGroup = null; User senderUser = null; boolean isOpOverride = config.isOpOverride(); + // DETERMINING PLAYER INFORMATION if (sender instanceof Player) { senderPlayer = (Player) sender; + + if (!lastError.isEmpty() && !commandLabel.equalsIgnoreCase("manload")) { + sender.sendMessage(ChatColor.RED + "All commands are locked due to an error. Check the log and then try a '/manload'.)"); + return true; + } + senderUser = worldsHolder.getWorldData(senderPlayer).getUser(senderPlayer.getName()); senderGroup = senderUser.getGroup(); isOpOverride = (isOpOverride && (senderPlayer.isOp() || worldsHolder.getWorldPermissions(senderPlayer).has(senderPlayer, "groupmanager.op"))); @@ -313,6 +381,12 @@ public class GroupManager extends JavaPlugin { playerCanDo = true; } } else if (sender instanceof ConsoleCommandSender) { + + if (!lastError.isEmpty() && !commandLabel.equalsIgnoreCase("manload")) { + sender.sendMessage(ChatColor.RED + "All commands are locked due to an error. Check the log and then try a '/manload'.)"); + return true; + } + isConsole = true; } @@ -1560,10 +1634,17 @@ public class GroupManager extends JavaPlugin { return true; case manload: + /** * Attempt to reload a specific world */ if (args.length > 0) { + + if (!lastError.isEmpty()) { + sender.sendMessage(ChatColor.RED + "All commands are locked due to an error. Check the log and then try a '/manload'.)"); + return true; + } + auxString = ""; for (int i = 0; i < args.length; i++) { auxString += args[i]; @@ -1588,6 +1669,11 @@ public class GroupManager extends JavaPlugin { /** * Reload all settings and data as no world was specified. */ + + /* + * Reset the last error as we are attempting a fresh load. + */ + lastError = ""; onDisable(); onEnable(); diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/utils/Tasks.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/utils/Tasks.java index f3defd94a..663da1123 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/utils/Tasks.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/utils/Tasks.java @@ -4,12 +4,17 @@ */ package org.anjocaido.groupmanager.utils; +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.List; @@ -17,11 +22,25 @@ import java.util.List; import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.data.Group; + /** * * @author gabrielcouto */ public abstract class Tasks { + + /** + * Gets the exception stack trace as a string. + * + * @param exception + * @return stack trace as a string + */ + public static String getStackTraceAsString(Exception exception) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + exception.printStackTrace(pw); + return sw.toString(); + } public static void copy(InputStream src, File dst) throws IOException { InputStream in = src; @@ -44,6 +63,28 @@ public abstract class Tasks { InputStream in = new FileInputStream(src); copy(in, dst); } + + /** + * Appends a string to a file + * + * @param data + * @param file + */ + public static void appendStringToFile(String data, String file) throws IOException { + + FileWriter outStream = new FileWriter("." + System.getProperty("file.separator") + file, true); + + BufferedWriter out = new BufferedWriter(outStream); + + data.replaceAll("\n", System.getProperty("line.separator")); + + out.append(new SimpleDateFormat("yyyy-MM-dd HH-mm").format(System.currentTimeMillis())); + out.append(System.getProperty("line.separator")); + out.append(data); + out.append(System.getProperty("line.separator")); + + out.close(); + } public static void removeOldFiles(GroupManager gm, File folder) { if (folder.isDirectory()) { |