Citizens2/src/net/citizensnpcs/Citizens.java

93 lines
2.9 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-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.Trait;
2012-01-18 05:04:08 +01:00
import net.citizensnpcs.api.npc.trait.trait.LocationTrait;
2012-01-15 00:51:37 +01:00
import net.citizensnpcs.npc.CitizensNPCManager;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.npc.trait.CitizensCharacterManager;
import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.storage.flatfile.YamlStorage;
import net.citizensnpcs.util.Messaging;
2012-01-15 00:51:37 +01:00
2012-01-18 05:04:08 +01:00
import org.bukkit.Bukkit;
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-15 00:51:37 +01:00
import org.bukkit.plugin.java.JavaPlugin;
public class Citizens extends JavaPlugin {
2012-01-18 05:04:08 +01:00
private CitizensNPCManager npcManager;
2012-01-19 11:52:58 +01:00
private CitizensCharacterManager characterManager;
private Storage saves;
2012-01-15 00:51:37 +01:00
@Override
public void onDisable() {
Messaging.log("v" + getDescription().getVersion() + " disabled.");
2012-01-15 00:51:37 +01:00
}
@Override
public void onEnable() {
2012-01-18 05:04:08 +01:00
npcManager = new CitizensNPCManager();
2012-01-19 11:52:58 +01:00
characterManager = new CitizensCharacterManager();
2012-01-18 05:04:08 +01:00
CitizensAPI.setNPCManager(npcManager);
2012-01-19 11:52:58 +01:00
CitizensAPI.setCharacterManager(characterManager);
// TODO database support
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
2012-01-18 05:04:08 +01:00
2012-01-19 08:38:40 +01:00
// Register events
new EventListen(this);
Messaging.log("v" + getDescription().getVersion() + " enabled.");
2012-01-18 05:04:08 +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() {
2012-01-19 11:52:58 +01:00
try {
setupNPCs();
} catch (NPCLoadException ex) {
ex.printStackTrace();
}
2012-01-18 05:04:08 +01:00
}
2012-01-19 11:52:58 +01:00
}, /* TODO how long should delay be? */100) == -1) {
2012-01-18 05:04:08 +01:00
Messaging.log("Issue enabling plugin. Disabling.");
2012-01-18 11:37:09 +01:00
getServer().getPluginManager().disablePlugin(this);
}
2012-01-18 05:04:08 +01:00
}
2012-01-19 11:52:58 +01:00
@Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
if (args[0].equals("test")) {
NPC npc = npcManager.createNPC("aPunch");
npc.spawn(((Player) sender).getLocation());
}
return true;
}
private void setupNPCs() throws NPCLoadException {
// TODO needs fixing
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
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.getCharacter(key.getString("character"));
NPC npc = npcManager.createNPC(key.getString("name"), character);
if (character != null) {
character.load(key);
}
for (Trait t : npc.getTraits()) {
t.load(key);
}
npc.spawn(npc.getTrait(LocationTrait.class).getLocation());
2012-01-18 05:04:08 +01:00
}
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs.");
}
2012-01-15 00:51:37 +01:00
}