mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-12-24 18:18:58 +01:00
traits should not load properly
This commit is contained in:
parent
d7a5ce293f
commit
6bcfcee30b
Binary file not shown.
@ -11,6 +11,7 @@ import net.citizensnpcs.api.npc.trait.Trait;
|
|||||||
import net.citizensnpcs.api.npc.trait.trait.LocationTrait;
|
import net.citizensnpcs.api.npc.trait.trait.LocationTrait;
|
||||||
import net.citizensnpcs.npc.CitizensNPCManager;
|
import net.citizensnpcs.npc.CitizensNPCManager;
|
||||||
import net.citizensnpcs.npc.trait.CitizensCharacterManager;
|
import net.citizensnpcs.npc.trait.CitizensCharacterManager;
|
||||||
|
import net.citizensnpcs.npc.trait.CitizensTraitManager;
|
||||||
import net.citizensnpcs.storage.Storage;
|
import net.citizensnpcs.storage.Storage;
|
||||||
import net.citizensnpcs.storage.flatfile.YamlStorage;
|
import net.citizensnpcs.storage.flatfile.YamlStorage;
|
||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
@ -24,6 +25,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
public class Citizens extends JavaPlugin {
|
public class Citizens extends JavaPlugin {
|
||||||
private CitizensNPCManager npcManager;
|
private CitizensNPCManager npcManager;
|
||||||
private CitizensCharacterManager characterManager;
|
private CitizensCharacterManager characterManager;
|
||||||
|
private CitizensTraitManager traitManager;
|
||||||
private Storage saves;
|
private Storage saves;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -33,10 +35,13 @@ public class Citizens extends JavaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
// Register API managers
|
||||||
npcManager = new CitizensNPCManager();
|
npcManager = new CitizensNPCManager();
|
||||||
characterManager = new CitizensCharacterManager();
|
characterManager = new CitizensCharacterManager();
|
||||||
|
traitManager = new CitizensTraitManager();
|
||||||
CitizensAPI.setNPCManager(npcManager);
|
CitizensAPI.setNPCManager(npcManager);
|
||||||
CitizensAPI.setCharacterManager(characterManager);
|
CitizensAPI.setCharacterManager(characterManager);
|
||||||
|
CitizensAPI.setTraitManager(traitManager);
|
||||||
|
|
||||||
// TODO database support
|
// TODO database support
|
||||||
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
|
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
|
||||||
@ -54,10 +59,10 @@ public class Citizens extends JavaPlugin {
|
|||||||
try {
|
try {
|
||||||
setupNPCs();
|
setupNPCs();
|
||||||
} catch (NPCLoadException ex) {
|
} catch (NPCLoadException ex) {
|
||||||
ex.printStackTrace();
|
Messaging.log("Failed to create NPC: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, /* TODO how long should delay be? */100) == -1) {
|
}) == -1) {
|
||||||
Messaging.log("Issue enabling plugin. Disabling.");
|
Messaging.log("Issue enabling plugin. Disabling.");
|
||||||
getServer().getPluginManager().disablePlugin(this);
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
}
|
}
|
||||||
@ -68,24 +73,42 @@ public class Citizens extends JavaPlugin {
|
|||||||
if (args[0].equals("test")) {
|
if (args[0].equals("test")) {
|
||||||
NPC npc = npcManager.createNPC("aPunch");
|
NPC npc = npcManager.createNPC("aPunch");
|
||||||
npc.spawn(((Player) sender).getLocation());
|
npc.spawn(((Player) sender).getLocation());
|
||||||
|
} else if (args[0].equals("testremove")) {
|
||||||
|
for(NPC npc : npcManager.getNPCs()) {
|
||||||
|
npc.despawn();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupNPCs() throws NPCLoadException {
|
private void setupNPCs() throws NPCLoadException {
|
||||||
// TODO needs fixing
|
// TODO needs fixing
|
||||||
|
traitManager.registerTrait(LocationTrait.class);
|
||||||
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
|
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
|
||||||
int id = Integer.parseInt(key.name());
|
int id = Integer.parseInt(key.name());
|
||||||
if (!key.keyExists("name"))
|
if (!key.keyExists("name"))
|
||||||
throw new NPCLoadException("Could not find a name for the NPC with ID '" + id + "'.");
|
throw new NPCLoadException("Could not find a name for the NPC with ID '" + id + "'.");
|
||||||
Character character = characterManager.getCharacter(key.getString("character"));
|
Character character = characterManager.getCharacter(key.getString("character"));
|
||||||
NPC npc = npcManager.createNPC(key.getString("name"), character);
|
NPC npc = npcManager.createNPC(key.getString("name"), character);
|
||||||
|
|
||||||
|
// Load the character if it exists
|
||||||
if (character != null) {
|
if (character != null) {
|
||||||
character.load(key);
|
character.load(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load traits
|
||||||
|
for (DataKey traitKey : key.getSubKeys()) {
|
||||||
|
for (Trait trait : traitManager.getRegisteredTraits()) {
|
||||||
|
if (trait.getName().equals(traitKey.name())) {
|
||||||
|
Messaging.debug("Found trait '" + trait.getName() + "' in the NPC with ID '" + id + "'.");
|
||||||
|
npc.addTrait(trait.getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
for (Trait trait : npc.getTraits()) {
|
for (Trait trait : npc.getTraits()) {
|
||||||
trait.load(key);
|
trait.load(key);
|
||||||
}
|
}
|
||||||
|
// Spawn the NPC
|
||||||
npc.spawn(npc.getTrait(LocationTrait.class).getLocation());
|
npc.spawn(npc.getTrait(LocationTrait.class).getLocation());
|
||||||
}
|
}
|
||||||
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs.");
|
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs.");
|
||||||
|
@ -139,9 +139,12 @@ public class CitizensNPC implements NPC {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
spawned = true;
|
if (mcEntity == null)
|
||||||
|
mcEntity = manager.spawn(this, loc);
|
||||||
|
else
|
||||||
|
manager.spawn(this, loc);
|
||||||
getTrait(LocationTrait.class).setLocation(loc);
|
getTrait(LocationTrait.class).setLocation(loc);
|
||||||
mcEntity = manager.spawn(this, loc);
|
spawned = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -153,9 +156,9 @@ public class CitizensNPC implements NPC {
|
|||||||
|
|
||||||
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
|
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
|
||||||
|
|
||||||
spawned = false;
|
|
||||||
mcEntity.die();
|
mcEntity.die();
|
||||||
manager.despawn(this);
|
manager.despawn(this);
|
||||||
|
spawned = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -174,8 +177,7 @@ public class CitizensNPC implements NPC {
|
|||||||
|
|
||||||
public <T extends Trait> T create(Class<T> clazz) {
|
public <T extends Trait> T create(Class<T> clazz) {
|
||||||
try {
|
try {
|
||||||
Trait trait = clazz.newInstance();
|
return clazz.cast(clazz.newInstance());
|
||||||
return clazz.cast(trait);
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -6,12 +6,15 @@ import java.util.HashSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.craftbukkit.CraftServer;
|
import org.bukkit.craftbukkit.CraftServer;
|
||||||
import org.bukkit.craftbukkit.CraftWorld;
|
import org.bukkit.craftbukkit.CraftWorld;
|
||||||
|
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
import net.citizensnpcs.api.npc.NPCManager;
|
import net.citizensnpcs.api.npc.NPCManager;
|
||||||
@ -22,6 +25,7 @@ import net.citizensnpcs.resources.lib.CraftNPC;
|
|||||||
|
|
||||||
import net.minecraft.server.ItemInWorldManager;
|
import net.minecraft.server.ItemInWorldManager;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.server.Packet29DestroyEntity;
|
||||||
import net.minecraft.server.WorldServer;
|
import net.minecraft.server.WorldServer;
|
||||||
|
|
||||||
public class CitizensNPCManager implements NPCManager {
|
public class CitizensNPCManager implements NPCManager {
|
||||||
@ -94,7 +98,9 @@ public class CitizensNPCManager implements NPCManager {
|
|||||||
|
|
||||||
public void despawn(NPC npc) {
|
public void despawn(NPC npc) {
|
||||||
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
|
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
|
||||||
// TODO send Packet29DestroyEntity
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet29DestroyEntity(mcEntity.id));
|
||||||
|
}
|
||||||
getWorldServer(npc.getTrait(LocationTrait.class).getLocation().getWorld()).removeEntity(mcEntity);
|
getWorldServer(npc.getTrait(LocationTrait.class).getLocation().getWorld()).removeEntity(mcEntity);
|
||||||
spawned.remove(mcEntity.getPlayer());
|
spawned.remove(mcEntity.getPlayer());
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.citizensnpcs.npc.trait;
|
package net.citizensnpcs.npc.trait;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -38,4 +39,9 @@ public class CitizensCharacterManager implements CharacterManager {
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Character> getRegisteredCharacters() {
|
||||||
|
return characters;
|
||||||
|
}
|
||||||
}
|
}
|
47
src/net/citizensnpcs/npc/trait/CitizensTraitManager.java
Normal file
47
src/net/citizensnpcs/npc/trait/CitizensTraitManager.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package net.citizensnpcs.npc.trait;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.citizensnpcs.api.npc.trait.Trait;
|
||||||
|
import net.citizensnpcs.api.npc.trait.TraitManager;
|
||||||
|
|
||||||
|
public class CitizensTraitManager implements TraitManager {
|
||||||
|
private final Map<String, Class<? extends Trait>> registered = new HashMap<String, Class<? extends Trait>>();
|
||||||
|
private final Set<Trait> traits = new HashSet<Trait>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Trait getTrait(String name) {
|
||||||
|
if (registered.get(name) == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (Trait trait : traits) {
|
||||||
|
if (trait.getName().equals(name)) {
|
||||||
|
return trait;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerTrait(Class<? extends Trait> trait) {
|
||||||
|
if (registered.containsValue(trait)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Trait register = trait.newInstance();
|
||||||
|
registered.put(register.getName(), trait);
|
||||||
|
traits.add(register);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Trait> getRegisteredTraits() {
|
||||||
|
return traits;
|
||||||
|
}
|
||||||
|
}
|
@ -173,11 +173,7 @@ public class YamlStorage implements Storage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getString(String key) {
|
public String getString(String key) {
|
||||||
String path = getKeyExt(key);
|
return config.getString(getKeyExt(key));
|
||||||
if (keyExists(path)) {
|
|
||||||
return config.get(path).toString();
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user