Merge CharacterManager and TraitManager

This commit is contained in:
fullwall 2012-01-23 14:12:45 +08:00
parent 891162e06e
commit 8071eb01f4
7 changed files with 39 additions and 131 deletions

View File

@ -7,12 +7,12 @@ import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.api.npc.trait.InstanceFactory;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.trait.CitizensCharacterManager;
import net.citizensnpcs.npc.trait.CitizensTraitManager;
import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.storage.flatfile.YamlStorage;
import net.citizensnpcs.util.ByIdArray;
@ -27,8 +27,8 @@ import org.bukkit.plugin.java.JavaPlugin;
public class Citizens extends JavaPlugin {
private static final CitizensNPCManager npcManager = new CitizensNPCManager();
private static final CitizensCharacterManager characterManager = new CitizensCharacterManager();
private static final CitizensTraitManager traitManager = new CitizensTraitManager();
private static final InstanceFactory<Character> characterManager = DefaultInstanceFactory.create();
private static final InstanceFactory<Trait> traitManager = DefaultInstanceFactory.create();
private Settings config;
private Storage saves;
@ -93,13 +93,13 @@ public class Citizens extends JavaPlugin {
// TODO separate this out some more
private void setupNPCs() throws NPCLoadException {
traitManager.registerTrait("location", SpawnLocation.class);
traitManager.register("location", SpawnLocation.class);
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"));
Character character = characterManager.getInstance(key.getString("character"));
NPC npc = npcManager.createNPC(key.getString("name"), character);
// Load the character if it exists, otherwise remove the character
@ -115,7 +115,7 @@ public class Citizens extends JavaPlugin {
// Load traits
for (DataKey traitKey : key.getSubKeys()) {
Trait trait = traitManager.getTrait(traitKey.name());
Trait trait = traitManager.getInstance(traitKey.name());
if (trait == null)
continue;
trait.load(traitKey);

View File

@ -7,7 +7,7 @@ import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.pathfinding.Navigator;
import net.citizensnpcs.api.npc.ai.Navigator;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
@ -165,6 +165,7 @@ public class CitizensNPC implements NPC {
return getHandle().getBukkitEntity();
}
@Override
public Iterable<Trait> getTraits() {
return traits.values();
}

View File

@ -1,49 +0,0 @@
package net.citizensnpcs.npc.trait;
import java.util.HashMap;
import java.util.Map;
import net.citizensnpcs.api.Factory;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.CharacterManager;
public class CitizensCharacterManager implements CharacterManager {
private final Map<String, Factory<? extends Character>> registered = new HashMap<String, Factory<? extends Character>>();
@Override
public Character getCharacter(String name) {
if (registered.get(name) == null)
return null;
return registered.get(name).create();
}
@Override
public void registerCharacter(String name, Class<? extends Character> clazz) {
registerCharacterWithFactory(name, new DefaultCharacterFactory(clazz));
}
@Override
public void registerCharacterWithFactory(String name, Factory<? extends Character> factory) {
if (registered.get(name) != null)
throw new IllegalArgumentException("A character factory for the character '" + name
+ "' has already been registered.");
registered.put(name, factory);
}
private static class DefaultCharacterFactory implements Factory<Character> {
private final Class<? extends Character> clazz;
private DefaultCharacterFactory(Class<? extends Character> clazz) {
this.clazz = clazz;
}
@Override
public Character create() {
try {
return clazz.newInstance();
} catch (Exception ex) {
return null;
}
}
}
}

View File

@ -1,49 +0,0 @@
package net.citizensnpcs.npc.trait;
import java.util.HashMap;
import java.util.Map;
import net.citizensnpcs.api.Factory;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.npc.trait.TraitManager;
public class CitizensTraitManager implements TraitManager {
private final Map<String, Factory<? extends Trait>> registered = new HashMap<String, Factory<? extends Trait>>();
@Override
public Trait getTrait(String name) {
if (registered.get(name) == null)
return null;
return registered.get(name).create();
}
@Override
public void registerTrait(String name, Class<? extends Trait> clazz) {
registerTraitWithFactory(name, new DefaultTraitFactory(clazz));
}
@Override
public void registerTraitWithFactory(String name, Factory<? extends Trait> factory) {
if (registered.get(name) != null)
throw new IllegalArgumentException("A trait factory for the trait '" + name
+ "' has already been registered.");
registered.put(name, factory);
}
private static class DefaultTraitFactory implements Factory<Trait> {
private final Class<? extends Trait> clazz;
private DefaultTraitFactory(Class<? extends Trait> clazz) {
this.clazz = clazz;
}
@Override
public Trait create() {
try {
return clazz.newInstance();
} catch (Exception ex) {
return null;
}
}
}
}

View File

@ -1,13 +1,8 @@
package net.citizensnpcs.resources.lib;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.ItemInWorldManager;
@ -16,15 +11,18 @@ import net.minecraft.server.NetHandler;
import net.minecraft.server.NetworkManager;
import net.minecraft.server.World;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Entity;
@SuppressWarnings("unchecked")
public class CraftNPC extends EntityPlayer {
public CraftNPC(MinecraftServer minecraftServer, World world, String string, ItemInWorldManager itemInWorldManager) {
super(minecraftServer, world, string, itemInWorldManager);
itemInWorldManager.setGameMode(0);
NPCSocket socket = new NPCSocket();
NetworkManager netMgr = new NPCNetworkManager(socket, "npc mgr", new NetHandler() {
NetworkManager netMgr = new NPCNetworkManager(new NPCSocket(), "npc mgr", new NetHandler() {
@Override
public boolean c() {
return false;
@ -32,12 +30,7 @@ public class CraftNPC extends EntityPlayer {
});
netServerHandler = new NPCNetHandler(minecraftServer, netMgr, this);
netMgr.a(netServerHandler);
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
netMgr.a(); // this interrupts the read/write threads
}
@Override
@ -45,18 +38,25 @@ public class CraftNPC extends EntityPlayer {
if (this.bukkitEntity == null) {
super.getBukkitEntity();
removeFromPlayerMap(name);
// Bukkit uses a map of player names to CraftPlayer instances to
// solve a reconnect issue, so NPC names will conflict with ordinary
// player names. Workaround.
}
return super.getBukkitEntity();
}
@SuppressWarnings("unchecked")
public void removeFromPlayerMap(String name) {
if (players != null)
players.remove(name);
}
private static Map<String, CraftPlayer> players;
static {
try {
Field f = CraftEntity.class.getDeclaredField("players");
f.setAccessible(true);
Map<String, CraftPlayer> players = (Map<String, CraftPlayer>) f.get(null);
if (players != null)
players.remove(name);
players = (Map<String, CraftPlayer>) f.get(null);
} catch (Exception ex) {
Messaging.log("Unable to fetch player map from CraftEntity: " + ex.getMessage());
}

View File

@ -1,20 +1,27 @@
package net.citizensnpcs.resources.lib;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class NPCSocket extends Socket {
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(new byte[10]);
return new InputStream() {
@Override
public int read() {
return 0; // NOP
}
};
}
@Override
public OutputStream getOutputStream() {
return new ByteArrayOutputStream();
return new OutputStream() {
@Override
public void write(int b) {
// NOP
}
};
}
}

View File

@ -3,9 +3,7 @@ package net.citizensnpcs.test;
import org.junit.Test;
public class StorageTest {
@Test
public void testYaml() {
}
}