Add /npc command --gcooldown

This commit is contained in:
fullwall 2020-12-16 18:12:37 +08:00
parent 1266298442
commit 040930bcde
4 changed files with 36 additions and 10 deletions

View File

@ -127,6 +127,8 @@ public class Settings {
"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_GLOBAL_COOLDOWN_MESSAGE("npc.commands.error-messages.on-global-cooldown",
"Please wait {0} more 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

@ -312,7 +312,7 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "command|cmd (add [command] | remove [id] | permissions [permissions] | sequential | random | cost) (-l[eft]/-r[ight]) (-p[layer] -o[p]), --cooldown [seconds] --delay [ticks] --permissions [perms] --n [max # of uses]",
usage = "command|cmd (add [command] | remove [id] | permissions [permissions] | sequential | random | cost [cost]) (-l[eft]/-r[ight]) (-p[layer] -o[p]), --cooldown [seconds] --delay [ticks] --permissions [perms] --n [max # of uses]",
desc = "Controls commands which will be run when clicking on an NPC",
help = Messages.NPC_COMMAND_HELP,
modifiers = { "command", "cmd" },
@ -333,10 +333,10 @@ public class NPCCommands {
if (args.hasValueFlag("permissions")) {
perms.addAll(Arrays.asList(args.getFlag("permissions").split(",")));
}
int id = commands.addCommand(
new NPCCommandBuilder(command, hand).addPerms(perms).player(args.hasFlag('p') || args.hasFlag('o'))
.op(args.hasFlag('o')).cooldown(args.getFlagInteger("cooldown", 0))
.n(args.getFlagInteger("n", -1)).delay(args.getFlagInteger("delay", 0)));
int id = commands.addCommand(new NPCCommandBuilder(command, hand).addPerms(perms)
.player(args.hasFlag('p') || args.hasFlag('o')).op(args.hasFlag('o'))
.cooldown(args.getFlagInteger("cooldown", 0)).globalCooldown(args.getFlagInteger("gcooldown", 0))
.n(args.getFlagInteger("n", -1)).delay(args.getFlagInteger("delay", 0)));
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
} else if (args.getString(1).equalsIgnoreCase("sequential")) {
commands.setExecutionMode(commands.getExecutionMode() == ExecutionMode.SEQUENTIAL ? ExecutionMode.LINEAR

View File

@ -53,6 +53,8 @@ public class CommandTrait extends Trait {
@Persist
private ExecutionMode executionMode = ExecutionMode.LINEAR;
@Persist
private final Map<String, Long> globalCooldowns = Maps.newHashMap();
@Persist
private final List<String> temporaryPermissions = Lists.newArrayList();
public CommandTrait() {
@ -265,7 +267,8 @@ public class CommandTrait extends Trait {
MAXIMUM_TIMES_USED(Setting.NPC_COMMAND_MAXIMUM_TIMES_USED_MESSAGE),
MISSING_MONEY(Setting.NPC_COMMAND_NOT_ENOUGH_MONEY_MESSAGE),
NO_PERMISSION(Setting.NPC_COMMAND_NO_PERMISSION_MESSAGE),
ON_COOLDOWN(Setting.NPC_COMMAND_ON_COOLDOWN_MESSAGE);
ON_COOLDOWN(Setting.NPC_COMMAND_ON_COOLDOWN_MESSAGE),
ON_GLOBAL_COOLDOWN(Setting.NPC_COMMAND_ON_GLOBAL_COOLDOWN_MESSAGE);
private final Setting setting;
@ -291,6 +294,7 @@ public class CommandTrait extends Trait {
String command;
int cooldown;
int delay;
int globalCooldown;
Hand hand;
int id;
int n;
@ -299,7 +303,7 @@ public class CommandTrait extends Trait {
boolean player;
public NPCCommand(int id, String command, Hand hand, boolean player, boolean op, int cooldown,
List<String> perms, int n, int delay) {
List<String> perms, int n, int delay, int globalCooldown) {
this.id = id;
this.command = command;
this.hand = hand;
@ -309,6 +313,7 @@ public class CommandTrait extends Trait {
this.perms = perms;
this.n = n;
this.delay = delay;
this.globalCooldown = globalCooldown;
List<String> split = Splitter.on(' ').omitEmptyStrings().trimResults().splitToList(command);
this.bungeeServer = split.size() == 2 && split.get(0).equalsIgnoreCase("server") ? split.get(1) : null;
}
@ -352,6 +357,7 @@ public class CommandTrait extends Trait {
String command;
int cooldown;
int delay;
private int globalCooldown;
Hand hand;
int n = -1;
boolean op;
@ -374,7 +380,7 @@ public class CommandTrait extends Trait {
}
private NPCCommand build(int id) {
return new NPCCommand(id, command, hand, player, op, cooldown, perms, n, delay);
return new NPCCommand(id, command, hand, player, op, cooldown, perms, n, delay, globalCooldown);
}
public NPCCommandBuilder command(String command) {
@ -392,6 +398,11 @@ public class CommandTrait extends Trait {
return this;
}
public NPCCommandBuilder globalCooldown(int cooldown) {
this.globalCooldown = cooldown;
return this;
}
public NPCCommandBuilder n(int n) {
this.n = n;
return this;
@ -421,7 +432,7 @@ public class CommandTrait extends Trait {
return new NPCCommand(Integer.parseInt(root.name()), root.getString("command"),
Hand.valueOf(root.getString("hand")), Boolean.valueOf(root.getString("player")),
Boolean.valueOf(root.getString("op")), root.getInt("cooldown"), perms, root.getInt("n"),
root.getInt("delay"));
root.getInt("delay"), root.getInt("globalcooldown"));
}
@Override
@ -431,6 +442,7 @@ public class CommandTrait extends Trait {
root.setBoolean("player", instance.player);
root.setBoolean("op", instance.op);
root.setInt("cooldown", instance.cooldown);
root.setInt("globalcooldown", instance.globalCooldown);
root.setInt("n", instance.n);
root.setInt("delay", instance.delay);
for (int i = 0; i < instance.perms.size(); i++) {
@ -474,6 +486,15 @@ public class CommandTrait extends Trait {
}
lastUsed.remove(commandKey);
}
if (command.globalCooldown > 0 && trait.globalCooldowns.containsKey(commandKey)) {
long lastUsedSec = trait.globalCooldowns.get(commandKey);
if (currentTimeSec < lastUsedSec + command.cooldown) {
trait.sendErrorMessage(player, CommandTraitMessages.ON_GLOBAL_COOLDOWN,
(lastUsedSec + command.cooldown) - currentTimeSec);
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);
@ -485,6 +506,9 @@ public class CommandTrait extends Trait {
if (command.n > 0) {
nUsed.put(commandKey, previouslyUsed + 1);
}
if (command.globalCooldown > 0) {
trait.globalCooldowns.put(commandKey, currentTimeSec);
}
lastUsedId = command.id;
return true;
}

View File

@ -49,7 +49,7 @@ citizens.commands.npc.command.left-hand-header=Commands to run on [[left click]]
citizens.commands.npc.command.right-hand-header=Commands to run on [[right click]]:
citizens.commands.npc.command.command-removed=Command [[{0}]] removed.
citizens.commands.npc.command.command-added=Command [[{0}]] added with id [[{1}]].
citizens.commands.npc.command.help=<br>Use the [[-l]] flag to make the command run on left click, [[-r]] on right click (default).<br>Set the per-player cooldown before the command can be used again using [[--cooldown]] (in [[seconds]]).<br>[[--delay]] will wait the specified amount in [[ticks]] before executing the command.<br>[[--permissions]] will set the command to require specific permissions (separate multiple with commas).<br>[[--n]] will only let the player run the command that number of times.<br>Use [[-o]] to temporarily execute the command as an op and [[-p]] to run the command as the clicking player instead of the server.<br>To give the player temporary permissions instead of op, use [[/npc command permissions]].<br>Set the cost of each click with [[/npc command cost]].<br>Commands can be executed one by one instead of all at once by using [[/npc command sequential]].
citizens.commands.npc.command.help=<br>Use the [[-l]] flag to make the command run on left click, [[-r]] on right click (default).<br>Set the per-player cooldown before the command can be used again using [[--cooldown]] (in [[seconds]]).<br>Set the server-wide cooldown in seconds using [[--gcooldown]].<br>[[--delay]] will wait the specified amount in [[ticks]] before executing the command.<br>[[--permissions]] will set the command to require specific permissions (separate multiple with commas).<br>[[--n]] will only let the player run the command that number of times.<br>Use [[-o]] to temporarily execute the command as an op and [[-p]] to run the command as the clicking player instead of the server.<br>To give the player temporary permissions instead of op, use [[/npc command permissions]].<br>Set the cost of each click with [[/npc command cost]].<br>Commands can be executed one by one instead of all at once by using [[/npc command sequential]].
citizens.commands.npc.command.unknown-id=Unknown command id [[{0}]] for this NPC.
citizens.commands.npc.command.temporary-permissions-set=Temporary permissions set to [[{0}]].
citizens.commands.npc.commands.sequential-set=Commands will now execute sequentially.