Fix pitch and yaw not saving properly. This fixes CITIZENS-22.

This commit is contained in:
aPunch 2012-03-12 01:26:08 -05:00
parent 111ea65551
commit 1082f57938
7 changed files with 72 additions and 18 deletions

Binary file not shown.

View File

@ -5,10 +5,10 @@ import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.trait.trait.SpawnLocation;
import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.entity.EntityHumanNPC;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.text.Text;
import net.citizensnpcs.util.Messaging;
@ -54,7 +54,7 @@ public class EventListen implements Listener {
return;
for (int id : toRespawn.get(coord)) {
NPC npc = npcManager.getNPC(id);
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation());
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
}
toRespawn.removeAll(coord);
}
@ -71,7 +71,6 @@ public class EventListen implements Listener {
Location loc = npc.getBukkitEntity().getLocation();
if (event.getWorld().equals(loc.getWorld()) && event.getChunk().getX() == loc.getChunk().getX()
&& event.getChunk().getZ() == loc.getChunk().getZ()) {
npc.getTrait(SpawnLocation.class).setLocation(loc);
npc.despawn();
toRespawn.put(coord, npc.getId());
}
@ -175,7 +174,7 @@ public class EventListen implements Listener {
continue;
for (int id : toRespawn.get(chunk)) {
NPC npc = npcManager.getNPC(id);
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation());
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
}
toRespawn.removeAll(chunk);
}
@ -189,10 +188,9 @@ public class EventListen implements Listener {
for (NPC npc : npcManager) {
if (!npc.isSpawned() || !npc.getBukkitEntity().getWorld().equals(event.getWorld()))
continue;
Location loc = npc.getBukkitEntity().getLocation();
npc.getTrait(SpawnLocation.class).setLocation(loc);
npc.despawn();
toRespawn.put(toIntPair(loc.getChunk()), npc.getId());
toRespawn.put(toIntPair(npc.getBukkitEntity().getLocation().getChunk()), npc.getId());
}
}

View File

@ -11,7 +11,6 @@ import net.citizensnpcs.api.npc.character.Character;
import net.citizensnpcs.api.npc.character.CharacterManager;
import net.citizensnpcs.api.trait.trait.MobType;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.trait.trait.SpawnLocation;
import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.command.Command;
import net.citizensnpcs.command.CommandContext;
@ -19,6 +18,7 @@ import net.citizensnpcs.command.Requirements;
import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.command.exception.NoPermissionsException;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.util.Messaging;
@ -347,7 +347,7 @@ public class NPCCommands {
public void tp(CommandContext args, Player player, NPC npc) {
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned())
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation());
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
player.teleport(npc.getBukkitEntity(), TeleportCause.COMMAND);
Messaging.send(player, ChatColor.GREEN + "You teleported to " + StringHelper.wrap(npc.getName()) + ".");
}
@ -363,9 +363,8 @@ public class NPCCommands {
public void tphere(CommandContext args, Player player, NPC npc) {
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned())
npc.spawn(npc.getTrait(SpawnLocation.class).getLocation());
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND);
npc.getTrait(SpawnLocation.class).setLocation(npc.getBukkitEntity().getLocation());
Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location.");
}

View File

@ -10,12 +10,13 @@ import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.AbstractNPC;
import net.citizensnpcs.api.npc.character.Character;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.SpawnLocation;
import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.npc.ai.CitizensAI;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper;
import net.minecraft.server.EntityLiving;
import org.bukkit.Bukkit;
@ -128,7 +129,7 @@ public abstract class CitizensNPC extends AbstractNPC {
mcEntity.world.players.remove(mcEntity);
// Set the location
getTrait(SpawnLocation.class).setLocation(loc);
getTrait(CurrentLocation.class).spawn(loc);
// Set the spawned state
getTrait(Spawned.class).setSpawned(true);
@ -173,7 +174,7 @@ public abstract class CitizensNPC extends AbstractNPC {
// Spawn the NPC
if (getTrait(Spawned.class).shouldSpawn())
spawn(getTrait(SpawnLocation.class).getLocation());
spawn(getTrait(CurrentLocation.class).getLocation());
}
public void save(DataKey root) {

View File

@ -11,7 +11,6 @@ import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.npc.character.Character;
import net.citizensnpcs.api.trait.trait.SpawnLocation;
import net.citizensnpcs.api.util.Storage;
import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.util.ByIdArray;
@ -53,7 +52,6 @@ public class CitizensNPCManager implements NPCManager {
}
public void despawn(NPC npc, boolean keepSelected) {
npc.getTrait(SpawnLocation.class).setLocation(npc.getBukkitEntity().getLocation());
if (!keepSelected)
npc.removeMetadata("selectors", plugin);
npc.getBukkitEntity().remove();

View File

@ -14,7 +14,7 @@ import net.citizensnpcs.api.trait.trait.Inventory;
import net.citizensnpcs.api.trait.trait.MobType;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.api.trait.trait.SpawnLocation;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.text.Text;
@ -32,7 +32,7 @@ public class CitizensTraitManager implements TraitManager {
registerTrait(new TraitFactory(Owner.class).withName("owner"));
registerTrait(new TraitFactory(Powered.class).withName("powered"));
registerTrait(new TraitFactory(Spawned.class).withName("spawned"));
registerTrait(new TraitFactory(SpawnLocation.class).withName("location"));
registerTrait(new TraitFactory(CurrentLocation.class).withName("location"));
registerTrait(new TraitFactory(Text.class).withName("text"));
registerTrait(new TraitFactory(Waypoints.class).withName("waypoints"));
}

View File

@ -0,0 +1,58 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import org.bukkit.Bukkit;
import org.bukkit.Location;
public class CurrentLocation extends Trait implements Runnable {
private Location loc;
private final NPC npc;
public CurrentLocation(NPC npc) {
this.npc = npc;
}
@Override
public void run() {
if (npc.getBukkitEntity() == null)
return;
loc = npc.getBukkitEntity().getLocation();
}
@Override
public void load(DataKey key) throws NPCLoadException {
if (Bukkit.getWorld(key.getString("world")) == null)
throw new NPCLoadException("'" + key.getString("world") + "' is not a valid world.");
loc = new Location(Bukkit.getWorld(key.getString("world")), key.getDouble("x"), key.getDouble("y"), key
.getDouble("z"), (float) key.getDouble("yaw"), (float) key.getDouble("pitch"));
}
@Override
public void save(DataKey key) {
key.setString("world", loc.getWorld().getName());
key.setDouble("x", loc.getX());
key.setDouble("y", loc.getY());
key.setDouble("z", loc.getZ());
key.setDouble("yaw", loc.getYaw());
key.setDouble("pitch", loc.getPitch());
}
public Location getLocation() {
return loc;
}
public void spawn(Location loc) {
this.loc = loc;
}
@Override
public String toString() {
return "SpawnLocation{" + loc + "}";
}
}