From 643e42f4ba76b97e55bdcd720c7b8640a917b161 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 13 Mar 2021 22:17:55 +0800 Subject: [PATCH] Add basic named parameters for time purposes in command trait --- .../main/java/net/citizensnpcs/Settings.java | 7 +-- .../citizensnpcs/commands/NPCCommands.java | 4 +- .../net/citizensnpcs/trait/CommandTrait.java | 46 ++++++++++++++++--- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/Settings.java b/main/src/main/java/net/citizensnpcs/Settings.java index 61e1b18b2..b6677588e 100644 --- a/main/src/main/java/net/citizensnpcs/Settings.java +++ b/main/src/main/java/net/citizensnpcs/Settings.java @@ -87,7 +87,7 @@ public class Settings { DEFAULT_PATHFINDING_RANGE("npc.default.pathfinding.range", 25F), DEFAULT_RANDOM_LOOK_CLOSE("npc.default.look-close.random-look-enabled", false), DEFAULT_RANDOM_LOOK_DELAY("npc.default.look-close.random-look-delay", 60), - DEFAULT_RANDOM_TALKER("npc.default.random-talker", true), + DEFAULT_RANDOM_TALKER("npc.default.random-talker", false), DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false), DEFAULT_STATIONARY_TICKS("npc.default.stationary-ticks", -1), DEFAULT_STRAIGHT_LINE_TARGETING_DISTANCE("npc.pathfinding.straight-line-targeting-distance", 5), @@ -129,9 +129,10 @@ public class Settings { NPC_COMMAND_NO_PERMISSION_MESSAGE("npc.commands.error-messages.no-permission", "You don't have permission to do that."), NPC_COMMAND_NOT_ENOUGH_MONEY_MESSAGE("npc.commands.error-messages.not-enough-money", "You need at least ${0}."), - NPC_COMMAND_ON_COOLDOWN_MESSAGE("npc.commands.error-messages.on-cooldown", "Please wait {0} more seconds."), + NPC_COMMAND_ON_COOLDOWN_MESSAGE("npc.commands.error-messages.on-cooldown", + "Please wait for {minutes} minutes and {seconds_over} seconds."), NPC_COMMAND_ON_GLOBAL_COOLDOWN_MESSAGE("npc.commands.error-messages.on-global-cooldown", - "Please wait {0} more seconds."), + "Please wait for {minutes} minutes and {seconds_over} seconds."), NPC_COST("economy.npc.cost", 100D), NPC_SKIN_RETRY_DELAY("npc.skins.retry-delay", 120), NPC_SKIN_ROTATION_UPDATE_DEGREES("npc.skins.rotation-update-degrees", 90f), diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 0bb40e3ee..1f676a753 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -1308,8 +1308,8 @@ public class NPCCommands { @Command( aliases = { "npc" }, modifiers = { "name" }, - usage = "name", - desc = "Toggle nameplate visibility", + usage = "name (-h(over))", + desc = "Toggle nameplate visibility, or only show names on hover", min = 1, max = 1, flags = "h", diff --git a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java index 02d4f1f27..77ca75e21 100644 --- a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java @@ -7,7 +7,10 @@ import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import org.apache.logging.log4j.core.lookup.StrSubstitutor; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.command.CommandSender; @@ -86,7 +89,7 @@ public class CommandTrait extends Trait { if (provider != null && provider.getProvider() != null) { Economy economy = provider.getProvider(); if (!economy.has(player, cost)) { - sendErrorMessage(player, CommandTraitMessages.MISSING_MONEY, cost); + sendErrorMessage(player, CommandTraitMessages.MISSING_MONEY, null, cost); return false; } economy.withdrawPlayer(player, cost); @@ -105,7 +108,7 @@ public class CommandTrait extends Trait { if (tempInventory.containsAtLeast(stack, stack.getAmount())) { tempInventory.removeItem(stack); } else { - sendErrorMessage(player, CommandTraitMessages.MISSING_ITEM, Util.prettyEnum(stack.getType()), + sendErrorMessage(player, CommandTraitMessages.MISSING_ITEM, null, Util.prettyEnum(stack.getType()), stack.getAmount()); return false; } @@ -276,7 +279,8 @@ public class CommandTrait extends Trait { commands.remove(String.valueOf(id)); } - private void sendErrorMessage(Player player, CommandTraitMessages msg, Object... objects) { + private void sendErrorMessage(Player player, CommandTraitMessages msg, Function transform, + Object... objects) { Set sent = executionErrors.get(player.getUniqueId().toString()); if (sent != null) { if (sent.contains(msg)) @@ -284,6 +288,9 @@ public class CommandTrait extends Trait { sent.add(msg); } String messageRaw = msg.setting.asString(); + if (transform != null) { + messageRaw = transform.apply(messageRaw); + } if (messageRaw != null && messageRaw.trim().length() > 0) { Messaging.send(player, Translator.format(messageRaw, objects)); } @@ -545,7 +552,7 @@ public class CommandTrait extends Trait { public boolean canUse(CommandTrait trait, Player player, NPCCommand command) { for (String perm : command.perms) { if (!player.hasPermission(perm)) { - trait.sendErrorMessage(player, CommandTraitMessages.NO_PERMISSION); + trait.sendErrorMessage(player, CommandTraitMessages.NO_PERMISSION, null); return false; } } @@ -560,8 +567,9 @@ public class CommandTrait extends Trait { } if (lastUsed.containsKey(commandKey)) { if (currentTimeSec < lastUsed.get(commandKey) + command.cooldown) { + long seconds = (lastUsed.get(commandKey) + command.cooldown) - currentTimeSec; trait.sendErrorMessage(player, CommandTraitMessages.ON_COOLDOWN, - (lastUsed.get(commandKey) + command.cooldown) - currentTimeSec); + new TimeVariableFormatter(seconds, TimeUnit.SECONDS), seconds); return false; } lastUsed.remove(commandKey); @@ -569,15 +577,16 @@ public class CommandTrait extends Trait { if (command.globalCooldown > 0 && trait.globalCooldowns.containsKey(commandKey)) { long lastUsedSec = trait.globalCooldowns.get(commandKey); if (currentTimeSec < lastUsedSec + command.cooldown) { + long seconds = (lastUsedSec + command.cooldown) - currentTimeSec; trait.sendErrorMessage(player, CommandTraitMessages.ON_GLOBAL_COOLDOWN, - (lastUsedSec + command.cooldown) - currentTimeSec); + new TimeVariableFormatter(seconds, TimeUnit.SECONDS), seconds); return false; } trait.globalCooldowns.remove(commandKey); } int previouslyUsed = nUsed.getOrDefault(commandKey, 0); if (command.n > 0 && command.n <= previouslyUsed) { - trait.sendErrorMessage(player, CommandTraitMessages.MAXIMUM_TIMES_USED, command.n); + trait.sendErrorMessage(player, CommandTraitMessages.MAXIMUM_TIMES_USED, null, command.n); return false; } if (command.cooldown > 0) { @@ -597,4 +606,27 @@ public class CommandTrait extends Trait { return command.cooldown > 0 || command.n > 0 || (command.perms != null && command.perms.size() > 0); } } + + private static class TimeVariableFormatter implements Function { + private final Map map = Maps.newHashMapWithExpectedSize(5); + + public TimeVariableFormatter(long source, TimeUnit unit) { + long seconds = TimeUnit.SECONDS.convert(source, unit); + long minutes = TimeUnit.MINUTES.convert(source, unit); + long hours = TimeUnit.HOURS.convert(source, unit); + long days = TimeUnit.DAYS.convert(source, unit); + map.put("seconds", "" + seconds); + map.put("seconds_over", "" + (seconds - TimeUnit.SECONDS.convert(minutes, TimeUnit.MINUTES))); + map.put("minutes", "" + minutes); + map.put("minutes_over", "" + (minutes - TimeUnit.MINUTES.convert(hours, TimeUnit.HOURS))); + map.put("hours", "" + hours); + map.put("hours_over", "" + (hours - TimeUnit.HOURS.convert(days, TimeUnit.DAYS))); + map.put("days", "" + days); + } + + @Override + public String apply(String t) { + return StrSubstitutor.replace(t, map); + } + } } \ No newline at end of file