Citizens2/src/net/citizensnpcs/Citizens.java

263 lines
10 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-01-23 15:30:15 +01:00
import java.util.HashMap;
import java.util.HashSet;
2012-01-23 15:30:15 +01:00
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
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-01-19 11:52:58 +01:00
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.exception.NPCLoadException;
2012-01-18 05:04:08 +01:00
import net.citizensnpcs.api.npc.NPC;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.api.npc.trait.InstanceFactory;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.api.npc.trait.Trait;
2012-01-21 17:21:21 +01:00
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.command.CommandManager;
2012-01-25 21:07:49 +01:00
import net.citizensnpcs.command.Injector;
import net.citizensnpcs.command.command.NPCCommands;
import net.citizensnpcs.command.exception.CommandUsageException;
import net.citizensnpcs.command.exception.MissingNestedCommandException;
import net.citizensnpcs.command.exception.NoPermissionsException;
import net.citizensnpcs.command.exception.RequirementMissingException;
import net.citizensnpcs.command.exception.ServerCommandException;
import net.citizensnpcs.command.exception.UnhandledCommandException;
import net.citizensnpcs.command.exception.WrappedCommandException;
2012-01-23 16:42:01 +01:00
import net.citizensnpcs.npc.CitizensNPC;
2012-01-15 00:51:37 +01:00
import net.citizensnpcs.npc.CitizensNPCManager;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.storage.Storage;
2012-01-23 15:30:15 +01:00
import net.citizensnpcs.storage.database.DatabaseStorage;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.storage.flatfile.YamlStorage;
2012-01-22 14:48:50 +01:00
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.Messaging;
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;
import org.bukkit.entity.Player;
2012-01-23 15:30:15 +01:00
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
2012-01-15 00:51:37 +01:00
import org.bukkit.plugin.java.JavaPlugin;
public class Citizens extends JavaPlugin {
private static Storage saves;
private CitizensNPCManager npcManager;
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
private final CommandManager cmdManager = new CommandManager();
2012-01-21 14:56:50 +01:00
private Settings config;
2012-01-23 09:45:34 +01:00
@Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
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();
String modifier = "";
if (args.length > 0)
modifier = args[0];
// No command found!
if (!cmdManager.hasCommand(split[0], modifier)) {
if (!modifier.isEmpty()) {
boolean value = handleMistake(sender, split[0], modifier);
return value;
}
}
NPC npc = null;
if (player != null && npcManager.hasNPCSelected(player))
npc = npcManager.getSelectedNPC(player);
try {
cmdManager.execute(split, player, player == null ? sender : player, npc);
} catch (ServerCommandException ex) {
sender.sendMessage("You must be in-game to execute that command.");
} catch (NoPermissionsException ex) {
Messaging.sendError(player, "You don't have permission to execute that command.");
} catch (MissingNestedCommandException ex) {
Messaging.sendError(player, ex.getUsage());
} catch (CommandUsageException ex) {
Messaging.sendError(player, ex.getMessage());
Messaging.sendError(player, ex.getUsage());
} catch (RequirementMissingException ex) {
Messaging.sendError(player, ex.getMessage());
} catch (WrappedCommandException e) {
throw e.getCause();
} catch (UnhandledCommandException e) {
return false;
}
} catch (NumberFormatException e) {
Messaging.sendError(player, "That is not a valid number.");
} catch (Throwable excp) {
excp.printStackTrace();
Messaging.sendError(player, "Please report this error: [See console]");
Messaging.sendError(player, excp.getClass().getName() + ": " + excp.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-26 15:45:42 +01:00
// Save and despawn all NPCs
2012-01-21 14:56:50 +01:00
config.save();
saveNPCs();
2012-01-26 15:45:42 +01:00
for (NPC npc : npcManager.getAllNPCs())
2012-01-26 00:09:02 +01:00
npc.despawn();
2012-01-22 08:10:25 +01:00
Bukkit.getScheduler().cancelTasks(this);
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-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
if (Setting.USE_DATABASE.getBoolean())
saves = new DatabaseStorage();
else
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
2012-01-18 05:04:08 +01:00
2012-01-23 17:05:22 +01:00
// Register API managers
npcManager = new CitizensNPCManager(saves);
CitizensAPI.setNPCManager(npcManager);
CitizensAPI.setCharacterManager(characterManager);
CitizensAPI.setTraitManager(traitManager);
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);
// Register commands and permissions
registerCommands();
2012-01-23 15:30:15 +01:00
registerPermissions();
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-23 15:30:15 +01:00
Messaging.log("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-19 12:43:21 +01:00
Messaging.log("Issue enabling plugin. Disabling.");
getServer().getPluginManager().disablePlugin(this);
}
}
2012-01-18 05:04:08 +01:00
public static Storage getNPCStorage() {
return saves;
}
2012-01-23 09:45:34 +01:00
private void saveNPCs() {
2012-01-23 16:42:01 +01:00
for (NPC npc : npcManager.getAllNPCs())
((CitizensNPC) npc).save();
getNPCStorage().save();
2012-01-19 12:43:21 +01:00
}
2012-01-19 11:52:58 +01:00
2012-01-19 12:43:21 +01:00
private void setupNPCs() throws NPCLoadException {
traitManager.register("location", SpawnLocation.class);
2012-01-22 18:43:58 +01:00
for (DataKey key : getNPCStorage().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 + "'.");
Character character = characterManager.getInstance(key.getString("character"));
2012-01-23 16:42:01 +01:00
NPC npc = npcManager.createNPC(id, key.getString("name"), character);
2012-01-20 08:27:14 +01:00
2012-01-21 13:53:58 +01:00
// Load the character if it exists, otherwise remove the character
2012-01-22 15:23:25 +01:00
if (character != null)
character.load(key.getRelative("characters." + character.getName()));
2012-01-22 15:23:25 +01:00
else {
2012-01-21 13:53:58 +01:00
if (key.keyExists("character")) {
Messaging.debug("Character '" + key.getString("character")
+ "' does not exist. Removing character from the NPC with ID '" + npc.getId() + "'.");
key.removeKey("character");
}
2012-01-19 12:43:21 +01:00
}
2012-01-20 08:27:14 +01:00
// Load traits
for (DataKey traitKey : key.getSubKeys()) {
Trait trait = traitManager.getInstance(traitKey.name());
2012-01-22 08:10:25 +01:00
if (trait == null)
continue;
trait.load(traitKey);
npc.addTrait(trait);
2012-01-19 12:43:21 +01:00
}
2012-01-20 16:15:32 +01:00
2012-01-20 08:27:14 +01:00
// Spawn the NPC
2012-01-22 14:48:50 +01:00
if (key.getBoolean("spawned"))
2012-01-21 17:21:21 +01:00
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation());
2012-01-19 12:43:21 +01:00
}
2012-01-22 14:48:50 +01:00
Messaging.log("Loaded " + ((ByIdArray<NPC>) npcManager.getAllNPCs()).size() + " NPCs ("
+ ((ByIdArray<NPC>) npcManager.getSpawnedNPCs()).size() + " spawned).");
2012-01-19 12:43:21 +01:00
}
2012-01-23 15:30:15 +01:00
private void registerPermissions() {
Map<String, Boolean> children = new HashMap<String, Boolean>();
2012-01-25 21:07:49 +01:00
children.put("citizens.npc.create", true);
children.put("citizens.npc.spawn", true);
children.put("citizens.npc.despawn", true);
2012-01-23 15:30:15 +01:00
children.put("citizens.npc.select", true);
Permission perm = new Permission("citizens.*", PermissionDefault.OP, children);
getServer().getPluginManager().addPermission(perm);
}
private void registerCommands() {
2012-01-25 21:40:01 +01:00
cmdManager.setInjector(new Injector(npcManager, characterManager));
2012-01-25 21:07:49 +01:00
cmdManager.register(NPCCommands.class);
}
private boolean handleMistake(CommandSender sender, String command, String modifier) {
String[] modifiers = cmdManager.getAllCommandModifiers(command);
Map<Integer, String> values = new TreeMap<Integer, String>();
int i = 0;
for (String string : modifiers) {
values.put(StringHelper.getLevenshteinDistance(modifier, string), modifiers[i]);
++i;
}
int best = 0;
boolean stop = false;
Set<String> possible = new HashSet<String>();
for (Entry<Integer, String> entry : values.entrySet()) {
if (!stop) {
best = entry.getKey();
stop = true;
} else if (entry.getKey() > best)
break;
possible.add(entry.getValue());
}
if (possible.size() > 0) {
sender.sendMessage(ChatColor.GRAY + "Unknown command. Did you mean:");
for (String string : possible)
sender.sendMessage(StringHelper.wrap(" /") + command + " " + StringHelper.wrap(string));
return true;
}
return false;
}
2012-01-15 00:51:37 +01:00
}