Skip to content

Commit

Permalink
Add -o option to /npc command
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 29, 2019
1 parent ab40609 commit 52e288e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 3 additions & 3 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -278,11 +278,11 @@ public void collidable(CommandContext args, CommandSender sender, NPC npc) throw

@Command(
aliases = { "npc" },
usage = "command|cmd (add [command] | remove [id]) (-l[eft]/-r[ight]) (-p[layer])",
usage = "command|cmd (add [command] | remove [id]) (-l[eft]/-r[ight]) (-p[layer] -o[p])",
desc = "Controls commands which will be run when clicking on an NPC",
modifiers = { "command", "cmd" },
min = 1,
flags = "lrp",
flags = "lrpo",
permission = "citizens.npc.command")
public void command(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
CommandTrait commands = npc.getTrait(CommandTrait.class);
Expand All @@ -293,7 +293,7 @@ public void command(CommandContext args, CommandSender sender, NPC npc) throws C
throw new CommandUsageException();
String command = args.getJoinedStrings(2);
CommandTrait.Hand hand = args.hasFlag('l') ? CommandTrait.Hand.LEFT : CommandTrait.Hand.RIGHT;
int id = commands.addCommand(command, hand, args.hasFlag('p'));
int id = commands.addCommand(command, hand, args.hasFlag('p'), args.hasFlag('o'));
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
} else if (args.getString(1).equalsIgnoreCase("remove")) {
if (args.argsLength() == 2)
Expand Down
18 changes: 14 additions & 4 deletions main/src/main/java/net/citizensnpcs/trait/CommandTrait.java
Expand Up @@ -30,9 +30,9 @@ public CommandTrait() {
super("commandtrait");
}

public int addCommand(String command, Hand hand, boolean player) {
public int addCommand(String command, Hand hand, boolean player, boolean op) {
int id = getNewId();
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand, player));
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand, player, op));
return id;
}

Expand Down Expand Up @@ -102,18 +102,27 @@ private static class NPCCommand {
Hand hand;
String id;
boolean player;
boolean op;

public NPCCommand(String id, String command, Hand hand, boolean player) {
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op) {
this.id = id;
this.command = command;
this.hand = hand;
this.player = player;
this.op = op;
}

public void run(NPC npc, Player clicker) {
String interpolatedCommand = command.replace("<npc>", npc.getName()).replace("<p>", clicker.getName());
if (player) {
boolean wasOp = clicker.isOp();
if (op) {
clicker.setOp(true);
}
clicker.performCommand(interpolatedCommand);
if (op) {
clicker.setOp(wasOp);
}
} else {
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), interpolatedCommand);
}
Expand All @@ -127,14 +136,15 @@ public NPCCommandPersister() {
@Override
public NPCCommand create(DataKey root) {
return new NPCCommand(root.name(), root.getString("command"), Hand.valueOf(root.getString("hand")),
Boolean.valueOf(root.getString("player")));
Boolean.valueOf(root.getString("player")), Boolean.valueOf(root.getString("op")));
}

@Override
public void save(NPCCommand instance, DataKey root) {
root.setString("command", instance.command);
root.setString("hand", instance.hand.name());
root.setBoolean("player", instance.player);
root.setBoolean("op", instance.op);
}
}
}

0 comments on commit 52e288e

Please sign in to comment.