commit 6ce73502e80bef397ab31638d188d795be93f2a4 Author: aPunch Date: Sat Jan 14 17:51:37 2012 -0600 initial Citizens2 commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..ae6d66ba7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/bin +/build +.classpath +.project +Citizens.jar \ No newline at end of file diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..a61600575 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Thu Jan 12 18:45:18 CST 2012 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/lib/CitizensAPI.jar b/lib/CitizensAPI.jar new file mode 100644 index 000000000..4e8832744 Binary files /dev/null and b/lib/CitizensAPI.jar differ diff --git a/lib/bukkit.jar b/lib/bukkit.jar new file mode 100644 index 000000000..283d2224d Binary files /dev/null and b/lib/bukkit.jar differ diff --git a/lib/craftbukkit.jar b/lib/craftbukkit.jar new file mode 100644 index 000000000..46ceb7f1a Binary files /dev/null and b/lib/craftbukkit.jar differ diff --git a/src/net/citizensnpcs/Citizens.java b/src/net/citizensnpcs/Citizens.java new file mode 100644 index 000000000..b80364950 --- /dev/null +++ b/src/net/citizensnpcs/Citizens.java @@ -0,0 +1,25 @@ +package net.citizensnpcs; + +import net.citizensnpcs.api.CitizensAPI; +import net.citizensnpcs.api.CitizensPlugin; +import net.citizensnpcs.api.npc.NPCManager; +import net.citizensnpcs.npc.CitizensNPCManager; + +import org.bukkit.plugin.java.JavaPlugin; + +public class Citizens extends JavaPlugin implements CitizensPlugin { + + @Override + public void onDisable() { + } + + @Override + public void onEnable() { + CitizensAPI.setInstance(this); + } + + @Override + public NPCManager getNPCManager() { + return new CitizensNPCManager(); + } +} \ No newline at end of file diff --git a/src/net/citizensnpcs/npc/CitizensNPC.java b/src/net/citizensnpcs/npc/CitizensNPC.java new file mode 100644 index 000000000..a28f71de6 --- /dev/null +++ b/src/net/citizensnpcs/npc/CitizensNPC.java @@ -0,0 +1,123 @@ +package net.citizensnpcs.npc; + +import java.util.HashSet; +import java.util.Set; + +import net.citizensnpcs.api.Citizens; +import net.citizensnpcs.api.event.NPCSpawnEvent; +import net.citizensnpcs.api.npc.trait.Character; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.npc.pathfinding.Navigator; +import net.citizensnpcs.api.npc.trait.Trait; +import net.citizensnpcs.npc.trait.LocationTrait; +import net.citizensnpcs.resources.lib.CraftNPC; + +import org.bukkit.Bukkit; +import org.bukkit.Location; + +public class CitizensNPC implements NPC { + private final int id; + private final Set traits = new HashSet(); + private Character character = null; + private CraftNPC mcEntity; + + protected CitizensNPC(Character character, Trait... traits) { + this.character = character; + for (Trait trait : traits) { + this.traits.add(trait); + } + this.id = ((CitizensNPCManager) Citizens.getNPCManager()).getUniqueID(); + } + + @Override + public void addTrait(Trait trait) { + if (!hasTrait(trait)) + traits.add(trait); + else + throw new IllegalArgumentException("The NPC already has the trait '" + trait.getName() + "'."); + } + + @Override + public void addTrait(String name) { + addTrait(Citizens.getTraitManager().getTrait(name)); + } + + @Override + public Character getCharacter() { + return character; + } + + @Override + public int getId() { + return id; + } + + @Override + public Navigator getNavigator() { + // TODO add default navigator + return null; + } + + @Override + public Trait getTrait(String name) { + for (Trait trait : traits) { + if (trait.getName().equals(name)) { + return trait; + } + } + return null; + } + + @Override + public Iterable getTraits() { + return traits; + } + + @Override + public boolean hasTrait(Trait trait) { + return traits.contains(trait); + } + + @Override + public boolean hasTrait(String name) { + return hasTrait(Citizens.getTraitManager().getTrait(name)); + } + + @Override + public void removeTrait(Trait trait) { + traits.remove(trait); + } + + @Override + public void removeTrait(String name) { + removeTrait(Citizens.getTraitManager().getTrait(name)); + } + + @Override + public void setCharacter(Character character) { + if (this.character.equals(character)) + throw new IllegalArgumentException("The NPC already has the character '" + character.getName() + "'."); + this.character = character; + } + + @Override + public void spawn(Location loc) { + NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, loc); + Bukkit.getPluginManager().callEvent(spawnEvent); + if (spawnEvent.isCancelled()) { + return; + } + addTrait(new LocationTrait(loc)); + mcEntity = ((CitizensNPCManager) Citizens.getNPCManager()).spawn(this); + } + + @Override + public void despawn() { + mcEntity.die(); + ((CitizensNPCManager) Citizens.getNPCManager()).despawn(this); + } + + public CraftNPC getHandle() { + return mcEntity; + } +} \ No newline at end of file diff --git a/src/net/citizensnpcs/npc/CitizensNPCManager.java b/src/net/citizensnpcs/npc/CitizensNPCManager.java new file mode 100644 index 000000000..0cfced4a5 --- /dev/null +++ b/src/net/citizensnpcs/npc/CitizensNPCManager.java @@ -0,0 +1,126 @@ +package net.citizensnpcs.npc; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.craftbukkit.CraftWorld; +import org.bukkit.entity.LivingEntity; + +import net.citizensnpcs.api.Citizens; +import net.citizensnpcs.api.event.NPCDespawnEvent; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.npc.NPCManager; +import net.citizensnpcs.api.npc.trait.Character; +import net.citizensnpcs.api.npc.trait.Trait; +import net.citizensnpcs.npc.trait.LocationTrait; +import net.citizensnpcs.resources.lib.CraftNPC; + +import net.minecraft.server.ItemInWorldManager; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.WorldServer; + +public class CitizensNPCManager implements NPCManager { + private Map spawned = new HashMap(); + + @Override + public NPC createNPC() { + return createNPC(null); + } + + @Override + public NPC createNPC(Character character) { + return createNPC(character); + } + + @Override + public NPC createNPC(Character character, Trait... traits) { + return new CitizensNPC(character, traits); + } + + @Override + public NPC getNPC(int id) { + for (NPC npc : spawned.values()) { + if (npc.getId() == id) { + return npc; + } + } + return null; + } + + @Override + public NPC getNPC(LivingEntity entity) { + return spawned.get(entity); + } + + @Override + public Collection getNPCs() { + return spawned.values(); + } + + @Override + public Collection getNPCs(Trait trait) { + Set npcs = new HashSet(); + for (NPC npc : spawned.values()) { + if (npc.hasTrait(trait)) { + npcs.add(npc); + } + } + return npcs; + } + + @Override + public Collection getNPCs(String name) { + return getNPCs(Citizens.getTraitManager().getTrait(name)); + } + + @Override + public boolean isNPC(LivingEntity entity) { + return spawned.containsKey(entity); + } + + public int getUniqueID() { + int count = 0; + while (true) { + if (getNPC(count) == null) + break; + count++; + } + return count; + } + + public CraftNPC spawn(NPC npc) { + Location loc = ((LocationTrait) npc.getTrait("location")).getLocation(); + WorldServer ws = getWorldServer(loc.getWorld()); + CraftNPC mcEntity = new CraftNPC(getMinecraftServer(ws.getServer()), ws, "", new ItemInWorldManager(ws)); + mcEntity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); + ws.addEntity(mcEntity); + ws.players.remove(mcEntity); + + spawned.put(mcEntity.getPlayer(), npc); + return mcEntity; + } + + public void despawn(NPC npc) { + Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(npc)); + + CraftNPC mcEntity = ((CitizensNPC) npc).getHandle(); + getWorldServer(((LocationTrait) npc.getTrait("location")).getLocation().getWorld()).removeEntity(mcEntity); + spawned.remove(mcEntity.getPlayer()); + } + + private WorldServer getWorldServer(World world) { + return ((CraftWorld) world).getHandle(); + } + + private MinecraftServer getMinecraftServer(Server server) { + return ((CraftServer) server).getServer(); + } +} \ No newline at end of file diff --git a/src/net/citizensnpcs/npc/trait/LocationTrait.java b/src/net/citizensnpcs/npc/trait/LocationTrait.java new file mode 100644 index 000000000..9526010ed --- /dev/null +++ b/src/net/citizensnpcs/npc/trait/LocationTrait.java @@ -0,0 +1,41 @@ +package net.citizensnpcs.npc.trait; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.configuration.ConfigurationSection; + +import net.citizensnpcs.api.npc.trait.Trait; + +public class LocationTrait implements Trait { + private Location loc; + + public LocationTrait(Location loc) { + this.loc = loc; + } + + @Override + public String getName() { + return "location"; + } + + @Override + public void load(ConfigurationSection cs) { + loc = new Location(Bukkit.getWorld(cs.getString("location.world")), cs.getDouble("location.x"), + cs.getDouble("location.y"), cs.getDouble("location.z"), (float) cs.getDouble("location.pitch"), + (float) cs.getDouble("location.yaw")); + } + + @Override + public void save(ConfigurationSection cs) { + cs.set("location.world", loc.getWorld()); + cs.set("location.x", loc.getX()); + cs.set("location.y", loc.getY()); + cs.set("location.z", loc.getZ()); + cs.set("location.pitch", loc.getPitch()); + cs.set("location.yaw", loc.getYaw()); + } + + public Location getLocation() { + return loc; + } +} \ No newline at end of file diff --git a/src/net/citizensnpcs/resources/lib/CraftNPC.java b/src/net/citizensnpcs/resources/lib/CraftNPC.java new file mode 100644 index 000000000..da281d7a7 --- /dev/null +++ b/src/net/citizensnpcs/resources/lib/CraftNPC.java @@ -0,0 +1,13 @@ +package net.citizensnpcs.resources.lib; + +import net.minecraft.server.EntityPlayer; +import net.minecraft.server.ItemInWorldManager; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.World; + +public class CraftNPC extends EntityPlayer { + + public CraftNPC(MinecraftServer minecraftserver, World world, String s, ItemInWorldManager iteminworldmanager) { + super(minecraftserver, world, s, iteminworldmanager); + } +} \ No newline at end of file