Add a preliminary wanderable regions mode to the wander waypoint provider

This commit is contained in:
fullwall 2018-03-15 18:25:24 +08:00
parent e20e45169a
commit f03a376db5
3 changed files with 113 additions and 6 deletions

View File

@ -1,25 +1,41 @@
package net.citizensnpcs.trait.waypoint;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.metadata.FixedMetadataValue;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.Goal;
import net.citizensnpcs.api.ai.goals.WanderGoal;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.cuboid.QuadCuboid;
import net.citizensnpcs.api.util.cuboid.QuadTree;
import net.citizensnpcs.util.Messages;
public class WanderWaypointProvider implements WaypointProvider {
private Goal currentGoal;
public class WanderWaypointProvider implements WaypointProvider, Supplier<QuadTree> {
private WanderGoal currentGoal;
private NPC npc;
private volatile boolean paused;
@Persist
private final List<Location> regionCentres = Lists.newArrayList();
private QuadTree tree = new QuadTree();
@Persist
public int xrange = DEFAULT_XRANGE;
@Persist
public int yrange = DEFAULT_YRANGE;
@ -27,14 +43,29 @@ public class WanderWaypointProvider implements WaypointProvider {
@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 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());
}
@EventHandler(ignoreCancelled = true)
@ -55,6 +86,10 @@ public class WanderWaypointProvider implements WaypointProvider {
} else {
yrange = range;
}
if (currentGoal != null) {
currentGoal.setXYRange(xrange, yrange);
}
recalculateTree();
} catch (Exception ex) {
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@ -63,12 +98,69 @@ public class WanderWaypointProvider implements WaypointProvider {
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_RANGE_SET, xrange, yrange);
}
});
} else if (message.startsWith("")) {
} 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();
}
}
}
@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()
|| event.getHand() == EquipmentSlot.OFF_HAND)
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 || event.getHand() == EquipmentSlot.OFF_HAND)
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();
}
};
}
@Override
public QuadTree get() {
return regionCentres.isEmpty() ? null : tree;
}
@Override
public boolean isPaused() {
return paused;
@ -76,6 +168,7 @@ public class WanderWaypointProvider implements WaypointProvider {
@Override
public void load(DataKey key) {
recalculateTree();
}
@Override
@ -87,11 +180,19 @@ public class WanderWaypointProvider implements WaypointProvider {
public void onSpawn(NPC npc) {
this.npc = npc;
if (currentGoal == null) {
currentGoal = WanderGoal.createWithNPCAndRange(npc, xrange, yrange);
currentGoal = WanderGoal.createWithNPCAndRangeAndTree(npc, xrange, yrange, WanderWaypointProvider.this);
}
npc.getDefaultGoalController().addGoal(currentGoal, 1);
}
private void recalculateTree() {
tree = new QuadTree();
for (Location loc : regionCentres) {
tree.insert(new QuadCuboid(loc.getBlockX() - xrange, loc.getBlockY() - yrange, loc.getBlockZ() - xrange,
loc.getBlockX() + xrange, loc.getBlockY() + yrange, loc.getBlockZ() + xrange));
}
}
@Override
public void save(DataKey key) {
}

View File

@ -275,9 +275,12 @@ public class Messages {
public static final String UNKNOWN_MATERIAL = "citizens.commands.npc.item.unknown-material";
public static final String VULNERABLE_SET = "citizens.commands.npc.vulnerable.set";
public static final String VULNERABLE_STOPPED = "citizens.commands.npc.vulnerable.stopped";
public static final String WANDER_WAYPOINTS_ADDED_REGION = "citizens.editors.waypoints.wander.added-region";
public static final String WANDER_WAYPOINTS_BEGIN = "citizens.editors.waypoints.wander.begin";
public static final String WANDER_WAYPOINTS_END = "citizens.editors.waypoints.wander.end";
public static final String WANDER_WAYPOINTS_RANGE_SET = "citizens.editors.waypoints.wander.range-set";
public static final String WANDER_WAYPOINTS_REGION_EDITING_START = "citizens.editors.waypoints.wander.editing-regions";
public static final String WANDER_WAYPOINTS_REMOVED_REGION = "citizens.editors.waypoints.wander.removed-region";
public static final String WAYPOINT_PROVIDER_SET = "citizens.waypoints.set-provider";
public static final String WAYPOINT_TELEPORTING_DISABLED = "citizens.commands.waypoints.disableteleporting.disabled";
public static final String WAYPOINT_TRIGGER_ADD_PROMPT = "citizens.editors.waypoints.triggers.add.prompt";

View File

@ -224,8 +224,11 @@ citizens.editors.text.removed-entry=[[Removed]] entry at index [[{0}]].
citizens.editors.text.start-prompt=Type [[add]] to add an entry, [[edit]] to edit entries, [[remove]] to remove entries, [[close]] to toggle the NPC as a close talker, [[item]] to set the item in hand pattern, [[range]] to set the talking range, [[delay]] to set the talking delay in ticks and [[random]] to toggle the NPC as a random talker. Type [[help]] to show this again.
citizens.editors.text.talk-item-set=[[Talk item pattern]] set to [[{0}]].
citizens.editors.waypoints.wander.range-set=Wander range set to xrange [[{0}]] and yrange [[{1}]].
citizens.editors.waypoints.wander.begin=<b>Entered the wander waypoint editor.<br> Type [[xrange <number>]] or [[yrange <number>]] to modify the random wander range.
citizens.editors.waypoints.wander.begin=<b>Entered the wander waypoint editor.<br> Type [[xrange <number>]] or [[yrange <number>]] to modify the random wander range. Type [[regions]] to enter the region editor.
citizens.editors.waypoints.wander.end=Exited the wander waypoint editor.
citizens.editors.waypoints.wander.added-region=[[Added]] wanderable region at ({0}) ([[{1}]]).
citizens.editors.waypoints.wander.removed-region=[[Removed]] wanderable region at ({0}) ([[{1}]] remaining).
citizens.editors.waypoints.wander.editing-regions=Now editing regions!<br> [[Left click]] to add a new wanderable region using the xrange/yrange box centred at that block.<br> [[Right click]] an existing marker to remove that region.<br> Type [[regions]] to stop or simply exit the editor. Regions should be overlapping with each other.
citizens.editors.waypoints.guided.end=Exited the guided waypoint editor.
citizens.editors.waypoints.guided.begin=<b>Entered the guided waypoint editor!<br> [[Left click]] to add a waypoint guide, [[right click]] an existing waypoint to remove.<br> [[Sneak]] while left clicking to add a destination waypoint.<br> Type [[toggle path]] to toggle showing entities at waypoints.
citizens.editors.waypoints.guided.added-guide=Added a [[guide]] waypoint. This will guide NPCs to their destination.