Citizens2/src/main/java/net/citizensnpcs/Citizens.java

381 lines
14 KiB
Java
Raw Normal View History

2012-01-15 00:51:37 +01:00
package net.citizensnpcs;
2012-01-19 11:52:58 +01:00
import java.io.File;
2012-05-26 16:31:01 +02:00
import java.io.IOException;
import java.util.Iterator;
2012-01-19 11:52:58 +01:00
2012-01-23 15:30:15 +01:00
import net.citizensnpcs.Settings.Setting;
2012-01-15 00:51:37 +01:00
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.CitizensPlugin;
2012-08-19 06:51:45 +02:00
import net.citizensnpcs.api.event.CitizensDisableEvent;
import net.citizensnpcs.api.event.CitizensEnableEvent;
2012-03-08 02:03:32 +01:00
import net.citizensnpcs.api.event.CitizensReloadEvent;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.api.exception.NPCLoadException;
2012-01-18 05:04:08 +01:00
import net.citizensnpcs.api.npc.NPC;
2012-05-17 15:34:03 +02:00
import net.citizensnpcs.api.npc.NPCRegistry;
2012-04-12 17:31:54 +02:00
import net.citizensnpcs.api.scripting.EventRegistrar;
2012-05-03 17:22:31 +02:00
import net.citizensnpcs.api.scripting.ObjectProvider;
2012-04-12 17:01:56 +02:00
import net.citizensnpcs.api.scripting.ScriptCompiler;
2012-10-08 06:37:45 +02:00
import net.citizensnpcs.api.trait.Trait;
2012-07-19 17:10:30 +02:00
import net.citizensnpcs.api.trait.TraitFactory;
2012-09-13 15:02:55 +02:00
import net.citizensnpcs.command.CommandContext;
import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.CommandManager.CommandInfo;
2012-01-25 21:07:49 +01:00
import net.citizensnpcs.command.Injector;
import net.citizensnpcs.command.command.AdminCommands;
2012-02-27 09:32:43 +01:00
import net.citizensnpcs.command.command.EditorCommands;
2012-02-08 09:26:14 +01:00
import net.citizensnpcs.command.command.HelpCommands;
import net.citizensnpcs.command.command.NPCCommands;
2012-04-13 17:59:51 +02:00
import net.citizensnpcs.command.command.ScriptCommands;
2012-09-29 17:54:05 +02:00
import net.citizensnpcs.command.command.TemplateCommands;
2012-08-05 15:07:42 +02:00
import net.citizensnpcs.command.command.TraitCommands;
import net.citizensnpcs.command.command.WaypointCommands;
2012-02-29 11:50:45 +01:00
import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.command.exception.CommandUsageException;
import net.citizensnpcs.command.exception.ServerCommandException;
import net.citizensnpcs.command.exception.UnhandledCommandException;
import net.citizensnpcs.command.exception.WrappedCommandException;
2012-02-27 16:59:39 +01:00
import net.citizensnpcs.editor.Editor;
2012-05-17 15:34:03 +02:00
import net.citizensnpcs.npc.CitizensNPCRegistry;
2012-07-19 17:10:30 +02:00
import net.citizensnpcs.npc.CitizensTraitFactory;
import net.citizensnpcs.npc.NPCSelector;
2012-09-16 13:20:01 +02:00
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper;
2012-08-15 18:06:13 +02:00
import net.milkbowl.vault.economy.Economy;
2012-01-15 00:51:37 +01:00
import org.bukkit.Bukkit;
2012-01-22 18:43:58 +01:00
import org.bukkit.ChatColor;
2012-01-19 11:52:58 +01:00
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
2012-01-29 21:58:19 +01:00
import org.bukkit.craftbukkit.CraftServer;
2012-01-19 11:52:58 +01:00
import org.bukkit.entity.Player;
2012-07-19 17:10:30 +02:00
import org.bukkit.plugin.Plugin;
2012-09-13 14:39:52 +02:00
import org.bukkit.plugin.PluginLoadOrder;
2012-08-15 18:06:13 +02:00
import org.bukkit.plugin.RegisteredServiceProvider;
2012-01-15 00:51:37 +01:00
import org.bukkit.plugin.java.JavaPlugin;
2012-05-26 16:31:01 +02:00
import com.google.common.collect.Iterables;
public class Citizens extends JavaPlugin implements CitizensPlugin {
2012-02-12 08:19:32 +01:00
private final CommandManager commands = new CommandManager();
2012-03-10 11:33:11 +01:00
private boolean compatible;
2012-03-27 16:42:15 +02:00
private Settings config;
2012-04-18 04:12:17 +02:00
private ClassLoader contextClassLoader;
2012-05-17 15:34:03 +02:00
private CitizensNPCRegistry npcRegistry;
2012-09-13 15:02:55 +02:00
private NPCDataStore saves;
private NPCSelector selector;
2012-07-19 17:10:30 +02:00
private CitizensTraitFactory traitFactory;
2012-03-02 14:04:40 +01:00
2012-07-19 17:10:30 +02:00
private void despawnNPCs() {
Iterator<NPC> itr = npcRegistry.iterator();
while (itr.hasNext()) {
NPC npc = itr.next();
2012-11-04 06:31:22 +01:00
try {
npc.despawn();
for (Trait t : npc.getTraits())
t.onRemove();
} catch (Exception e) {
e.printStackTrace();
// ensure that all entities are despawned
}
2012-07-24 08:21:59 +02:00
itr.remove();
2012-07-19 17:10:30 +02:00
}
}
private void enableSubPlugins() {
File root = new File(getDataFolder(), Setting.SUBPLUGIN_FOLDER.asString());
if (!root.exists() || !root.isDirectory())
return;
2012-09-13 14:39:52 +02:00
File[] files = root.listFiles();
for (File file : files) {
Plugin plugin;
try {
plugin = Bukkit.getPluginManager().loadPlugin(file);
} catch (Exception e) {
continue;
}
if (plugin == null)
continue;
// code beneath modified from CraftServer
2012-07-19 17:10:30 +02:00
try {
Messaging.logTr(Messages.LOADING_SUB_PLUGIN, plugin.getDescription().getFullName());
2012-07-19 17:10:30 +02:00
plugin.onLoad();
} catch (Throwable ex) {
Messaging.severeTr(Messages.ERROR_INITALISING_SUB_PLUGIN, ex.getMessage(), plugin
.getDescription().getFullName());
2012-07-19 17:10:30 +02:00
ex.printStackTrace();
}
}
2012-09-13 14:39:52 +02:00
((CraftServer) Bukkit.getServer()).enablePlugins(PluginLoadOrder.POSTWORLD);
}
public CommandInfo getCommandInfo(String rootCommand, String modifier) {
return commands.getCommand(rootCommand, modifier);
}
public Iterable<CommandInfo> getCommands(String base) {
2012-05-17 15:34:03 +02:00
return commands.getCommands(base);
2012-03-02 11:36:54 +01:00
}
@Override
2012-05-17 15:34:03 +02:00
public NPCRegistry getNPCRegistry() {
return npcRegistry;
2012-03-02 11:36:54 +01:00
}
public NPCSelector getNPCSelector() {
return selector;
}
@Override
public File getScriptFolder() {
return new File(getDataFolder(), "scripts");
}
@Override
2012-07-19 17:10:30 +02:00
public TraitFactory getTraitFactory() {
return traitFactory;
}
2012-01-23 09:45:34 +01:00
@Override
public boolean onCommand(CommandSender sender, Command command, String cmdName, String[] args) {
try {
2012-02-12 08:19:32 +01:00
String modifier = args.length > 0 ? args[0] : "";
if (!commands.hasCommand(command, modifier) && !modifier.isEmpty()) {
return suggestClosestModifier(sender, command.getName().toLowerCase(), modifier);
}
NPC npc = selector.getSelected(sender);
// TODO: change the args supplied to a context style system for
// flexibility (ie. adding more context in the future without
// changing everything)
try {
commands.execute(command, args, sender, sender, npc);
} catch (ServerCommandException ex) {
2012-09-29 17:07:48 +02:00
Messaging.sendTr(sender, Messages.COMMAND_MUST_BE_INGAME);
} catch (CommandUsageException ex) {
Messaging.sendError(sender, ex.getMessage());
Messaging.sendError(sender, ex.getUsage());
2012-02-29 11:50:45 +01:00
} catch (WrappedCommandException ex) {
throw ex.getCause();
} catch (UnhandledCommandException ex) {
return false;
2012-02-29 11:50:45 +01:00
} catch (CommandException ex) {
Messaging.sendError(sender, ex.getMessage());
}
2012-02-29 11:50:45 +01:00
} catch (NumberFormatException ex) {
Messaging.sendErrorTr(sender, Messages.COMMAND_INVALID_NUMBER);
2012-02-29 11:50:45 +01:00
} catch (Throwable ex) {
ex.printStackTrace();
if (sender instanceof Player) {
Messaging.sendErrorTr(sender, Messages.COMMAND_REPORT_ERROR);
Messaging.sendError(sender, ex.getClass().getName() + ": " + ex.getMessage());
}
2012-01-23 09:45:34 +01:00
}
return true;
}
2012-01-19 12:43:21 +01:00
@Override
public void onDisable() {
Bukkit.getPluginManager().callEvent(new CitizensDisableEvent());
2012-07-22 08:18:24 +02:00
Editor.leaveAll();
CitizensAPI.shutdown();
tearDownScripting();
2012-01-29 21:58:19 +01:00
// Don't bother with this part if MC versions are not compatible
2012-01-30 12:58:59 +01:00
if (compatible) {
2012-09-14 08:53:10 +02:00
saves.storeAll(npcRegistry);
2012-09-13 15:02:55 +02:00
saves.saveToDiskImmediate();
despawnNPCs();
2012-05-17 15:34:03 +02:00
npcRegistry = null;
2012-01-29 21:58:19 +01:00
}
2012-01-21 13:53:58 +01:00
Messaging.logTr(Messages.CITIZENS_DISABLED, getDescription().getVersion());
2012-01-19 12:43:21 +01:00
}
2012-01-15 00:51:37 +01:00
2012-01-19 12:43:21 +01:00
@Override
public void onEnable() {
2012-11-13 06:54:37 +01:00
CitizensAPI.setImplementation(this);
2012-01-29 21:58:19 +01:00
// Disable if the server is not using the compatible Minecraft version
2012-01-30 12:58:59 +01:00
String mcVersion = ((CraftServer) getServer()).getServer().getVersion();
2012-04-05 17:03:30 +02:00
compatible = mcVersion.startsWith(COMPATIBLE_MC_VERSION);
2012-01-30 12:58:59 +01:00
if (!compatible) {
Messaging.severeTr(Messages.CITIZENS_INCOMPATIBLE, getDescription().getVersion(), mcVersion);
2012-01-29 21:58:19 +01:00
getServer().getPluginManager().disablePlugin(this);
return;
}
config = new Settings(getDataFolder());
2012-04-18 04:12:17 +02:00
registerScriptHelpers();
2012-01-29 21:58:19 +01:00
2012-09-13 15:02:55 +02:00
saves = NPCDataStore.create(getDataFolder());
if (saves == null) {
2012-09-16 13:20:01 +02:00
Messaging.severeTr(Messages.FAILED_LOAD_SAVES);
getServer().getPluginManager().disablePlugin(this);
return;
}
2012-02-12 08:19:32 +01:00
2012-05-17 15:34:03 +02:00
npcRegistry = new CitizensNPCRegistry(saves);
2012-07-19 17:10:30 +02:00
traitFactory = new CitizensTraitFactory();
selector = new NPCSelector(this);
2012-01-23 17:05:22 +01:00
2012-05-17 15:34:03 +02:00
getServer().getPluginManager().registerEvents(new EventListen(), this);
2012-09-16 13:20:01 +02:00
if (Setting.NPC_COST.asDouble() > 0)
setupEconomy();
registerCommands();
2012-09-20 02:52:44 +02:00
enableSubPlugins();
Messaging.logTr(Messages.CITIZENS_ENABLED, getDescription().getVersion());
2012-01-18 05:04:08 +01:00
2012-01-19 12:43:21 +01:00
// Setup NPCs after all plugins have been enabled (allows for multiworld
// support and for NPCs to properly register external settings)
if (getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
2012-01-19 12:43:21 +01:00
@Override
public void run() {
2012-09-13 15:02:55 +02:00
saves.loadInto(npcRegistry);
2012-05-26 16:31:01 +02:00
startMetrics();
2012-08-15 18:06:13 +02:00
scheduleSaveTask(Setting.SAVE_TASK_DELAY.asInt());
2012-08-19 06:51:45 +02:00
Bukkit.getPluginManager().callEvent(new CitizensEnableEvent());
2012-01-19 12:43:21 +01:00
}
2012-10-08 04:16:19 +02:00
}, 1) == -1) {
Messaging.severeTr(Messages.LOAD_TASK_NOT_SCHEDULED);
2012-01-19 12:43:21 +01:00
getServer().getPluginManager().disablePlugin(this);
}
}
@Override
public void onImplementationChanged() {
2012-09-16 13:20:01 +02:00
Messaging.severeTr(Messages.CITIZENS_IMPLEMENTATION_DISABLED);
Bukkit.getPluginManager().disablePlugin(this);
}
public void registerCommandClass(Class<?> clazz) {
try {
commands.register(clazz);
} catch (Throwable ex) {
Messaging.logTr(Messages.CITIZENS_INVALID_COMMAND_CLASS);
ex.printStackTrace();
}
}
2012-03-02 11:36:54 +01:00
private void registerCommands() {
commands.setInjector(new Injector(this));
// Register command classes
commands.register(AdminCommands.class);
commands.register(EditorCommands.class);
commands.register(HelpCommands.class);
commands.register(NPCCommands.class);
2012-04-13 17:59:51 +02:00
commands.register(ScriptCommands.class);
2012-09-29 17:54:05 +02:00
commands.register(TemplateCommands.class);
2012-08-05 15:07:42 +02:00
commands.register(TraitCommands.class);
commands.register(WaypointCommands.class);
2012-04-13 17:59:51 +02:00
}
private void registerScriptHelpers() {
setupScripting();
2012-04-13 17:59:51 +02:00
ScriptCompiler compiler = CitizensAPI.getScriptCompiler();
compiler.registerGlobalContextProvider(new EventRegistrar(this));
2012-05-03 17:22:31 +02:00
compiler.registerGlobalContextProvider(new ObjectProvider("plugin", this));
2012-02-28 08:41:15 +01:00
}
public void reload() throws NPCLoadException {
2012-02-27 16:59:39 +01:00
Editor.leaveAll();
2012-04-15 01:32:43 +02:00
config.reload();
despawnNPCs();
2012-09-13 15:02:55 +02:00
saves.loadInto(npcRegistry);
2012-03-08 02:03:32 +01:00
getServer().getPluginManager().callEvent(new CitizensReloadEvent());
}
2012-08-15 18:06:13 +02:00
private void scheduleSaveTask(int delay) {
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
2012-09-13 15:02:55 +02:00
storeNPCs();
saves.saveToDisk();
2012-08-15 18:06:13 +02:00
}
});
}
2012-09-14 10:20:22 +02:00
private void setupEconomy() {
try {
RegisteredServiceProvider<Economy> provider = Bukkit.getServicesManager().getRegistration(
Economy.class);
if (provider != null && provider.getProvider() != null) {
Economy economy = provider.getProvider();
Bukkit.getPluginManager().registerEvents(new PaymentListener(economy), this);
}
} catch (NoClassDefFoundError e) {
Messaging.logTr(Messages.ERROR_LOADING_ECONOMY);
2012-09-14 10:20:22 +02:00
}
}
private void setupScripting() {
contextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClassLoader());
2012-07-19 17:10:30 +02:00
// Workaround to fix scripts not loading plugin classes properly.
// The built in Sun Rhino Javascript engine uses the context classloader
// to search for class imports. Since the context classloader only has
// CraftBukkit classes, we replace it with a PluginClassLoader, which
// allows all plugin classes to be imported.
}
2012-07-19 17:10:30 +02:00
private void startMetrics() {
try {
Metrics metrics = new Metrics(Citizens.this);
if (metrics.isOptOut())
return;
metrics.addCustomData(new Metrics.Plotter("Total NPCs") {
@Override
public int getValue() {
2012-11-19 06:08:11 +01:00
if (npcRegistry == null)
return 0;
return Iterables.size(npcRegistry);
2012-07-19 17:10:30 +02:00
}
});
traitFactory.addPlotters(metrics.createGraph("traits"));
metrics.start();
Messaging.logTr(Messages.METRICS_NOTIFICATION);
} catch (IOException e) {
Messaging.logTr(Messages.METRICS_ERROR_NOTIFICATION, e.getMessage());
}
2012-07-19 17:10:30 +02:00
}
2012-09-13 15:02:55 +02:00
public void storeNPCs() {
if (saves == null)
return;
for (NPC npc : npcRegistry)
saves.store(npc);
}
public void storeNPCs(CommandContext args) {
storeNPCs();
boolean async = args.hasFlag('a');
if (async)
saves.saveToDisk();
else
saves.saveToDiskImmediate();
}
2012-03-02 11:36:54 +01:00
private boolean suggestClosestModifier(CommandSender sender, String command, String modifier) {
2012-11-28 09:28:55 +01:00
String closest = commands.getClosestCommandModifier(command, modifier);
2012-03-02 11:36:54 +01:00
if (!closest.isEmpty()) {
sender.sendMessage(ChatColor.GRAY + Messaging.tr(Messages.UNKNOWN_COMMAND));
2012-03-02 11:36:54 +01:00
sender.sendMessage(StringHelper.wrap(" /") + command + " " + StringHelper.wrap(closest));
return true;
}
return false;
}
2012-03-27 16:42:15 +02:00
private void tearDownScripting() {
if (contextClassLoader == null)
return;
2012-07-22 08:18:24 +02:00
if (Thread.currentThread().getContextClassLoader() == getClassLoader())
Thread.currentThread().setContextClassLoader(contextClassLoader);
contextClassLoader = null;
}
2012-10-28 05:37:37 +01:00
private static final String COMPATIBLE_MC_VERSION = "1.4";
2012-01-15 00:51:37 +01:00
}