Properly remove wander waypoint marker

This commit is contained in:
fullwall 2020-01-12 00:33:43 +09:00
parent a0e3b23bc3
commit 41f4c6e1b4

View File

@ -32,289 +32,291 @@ import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Util;
/**
* A wandering waypoint provider that wanders between either a box centered at the current location or inside a region
* defined by a list of boxes.
* A wandering waypoint provider that wanders between either a box centered at
* the current location or inside a region defined by a list of boxes.
*/
public class WanderWaypointProvider
implements WaypointProvider, Supplier<PhTreeSolid<Boolean>>, Function<NPC, Location> {
private WanderGoal currentGoal;
@Persist
public int delay = -1;
private NPC npc;
private volatile boolean paused;
@Persist
private final List<Location> regionCentres = Lists.newArrayList();
private PhTreeSolid<Boolean> tree = PhTreeSolid.create(3);
@Persist
public int xrange = DEFAULT_XRANGE;
@Persist
public int yrange = DEFAULT_YRANGE;
implements WaypointProvider, Supplier<PhTreeSolid<Boolean>>, Function<NPC, Location> {
private WanderGoal currentGoal;
@Persist
public int delay = -1;
private NPC npc;
private volatile boolean paused;
@Persist
private final List<Location> regionCentres = Lists.newArrayList();
private PhTreeSolid<Boolean> tree = PhTreeSolid.create(3);
@Persist
public int xrange = DEFAULT_XRANGE;
@Persist
public int yrange = DEFAULT_YRANGE;
public void addRegionCentre(Location centre) {
regionCentres.add(centre);
recalculateTree();
}
public void addRegionCentre(Location centre) {
regionCentres.add(centre);
recalculateTree();
}
public void addRegionCentres(Collection<Location> centre) {
regionCentres.addAll(centre);
recalculateTree();
}
public void addRegionCentres(Collection<Location> centre) {
regionCentres.addAll(centre);
recalculateTree();
}
@Override
public Location apply(NPC npc) {
Location closestCentre = null;
double minDist = Double.MAX_VALUE;
for (Location centre : regionCentres) {
double d = centre.distanceSquared(npc.getStoredLocation());
if (d < minDist) {
minDist = d;
closestCentre = centre;
}
}
if (closestCentre != null) {
// TODO: should find closest edge block that is valid
return MinecraftBlockExaminer.findValidLocation(closestCentre, xrange, yrange);
}
return null;
}
@Override
public Location apply(NPC npc) {
Location closestCentre = null;
double minDist = Double.MAX_VALUE;
for (Location centre : regionCentres) {
double d = centre.distanceSquared(npc.getStoredLocation());
if (d < minDist) {
minDist = d;
closestCentre = centre;
}
}
if (closestCentre != null) {
// TODO: should find closest edge block that is valid
return MinecraftBlockExaminer.findValidLocation(closestCentre, xrange, yrange);
}
return null;
}
@Override
public WaypointEditor createEditor(final CommandSender sender, CommandContext args) {
return new WaypointEditor() {
boolean editingRegions = false;
EntityMarkers<Location> markers = new EntityMarkers<Location>();
@Override
public WaypointEditor createEditor(final CommandSender sender, CommandContext args) {
return new WaypointEditor() {
boolean editingRegions = false;
EntityMarkers<Location> markers = new EntityMarkers<Location>();
@Override
public void begin() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_BEGIN);
if (currentGoal != null) {
currentGoal.pause();
}
}
@Override
public void begin() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_BEGIN);
if (currentGoal != null) {
currentGoal.pause();
}
}
@Override
public void end() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_END);
editingRegions = false;
if (currentGoal != null) {
currentGoal.unpause();
}
}
@Override
public void end() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_END);
editingRegions = false;
if (currentGoal != null) {
currentGoal.unpause();
}
}
private String formatLoc(Location location) {
return String.format("[[%d]], [[%d]], [[%d]]", location.getBlockX(), location.getBlockY(),
location.getBlockZ());
}
private String formatLoc(Location location) {
return String.format("[[%d]], [[%d]], [[%d]]", location.getBlockX(), location.getBlockY(),
location.getBlockZ());
}
@EventHandler(ignoreCancelled = true)
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (!event.getPlayer().equals(sender))
return;
String message = event.getMessage().toLowerCase();
if (message.startsWith("xrange") || message.startsWith("yrange")) {
event.setCancelled(true);
int range = 0;
try {
range = Integer.parseInt(message.split(" ", 2)[1]);
if (range <= 0) {
range = 0;
}
if (message.startsWith("xrange")) {
xrange = range;
} else {
yrange = range;
}
if (currentGoal != null) {
currentGoal.setXYRange(xrange, yrange);
}
recalculateTree();
} catch (Exception ex) {
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_RANGE_SET, xrange, yrange);
}
});
} else if (message.startsWith("regions")) {
event.setCancelled(true);
editingRegions = !editingRegions;
if (editingRegions) {
for (Location regionCentre : regionCentres) {
Entity entity = markers.createMarker(regionCentre, regionCentre);
entity.setMetadata("wandermarker",
new FixedMetadataValue(CitizensAPI.getPlugin(), regionCentre));
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_REGION_EDITING_START);
}
});
} else {
markers.destroyMarkers();
}
} else if (message.startsWith("delay")) {
event.setCancelled(true);
try {
delay = Integer.parseInt(message.split(" ")[1]);
if (currentGoal != null) {
currentGoal.setDelay(delay);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@EventHandler(ignoreCancelled = true)
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (!event.getPlayer().equals(sender))
return;
String message = event.getMessage().toLowerCase();
if (message.startsWith("xrange") || message.startsWith("yrange")) {
event.setCancelled(true);
int range = 0;
try {
range = Integer.parseInt(message.split(" ", 2)[1]);
if (range <= 0) {
range = 0;
}
if (message.startsWith("xrange")) {
xrange = range;
} else {
yrange = range;
}
if (currentGoal != null) {
currentGoal.setXYRange(xrange, yrange);
}
recalculateTree();
} catch (Exception ex) {
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_RANGE_SET, xrange, yrange);
}
});
} else if (message.startsWith("regions")) {
event.setCancelled(true);
editingRegions = !editingRegions;
if (editingRegions) {
for (Location regionCentre : regionCentres) {
Entity entity = markers.createMarker(regionCentre, regionCentre);
entity.setMetadata("wandermarker",
new FixedMetadataValue(CitizensAPI.getPlugin(), regionCentre));
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_REGION_EDITING_START);
}
});
} else {
markers.destroyMarkers();
}
} else if (message.startsWith("delay")) {
event.setCancelled(true);
try {
delay = Integer.parseInt(message.split(" ")[1]);
if (currentGoal != null) {
currentGoal.setDelay(delay);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_DELAY_SET, delay);
}
});
} catch (
@Override
public void run() {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_DELAY_SET, delay);
}
});
} catch (
Exception e) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendErrorTr(sender, Messages.WANDER_WAYPOINTS_INVALID_DELAY);
}
});
}
}
}
Exception e) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
Messaging.sendErrorTr(sender, Messages.WANDER_WAYPOINTS_INVALID_DELAY);
}
});
}
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
if (!event.getPlayer().equals(sender) || event.getAction() == Action.PHYSICAL || !npc.isSpawned()
|| event.getPlayer().getWorld() != npc.getEntity().getWorld() || Util.isOffHand(event))
return;
if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) {
if (event.getClickedBlock() == null)
return;
event.setCancelled(true);
Location at = event.getClickedBlock().getLocation().add(0, 1, 0);
if (!regionCentres.contains(at)) {
regionCentres.add(at);
Entity entity = markers.createMarker(at, at);
entity.setMetadata("wandermarker", new FixedMetadataValue(CitizensAPI.getPlugin(), at));
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_ADDED_REGION, formatLoc(at),
regionCentres.size());
recalculateTree();
}
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
if (!event.getPlayer().equals(sender) || event.getAction() == Action.PHYSICAL || !npc.isSpawned()
|| !editingRegions || event.getPlayer().getWorld() != npc.getEntity().getWorld()
|| Util.isOffHand(event))
return;
if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) {
if (event.getClickedBlock() == null)
return;
event.setCancelled(true);
Location at = event.getClickedBlock().getLocation().add(0, 1, 0);
if (!regionCentres.contains(at)) {
regionCentres.add(at);
Entity entity = markers.createMarker(at, at);
entity.setMetadata("wandermarker", new FixedMetadataValue(CitizensAPI.getPlugin(), at));
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_ADDED_REGION, formatLoc(at),
regionCentres.size());
recalculateTree();
}
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (!sender.equals(event.getPlayer()) || !editingRegions || Util.isOffHand(event))
return;
if (!event.getRightClicked().hasMetadata("wandermarker"))
return;
regionCentres.remove(event.getRightClicked().getMetadata("wandermarker").get(0).value());
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_REMOVED_REGION,
formatLoc((Location) event.getRightClicked().getMetadata("wandermarker").get(0).value()),
regionCentres.size());
recalculateTree();
}
@EventHandler(ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (!sender.equals(event.getPlayer()) || !editingRegions || Util.isOffHand(event))
return;
if (!event.getRightClicked().hasMetadata("wandermarker"))
return;
regionCentres.remove(event.getRightClicked().getMetadata("wandermarker").get(0).value());
markers.removeMarker((Location) event.getRightClicked().getMetadata("wandermarker").get(0).value());
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_REMOVED_REGION,
formatLoc((Location) event.getRightClicked().getMetadata("wandermarker").get(0).value()),
regionCentres.size());
recalculateTree();
}
};
}
};
}
@Override
public PhTreeSolid<Boolean> get() {
return regionCentres.isEmpty() ? null : tree;
}
@Override
public PhTreeSolid<Boolean> get() {
return regionCentres.isEmpty() ? null : tree;
}
public List<Location> getRegionCentres() {
return new RecalculateList();
}
public List<Location> getRegionCentres() {
return new RecalculateList();
}
@Override
public boolean isPaused() {
return paused;
}
@Override
public boolean isPaused() {
return paused;
}
@Override
public void load(DataKey key) {
recalculateTree();
}
@Override
public void load(DataKey key) {
recalculateTree();
}
@Override
public void onRemove() {
npc.getDefaultGoalController().removeGoal(currentGoal);
}
@Override
public void onRemove() {
npc.getDefaultGoalController().removeGoal(currentGoal);
}
@Override
public void onSpawn(NPC npc) {
this.npc = npc;
if (currentGoal == null) {
currentGoal = WanderGoal.createWithNPCAndRangeAndTreeAndFallback(npc, xrange, yrange,
WanderWaypointProvider.this, WanderWaypointProvider.this);
currentGoal.setDelay(delay);
}
npc.getDefaultGoalController().addGoal(currentGoal, 1);
}
@Override
public void onSpawn(NPC npc) {
this.npc = npc;
if (currentGoal == null) {
currentGoal = WanderGoal.createWithNPCAndRangeAndTreeAndFallback(npc, xrange, yrange,
WanderWaypointProvider.this, WanderWaypointProvider.this);
currentGoal.setDelay(delay);
}
npc.getDefaultGoalController().addGoal(currentGoal, 1);
}
private void recalculateTree() {
tree = PhTreeSolid.create(3);
for (Location loc : regionCentres) {
long[] lower = { loc.getBlockX() - xrange, loc.getBlockY() - yrange, loc.getBlockZ() - xrange };
long[] upper = { loc.getBlockX() + xrange, loc.getBlockY() + yrange, loc.getBlockZ() + xrange };
tree.put(lower, upper, true);
}
}
private void recalculateTree() {
tree = PhTreeSolid.create(3);
for (Location loc : regionCentres) {
long[] lower = { loc.getBlockX() - xrange, loc.getBlockY() - yrange, loc.getBlockZ() - xrange };
long[] upper = { loc.getBlockX() + xrange, loc.getBlockY() + yrange, loc.getBlockZ() + xrange };
tree.put(lower, upper, true);
}
}
public void removeRegionCentre(Location centre) {
regionCentres.remove(centre);
recalculateTree();
}
public void removeRegionCentre(Location centre) {
regionCentres.remove(centre);
recalculateTree();
}
public void removeRegionCentres(Collection<Location> centre) {
regionCentres.removeAll(centre);
recalculateTree();
}
public void removeRegionCentres(Collection<Location> centre) {
regionCentres.removeAll(centre);
recalculateTree();
}
@Override
public void save(DataKey key) {
}
@Override
public void save(DataKey key) {
}
@Override
public void setPaused(boolean paused) {
this.paused = paused;
}
@Override
public void setPaused(boolean paused) {
this.paused = paused;
}
private class RecalculateList extends ForwardingList<Location> {
@Override
public void add(int idx, Location loc) {
super.add(idx, loc);
recalculateTree();
}
private class RecalculateList extends ForwardingList<Location> {
@Override
public void add(int idx, Location loc) {
super.add(idx, loc);
recalculateTree();
}
@Override
public boolean add(Location loc) {
boolean val = super.add(loc);
recalculateTree();
return val;
}
@Override
public boolean add(Location loc) {
boolean val = super.add(loc);
recalculateTree();
return val;
}
@Override
protected List<Location> delegate() {
return regionCentres;
}
@Override
protected List<Location> delegate() {
return regionCentres;
}
@Override
public Location remove(int idx) {
Location val = super.remove(idx);
recalculateTree();
return val;
}
@Override
public Location remove(int idx) {
Location val = super.remove(idx);
recalculateTree();
return val;
}
@Override
public Location set(int idx, Location idx2) {
Location val = super.set(idx, idx2);
recalculateTree();
return val;
}
}
@Override
public Location set(int idx, Location idx2) {
Location val = super.set(idx, idx2);
recalculateTree();
return val;
}
}
private static final int DEFAULT_XRANGE = 3;
private static final int DEFAULT_YRANGE = 25;
private static final int DEFAULT_XRANGE = 3;
private static final int DEFAULT_YRANGE = 25;
}