From 4cf042eb3f789a6592ddce1202f297b0194ddbe8 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 27 Oct 2024 20:14:43 +0800 Subject: [PATCH] Add /npc pausepathfinding --lockout, fix /npc text speech bubbles duration --- main/pom.xml | 8 ++-- .../citizensnpcs/commands/NPCCommands.java | 19 +++++---- .../trait/PausePathfindingTrait.java | 40 +++++++++++++++---- .../net/citizensnpcs/trait/text/Text.java | 3 +- .../trait/text/TextBasePrompt.java | 3 +- .../java/net/citizensnpcs/util/Messages.java | 1 + main/src/main/resources/en.json | 1 + main/src/main/resources/plugin.yml | 5 ++- 8 files changed, 57 insertions(+), 23 deletions(-) diff --git a/main/pom.xml b/main/pom.xml index b7c64a878..e478616e8 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -11,7 +11,7 @@ citizens-main UTF-8 - 1.21.2-R0.1-SNAPSHOT + 1.21.3-R0.1-SNAPSHOT 2.11.5 ${project.version} 7.1.0-SNAPSHOT @@ -57,7 +57,7 @@ it.unimi.dsi fastutil - 8.5.14 + 8.5.15 provided @@ -69,13 +69,13 @@ ch.ethz.globis.phtree phtree - 2.8.0 + 2.8.1 provided org.joml joml - 1.10.5 + 1.10.8 provided diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 3ffbf321a..4f5130a0a 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -2335,7 +2335,7 @@ public class NPCCommands { @Command( aliases = { "npc" }, - usage = "pausepathfinding --onrightclick [true|false] --when-player-within [range in blocks] --pauseticks [ticks]", + usage = "pausepathfinding --onrightclick [true|false] --when-player-within [range in blocks] --pauseduration [duration] --lockoutduration [duration]", desc = "", modifiers = { "pausepathfinding" }, min = 1, @@ -2343,23 +2343,28 @@ public class NPCCommands { permission = "citizens.npc.pausepathfinding") public void pausepathfinding(CommandContext args, CommandSender sender, NPC npc, @Flag("onrightclick") Boolean rightclick, @Flag("when-player-within") Double playerRange, - @Flag("pauseticks") Integer ticks) throws CommandException { + @Flag("pauseduration") Duration pauseDuration, @Flag("lockoutduration") Duration lockoutDuration) + throws CommandException { PausePathfindingTrait trait = npc.getOrAddTrait(PausePathfindingTrait.class); if (playerRange != null) { if (playerRange <= 0) throw new CommandException("Invalid range"); - trait.setPlayerRangeBlocks(playerRange); + trait.setPlayerRange(playerRange); Messaging.sendTr(sender, Messages.PAUSEPATHFINDING_RANGE_SET, npc.getName(), playerRange); } if (rightclick != null) { - trait.setRightClick(rightclick); + trait.setPauseOnRightClick(rightclick); Messaging.sendTr(sender, rightclick ? Messages.PAUSEPATHFINDING_RIGHTCLICK_SET : Messages.PAUSEPATHFINDING_RIGHTCLICK_UNSET, npc.getName()); } - if (ticks != null) { - trait.setPauseTicks(ticks); - Messaging.sendTr(sender, Messages.PAUSEPATHFINDING_TICKS_SET, npc.getName(), ticks); + if (lockoutDuration != null) { + trait.setLockoutDuration(Util.toTicks(lockoutDuration)); + Messaging.sendTr(sender, Messages.PAUSEPATHFINDING_LOCKOUT_DURATION_SET, npc.getName(), lockoutDuration); + } + if (pauseDuration != null) { + trait.setPauseDuration(Util.toTicks(pauseDuration)); + Messaging.sendTr(sender, Messages.PAUSEPATHFINDING_TICKS_SET, npc.getName(), pauseDuration); } } diff --git a/main/src/main/java/net/citizensnpcs/trait/PausePathfindingTrait.java b/main/src/main/java/net/citizensnpcs/trait/PausePathfindingTrait.java index 4860d5b2b..b172c2529 100644 --- a/main/src/main/java/net/citizensnpcs/trait/PausePathfindingTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/PausePathfindingTrait.java @@ -12,21 +12,36 @@ import net.citizensnpcs.util.NMS; @TraitName("pausepathfinding") public class PausePathfindingTrait extends Trait { + @Persist("lockoutduration") + private int lockoutDuration = -1; @Persist("pauseticks") private int pauseTicks; @Persist("playerrange") private double playerRange = -1; @Persist("rightclick") private boolean rightclick; + private int t; private int unpauseTaskId = -1; public PausePathfindingTrait() { super("pausepathfinding"); } + public int getLockoutDuration() { + return lockoutDuration; + } + + public int getPauseDuration() { + return pauseTicks; + } + + public double getPlayerRangeInBlocks() { + return playerRange; + } + @EventHandler(ignoreCancelled = true) public void onInteract(NPCRightClickEvent event) { - if (!rightclick || event.getNPC() != npc) + if (lockoutDuration > t || !rightclick || event.getNPC() != npc) return; pause(); event.setDelayedCancellation(true); @@ -42,27 +57,38 @@ public class PausePathfindingTrait extends Trait { NMS.setPitch(npc.getEntity(), 0); npc.getNavigator().setPaused(false); }, pauseTicks <= 0 ? 20 : pauseTicks); + t = 0; + } + + public boolean pauseOnRightClick() { + return rightclick; } @Override public void run() { - if (playerRange == -1 || !npc.isSpawned() || unpauseTaskId == -1 && !npc.getNavigator().isNavigating()) + if (lockoutDuration > t++ || playerRange == -1 || !npc.isSpawned() + || unpauseTaskId == -1 && !npc.getNavigator().isNavigating()) return; + if (CitizensAPI.getLocationLookup() .getNearbyVisiblePlayers(npc.getEntity(), npc.getStoredLocation(), playerRange).iterator().hasNext()) { pause(); } } - public void setPauseTicks(int pauseTicks) { - this.pauseTicks = pauseTicks; + public void setLockoutDuration(int ticks) { + this.lockoutDuration = ticks; } - public void setPlayerRangeBlocks(double range) { - playerRange = range; + public void setPauseDuration(int ticks) { + this.pauseTicks = ticks; } - public void setRightClick(boolean rightclick) { + public void setPauseOnRightClick(boolean rightclick) { this.rightclick = rightclick; } + + public void setPlayerRange(double blockRange) { + playerRange = blockRange; + } } \ No newline at end of file diff --git a/main/src/main/java/net/citizensnpcs/trait/text/Text.java b/main/src/main/java/net/citizensnpcs/trait/text/Text.java index 3a67b7437..1c4078b76 100644 --- a/main/src/main/java/net/citizensnpcs/trait/text/Text.java +++ b/main/src/main/java/net/citizensnpcs/trait/text/Text.java @@ -1,7 +1,6 @@ package net.citizensnpcs.trait.text; import java.time.Duration; -import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -253,7 +252,7 @@ public class Text extends Trait implements Runnable, Listener { } public void setSpeechBubbleDuration(Duration duration) { - this.speechBubbleDuration = (int) (duration.get(ChronoUnit.MILLIS) / 50); + this.speechBubbleDuration = Util.toTicks(duration); } /** diff --git a/main/src/main/java/net/citizensnpcs/trait/text/TextBasePrompt.java b/main/src/main/java/net/citizensnpcs/trait/text/TextBasePrompt.java index 646f5a35a..f67a4907f 100644 --- a/main/src/main/java/net/citizensnpcs/trait/text/TextBasePrompt.java +++ b/main/src/main/java/net/citizensnpcs/trait/text/TextBasePrompt.java @@ -1,5 +1,6 @@ package net.citizensnpcs.trait.text; +import java.time.DateTimeException; import java.time.Duration; import java.util.Arrays; @@ -86,7 +87,7 @@ public class TextBasePrompt extends StringPrompt { null); text.setSpeechBubbleDuration(duration); Messaging.sendErrorTr(sender, Messages.SPEECH_BUBBLES_DURATION_SET, duration); - } catch (Exception exception) { + } catch (DateTimeException ex) { Messaging.sendErrorTr(sender, Messages.INVALID_SPEECH_BUBBLES_DURATION); } } else if (input.equalsIgnoreCase("close") || original.trim().equalsIgnoreCase("talk close")) { diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index 091e042c5..40207cccb 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -339,6 +339,7 @@ public class Messages { public static final String PATHFINDING_OPTIONS_STATIONARY_TICKS_SET = "citizens.commands.npc.pathopt.stationary-ticks-set"; public static final String PATHFINDING_OPTIONS_USE_NEW_FINDER = "citizens.commands.npc.pathopt.use-new-finder"; public static final String PATHFINDING_RANGE_SET = "citizens.commands.npc.pathfindingrange.set"; + public static final String PAUSEPATHFINDING_LOCKOUT_DURATION_SET = "citizens.commands.npc.pausepathfinding.lockout-duration-set"; public static final String PAUSEPATHFINDING_RANGE_SET = "citizens.commands.npc.pausepathfinding.pause-range-set"; public static final String PAUSEPATHFINDING_RIGHTCLICK_SET = "citizens.commands.npc.pausepathfinding.rightclick-set"; public static final String PAUSEPATHFINDING_RIGHTCLICK_UNSET = "citizens.commands.npc.pausepathfinding.rightclick-unset"; diff --git a/main/src/main/resources/en.json b/main/src/main/resources/en.json index 7a5f85de6..e52b534b4 100644 --- a/main/src/main/resources/en.json +++ b/main/src/main/resources/en.json @@ -415,6 +415,7 @@ "citizens.commands.npc.pathopt.use-new-finder" : "[[{0}]]''s use new finder set to [[{1}]].", "citizens.commands.npc.pathto.description" : "Starts pathfinding to a certain location", "citizens.commands.npc.pathto.help" : "", + "citizens.commands.npc.pausepathfinding.lockout-duration-set" : "[[{0}]] will now wait for [[{1}]] ticks before pausing again.", "citizens.commands.npc.pausepathfinding.description" : "Sets pathfinding pause", "citizens.commands.npc.pausepathfinding.help" : "", "citizens.commands.npc.pausepathfinding.pause-range-set" : "[{0}]] will now pause pathfinding when a player is within [[{1}]] blocks.", diff --git a/main/src/main/resources/plugin.yml b/main/src/main/resources/plugin.yml index 6976b5c64..b5784709a 100644 --- a/main/src/main/resources/plugin.yml +++ b/main/src/main/resources/plugin.yml @@ -6,8 +6,9 @@ main: net.citizensnpcs.Citizens website: https://www.citizensnpcs.co api-version: "1.13" libraries: - - ch.ethz.globis.phtree:phtree:2.8.0 - - org.joml:joml:1.10.5 + - ch.ethz.globis.phtree:phtree:2.8.1 + - org.joml:joml:1.10.8 + - it.unimi.dsi:fastutil:8.5.15 - net.kyori:adventure-platform-bukkit:4.3.3 - net.kyori:adventure-text-minimessage:4.17.0 commands: