changes and bugfixes

This commit is contained in:
aPunch 2012-02-04 02:13:20 -06:00
parent 739d7490fd
commit ab0a8dcae3
36 changed files with 132 additions and 96 deletions

Binary file not shown.

View File

@ -54,9 +54,8 @@ public class Citizens extends JavaPlugin {
private static final String COMPATIBLE_MC_VERSION = "1.1";
@SuppressWarnings("unchecked")
private static final List<Class<? extends Trait>> traits = Lists.newArrayList(Owner.class, Spawned.class,
private static final List<Class<? extends Trait>> defaultTraits = Lists.newArrayList(Owner.class, Spawned.class,
LookClose.class, SpawnLocation.class);
// TODO: automatically register trait classes in CitizensNPC?
private volatile CitizensNPCManager npcManager;
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
@ -190,7 +189,8 @@ public class Citizens extends JavaPlugin {
registerCommands();
registerPermissions();
traitManager.registerAll(traits);
// Register default traits
traitManager.registerAll(defaultTraits);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new NPCUpdater(npcManager), 0, 1);
@ -217,7 +217,6 @@ public class Citizens extends JavaPlugin {
cmdManager = new CommandManager();
cmdManager.setInjector(new Injector(npcManager, characterManager));
// cmdManager.register(AdminCommands.class);
cmdManager.register(NPCCommands.class);
}
@ -231,6 +230,7 @@ public class Citizens extends JavaPlugin {
children.put("citizens.npc.select", true);
children.put("citizens.npc.tp", true);
children.put("citizens.npc.tphere", true);
children.put("citizens.npc.look-close", true);
Permission perm = new Permission("citizens.*", PermissionDefault.OP, children);
getServer().getPluginManager().addPermission(perm);

View File

@ -2,7 +2,7 @@ package net.citizensnpcs;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.entity.CitizensHumanNPC;
import net.citizensnpcs.npc.ai.CitizensNavigator;
public class NPCUpdater implements Runnable {
private final CitizensNPCManager npcManager;
@ -14,9 +14,6 @@ public class NPCUpdater implements Runnable {
@Override
public void run() {
for (NPC npc : npcManager)
// For now only do this for human NPCs
if (npc instanceof CitizensHumanNPC) {
((CitizensHumanNPC) npc).tick();
}
((CitizensNavigator) npc.getNavigator()).update();
}
}

View File

@ -51,6 +51,7 @@ import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
public class CommandManager {
/*
* Attempt to execute a command. This version takes a separate command name
* (for the root command) and then a list of following arguments.

View File

@ -32,8 +32,13 @@ public class NPCCommands {
this.characterManager = characterManager;
}
@Command(aliases = { "npc" }, usage = "create [name] [type] (character)", desc = "Create a new NPC",
modifiers = { "create" }, min = 3, max = 4)
@Command(
aliases = { "npc" },
usage = "create [name] [type] (character)",
desc = "Create a new NPC",
modifiers = { "create" },
min = 3,
max = 4)
@Permission("npc.create")
@Requirements
public void createNPC(CommandContext args, Player player, NPC npc) {
@ -72,8 +77,13 @@ public class NPCCommands {
Messaging.send(player, successMsg);
}
@Command(aliases = { "npc" }, usage = "despawn", desc = "Despawn an NPC", modifiers = { "despawn" }, min = 1,
max = 1)
@Command(
aliases = { "npc" },
usage = "despawn",
desc = "Despawn an NPC",
modifiers = { "despawn" },
min = 1,
max = 1)
@Permission("npc.despawn")
public void despawnNPC(CommandContext args, Player player, NPC npc) {
npc.getTrait(Spawned.class).setSpawned(false);
@ -81,8 +91,13 @@ public class NPCCommands {
Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(aliases = { "npc" }, usage = "select [id]", desc = "Select an NPC", modifiers = { "select" }, min = 2,
max = 2)
@Command(
aliases = { "npc" },
usage = "select [id]",
desc = "Select an NPC",
modifiers = { "select" },
min = 2,
max = 2)
@Permission("npc.select")
@Requirements(ownership = true)
public void selectNPC(CommandContext args, Player player, NPC npc) {
@ -99,8 +114,13 @@ public class NPCCommands {
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect);
}
@Command(aliases = { "npc" }, usage = "spawn [id]", desc = "Spawn an existing NPC", modifiers = { "spawn" },
min = 2, max = 2)
@Command(
aliases = { "npc" },
usage = "spawn [id]",
desc = "Spawn an existing NPC",
modifiers = { "spawn" },
min = 2,
max = 2)
@Permission("npc.spawn")
@Requirements
public void spawnNPC(CommandContext args, Player player, NPC npc) {
@ -127,24 +147,39 @@ public class NPCCommands {
+ " is already spawned at another location. Use '/npc tphere' to teleport the NPC to your location.");
}
@Command(aliases = { "npc" }, usage = "tphere", desc = "Teleport an NPC to your location",
modifiers = { "tphere" }, min = 1, max = 1)
@Command(
aliases = { "npc" },
usage = "tphere",
desc = "Teleport an NPC to your location",
modifiers = { "tphere" },
min = 1,
max = 1)
@Permission("npc.tphere")
public void teleportNPCToPlayer(CommandContext args, Player player, NPC npc) {
npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND);
Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location.");
}
@Command(aliases = { "npc" }, usage = "tp", desc = "Teleport to an NPC", modifiers = { "tp", "teleport" }, min = 1,
max = 1)
@Command(
aliases = { "npc" },
usage = "tp",
desc = "Teleport to an NPC",
modifiers = { "tp", "teleport" },
min = 1,
max = 1)
@Permission("npc.tp")
public void teleportToNPC(CommandContext args, Player player, NPC npc) {
player.teleport(npc.getBukkitEntity(), TeleportCause.COMMAND);
Messaging.send(player, ChatColor.GREEN + "You teleported to " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(aliases = { "npc" }, usage = "lookclose", desc = "Toggle an NPC's look-close state", modifiers = {
"lookclose", "look", "rotate" }, min = 1, max = 1)
@Command(
aliases = { "npc" },
usage = "lookclose",
desc = "Toggle an NPC's look-close state",
modifiers = { "lookclose", "look", "rotate" },
min = 1,
max = 1)
@Permission("npc.look-close")
public void toggleNPCLookClose(CommandContext args, Player player, NPC npc) {
npc.getTrait(LookClose.class).setLookClose(!npc.getTrait(LookClose.class).shouldLookClose());

View File

@ -1,20 +1,19 @@
package net.citizensnpcs.npc.entity;
package net.citizensnpcs.npc;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.Entity;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityTypes;
import net.minecraft.server.World;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.LivingEntity;
@SuppressWarnings("unchecked")
public abstract class CitizensMobNPC extends CitizensNPC {
private final Constructor<? extends EntityLiving> constructor;
@ -29,7 +28,7 @@ public abstract class CitizensMobNPC extends CitizensNPC {
registerEntityClass(clazz);
}
private EntityLiving createEntityFromClass(net.minecraft.server.World world) {
private EntityLiving createEntityFromClass(World world) {
try {
return constructor.newInstance(world);
} catch (Exception ex) {
@ -41,7 +40,7 @@ public abstract class CitizensMobNPC extends CitizensNPC {
@Override
protected EntityLiving createHandle(Location loc) {
EntityLiving entity = createEntityFromClass(((CraftWorld) loc.getWorld()).getHandle());
mcEntity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
entity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
return entity;
}
@ -76,8 +75,6 @@ public abstract class CitizensMobNPC extends CitizensNPC {
classToInt = (Map<Class<? extends Entity>, Integer>) field.get(null);
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalStateException(
"Unable to fetch entity class mapping - is Citizens updated for this version of CraftBukkit?");
}
}
}
}

View File

@ -7,7 +7,6 @@ import net.citizensnpcs.api.npc.ai.Navigator;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.api.npc.trait.trait.Spawned;
import net.citizensnpcs.npc.ai.CitizensNavigator;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.util.Messaging;
import org.bukkit.Bukkit;
@ -15,7 +14,6 @@ import org.bukkit.Location;
import org.bukkit.entity.Entity;
public abstract class CitizensNPC extends AbstractNPC {
private static final double lookRange = 5;
protected final CitizensNPCManager manager;
protected net.minecraft.server.Entity mcEntity;
@ -41,29 +39,6 @@ public abstract class CitizensNPC extends AbstractNPC {
return true;
}
// TODO: is this necessary? it's a helper method...
protected void faceEntity(Location target) {
if (getBukkitEntity().getWorld() != target.getWorld())
return;
Location loc = getBukkitEntity().getLocation();
double xDiff = target.getX() - loc.getX();
double yDiff = target.getY() - loc.getY();
double zDiff = target.getZ() - loc.getZ();
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI);
double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90;
if (zDiff < 0.0) {
yaw = yaw + (Math.abs(180 - yaw) * 2);
}
mcEntity.yaw = (float) yaw - 90;
mcEntity.pitch = (float) pitch;
}
@Override
public Entity getBukkitEntity() {
return getHandle().getBukkitEntity();
@ -111,14 +86,4 @@ public abstract class CitizensNPC extends AbstractNPC {
addTrait(new Spawned(true));
return true;
}
public void tick() {
// TODO: this needs to be less hard-coded... does everyone want this
// behaviour?
if (mcEntity != null) {
if (getTrait(LookClose.class).shouldLookClose()
&& mcEntity.world.findNearbyPlayer(mcEntity, lookRange) != null)
faceEntity(mcEntity.world.findNearbyPlayer(mcEntity, lookRange).getBukkitEntity().getLocation());
}
}
}

View File

@ -3,8 +3,10 @@ package net.citizensnpcs.npc.ai;
import net.citizensnpcs.api.npc.ai.Navigator;
import net.citizensnpcs.api.npc.ai.NavigatorCallback;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.trait.LookClose;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
public class CitizensNavigator implements Navigator {
@ -20,9 +22,12 @@ public class CitizensNavigator implements Navigator {
}
public void update() {
if (executing != null) {
if (npc.getHandle() != null && npc.getHandle().world.findNearbyPlayer(npc.getHandle(), 5) != null)
if (npc.getTrait(LookClose.class).shouldLookClose()
&& npc.getHandle().world.findNearbyPlayer(npc.getHandle(), 5) != null)
faceEntity(npc.getHandle().world.findNearbyPlayer(npc.getHandle(), 5).getBukkitEntity());
if (executing != null)
executing.update();
}
}
@Override
@ -34,4 +39,26 @@ public class CitizensNavigator implements Navigator {
public void setTarget(LivingEntity target, boolean aggressive) {
executing = new TargetStrategy(npc, target, aggressive);
}
private void faceEntity(Entity target) {
if (npc.getBukkitEntity().getWorld() != target.getWorld())
return;
Location loc = npc.getBukkitEntity().getLocation();
double xDiff = target.getLocation().getX() - loc.getX();
double yDiff = target.getLocation().getY() - loc.getY();
double zDiff = target.getLocation().getZ() - loc.getZ();
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI);
double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90;
if (zDiff < 0.0) {
yaw = yaw + (Math.abs(180 - yaw) * 2);
}
npc.getHandle().yaw = (float) yaw - 90;
npc.getHandle().pitch = (float) pitch;
}
}

View File

@ -12,6 +12,8 @@ import org.bukkit.Location;
import org.bukkit.entity.Player;
public class MoveStrategy implements PathStrategy {
private static final double JUMP_VELOCITY = 0.49D;
private final EntityLiving handle;
private final PathEntity path;
private final Random random = new Random();
@ -33,11 +35,10 @@ public class MoveStrategy implements PathStrategy {
lengthSq *= lengthSq;
while (vec3d != null && vec3d.d(handle.locX, vec3d.b, handle.locZ) < lengthSq) {
this.path.a(); // Increment path index.
if (this.path.b()) { // finished.
if (this.path.b())// finished.
return null;
} else {
else
vec3d = this.path.a(handle);
}
}
return vec3d;
}
@ -56,18 +57,15 @@ public class MoveStrategy implements PathStrategy {
double diffZ = vector.c - handle.locZ;
handle.yaw += getYawDifference(diffZ, diffX);
if (vector.b - yHeight > 0.0D) {
if (vector.b - yHeight > 0.0D)
jump();
}
handle.d();
// handle.walk();
if (handle.positionChanged) {
if (handle.positionChanged)
jump();
}
if (random.nextFloat() < 0.8F && (inWater || onFire)) {
if (random.nextFloat() < 0.8F && (inWater || onFire))
handle.motY += 0.04D;
}
return false;
}
@ -81,6 +79,4 @@ public class MoveStrategy implements PathStrategy {
if (handle.onGround)
handle.motY = JUMP_VELOCITY;
}
private static final double JUMP_VELOCITY = 0.49D;
}
}

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.ai;
public interface PathStrategy {
boolean update();
}
boolean update();
}

View File

@ -24,16 +24,13 @@ public class TargetStrategy implements PathStrategy {
if (target == null || target.dead)
return true;
current = new MoveStrategy(handle, handle.world.findPath(handle, target, 16F));
// NPCManager.faceEntity(handle.getBukkitEntity(),
// target.getBukkitEntity());
if (aggro) {
if (handle instanceof EntityMonster) {
if (aggro)
if (handle instanceof EntityMonster)
((EntityMonster) handle).d(target);
} else if (handle instanceof EntityHuman) {
else if (handle instanceof EntityHuman)
((EntityHuman) handle).attack(target);
}
}
current.update();
return false;
}
}
}

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityBlaze;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityCaveSpider;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityChicken;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityCow;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityCreeper;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityEnderDragon;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityEnderman;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityGhast;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityGiantZombie;
import net.minecraft.server.World;

View File

@ -2,7 +2,7 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityHumanNPC;
import net.citizensnpcs.resource.lib.EntityHumanNPC;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.ItemInWorldManager;
import net.minecraft.server.MinecraftServer;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityMagmaCube;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityMushroomCow;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityPig;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityPigZombie;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntitySheep;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntitySilverfish;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntitySkeleton;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntitySlime;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntitySnowman;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntitySpider;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntitySquid;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityVillager;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityWolf;
import net.minecraft.server.World;

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensMobNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.EntityZombie;
import net.minecraft.server.World;

View File

@ -1,12 +1,9 @@
package net.citizensnpcs.resource.lib.entity;
package net.citizensnpcs.resource.lib;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
import net.citizensnpcs.resource.lib.NPCNetHandler;
import net.citizensnpcs.resource.lib.NPCNetworkManager;
import net.citizensnpcs.resource.lib.NPCSocket;
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.ItemInWorldManager;