Adjust /npc text to consider delay in right click as well

This commit is contained in:
fullwall 2022-10-15 20:40:05 +08:00
parent 5a5bc1349d
commit dee442e935

View File

@ -143,6 +143,7 @@ public class Text extends Trait implements Runnable, Listener, ConversationAband
for (DataKey sub : key.getRelative("text").getIntegerSubKeys()) { for (DataKey sub : key.getRelative("text").getIntegerSubKeys()) {
text.add(sub.getString("")); text.add(sub.getString(""));
} }
if (text.isEmpty()) { if (text.isEmpty()) {
populateDefaultText(); populateDefaultText();
} }
@ -162,7 +163,7 @@ public class Text extends Trait implements Runnable, Listener, ConversationAband
return; return;
String localPattern = itemInHandPattern.equals("default") ? Setting.TALK_ITEM.asString() : itemInHandPattern; String localPattern = itemInHandPattern.equals("default") ? Setting.TALK_ITEM.asString() : itemInHandPattern;
if (Util.matchesItemInHand(event.getClicker(), localPattern) && !shouldTalkClose()) { if (Util.matchesItemInHand(event.getClicker(), localPattern) && !shouldTalkClose()) {
sendText(event.getClicker()); sendCooldownMessage(event.getClicker());
event.setCancelled(true); event.setCancelled(true);
} }
} }
@ -190,25 +191,7 @@ public class Text extends Trait implements Runnable, Listener, ConversationAband
for (Entity search : nearby) { for (Entity search : nearby) {
if (!(search instanceof Player) || ((Player) search).getGameMode() == GameMode.SPECTATOR) if (!(search instanceof Player) || ((Player) search).getGameMode() == GameMode.SPECTATOR)
continue; continue;
Player player = (Player) search; sendCooldownMessage((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);
} }
} }
@ -227,6 +210,26 @@ public class Text extends Trait implements Runnable, Listener, ConversationAband
} }
} }
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) { boolean sendPage(CommandSender player, int page) {
Paginator paginator = new Paginator().header("Current Texts").enablePageSwitcher(); Paginator paginator = new Paginator().header("Current Texts").enablePageSwitcher();
for (int i = 0; i < text.size(); i++) { for (int i = 0; i < text.size(); i++) {