Fix wander waypoint provider, stack overflow bug

This commit is contained in:
fullwall 2012-09-30 23:13:49 +08:00
parent 7d6dc1c40a
commit 9719873ffe
5 changed files with 138 additions and 10 deletions

View File

@ -381,7 +381,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
}
}
if (!closest.isEmpty()) {
sender.sendMessage(ChatColor.GRAY + Translator.tr(Messages.UNKNOWN_COMMAND));
sender.sendMessage(ChatColor.GRAY + Messaging.tr(Messages.UNKNOWN_COMMAND));
sender.sendMessage(StringHelper.wrap(" /") + command + " " + StringHelper.wrap(closest));
return true;
}

View File

@ -14,7 +14,7 @@ import net.citizensnpcs.util.StringHelper;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
@Requirements(ownership = true, selected = true, traits = Waypoints.class)
@Requirements(ownership = true, selected = true)
public class WaypointCommands {
public WaypointCommands(Citizens plugin) {
}
@ -26,6 +26,7 @@ public class WaypointCommands {
modifiers = { "provider" },
min = 1,
max = 2,
flags = "a",
permission = "waypoints.provider")
public void provider(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Waypoints waypoints = npc.getTrait(Waypoints.class);

View File

@ -0,0 +1,130 @@
package net.citizensnpcs.trait.waypoint;
import java.util.Random;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.Goal;
import net.citizensnpcs.api.ai.GoalSelector;
import net.citizensnpcs.api.ai.event.NavigationCompleteEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.editor.Editor;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
public class WanderWaypointProvider implements WaypointProvider {
private WanderGoal currentGoal;
private NPC npc;
private volatile boolean paused;
private int xrange, yrange;
@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 paused;
}
@Override
public void load(DataKey key) {
xrange = key.getInt("xrange", DEFAULT_XRANGE);
yrange = key.getInt("yrange", DEFAULT_YRANGE);
}
@Override
public void onSpawn(NPC npc) {
this.npc = npc;
if (currentGoal == null) {
currentGoal = new WanderGoal();
CitizensAPI.registerEvents(currentGoal);
}
npc.getDefaultGoalController().addGoal(currentGoal, 1);
}
@Override
public void save(DataKey key) {
key.setInt("xrange", xrange);
key.setInt("yrange", yrange);
}
@Override
public void setPaused(boolean paused) {
this.paused = paused;
}
private class WanderGoal implements Goal {
private final Random random = new Random();
private GoalSelector selector;
private Location findRandomPosition() {
Location base = npc.getBukkitEntity().getLocation();
Location found = null;
int range = 10;
int yrange = 2;
for (int i = 0; i < 10; i++) {
int x = base.getBlockX() + random.nextInt(2 * range) - range;
int y = base.getBlockY() + random.nextInt(2 * yrange) - yrange;
int z = base.getBlockZ() + random.nextInt(2 * range) - range;
Block block = base.getWorld().getBlockAt(x, y, z);
if (block.isEmpty() && block.getRelative(BlockFace.DOWN).isEmpty()) {
found = block.getLocation();
break;
}
}
return found;
}
@EventHandler
public void onFinish(NavigationCompleteEvent event) {
if (selector != null)
selector.finish();
}
@Override
public void reset() {
selector = null;
}
@Override
public void run(GoalSelector selector) {
if (!npc.getNavigator().isNavigating())
selector.finish();
}
@Override
public boolean shouldExecute(GoalSelector selector) {
if (!npc.isSpawned() || paused || npc.getNavigator().isNavigating())
return false;
Location dest = findRandomPosition();
if (dest == null)
return false;
npc.getNavigator().setTarget(dest);
this.selector = selector;
return true;
}
}
private static final int DEFAULT_XRANGE = 3;
private static final int DEFAULT_YRANGE = 25;
}

View File

@ -26,11 +26,10 @@ public class Waypoints extends Trait {
}
private WaypointProvider create(Class<? extends WaypointProvider> clazz) {
if (!providers.containsKey(clazz))
return null;
try {
return clazz.newInstance();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
@ -109,6 +108,8 @@ public class Waypoints extends Trait {
if (provider == null)
return false;
providerName = name;
if (npc != null && npc.isSpawned())
provider.onSpawn(npc);
return true;
}
@ -129,6 +130,6 @@ public class Waypoints extends Trait {
static {
providers.put("linear", LinearWaypointProvider.class);
providers.put("wander", WanderingWaypointProvider.class);
providers.put("wander", WanderWaypointProvider.class);
}
}

View File

@ -99,11 +99,7 @@ public class Translator {
instance = new Translator(resourceFile, locale);
}
public static String tr(String key) {
return tr(key);
}
public static String tr(String key, Object... msg) {
static String tr(String key, Object... msg) {
return msg.length == 0 ? instance.translate(key) : instance.format(key, msg);
}
}