Gracefully handle botched plugin starts

This commit is contained in:
KHobbits 2013-08-13 23:37:33 +01:00
parent 66eb9202ce
commit ff41588c3b
2 changed files with 132 additions and 103 deletions

View File

@ -132,121 +132,120 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
@Override @Override
public void onEnable() public void onEnable()
{ {
execTimer = new ExecuteTimer();
execTimer.start();
i18n = new I18n(this);
i18n.onEnable();
execTimer.mark("I18n1");
scm = new SimpleCommandMap(this.getServer());
final PluginManager pm = getServer().getPluginManager();
for (Plugin plugin : pm.getPlugins())
{
if (plugin.getDescription().getName().startsWith("Essentials")
&& !plugin.getDescription().getVersion().equals(this.getDescription().getVersion())
&& !plugin.getDescription().getName().equals("EssentialsAntiCheat"))
{
LOGGER.log(Level.WARNING, _("versionMismatch", plugin.getDescription().getName()));
}
}
final Matcher versionMatch = Pattern.compile("git-Bukkit-(?:(?:[0-9]+)\\.)+[0-9]+-R[\\.0-9]+-(?:[0-9]+-g[0-9a-f]+-)?b([0-9]+)jnks.*").matcher(getServer().getVersion());
if (versionMatch.matches())
{
final int versionNumber = Integer.parseInt(versionMatch.group(1));
if (versionNumber < BUKKIT_VERSION && versionNumber > 100)
{
LOGGER.log(Level.SEVERE, " * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! *");
LOGGER.log(Level.SEVERE, _("notRecommendedBukkit"));
LOGGER.log(Level.SEVERE, _("requiredBukkit", Integer.toString(BUKKIT_VERSION)));
LOGGER.log(Level.SEVERE, " * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! *");
this.setEnabled(false);
return;
}
}
else
{
LOGGER.log(Level.INFO, _("bukkitFormatChanged"));
LOGGER.log(Level.INFO, getServer().getVersion());
LOGGER.log(Level.INFO, getServer().getBukkitVersion());
}
execTimer.mark("BukkitCheck");
try try
{ {
final EssentialsUpgrade upgrade = new EssentialsUpgrade(this); execTimer = new ExecuteTimer();
upgrade.beforeSettings(); execTimer.start();
execTimer.mark("Upgrade"); i18n = new I18n(this);
confList = new ArrayList<IConf>(); i18n.onEnable();
settings = new Settings(this); execTimer.mark("I18n1");
confList.add(settings); scm = new SimpleCommandMap(this.getServer());
execTimer.mark("Settings"); final PluginManager pm = getServer().getPluginManager();
upgrade.afterSettings(); for (Plugin plugin : pm.getPlugins())
execTimer.mark("Upgrade2");
i18n.updateLocale(settings.getLocale());
userMap = new UserMap(this);
confList.add(userMap);
execTimer.mark("Init(Usermap)");
warps = new Warps(getServer(), this.getDataFolder());
confList.add(warps);
execTimer.mark("Init(Spawn/Warp)");
worth = new Worth(this.getDataFolder());
confList.add(worth);
itemDb = new ItemDb(this);
confList.add(itemDb);
execTimer.mark("Init(Worth/ItemDB)");
jails = new Jails(this);
confList.add(jails);
reload();
}
catch (YAMLException exception)
{
if (pm.getPlugin("EssentialsUpdate") != null)
{ {
LOGGER.log(Level.SEVERE, _("essentialsHelp2")); if (plugin.getDescription().getName().startsWith("Essentials")
&& !plugin.getDescription().getVersion().equals(this.getDescription().getVersion())
&& !plugin.getDescription().getName().equals("EssentialsAntiCheat"))
{
LOGGER.log(Level.WARNING, _("versionMismatch", plugin.getDescription().getName()));
}
}
final Matcher versionMatch = Pattern.compile("git-Bukkit-(?:(?:[0-9]+)\\.)+[0-9]+-R[\\.0-9]+-(?:[0-9]+-g[0-9a-f]+-)?b([0-9]+)jnks.*").matcher(getServer().getVersion());
if (versionMatch.matches())
{
final int versionNumber = Integer.parseInt(versionMatch.group(1));
if (versionNumber < BUKKIT_VERSION && versionNumber > 100)
{
LOGGER.log(Level.SEVERE, " * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! *");
LOGGER.log(Level.SEVERE, _("notRecommendedBukkit"));
LOGGER.log(Level.SEVERE, _("requiredBukkit", Integer.toString(BUKKIT_VERSION)));
LOGGER.log(Level.SEVERE, " * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! * ! *");
this.setEnabled(false);
return;
}
} }
else else
{ {
LOGGER.log(Level.SEVERE, _("essentialsHelp1")); LOGGER.log(Level.INFO, _("bukkitFormatChanged"));
LOGGER.log(Level.INFO, getServer().getVersion());
LOGGER.log(Level.INFO, getServer().getBukkitVersion());
} }
LOGGER.log(Level.SEVERE, exception.toString()); execTimer.mark("BukkitCheck");
pm.registerEvents(new Listener() try
{ {
@EventHandler(priority = EventPriority.LOW) final EssentialsUpgrade upgrade = new EssentialsUpgrade(this);
public void onPlayerJoin(final PlayerJoinEvent event) upgrade.beforeSettings();
execTimer.mark("Upgrade");
confList = new ArrayList<IConf>();
settings = new Settings(this);
confList.add(settings);
execTimer.mark("Settings");
upgrade.afterSettings();
execTimer.mark("Upgrade2");
i18n.updateLocale(settings.getLocale());
userMap = new UserMap(this);
confList.add(userMap);
execTimer.mark("Init(Usermap)");
warps = new Warps(getServer(), this.getDataFolder());
confList.add(warps);
execTimer.mark("Init(Spawn/Warp)");
worth = new Worth(this.getDataFolder());
confList.add(worth);
itemDb = new ItemDb(this);
confList.add(itemDb);
execTimer.mark("Init(Worth/ItemDB)");
jails = new Jails(this);
confList.add(jails);
reload();
}
catch (YAMLException exception)
{
if (pm.getPlugin("EssentialsUpdate") != null)
{ {
event.getPlayer().sendMessage("Essentials failed to load, read the log file."); LOGGER.log(Level.SEVERE, _("essentialsHelp2"));
} }
}, this); else
for (Player player : getServer().getOnlinePlayers()) {
{ LOGGER.log(Level.SEVERE, _("essentialsHelp1"));
player.sendMessage("Essentials failed to load, read the log file."); }
handleCrash(exception);
return;
}
backup = new Backup(this);
permissionsHandler = new PermissionsHandler(this, settings.useBukkitPermissions());
alternativeCommandsHandler = new AlternativeCommandsHandler(this);
timer = new EssentialsTimer(this);
scheduleSyncRepeatingTask(timer, 1000, 50);
Economy.setEss(this);
execTimer.mark("RegHandler");
final MetricsStarter metricsStarter = new MetricsStarter(this);
if (metricsStarter.getStart() != null && metricsStarter.getStart() == true)
{
runTaskLaterAsynchronously(metricsStarter, 1);
}
else if (metricsStarter.getStart() != null && metricsStarter.getStart() == false)
{
final MetricsListener metricsListener = new MetricsListener(this, metricsStarter);
pm.registerEvents(metricsListener, this);
}
final String timeroutput = execTimer.end();
if (getSettings().isDebug())
{
LOGGER.log(Level.INFO, "Essentials load " + timeroutput);
} }
this.setEnabled(false);
return;
} }
backup = new Backup(this); catch (Exception ex)
permissionsHandler = new PermissionsHandler(this, settings.useBukkitPermissions());
alternativeCommandsHandler = new AlternativeCommandsHandler(this);
timer = new EssentialsTimer(this);
scheduleSyncRepeatingTask(timer, 1000, 50);
Economy.setEss(this);
execTimer.mark("RegHandler");
final MetricsStarter metricsStarter = new MetricsStarter(this);
if (metricsStarter.getStart() != null && metricsStarter.getStart() == true)
{ {
runTaskLaterAsynchronously(metricsStarter, 1); handleCrash(ex);
} }
else if (metricsStarter.getStart() != null && metricsStarter.getStart() == false) catch (Error ex)
{ {
final MetricsListener metricsListener = new MetricsListener(this, metricsStarter); handleCrash(ex);
pm.registerEvents(metricsListener, this); throw ex;
}
final String timeroutput = execTimer.end();
if (getSettings().isDebug())
{
LOGGER.log(Level.INFO, "Essentials load " + timeroutput);
} }
} }
@ -308,8 +307,14 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
} }
} }
cleanupOpenInventories(); cleanupOpenInventories();
i18n.onDisable(); if (i18n != null)
backup.stopTask(); {
i18n.onDisable();
}
if (backup != null)
{
backup.stopTask();
}
Economy.setEss(null); Economy.setEss(null);
Trade.closeLog(); Trade.closeLog();
} }
@ -624,6 +629,25 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
return user; return user;
} }
private void handleCrash(Throwable exception)
{
final PluginManager pm = getServer().getPluginManager();
LOGGER.log(Level.SEVERE, exception.toString());
pm.registerEvents(new Listener()
{
@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(final PlayerJoinEvent event)
{
event.getPlayer().sendMessage("Essentials failed to load, read the log file.");
}
}, this);
for (Player player : getServer().getOnlinePlayers())
{
player.sendMessage("Essentials failed to load, read the log file.");
}
this.setEnabled(false);
}
@Override @Override
public World getWorld(final String name) public World getWorld(final String name)
{ {

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import com.earth2me.essentials.perm.PermissionsHandler;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -40,7 +41,11 @@ public class EssentialsPluginListener implements Listener, IConf
{ {
ess.getSettings().setEssentialsChatActive(false); ess.getSettings().setEssentialsChatActive(false);
} }
ess.getPermissionsHandler().checkPermissions(); PermissionsHandler permHandler = ess.getPermissionsHandler();
if (permHandler != null)
{
permHandler.checkPermissions();
}
ess.getAlternativeCommandsHandler().removePlugin(event.getPlugin()); ess.getAlternativeCommandsHandler().removePlugin(event.getPlugin());
// Check to see if the plugin thats being disabled is the one we are using // Check to see if the plugin thats being disabled is the one we are using
if (ess.getPaymentMethod() != null && ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().checkDisabled(event.getPlugin())) if (ess.getPaymentMethod() != null && ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().checkDisabled(event.getPlugin()))