Skip to content

Commit

Permalink
Adjust /npc text to consider delay in right click as well
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Oct 15, 2022
1 parent 5a5bc13 commit dee442e
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions main/src/main/java/net/citizensnpcs/trait/text/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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++) {
Expand Down

0 comments on commit dee442e

Please sign in to comment.