Restore PR, add --n to /npc command to allow a maximum number of uses per command

This commit is contained in:
fullwall 2020-03-03 23:40:42 +08:00
parent 9f3841c3ec
commit f345ef3f16
3 changed files with 43 additions and 30 deletions

View File

@ -277,7 +277,7 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "command|cmd (add [command] | remove [id]) (-l[eft]/-r[ight]) (-p[layer] -o[p]), --cooldown [seconds] --permissions [perms]",
usage = "command|cmd (add [command] | remove [id]) (-l[eft]/-r[ight]) (-p[layer] -o[p]), --cooldown [seconds] --permissions [perms] --n [max # of uses]",
desc = "Controls commands which will be run when clicking on an NPC",
modifiers = { "command", "cmd" },
min = 1,
@ -298,7 +298,7 @@ public class NPCCommands {
perms.addAll(Arrays.asList(args.getFlag("permissions").split(",")));
}
int id = commands.addCommand(command, hand, args.hasFlag('p'), args.hasFlag('o'),
args.getFlagInteger("cooldown", 0), perms);
args.getFlagInteger("cooldown", 0), perms, args.getFlagInteger("n", -1));
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
} else if (args.getString(1).equalsIgnoreCase("remove")) {
if (args.argsLength() == 2)

View File

@ -36,10 +36,11 @@ public class CommandTrait extends Trait {
super("commandtrait");
}
public int addCommand(String command, Hand hand, boolean player, boolean op, int cooldown, List<String> perms) {
public int addCommand(String command, Hand hand, boolean player, boolean op, int cooldown, List<String> perms,
int n) {
int id = getNewId();
commands.put(String.valueOf(id),
new NPCCommand(String.valueOf(id), command, hand, player, op, cooldown, perms));
new NPCCommand(String.valueOf(id), command, hand, player, op, cooldown, perms, n));
return id;
}
@ -129,12 +130,13 @@ public class CommandTrait extends Trait {
int cooldown;
Hand hand;
String id;
int n;
boolean op;
List<String> perms;
boolean player;
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op, int cooldown,
List<String> perms) {
List<String> perms, int n) {
this.id = id;
this.command = command;
this.hand = hand;
@ -142,6 +144,7 @@ public class CommandTrait extends Trait {
this.op = op;
this.cooldown = cooldown;
this.perms = perms;
this.n = n;
}
public void run(NPC npc, Player clicker) {
@ -177,7 +180,7 @@ public class CommandTrait extends Trait {
}
return new NPCCommand(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("cooldown"), perms, root.getInt("n"));
}
@Override
@ -187,6 +190,7 @@ public class CommandTrait extends Trait {
root.setBoolean("player", instance.player);
root.setBoolean("op", instance.op);
root.setInt("cooldown", instance.cooldown);
root.setInt("n", instance.n);
for (int i = 0; i < instance.perms.size(); i++) {
root.setString("permissions." + i, instance.perms.get(i));
}
@ -196,6 +200,8 @@ public class CommandTrait extends Trait {
private static class PlayerNPCCommand {
@Persist
Map<String, Long> lastUsed = Maps.newHashMap();
@Persist
Map<String, Integer> nUsed = Maps.newHashMap();
public PlayerNPCCommand() {
}
@ -217,9 +223,16 @@ public class CommandTrait extends Trait {
}
lastUsed.remove(command.command);
}
int previouslyUsed = nUsed.getOrDefault(command.command, 0);
if (command.n > 0 && command.n <= previouslyUsed) {
return false;
}
if (command.cooldown > 0) {
lastUsed.put(command.command, currentTimeSec);
}
if (command.n > 0) {
nUsed.put(command.command, previouslyUsed + 1);
}
return true;
}
}

View File

@ -76,7 +76,7 @@ public class Commands {
}
if (args.hasFlag('n')) {
trait.setNectar(!trait.hasNectar());
output += ' ' + (trait.hasStung() ? Messaging.tr(Messages.BEE_HAS_NECTAR, npc.getName())
output += ' ' + (trait.hasNectar() ? Messaging.tr(Messages.BEE_HAS_NECTAR, npc.getName())
: Messaging.tr(Messages.BEE_NO_NECTAR, npc.getName()));
}
if (!output.isEmpty()) {
@ -419,6 +419,29 @@ public class Commands {
}
}
@Command(
aliases = { "npc" },
usage = "snowman (-d[erp])",
desc = "Sets snowman modifiers.",
modifiers = { "snowman" },
min = 1,
max = 1,
flags = "d",
permission = "citizens.npc.snowman")
@Requirements(selected = true, ownership = true, types = { EntityType.SNOWMAN })
public void snowman(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
SnowmanTrait trait = npc.getTrait(SnowmanTrait.class);
boolean hasArg = false;
if (args.hasFlag('d')) {
boolean isDerp = trait.toggleDerp();
Messaging.sendTr(sender, isDerp ? Messages.SNOWMAN_DERP_SET : Messages.SNOWMAN_DERP_STOPPED, npc.getName());
hasArg = true;
}
if (!hasArg) {
throw new CommandUsageException();
}
}
@Command(
aliases = { "npc" },
usage = "tfish (--body color) (--pattern pattern) (--patterncolor color)",
@ -508,27 +531,4 @@ public class Commands {
throw new CommandUsageException();
}
}
@Command(
aliases = { "npc" },
usage = "snowman (-d[erp])",
desc = "Sets snowman modifiers.",
modifiers = { "snowman" },
min = 1,
max = 1,
flags = "d",
permission = "citizens.npc.snowman")
@Requirements(selected = true, ownership = true, types = { EntityType.SNOWMAN })
public void snowman(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
SnowmanTrait trait = npc.getTrait(SnowmanTrait.class);
boolean hasArg = false;
if (args.hasFlag('d')) {
boolean isDerp = trait.toggleDerp();
Messaging.sendTr(sender, isDerp ? Messages.SNOWMAN_DERP_SET : Messages.SNOWMAN_DERP_STOPPED, npc.getName());
hasArg = true;
}
if (!hasArg) {
throw new CommandUsageException();
}
}
}