Fix look-close and talk-close defaults. This fixes CITIZENS-27.

This commit is contained in:
aPunch 2012-03-14 00:16:42 -05:00
parent 4a17a478bf
commit 0a52a851c8
5 changed files with 51 additions and 30 deletions

Binary file not shown.

View File

@ -21,6 +21,7 @@ import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.text.Text;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.StringHelper;
@ -113,11 +114,11 @@ public class NPCCommands {
}
msg += " at your location.";
// Set the owner
// Initialize necessary traits
create.getTrait(Owner.class).setOwner(player.getName());
// Set the mob type
create.getTrait(MobType.class).setType(type.toString());
create.addTrait(LookClose.class);
create.addTrait(Text.class);
create.spawn(player.getLocation());
npcManager.selectNPC(player, create);

View File

@ -22,6 +22,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
public abstract class CitizensNPC extends AbstractNPC {
@ -36,6 +37,25 @@ public abstract class CitizensNPC extends AbstractNPC {
traitManager = (CitizensTraitManager) CitizensAPI.getTraitManager();
}
@Override
public void addTrait(Class<? extends Trait> clazz) {
Trait trait = traitManager.getTrait(clazz, this);
if (trait == null) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot register a null trait. Was it registered properly?");
return;
}
if (trait instanceof Runnable) {
runnables.add((Runnable) trait);
if (traits.containsKey(trait.getClass()))
runnables.remove(traits.get(trait.getClass()));
}
if (trait instanceof Listener) {
Bukkit.getPluginManager().registerEvents((Listener) trait, null);
// TODO: insert plugin instance somehow
}
traits.put(trait.getClass(), trait);
}
@Override
public void chat(Player player, String message) {
Messaging.sendWithNPC(player, Setting.CHAT_PREFIX.asString() + message, this);
@ -89,7 +109,7 @@ public abstract class CitizensNPC extends AbstractNPC {
public <T extends Trait> T getTrait(Class<T> clazz) {
Trait t = traits.get(clazz);
if (t == null)
addTrait(traitManager.getTrait(clazz, this));
addTrait(clazz);
return traits.get(clazz) != null ? clazz.cast(traits.get(clazz)) : null;
}
@ -170,7 +190,7 @@ public abstract class CitizensNPC extends AbstractNPC {
+ ex.getMessage());
ex.printStackTrace();
}
addTrait(trait);
addTrait(trait.getClass());
}
// Spawn the NPC

View File

@ -19,6 +19,7 @@ import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.text.Text;
import net.citizensnpcs.trait.waypoint.Waypoints;
import net.citizensnpcs.util.Messaging;
public class CitizensTraitManager implements TraitManager {
private final Map<String, Class<? extends Trait>> registered = new HashMap<String, Class<? extends Trait>>();
@ -86,8 +87,7 @@ public class CitizensTraitManager implements TraitManager {
if (!CACHED_CTORS.containsKey(trait)) {
try {
constructor = trait.getConstructor(NPC.class);
constructor.setAccessible(true); // do we want to allow private
// constructors?
constructor.setAccessible(true);
} catch (Exception ex) {
constructor = null;
}

View File

@ -20,29 +20,6 @@ public class LookClose extends Trait implements Runnable, Toggleable {
this.npc = npc;
}
private void faceEntity(CitizensNPC npc, 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;
npc.getHandle().X = npc.getHandle().yaw;
}
@Override
public void load(DataKey key) throws NPCLoadException {
lookClose = key.getBoolean("");
@ -67,6 +44,29 @@ public class LookClose extends Trait implements Runnable, Toggleable {
return lookClose;
}
private void faceEntity(CitizensNPC npc, 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;
npc.getHandle().X = npc.getHandle().yaw;
}
@Override
public String toString() {
return "LookClose{" + lookClose + "}";