mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-18 13:21:45 +01:00
Add a preliminary wanderable regions mode to the wander waypoint provider
This commit is contained in:
parent
e20e45169a
commit
f03a376db5
@ -1,25 +1,41 @@
|
|||||||
package net.citizensnpcs.trait.waypoint;
|
package net.citizensnpcs.trait.waypoint;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Location;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.block.Action;
|
||||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
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.CitizensAPI;
|
||||||
import net.citizensnpcs.api.ai.Goal;
|
|
||||||
import net.citizensnpcs.api.ai.goals.WanderGoal;
|
import net.citizensnpcs.api.ai.goals.WanderGoal;
|
||||||
import net.citizensnpcs.api.command.CommandContext;
|
import net.citizensnpcs.api.command.CommandContext;
|
||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
import net.citizensnpcs.api.persistence.Persist;
|
import net.citizensnpcs.api.persistence.Persist;
|
||||||
import net.citizensnpcs.api.util.DataKey;
|
import net.citizensnpcs.api.util.DataKey;
|
||||||
import net.citizensnpcs.api.util.Messaging;
|
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;
|
import net.citizensnpcs.util.Messages;
|
||||||
|
|
||||||
public class WanderWaypointProvider implements WaypointProvider {
|
public class WanderWaypointProvider implements WaypointProvider, Supplier<QuadTree> {
|
||||||
private Goal currentGoal;
|
private WanderGoal currentGoal;
|
||||||
private NPC npc;
|
private NPC npc;
|
||||||
private volatile boolean paused;
|
private volatile boolean paused;
|
||||||
@Persist
|
@Persist
|
||||||
|
private final List<Location> regionCentres = Lists.newArrayList();
|
||||||
|
private QuadTree tree = new QuadTree();
|
||||||
|
@Persist
|
||||||
public int xrange = DEFAULT_XRANGE;
|
public int xrange = DEFAULT_XRANGE;
|
||||||
@Persist
|
@Persist
|
||||||
public int yrange = DEFAULT_YRANGE;
|
public int yrange = DEFAULT_YRANGE;
|
||||||
@ -27,14 +43,29 @@ public class WanderWaypointProvider implements WaypointProvider {
|
|||||||
@Override
|
@Override
|
||||||
public WaypointEditor createEditor(final CommandSender sender, CommandContext args) {
|
public WaypointEditor createEditor(final CommandSender sender, CommandContext args) {
|
||||||
return new WaypointEditor() {
|
return new WaypointEditor() {
|
||||||
|
boolean editingRegions = false;
|
||||||
|
EntityMarkers<Location> markers = new EntityMarkers<Location>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void begin() {
|
public void begin() {
|
||||||
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_BEGIN);
|
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_BEGIN);
|
||||||
|
if (currentGoal != null) {
|
||||||
|
currentGoal.pause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void end() {
|
public void end() {
|
||||||
Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_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)
|
@EventHandler(ignoreCancelled = true)
|
||||||
@ -55,6 +86,10 @@ public class WanderWaypointProvider implements WaypointProvider {
|
|||||||
} else {
|
} else {
|
||||||
yrange = range;
|
yrange = range;
|
||||||
}
|
}
|
||||||
|
if (currentGoal != null) {
|
||||||
|
currentGoal.setXYRange(xrange, yrange);
|
||||||
|
}
|
||||||
|
recalculateTree();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
}
|
}
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
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);
|
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
|
@Override
|
||||||
public boolean isPaused() {
|
public boolean isPaused() {
|
||||||
return paused;
|
return paused;
|
||||||
@ -76,6 +168,7 @@ public class WanderWaypointProvider implements WaypointProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load(DataKey key) {
|
public void load(DataKey key) {
|
||||||
|
recalculateTree();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -87,11 +180,19 @@ public class WanderWaypointProvider implements WaypointProvider {
|
|||||||
public void onSpawn(NPC npc) {
|
public void onSpawn(NPC npc) {
|
||||||
this.npc = npc;
|
this.npc = npc;
|
||||||
if (currentGoal == null) {
|
if (currentGoal == null) {
|
||||||
currentGoal = WanderGoal.createWithNPCAndRange(npc, xrange, yrange);
|
currentGoal = WanderGoal.createWithNPCAndRangeAndTree(npc, xrange, yrange, WanderWaypointProvider.this);
|
||||||
}
|
}
|
||||||
npc.getDefaultGoalController().addGoal(currentGoal, 1);
|
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
|
@Override
|
||||||
public void save(DataKey key) {
|
public void save(DataKey key) {
|
||||||
}
|
}
|
||||||
|
@ -275,9 +275,12 @@ public class Messages {
|
|||||||
public static final String UNKNOWN_MATERIAL = "citizens.commands.npc.item.unknown-material";
|
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_SET = "citizens.commands.npc.vulnerable.set";
|
||||||
public static final String VULNERABLE_STOPPED = "citizens.commands.npc.vulnerable.stopped";
|
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_BEGIN = "citizens.editors.waypoints.wander.begin";
|
||||||
public static final String WANDER_WAYPOINTS_END = "citizens.editors.waypoints.wander.end";
|
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_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_PROVIDER_SET = "citizens.waypoints.set-provider";
|
||||||
public static final String WAYPOINT_TELEPORTING_DISABLED = "citizens.commands.waypoints.disableteleporting.disabled";
|
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";
|
public static final String WAYPOINT_TRIGGER_ADD_PROMPT = "citizens.editors.waypoints.triggers.add.prompt";
|
||||||
|
@ -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.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.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.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.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.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.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.
|
citizens.editors.waypoints.guided.added-guide=Added a [[guide]] waypoint. This will guide NPCs to their destination.
|
||||||
|
Loading…
Reference in New Issue
Block a user