mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-22 15:22:11 +01:00
Add --permissions to /npc command
This commit is contained in:
parent
767891588a
commit
188c65f203
@ -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]",
|
||||
usage = "command|cmd (add [command] | remove [id]) (-l[eft]/-r[ight]) (-p[layer] -o[p]), --cooldown [seconds] --permissions [perms]",
|
||||
desc = "Controls commands which will be run when clicking on an NPC",
|
||||
modifiers = { "command", "cmd" },
|
||||
min = 1,
|
||||
@ -293,8 +293,12 @@ public class NPCCommands {
|
||||
String command = args.getJoinedStrings(2);
|
||||
CommandTrait.Hand hand = args.hasFlag('l') && args.hasFlag('r') ? CommandTrait.Hand.BOTH
|
||||
: args.hasFlag('l') ? CommandTrait.Hand.LEFT : CommandTrait.Hand.RIGHT;
|
||||
List<String> perms = Lists.newArrayList();
|
||||
if (args.hasValueFlag("permissions")) {
|
||||
perms.addAll(Arrays.asList(args.getFlag("permissions").split(",")));
|
||||
}
|
||||
int id = commands.addCommand(command, hand, args.hasFlag('p'), args.hasFlag('o'),
|
||||
args.getFlagInteger("cooldown", 0));
|
||||
args.getFlagInteger("cooldown", 0), perms);
|
||||
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
|
||||
} else if (args.getString(1).equalsIgnoreCase("remove")) {
|
||||
if (args.argsLength() == 2)
|
||||
|
@ -36,9 +36,10 @@ public class CommandTrait extends Trait {
|
||||
super("commandtrait");
|
||||
}
|
||||
|
||||
public int addCommand(String command, Hand hand, boolean player, boolean op, int cooldown) {
|
||||
public int addCommand(String command, Hand hand, boolean player, boolean op, int cooldown, List<String> perms) {
|
||||
int id = getNewId();
|
||||
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand, player, op, cooldown));
|
||||
commands.put(String.valueOf(id),
|
||||
new NPCCommand(String.valueOf(id), command, hand, player, op, cooldown, perms));
|
||||
return id;
|
||||
}
|
||||
|
||||
@ -91,7 +92,7 @@ public class CommandTrait extends Trait {
|
||||
if (command.hand != hand && command.hand != Hand.BOTH)
|
||||
continue;
|
||||
PlayerNPCCommand info = cooldowns.get(player.getUniqueId());
|
||||
if (info != null && !info.canUse(command)) {
|
||||
if (info != null && !info.canUse(player, command)) {
|
||||
continue;
|
||||
}
|
||||
command.run(npc, player);
|
||||
@ -129,15 +130,18 @@ public class CommandTrait extends Trait {
|
||||
Hand hand;
|
||||
String id;
|
||||
boolean op;
|
||||
List<String> perms;
|
||||
boolean player;
|
||||
|
||||
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op, int cooldown) {
|
||||
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op, int cooldown,
|
||||
List<String> perms) {
|
||||
this.id = id;
|
||||
this.command = command;
|
||||
this.hand = hand;
|
||||
this.player = player;
|
||||
this.op = op;
|
||||
this.cooldown = cooldown;
|
||||
this.perms = perms;
|
||||
}
|
||||
|
||||
public void run(NPC npc, Player clicker) {
|
||||
@ -167,9 +171,13 @@ public class CommandTrait extends Trait {
|
||||
|
||||
@Override
|
||||
public NPCCommand create(DataKey root) {
|
||||
List<String> perms = Lists.newArrayList();
|
||||
for (DataKey key : root.getRelative("permissions").getIntegerSubKeys()) {
|
||||
perms.add(key.getString(""));
|
||||
}
|
||||
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"));
|
||||
root.getInt("cooldown"), perms);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -179,6 +187,9 @@ public class CommandTrait extends Trait {
|
||||
root.setBoolean("player", instance.player);
|
||||
root.setBoolean("op", instance.op);
|
||||
root.setInt("cooldown", instance.cooldown);
|
||||
for (int i = 0; i < instance.perms.size(); i++) {
|
||||
root.setString("permissions." + i, instance.perms.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -193,7 +204,11 @@ public class CommandTrait extends Trait {
|
||||
lastUsed.put(command.command, System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
public boolean canUse(NPCCommand command) {
|
||||
public boolean canUse(Player player, NPCCommand command) {
|
||||
for (String perm : command.perms) {
|
||||
if (!player.hasPermission(perm))
|
||||
return false;
|
||||
}
|
||||
long currentTimeSec = System.currentTimeMillis() / 1000;
|
||||
if (lastUsed.containsKey(command.command)) {
|
||||
if (currentTimeSec < lastUsed.get(command.command) + command.cooldown) {
|
||||
|
Loading…
Reference in New Issue
Block a user