2012-01-15 00:51:37 +01:00
|
|
|
package net.citizensnpcs;
|
|
|
|
|
2012-01-19 11:52:58 +01:00
|
|
|
import java.io.File;
|
2012-02-12 08:19:32 +01:00
|
|
|
import java.io.IOException;
|
2012-02-17 08:48:20 +01:00
|
|
|
import java.sql.SQLException;
|
2012-01-29 21:58:19 +01:00
|
|
|
import java.util.logging.Level;
|
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;
|
2012-03-08 02:03:32 +01:00
|
|
|
import net.citizensnpcs.api.event.CitizensReloadEvent;
|
2012-02-04 00:35:24 +01:00
|
|
|
import net.citizensnpcs.api.exception.NPCException;
|
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-02-12 09:00:19 +01:00
|
|
|
import net.citizensnpcs.api.util.DataKey;
|
|
|
|
import net.citizensnpcs.api.util.DatabaseStorage;
|
|
|
|
import net.citizensnpcs.api.util.Storage;
|
|
|
|
import net.citizensnpcs.api.util.YamlStorage;
|
2012-01-25 16:29:54 +01:00
|
|
|
import net.citizensnpcs.command.CommandManager;
|
2012-01-25 21:07:49 +01:00
|
|
|
import net.citizensnpcs.command.Injector;
|
2012-02-08 16:22:42 +01:00
|
|
|
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;
|
2012-01-25 16:29:54 +01:00
|
|
|
import net.citizensnpcs.command.command.NPCCommands;
|
2012-02-29 11:50:45 +01:00
|
|
|
import net.citizensnpcs.command.exception.CommandException;
|
2012-01-25 16:29:54 +01:00
|
|
|
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-03-08 02:03:32 +01:00
|
|
|
import net.citizensnpcs.npc.CitizensCharacterManager;
|
2012-03-09 21:53:02 +01:00
|
|
|
import net.citizensnpcs.npc.CitizensNPC;
|
2012-01-15 00:51:37 +01:00
|
|
|
import net.citizensnpcs.npc.CitizensNPCManager;
|
2012-03-08 02:03:32 +01:00
|
|
|
import net.citizensnpcs.npc.CitizensTraitManager;
|
2012-01-16 04:15:32 +01:00
|
|
|
import net.citizensnpcs.util.Messaging;
|
2012-02-12 08:19:32 +01:00
|
|
|
import net.citizensnpcs.util.Metrics;
|
2012-01-25 16:29:54 +01:00
|
|
|
import net.citizensnpcs.util.StringHelper;
|
2012-01-15 00:51:37 +01:00
|
|
|
|
2012-01-18 05:04:08 +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-02-24 00:45:45 +01:00
|
|
|
import org.bukkit.entity.EntityType;
|
2012-01-19 11:52:58 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2012-01-15 00:51:37 +01:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
2012-02-12 08:19:32 +01:00
|
|
|
import com.google.common.collect.Iterators;
|
2012-02-04 07:48:23 +01:00
|
|
|
|
2012-01-15 06:09:31 +01:00
|
|
|
public class Citizens extends JavaPlugin {
|
2012-03-08 02:03:32 +01:00
|
|
|
private static final String COMPATIBLE_MC_VERSION = "1.2.3";
|
2012-03-02 11:36:54 +01:00
|
|
|
|
2012-02-12 08:19:32 +01:00
|
|
|
private final CommandManager commands = new CommandManager();
|
2012-03-02 11:36:54 +01:00
|
|
|
private boolean compatible;
|
2012-01-21 14:56:50 +01:00
|
|
|
private Settings config;
|
2012-03-08 02:03:32 +01:00
|
|
|
private CitizensCharacterManager characterManager;
|
2012-03-02 11:36:54 +01:00
|
|
|
private volatile CitizensNPCManager npcManager;
|
2012-01-29 21:58:19 +01:00
|
|
|
private Storage saves;
|
2012-03-02 14:04:40 +01:00
|
|
|
|
2012-03-08 02:03:32 +01:00
|
|
|
public CitizensCharacterManager getCharacterManager() {
|
2012-03-02 11:36:54 +01:00
|
|
|
return characterManager;
|
|
|
|
}
|
2012-01-21 14:56:50 +01:00
|
|
|
|
2012-03-02 11:36:54 +01:00
|
|
|
public CommandManager getCommandManager() {
|
|
|
|
return commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CitizensNPCManager getNPCManager() {
|
|
|
|
return npcManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Storage getStorage() {
|
|
|
|
return saves;
|
2012-02-04 07:48:23 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 09:45:34 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
|
2012-01-25 16:29:54 +01:00
|
|
|
Player player = null;
|
|
|
|
if (sender instanceof Player)
|
|
|
|
player = (Player) sender;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// must put command into split.
|
|
|
|
String[] split = new String[args.length + 1];
|
|
|
|
System.arraycopy(args, 0, split, 1, args.length);
|
|
|
|
split[0] = cmd.getName().toLowerCase();
|
|
|
|
|
2012-02-12 08:19:32 +01:00
|
|
|
String modifier = args.length > 0 ? args[0] : "";
|
2012-01-25 16:29:54 +01:00
|
|
|
|
2012-02-12 08:19:32 +01:00
|
|
|
if (!commands.hasCommand(split[0], modifier) && !modifier.isEmpty()) {
|
|
|
|
return suggestClosestModifier(sender, split[0], modifier);
|
2012-01-25 16:29:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NPC npc = null;
|
2012-03-01 11:45:44 +01:00
|
|
|
if (player != null && player.getMetadata("selected").size() > 0)
|
|
|
|
npc = npcManager.getNPC(player.getMetadata("selected").get(0).asInt());
|
2012-01-25 16:29:54 +01:00
|
|
|
|
|
|
|
try {
|
2012-02-12 08:19:32 +01:00
|
|
|
commands.execute(split, player, player == null ? sender : player, npc);
|
2012-01-25 16:29:54 +01:00
|
|
|
} catch (ServerCommandException ex) {
|
2012-02-29 11:50:45 +01:00
|
|
|
Messaging.send(sender, "You must be in-game to execute that command.");
|
2012-01-25 16:29:54 +01:00
|
|
|
} catch (CommandUsageException ex) {
|
|
|
|
Messaging.sendError(player, ex.getMessage());
|
|
|
|
Messaging.sendError(player, ex.getUsage());
|
2012-02-29 11:50:45 +01:00
|
|
|
} catch (WrappedCommandException ex) {
|
|
|
|
throw ex.getCause();
|
|
|
|
} catch (UnhandledCommandException ex) {
|
2012-01-25 16:29:54 +01:00
|
|
|
return false;
|
2012-02-29 11:50:45 +01:00
|
|
|
} catch (CommandException ex) {
|
|
|
|
Messaging.sendError(player, ex.getMessage());
|
2012-01-25 16:29:54 +01:00
|
|
|
}
|
2012-02-29 11:50:45 +01:00
|
|
|
} catch (NumberFormatException ex) {
|
2012-01-25 16:29:54 +01:00
|
|
|
Messaging.sendError(player, "That is not a valid number.");
|
2012-02-29 11:50:45 +01:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
ex.printStackTrace();
|
2012-01-25 16:29:54 +01:00
|
|
|
Messaging.sendError(player, "Please report this error: [See console]");
|
2012-02-29 11:50:45 +01:00
|
|
|
Messaging.sendError(player, 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() {
|
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-02-28 08:41:15 +01:00
|
|
|
save();
|
2012-01-29 21:58:19 +01:00
|
|
|
for (NPC npc : npcManager)
|
|
|
|
npc.despawn();
|
2012-02-27 11:59:33 +01:00
|
|
|
getServer().getScheduler().cancelTasks(this);
|
2012-01-29 21:58:19 +01:00
|
|
|
}
|
2012-01-21 13:53:58 +01:00
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
Messaging.log("v" + getDescription().getVersion() + " disabled.");
|
|
|
|
}
|
2012-01-15 00:51:37 +01:00
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
2012-01-29 21:58:19 +01:00
|
|
|
// Disable if the server is not using the compatible Minecraft version
|
2012-01-30 19:55:16 +01:00
|
|
|
compatible = ((CraftServer) getServer()).getServer().getVersion().startsWith(COMPATIBLE_MC_VERSION);
|
2012-01-30 12:58:59 +01:00
|
|
|
String mcVersion = ((CraftServer) getServer()).getServer().getVersion();
|
|
|
|
if (!compatible) {
|
2012-01-29 21:58:19 +01:00
|
|
|
Messaging.log(Level.SEVERE, "v" + getDescription().getVersion() + " is not compatible with Minecraft v"
|
|
|
|
+ mcVersion + ". Disabling.");
|
|
|
|
getServer().getPluginManager().disablePlugin(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-21 14:56:50 +01:00
|
|
|
// Configuration file
|
|
|
|
config = new Settings(this);
|
|
|
|
config.load();
|
2012-01-19 11:52:58 +01:00
|
|
|
|
2012-01-23 15:30:15 +01:00
|
|
|
// NPC storage
|
2012-02-12 08:19:32 +01:00
|
|
|
if (Setting.USE_DATABASE.asBoolean()) {
|
2012-02-17 08:48:20 +01:00
|
|
|
try {
|
|
|
|
saves = new DatabaseStorage(Setting.DATABASE_DRIVER.asString(), Setting.DATABASE_URL.asString(),
|
|
|
|
Setting.DATABASE_USERNAME.asString(), Setting.DATABASE_PASSWORD.asString());
|
|
|
|
} catch (SQLException e) {
|
|
|
|
Messaging.log("Unable to connect to database, falling back to YAML");
|
|
|
|
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml", "Citizens NPC Storage");
|
|
|
|
}
|
2012-02-12 08:19:32 +01:00
|
|
|
} else {
|
2012-02-08 13:07:17 +01:00
|
|
|
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml", "Citizens NPC Storage");
|
2012-02-12 08:19:32 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 17:05:22 +01:00
|
|
|
// Register API managers
|
|
|
|
npcManager = new CitizensNPCManager(saves);
|
2012-03-08 02:03:32 +01:00
|
|
|
characterManager = new CitizensCharacterManager();
|
2012-01-23 17:05:22 +01:00
|
|
|
CitizensAPI.setNPCManager(npcManager);
|
|
|
|
CitizensAPI.setCharacterManager(characterManager);
|
2012-03-08 02:03:32 +01:00
|
|
|
CitizensAPI.setTraitManager(new CitizensTraitManager());
|
2012-01-23 17:05:22 +01:00
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
// Register events
|
2012-01-22 08:10:25 +01:00
|
|
|
getServer().getPluginManager().registerEvents(new EventListen(npcManager), this);
|
2012-01-16 04:15:32 +01:00
|
|
|
|
2012-02-11 10:21:02 +01:00
|
|
|
// Register commands
|
2012-01-25 16:29:54 +01:00
|
|
|
registerCommands();
|
2012-01-23 15:30:15 +01:00
|
|
|
|
2012-02-03 10:20:48 +01:00
|
|
|
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new NPCUpdater(npcManager), 0, 1);
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
Messaging.log("v" + getDescription().getVersion() + " enabled.");
|
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 (Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
setupNPCs();
|
|
|
|
} catch (NPCLoadException ex) {
|
2012-01-29 21:58:19 +01:00
|
|
|
Messaging.log(Level.SEVERE, "Issue when loading NPCs: " + ex.getMessage());
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-20 08:27:14 +01:00
|
|
|
}) == -1) {
|
2012-01-29 21:58:19 +01:00
|
|
|
Messaging.log(Level.SEVERE, "Issue enabling plugin. Disabling.");
|
2012-01-19 12:43:21 +01:00
|
|
|
getServer().getPluginManager().disablePlugin(this);
|
|
|
|
}
|
2012-02-12 11:02:36 +01:00
|
|
|
|
|
|
|
// Run metrics last
|
|
|
|
new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Metrics metrics = new Metrics();
|
|
|
|
metrics.addCustomData(Citizens.this, new Metrics.Plotter() {
|
|
|
|
@Override
|
2012-03-02 11:36:54 +01:00
|
|
|
public String getColumnName() {
|
|
|
|
return "Total NPCs";
|
2012-02-12 11:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-03-02 11:36:54 +01:00
|
|
|
public int getValue() {
|
|
|
|
return Iterators.size(npcManager.iterator());
|
2012-02-12 11:02:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
metrics.beginMeasuringPlugin(Citizens.this);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Messaging.log("Unable to load metrics");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
2012-01-18 05:04:08 +01:00
|
|
|
|
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-02-28 08:41:15 +01:00
|
|
|
}
|
|
|
|
|
2012-02-27 11:59:33 +01:00
|
|
|
public void reload() throws NPCLoadException {
|
2012-02-27 16:59:39 +01:00
|
|
|
Editor.leaveAll();
|
2012-02-27 11:59:33 +01:00
|
|
|
config.load();
|
|
|
|
for (NPC npc : npcManager)
|
|
|
|
npc.despawn();
|
|
|
|
|
|
|
|
saves.load();
|
|
|
|
setupNPCs();
|
2012-03-08 02:03:32 +01:00
|
|
|
|
|
|
|
getServer().getPluginManager().callEvent(new CitizensReloadEvent());
|
2012-02-27 11:59:33 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 11:36:54 +01:00
|
|
|
public void save() {
|
|
|
|
config.save();
|
|
|
|
for (NPC npc : npcManager)
|
2012-03-09 21:53:02 +01:00
|
|
|
((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId()));
|
2012-03-02 11:36:54 +01:00
|
|
|
saves.save();
|
2012-02-04 07:48:23 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
private void setupNPCs() throws NPCLoadException {
|
2012-01-29 12:43:27 +01:00
|
|
|
int created = 0, spawned = 0;
|
|
|
|
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
|
2012-01-19 12:43:21 +01:00
|
|
|
int id = Integer.parseInt(key.name());
|
|
|
|
if (!key.keyExists("name"))
|
|
|
|
throw new NPCLoadException("Could not find a name for the NPC with ID '" + id + "'.");
|
2012-02-03 10:20:48 +01:00
|
|
|
|
2012-03-10 06:59:37 +01:00
|
|
|
NPC npc = npcManager.createNPC(EntityType.valueOf(key.getString("traits.type").toUpperCase()), id,
|
|
|
|
key.getString("name"), null);
|
2012-02-04 00:35:24 +01:00
|
|
|
try {
|
2012-03-09 21:53:02 +01:00
|
|
|
((CitizensNPC) npc).load(key);
|
2012-02-04 00:35:24 +01:00
|
|
|
} catch (NPCException ex) {
|
|
|
|
Messaging.log(ex.getMessage());
|
|
|
|
}
|
2012-02-12 08:19:32 +01:00
|
|
|
|
2012-01-29 12:43:27 +01:00
|
|
|
++created;
|
|
|
|
if (npc.isSpawned())
|
|
|
|
++spawned;
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
2012-01-30 19:55:16 +01:00
|
|
|
Messaging.log("Loaded " + created + " NPCs (" + spawned + " spawned).");
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
2012-03-02 11:36:54 +01:00
|
|
|
|
|
|
|
private boolean suggestClosestModifier(CommandSender sender, String command, String modifier) {
|
|
|
|
int minDist = Integer.MAX_VALUE;
|
|
|
|
String closest = "";
|
|
|
|
for (String string : commands.getAllCommandModifiers(command)) {
|
|
|
|
int distance = StringHelper.getLevenshteinDistance(modifier, string);
|
|
|
|
if (minDist > distance) {
|
|
|
|
minDist = distance;
|
|
|
|
closest = string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!closest.isEmpty()) {
|
|
|
|
sender.sendMessage(ChatColor.GRAY + "Unknown command. Did you mean:");
|
|
|
|
sender.sendMessage(StringHelper.wrap(" /") + command + " " + StringHelper.wrap(closest));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-15 00:51:37 +01:00
|
|
|
}
|