Add -p flag to /npc command to run as the clicking player

This commit is contained in:
fullwall 2019-12-02 11:27:15 +08:00
parent 798c82536c
commit 0d558c8ee1
2 changed files with 15 additions and 7 deletions

View File

@ -278,7 +278,7 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "command|cmd (add [command] | remove [id]) (-l/-r)",
usage = "command|cmd (add [command] | remove [id]) (-l[eft]/-r[ight]) (-p[layer])",
desc = "Controls commands which will be run when clicking on an NPC",
modifiers = { "command", "cmd" },
min = 1,
@ -293,7 +293,7 @@ public class NPCCommands {
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);
int id = commands.addCommand(command, hand, args.hasFlag('p'));
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
} else if (args.getString(1).equalsIgnoreCase("remove")) {
if (args.argsLength() == 2)

View File

@ -30,9 +30,9 @@ public class CommandTrait extends Trait {
super("commandtrait");
}
public int addCommand(String command, Hand hand) {
public int addCommand(String command, Hand hand, boolean player) {
int id = getNewId();
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand));
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand, player));
return id;
}
@ -101,16 +101,22 @@ public class CommandTrait extends Trait {
String command;
Hand hand;
String id;
boolean player;
public NPCCommand(String id, String command, Hand hand) {
public NPCCommand(String id, String command, Hand hand, boolean player) {
this.id = id;
this.command = command;
this.hand = hand;
this.player = player;
}
public void run(NPC npc, Player clicker) {
String interpolatedCommand = command.replace("<npc>", npc.getName()).replace("<p>", clicker.getName());
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), interpolatedCommand);
if (player) {
clicker.performCommand(interpolatedCommand);
} else {
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), interpolatedCommand);
}
}
}
@ -120,13 +126,15 @@ public class CommandTrait extends Trait {
@Override
public NPCCommand create(DataKey root) {
return new NPCCommand(root.name(), root.getString("command"), Hand.valueOf(root.getString("hand")));
return new NPCCommand(root.name(), root.getString("command"), Hand.valueOf(root.getString("hand")),
Boolean.valueOf(root.getString("player")));
}
@Override
public void save(NPCCommand instance, DataKey root) {
root.setString("command", instance.command);
root.setString("hand", instance.hand.name());
root.setBoolean("player", instance.player);
}
}
}