Restrict Pose object to inside Poses, metrics version should only report the major version

This commit is contained in:
fullwall 2012-11-06 13:42:32 +08:00
parent f0a29c3f7f
commit fc48b53d74
7 changed files with 69 additions and 56 deletions

View File

@ -72,14 +72,19 @@ public class EventListen implements Listener {
List<Integer> ids = toRespawn.get(coord);
for (int i = 0; i < ids.size(); i++) {
int id = ids.get(i);
NPC npc = npcRegistry.getById(id);
if (npc == null)
continue;
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
spawn(id);
Messaging.debug("Spawned", id, "due to chunk load at [" + coord.x + "," + coord.z + "]");
}
toRespawn.removeAll(coord);
}
private void spawn(int id) {
NPC npc = npcRegistry.getById(id);
if (npc == null)
return;
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
}
@EventHandler(ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) {
ChunkCoord coord = toCoord(event.getChunk());
@ -91,6 +96,8 @@ public class EventListen implements Listener {
if (event.getWorld().equals(loc.getWorld()) && sameChunkCoordinates) {
npc.despawn();
toRespawn.put(coord, npc.getId());
Messaging.debug("Despawned", npc.getId(), "due to chunk unload at [" + coord.x + ","
+ coord.z + "]");
}
}
}
@ -244,9 +251,9 @@ public class EventListen implements Listener {
continue;
List<Integer> ids = toRespawn.get(chunk);
for (int i = 0; i < ids.size(); i++) {
int id = ids.get(i);
NPC npc = npcRegistry.getById(id);
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
spawn(ids.get(i));
Messaging
.debug("Spawned", ids.get(0), "due to world " + event.getWorld().getName() + " load");
}
toRespawn.removeAll(chunk);
}
@ -259,6 +266,7 @@ public class EventListen implements Listener {
continue;
storeForRespawn(npc);
npc.despawn();
Messaging.debug("Despawned", npc.getId() + "due to world unload at", event.getWorld().getName());
}
}

View File

@ -307,7 +307,7 @@ public class Metrics {
// Construct the post data
final StringBuilder data = new StringBuilder();
data.append(encode("guid")).append('=').append(encode(guid));
encodeDataPair(data, "version", description.getVersion());
encodeDataPair(data, "version", description.getVersion().replaceAll("\\(.*$", ""));
encodeDataPair(data, "server", Bukkit.getVersion());
encodeDataPair(data, "players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length));
encodeDataPair(data, "revision", String.valueOf(REVISION));

View File

@ -45,7 +45,7 @@ public class NPCDataStore {
created++;
}
Messaging.logTr(Messages.NUM_LOADED_NOTIFICATION, created);
Messaging.logTr(Messages.NUM_LOADED_NOTIFICATION, created, "?");
}
public void remove(NPC npc) {

View File

@ -43,7 +43,6 @@ import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.Pose;
import net.citizensnpcs.util.StringHelper;
import net.citizensnpcs.util.Util;
@ -705,32 +704,22 @@ public class NPCCommands {
} else
throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("assume"));
} else if (args.hasValueFlag("assume")) {
if (args.getFlag("assume").isEmpty())
String pose = args.getFlag("assume");
if (pose.isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME);
Pose pose = trait.getPose(args.getFlag("assume"));
if (pose == null)
throw new CommandException(Messages.POSE_MISSING, args.getFlag("assume"));
if (!trait.hasPose(pose))
throw new CommandException(Messages.POSE_MISSING, pose);
trait.assumePose(pose);
} else if (args.hasValueFlag("remove")) {
if (args.getFlag("remove").isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME);
if (trait.removePose(trait.getPose(args.getFlag("remove"))))
if (trait.removePose(args.getFlag("remove"))) {
Messaging.sendTr(sender, Messages.POSE_REMOVED);
else
} else
throw new CommandException(Messages.POSE_MISSING, args.getFlag("remove"));
} else if (!args.hasFlag('a')) {
Paginator paginator = new Paginator().header("Pose");
paginator.addLine("<e>Key: <a>ID <b>Name <c>Pitch/Yaw");
for (int i = 0; i < trait.getPoses().size(); i++) {
String line = "<a>" + i + "<b> " + trait.getPoses().get(i).getName() + "<c> "
+ trait.getPoses().get(i).getPitch() + "/" + trait.getPoses().get(i).getYaw();
paginator.addLine(line);
}
int page = args.getInteger(1, 1);
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
trait.describe(sender, args.getInteger(1, 1));
}
// Assume Player's pose
@ -738,7 +727,7 @@ public class NPCCommands {
return;
if (sender instanceof Player) {
Location location = ((Player) sender).getLocation();
trait.assumePose(new Pose(sender.getName(), location.getPitch(), location.getYaw()));
trait.assumePose(location);
} else
throw new ServerCommandException();
}

