Add basic named parameters for time purposes in command trait

This commit is contained in:
fullwall 2021-03-13 22:17:55 +08:00
parent f46285b238
commit 643e42f4ba
3 changed files with 45 additions and 12 deletions

View File

@ -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),

View File

@ -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",

View File

@ -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<String, String> transform,
Object... objects) {
Set<CommandTraitMessages> 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<String, String> {
private final Map<String, String> 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);
}
}
}