C:/Program Files/Git/npc speed [speed] and speed/range saving

This commit is contained in:
fullwall 2012-08-03 22:30:11 +08:00
parent ec9962b36f
commit 3a03525a41
10 changed files with 148 additions and 18 deletions

View File

@ -484,6 +484,21 @@ public class NPCCommands {
}
}
@Command(
aliases = { "npc" },
usage = "speed [speed]",
desc = "Sets the movement speed of an NPC",
modifiers = { "speed" },
min = 2,
max = 2,
permission = "npc.speed")
public void speed(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
float newSpeed = (float) args.getDouble(1);
npc.getNavigator().setSpeed(newSpeed);
Messaging.sendF(sender, ChatColor.GREEN + "NPC speed set to %f.", newSpeed);
}
@Command(
aliases = { "npc" },
usage = "tp",

View File

@ -98,12 +98,15 @@ public abstract class CitizensNPC extends AbstractNPC {
if (spawnLoc != null)
spawn(spawnLoc);
}
navigator.load(root.getRelative("navigator"));
}
public void save(DataKey root) {
root.setString("name", getFullName());
metadata.saveTo(root.getRelative("metadata"));
navigator.save(root.getRelative("navigator"));
// Save all existing traits
for (Trait trait : traits.values()) {

View File

@ -7,6 +7,7 @@ import net.citizensnpcs.api.ai.TargetType;
import net.citizensnpcs.api.ai.event.NavigationBeginEvent;
import net.citizensnpcs.api.ai.event.NavigationCancelEvent;
import net.citizensnpcs.api.ai.event.NavigationReplaceEvent;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.util.NMSReflection;
@ -62,12 +63,22 @@ public class CitizensNavigator implements Navigator {
return executing != null;
}
public void load(DataKey root) {
speed = (float) root.getDouble("speed", speed);
pathfindingRange = (float) root.getDouble("pathfinding-range", pathfindingRange);
}
public void onSpawn() {
if (speed == -1)
this.speed = NMSReflection.getSpeedFor(npc.getHandle());
updatePathfindingRange();
}
public void save(DataKey root) {
root.setDouble("speed", speed);
root.setDouble("pathfinding-range", pathfindingRange);
}
@Override
public void setPathfindingRange(float newRange) {
pathfindingRange = newRange;

View File

@ -1,8 +1,9 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.npc.NPC;
@ -20,9 +21,11 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoint> {
public class LinearWaypointProvider implements WaypointProvider {
private WaypointGoal currentGoal;
private NPC npc;
private final List<Waypoint> waypoints = Lists.newArrayList();
@ -113,11 +116,6 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
return currentGoal.isPaused();
}
@Override
public Iterator<Waypoint> iterator() {
return waypoints.iterator();
}
@Override
public void load(DataKey key) {
for (DataKey root : key.getRelative("points").getIntegerSubKeys()) {
@ -132,7 +130,8 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
public void onSpawn(NPC npc) {
this.npc = npc;
if (currentGoal == null) {
currentGoal = new WaypointGoal(this, npc.getNavigator());
Iterable<Location> provider = Iterables.transform(waypoints, WAYPOINT_TRANSFORMER);
currentGoal = new WaypointGoal(provider, npc.getNavigator());
CitizensAPI.registerEvents(currentGoal);
}
npc.getDefaultGoalController().addGoal(currentGoal, 1);
@ -158,4 +157,11 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
public void setPaused(boolean paused) {
currentGoal.setPaused(paused);
}
private static final Function<Waypoint, Location> WAYPOINT_TRANSFORMER = new Function<Waypoint, Location>() {
@Override
public Location apply(@Nullable Waypoint input) {
return input == null ? null : input.getLocation();
}
};
}

View File

@ -0,0 +1,24 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Iterator;
import org.bukkit.Location;
public class RandomPointFinder implements Iterator<Location> {
@Override
public boolean hasNext() {
return true;
}
@Override
public Location next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,67 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Iterator;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.editor.Editor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class WanderingWaypointProvider implements WaypointProvider, Iterable<Location> {
private WaypointGoal currentGoal;
private final Iterator<Location> iterator = new RandomPointFinder();
private NPC npc;
@Override
public Editor createEditor(Player player) {
return new Editor() {
@Override
public void begin() {
// TODO Auto-generated method stub
}
@Override
public void end() {
// TODO Auto-generated method stub
}
};
}
@Override
public boolean isPaused() {
return currentGoal.isPaused();
}
@Override
public void load(DataKey key) {
}
@Override
public void onSpawn(NPC npc) {
this.npc = npc;
if (currentGoal == null) {
currentGoal = new WaypointGoal(this, npc.getNavigator());
CitizensAPI.registerEvents(currentGoal);
}
npc.getDefaultGoalController().addGoal(currentGoal, 1);
}
@Override
public void save(DataKey key) {
}
@Override
public void setPaused(boolean paused) {
currentGoal.setPaused(paused);
}
@Override
public Iterator<Location> iterator() {
return iterator;
}
}

View File

@ -13,13 +13,13 @@ import org.bukkit.event.EventHandler;
public class WaypointGoal implements Goal {
private Location currentDestination;
private Iterator<Waypoint> itr;
private Iterator<Location> itr;
private final Navigator navigator;
private boolean paused;
private final Iterable<Waypoint> provider;
private final Iterable<Location> provider;
private GoalSelector selector;
public WaypointGoal(Iterable<Waypoint> provider, Navigator navigator) {
public WaypointGoal(Iterable<Location> provider, Navigator navigator) {
this.provider = provider;
this.navigator = navigator;
}
@ -80,7 +80,7 @@ public class WaypointGoal implements Goal {
boolean shouldExecute = itr.hasNext();
if (shouldExecute) {
this.selector = selector;
currentDestination = itr.next().getLocation();
currentDestination = itr.next();
navigator.setTarget(currentDestination);
}
return shouldExecute;

View File

@ -104,5 +104,6 @@ public class Waypoints extends Trait {
static {
providers.put(LinearWaypointProvider.class, "linear");
providers.put(WanderingWaypointProvider.class, "wander");
}
}

View File

@ -47,9 +47,7 @@ public class Messaging {
}
public static void send(CommandSender sender, Object... msg) {
String joined = SPACE.join(msg);
joined = StringHelper.parseColors(joined);
sender.sendMessage(joined);
sendMessageTo(sender, SPACE.join(msg));
}
public static void sendError(CommandSender sender, Object... msg) {
@ -61,9 +59,12 @@ public class Messaging {
}
public static void sendF(CommandSender sender, Object... msg) {
String joined = getFormatted(msg);
joined = StringHelper.parseColors(joined);
sender.sendMessage(joined);
sendMessageTo(sender, getFormatted(msg));
}
private static void sendMessageTo(CommandSender sender, String msg) {
msg = StringHelper.parseColors(msg);
sender.sendMessage(msg);
}
public static void sendWithNPC(CommandSender sender, Object msg, NPC npc) {

View File

@ -88,6 +88,7 @@ public class NMSReflection {
}
throw new IllegalArgumentException("unable to find valid entity superclass");
}
public static void stopNetworkThreads(NetworkManager manager) {
if (THREAD_STOPPER == null)
return;
@ -96,6 +97,7 @@ public class NMSReflection {
} catch (Exception e) {
}
}
public static void updatePathfindingRange(CitizensNPC npc, float pathfindingRange) {
if (PATHFINDING_RANGE == null)
return;