View File

@ -32,7 +32,6 @@ public class CitizensHumanNPC extends CitizensNPC implements Equipable {
final EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws,
StringHelper.parseColors(getFullName()), new ItemInWorldManager(ws), this);
handle.getBukkitEntity().teleport(loc);
NMS.setHeadYaw(handle, loc.getYaw() % 360);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {

View File

@ -1,20 +1,24 @@
package net.citizensnpcs.trait;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.Pose;
import net.citizensnpcs.util.Util;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import com.google.common.collect.Maps;
public class Poses extends Trait {
private final List<Pose> poses = new ArrayList<Pose>();
private final Map<String, Pose> poses = Maps.newHashMap();
public Poses() {
super("poses");
@ -22,28 +26,21 @@ public class Poses extends Trait {
public boolean addPose(String name, Location location) {
Pose newPose = new Pose(name, location.getPitch(), location.getYaw());
if (poses.contains(newPose))
if (poses.containsValue(newPose) || poses.containsKey(name))
return false;
poses.add(newPose);
poses.put(name.toLowerCase(), newPose);
return true;
}
public void assumePose(Pose pose) {
public void assumePose(Location location) {
assumePose(location.getYaw(), location.getPitch());
}
private void assumePose(float yaw, float pitch) {
if (!npc.isSpawned())
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
Util.assumePose(npc.getBukkitEntity(), pose);
}
public Pose getPose(String name) {
for (Pose pose : poses)
if (pose.getName().equalsIgnoreCase(name))
return pose;
return null;
}
public List<Pose> getPoses() {
return poses;
Util.assumePose(npc.getBukkitEntity(), yaw, pitch);
}
@Override
@ -51,18 +48,14 @@ public class Poses extends Trait {
for (DataKey sub : key.getRelative("list").getIntegerSubKeys())
try {
String[] parts = sub.getString("").split(";");
poses.add(new Pose(parts[0], Float.valueOf(parts[1]), Float.valueOf(parts[2])));
poses.put(parts[0], new Pose(parts[0], Float.valueOf(parts[1]), Float.valueOf(parts[2])));
} catch (NumberFormatException e) {
Messaging.logTr(Messages.SKIPPING_INVALID_POSE, sub.name(), e.getMessage());
}
}
public boolean removePose(Pose pose) {
if (poses.contains(pose)) {
poses.remove(pose);
return true;
}
return false;
public boolean removePose(String pose) {
return poses.remove(pose.toLowerCase()) != null;
}
@Override
@ -71,4 +64,28 @@ public class Poses extends Trait {
for (int i = 0; i < poses.size(); i++)
key.setString("list." + String.valueOf(i), poses.get(i).stringValue());
}
public void assumePose(String flag) {
Pose pose = poses.get(flag.toLowerCase());
assumePose(pose.getYaw(), pose.getPitch());
}
public boolean hasPose(String pose) {
return poses.containsKey(pose.toLowerCase());
}
public void describe(CommandSender sender, int page) throws CommandException {
Paginator paginator = new Paginator().header("Pose");
paginator.addLine("<e>Key: <a>ID <b>Name <c>Pitch/Yaw");
int i = 0;
for (Pose pose : poses.values()) {
String line = "<a>" + i + "<b> " + pose.getName() + "<c> " + pose.getPitch() + "/"
+ pose.getYaw();
paginator.addLine(line);
i++;
}
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
}

View File

@ -27,9 +27,9 @@ public class Util {
private Util() {
}
public static void assumePose(org.bukkit.entity.Entity entity, Pose pose) {
public static void assumePose(org.bukkit.entity.Entity entity, float yaw, float pitch) {
EntityLiving handle = ((CraftLivingEntity) entity).getHandle();
NMS.look(handle, pose.getYaw(), pose.getPitch());
NMS.look(handle, yaw,pitch);
}
public static void callCollisionEvent(NPC npc, net.minecraft.server.Entity entity) {