initial Citizens2 commit

This commit is contained in:
aPunch 2012-01-14 17:51:37 -06:00
commit 32abcf8a79
9 changed files with 333 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/bin
/build
.classpath
.project
Citizens.jar

BIN
lib/CitizensAPI.jar Normal file

Binary file not shown.

BIN
lib/bukkit.jar Normal file

Binary file not shown.

BIN
lib/craftbukkit.jar Normal file

Binary file not shown.

View File

@ -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();
}
}

View File

@ -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<Trait> traits = new HashSet<Trait>();
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<Trait> 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;
}
}

View File

@ -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<LivingEntity, NPC> spawned = new HashMap<LivingEntity, NPC>();
@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<NPC> getNPCs() {
return spawned.values();
}
@Override
public Collection<NPC> getNPCs(Trait trait) {
Set<NPC> npcs = new HashSet<NPC>();
for (NPC npc : spawned.values()) {
if (npc.hasTrait(trait)) {
npcs.add(npc);
}
}
return npcs;
}
@Override
public Collection<NPC> 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();
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}