From 9bc4b54db5ab399d4232c93fc46aba3facb4806b Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 23 Jan 2012 14:12:45 +0800 Subject: [PATCH] Merge CharacterManager and TraitManager --- src/net/citizensnpcs/Citizens.java | 14 +++--- src/net/citizensnpcs/npc/CitizensNPC.java | 3 +- .../npc/trait/CitizensCharacterManager.java | 49 ------------------- .../npc/trait/CitizensTraitManager.java | 49 ------------------- .../citizensnpcs/resources/lib/CraftNPC.java | 36 +++++++------- .../citizensnpcs/resources/lib/NPCSocket.java | 17 +++++-- src/net/citizensnpcs/test/StorageTest.java | 2 - 7 files changed, 39 insertions(+), 131 deletions(-) delete mode 100644 src/net/citizensnpcs/npc/trait/CitizensCharacterManager.java delete mode 100644 src/net/citizensnpcs/npc/trait/CitizensTraitManager.java diff --git a/src/net/citizensnpcs/Citizens.java b/src/net/citizensnpcs/Citizens.java index 294050039..d22e904fb 100644 --- a/src/net/citizensnpcs/Citizens.java +++ b/src/net/citizensnpcs/Citizens.java @@ -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 characterManager = DefaultInstanceFactory.create(); + private static final InstanceFactory 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); diff --git a/src/net/citizensnpcs/npc/CitizensNPC.java b/src/net/citizensnpcs/npc/CitizensNPC.java index 9b396fb56..4a8bad8b5 100644 --- a/src/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/net/citizensnpcs/npc/CitizensNPC.java @@ -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 getTraits() { return traits.values(); } diff --git a/src/net/citizensnpcs/npc/trait/CitizensCharacterManager.java b/src/net/citizensnpcs/npc/trait/CitizensCharacterManager.java deleted file mode 100644 index a5f1f99d4..000000000 --- a/src/net/citizensnpcs/npc/trait/CitizensCharacterManager.java +++ /dev/null @@ -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> registered = new HashMap>(); - - @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 clazz) { - registerCharacterWithFactory(name, new DefaultCharacterFactory(clazz)); - } - - @Override - public void registerCharacterWithFactory(String name, Factory 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 { - private final Class clazz; - - private DefaultCharacterFactory(Class clazz) { - this.clazz = clazz; - } - - @Override - public Character create() { - try { - return clazz.newInstance(); - } catch (Exception ex) { - return null; - } - } - } -} \ No newline at end of file diff --git a/src/net/citizensnpcs/npc/trait/CitizensTraitManager.java b/src/net/citizensnpcs/npc/trait/CitizensTraitManager.java deleted file mode 100644 index 1f623bf2b..000000000 --- a/src/net/citizensnpcs/npc/trait/CitizensTraitManager.java +++ /dev/null @@ -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> registered = new HashMap>(); - - @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 clazz) { - registerTraitWithFactory(name, new DefaultTraitFactory(clazz)); - } - - @Override - public void registerTraitWithFactory(String name, Factory 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 { - private final Class clazz; - - private DefaultTraitFactory(Class clazz) { - this.clazz = clazz; - } - - @Override - public Trait create() { - try { - return clazz.newInstance(); - } catch (Exception ex) { - return null; - } - } - } -} \ No newline at end of file diff --git a/src/net/citizensnpcs/resources/lib/CraftNPC.java b/src/net/citizensnpcs/resources/lib/CraftNPC.java index c25d63487..6ea1ed7b2 100644 --- a/src/net/citizensnpcs/resources/lib/CraftNPC.java +++ b/src/net/citizensnpcs/resources/lib/CraftNPC.java @@ -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 players; + + static { try { Field f = CraftEntity.class.getDeclaredField("players"); f.setAccessible(true); - Map players = (Map) f.get(null); - if (players != null) - players.remove(name); + players = (Map) f.get(null); } catch (Exception ex) { Messaging.log("Unable to fetch player map from CraftEntity: " + ex.getMessage()); } diff --git a/src/net/citizensnpcs/resources/lib/NPCSocket.java b/src/net/citizensnpcs/resources/lib/NPCSocket.java index cc78eed86..d087dbba3 100644 --- a/src/net/citizensnpcs/resources/lib/NPCSocket.java +++ b/src/net/citizensnpcs/resources/lib/NPCSocket.java @@ -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 + } + }; } } \ No newline at end of file diff --git a/src/net/citizensnpcs/test/StorageTest.java b/src/net/citizensnpcs/test/StorageTest.java index f11673183..6626d997e 100644 --- a/src/net/citizensnpcs/test/StorageTest.java +++ b/src/net/citizensnpcs/test/StorageTest.java @@ -3,9 +3,7 @@ package net.citizensnpcs.test; import org.junit.Test; public class StorageTest { - @Test public void testYaml() { - } } \ No newline at end of file