package net.sourceforge.pain.logic.event.console.command;
import net.sourceforge.pain.*;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.network.console.*;
import net.sourceforge.pain.plugin.*;
import net.sourceforge.pain.util.*;
import java.util.*;
public final class ReloadCode extends GrantedCommand {
public void processCommand() {
if (commandParams != null) {
try {
commandParams = commandParams.trim();
if (commandParams.equals("LOGIC")) {
reloadLogic();
} else {
if (!commandParams.startsWith("plugin.")) {
showHelp();
} else {
String pluginName = commandParams.substring("plugin.".length());
PluginManager plm = Core.getPluginManager();
Plugin p = plm.getPlugin(pluginName);
if ("UNLOAD".equals(command.tag) || "RELOAD".equals(command.tag)) {
if (p == null) {
MessageOutFn.outln(console, "Plugin:{W'" + pluginName + "'{x was not loaded!");
} else {
for (Iterator it = p.getDependedPlugins().iterator(); it.hasNext();) {
String childName = (String) it.next();
MessageOutFn.outln(console, "Direct depended plugins to unload:{W'" + childName + "'{x");
}
MessageOutFn.outln(console, "Unloading plugin:{W'" + pluginName + "'{x");
plm.unloadPlugin(p);
}
}
if ("RELOAD".equals(command.tag) || "LOAD".equals(command.tag)) {
if (plm.getPlugin(pluginName) != null) {
MessageOutFn.outln(console, "Plugin:{W'" + pluginName + "'{x is already loaded");
} else {
MessageOutFn.outln(console, "Loading plugin:'{W" + pluginName + "'{x");
plm.loadPlugin(pluginName);
}
}
}
}
} catch (Exception e) {
Log.error(e.getMessage(), e);
MessageOutFn.outln(console, "{RReloading error!:" + e.getClass().getName() + "\n{G" + e.getMessage() + "{x");
} catch (NoClassDefFoundError e) {
MessageOutFn.outln(console, "{RCritical reloading error!:" + e.getClass().getName() + "\n{G" + e.getMessage() + "{x");
}
} else {
showHelp();
}
}
private void reloadLogic() {
if (!"RELOAD".equals(command.tag)) {
MessageOutFn.outln(console, "You can RELOAD logic classes only!");
} else {
// Moving all consoles to command mode!
final ConsoleManager cm = Core.getConsoleManager();
for (Iterator it = cm.consoles().iterator(); it.hasNext();) {
Console c = (Console) it.next();
final Player p = c.getPlayer();
if (p != null) {
Log.debug(p.asInteractive().getName() + " raw:" + c.isRawMode());
if (c.isRawMode() && c.getPlayer() != null) {
c.setCommandMode();
MessageOutFn.outln(c, "{wCode reloaded!{x");
ShowFn.showSpace(p.asReceptive(), p.asLocated().getLocation());
} else {
c.setCommandMode();
}
} else {// player is just logging in
MessageOutFn.outln(c, "\n{wSorry, server code is reloading!{x");
c.setCommandMode();
ConsoleManager.closeConsole(c);
}
}
Core.getLogicLoader().reload();
MessageOutFn.outln(console, "{GLogic classes reloaded!{x"); // all classes in this code still from the old code
}
}
public void showHelp() {
if ("LOAD".equals(command.tag)) {
MessageOutFn.outln(console, "This command loads plugins");
MessageOutFn.outln(console, "Specify plugin name to load plugin: 'plugin.name...'");
} else if ("RELOAD".equals(command.tag)) {
MessageOutFn.outln(console, "This command reloads logic code");
MessageOutFn.outln(console, "Specify 'LOGIC' to reload all classes in 'logic' package ");
MessageOutFn.outln(console, "OR specify plugin name to reload plugin: 'plugin.name..'");
MessageOutFn.outln(console, "WARN: all depended plugin will be unloaded and will not be loaded again!");
} else if ("UNLOAD".equals(command.tag)) {
MessageOutFn.outln(console, "This command unloads plugins");
MessageOutFn.outln(console, "Specify plugin name to unload plugin: 'plugin.name..'");
MessageOutFn.outln(console, "WARN: all depended plugin will be unloaded!");
}
}
}