From dee442e935a7e70b6820cdd08325788153945c4b Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 15 Oct 2022 20:40:05 +0800 Subject: [PATCH] Adjust /npc text to consider delay in right click as well --- .../net/citizensnpcs/trait/text/Text.java | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) 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 e98e9cacb8..9b673998e8 100644 --- a/main/src/main/java/net/citizensnpcs/trait/text/Text.java +++ b/main/src/main/java/net/citizensnpcs/trait/text/Text.java @@ -143,6 +143,7 @@ public void load(DataKey key) throws NPCLoadException { for (DataKey sub : key.getRelative("text").getIntegerSubKeys()) { text.add(sub.getString("")); } + if (text.isEmpty()) { populateDefaultText(); } @@ -162,7 +163,7 @@ private void onRightClick(NPCRightClickEvent event) { return; String localPattern = itemInHandPattern.equals("default") ? Setting.TALK_ITEM.asString() : itemInHandPattern; if (Util.matchesItemInHand(event.getClicker(), localPattern) && !shouldTalkClose()) { - sendText(event.getClicker()); + sendCooldownMessage(event.getClicker()); event.setCancelled(true); } } @@ -190,25 +191,7 @@ public void run() { for (Entity search : nearby) { if (!(search instanceof Player) || ((Player) search).getGameMode() == GameMode.SPECTATOR) continue; - Player player = (Player) search; - - Long cooldown = cooldowns.get(player.getUniqueId()); - if (cooldown != null) { - if (System.currentTimeMillis() < cooldown) { - return; - } - cooldowns.remove(player.getUniqueId()); - } - - sendText(player); - - int secondsDelta = delay != -1 ? delay - : RANDOM.nextInt(Setting.TALK_CLOSE_MAXIMUM_COOLDOWN.asInt()) - + Setting.TALK_CLOSE_MINIMUM_COOLDOWN.asInt(); - if (secondsDelta <= 0) - return; - long millisecondsDelta = TimeUnit.MILLISECONDS.convert(secondsDelta, TimeUnit.SECONDS); - cooldowns.put(player.getUniqueId(), System.currentTimeMillis() + millisecondsDelta); + sendCooldownMessage((Player) search); } } @@ -227,6 +210,26 @@ public void save(DataKey key) { } } + private void sendCooldownMessage(Player player) { + Long cooldown = cooldowns.get(player.getUniqueId()); + if (cooldown != null) { + if (System.currentTimeMillis() < cooldown) + return; + + cooldowns.remove(player.getUniqueId()); + } + + sendText(player); + + int secondsDelta = delay != -1 ? delay + : RANDOM.nextInt(Setting.TALK_CLOSE_MAXIMUM_COOLDOWN.asInt()) + + Setting.TALK_CLOSE_MINIMUM_COOLDOWN.asInt(); + if (secondsDelta <= 0) + return; + long millisecondsDelta = TimeUnit.MILLISECONDS.convert(secondsDelta, TimeUnit.SECONDS); + cooldowns.put(player.getUniqueId(), System.currentTimeMillis() + millisecondsDelta); + } + boolean sendPage(CommandSender player, int page) { Paginator paginator = new Paginator().header("Current Texts").enablePageSwitcher(); for (int i = 0; i < text.size(); i++) {