Add /npc command permissions for temporary permissions during the command

This commit is contained in:
fullwall 2020-04-30 21:32:38 +08:00
parent 12ffd56981
commit 521c1d3fda
4 changed files with 23 additions and 1 deletions

View File

@ -288,7 +288,7 @@ public class NPCCommands {
@Command( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "command|cmd (add [command] | remove [id]) (-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]) (-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", desc = "Controls commands which will be run when clicking on an NPC",
modifiers = { "command", "cmd" }, modifiers = { "command", "cmd" },
min = 1, min = 1,
@ -320,6 +320,11 @@ public class NPCCommands {
throw new CommandException(Messages.COMMAND_UNKNOWN_COMMAND_ID, id); throw new CommandException(Messages.COMMAND_UNKNOWN_COMMAND_ID, id);
commands.removeCommandById(id); commands.removeCommandById(id);
Messaging.sendTr(sender, Messages.COMMAND_REMOVED, id); Messaging.sendTr(sender, Messages.COMMAND_REMOVED, id);
} else if (args.getString(1).equalsIgnoreCase("permissions") || args.getString(1).equalsIgnoreCase("perms")) {
List<String> temporaryPermissions = Arrays.asList(args.getSlice(2));
commands.setTemporaryPermissions(temporaryPermissions);
Messaging.sendTr(sender, Messages.COMMAND_TEMPORARY_PERMISSIONS_SET,
Joiner.on(' ').join(temporaryPermissions));
} else { } else {
throw new CommandUsageException(); throw new CommandUsageException();
} }

View File

@ -6,6 +6,7 @@ import java.util.Map;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@ -34,6 +35,8 @@ public class CommandTrait extends Trait {
@Persist @Persist
@DelegatePersistence(PlayerNPCCommandPersister.class) @DelegatePersistence(PlayerNPCCommandPersister.class)
private final Map<String, PlayerNPCCommand> cooldowns = Maps.newHashMap(); private final Map<String, PlayerNPCCommand> cooldowns = Maps.newHashMap();
@Persist
private final List<String> temporaryPermissions = Lists.newArrayList();
public CommandTrait() { public CommandTrait() {
super("commandtrait"); super("commandtrait");
@ -103,10 +106,17 @@ public class CommandTrait extends Trait {
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
PermissionAttachment attachment = new PermissionAttachment(CitizensAPI.getPlugin(), player);
if (temporaryPermissions.size() > 0) {
for (String permission : temporaryPermissions) {
attachment.setPermission(permission, true);
}
}
command.run(npc, player); command.run(npc, player);
if (command.cooldown > 0 && info == null) { if (command.cooldown > 0 && info == null) {
cooldowns.put(player.getUniqueId().toString(), new PlayerNPCCommand(command)); cooldowns.put(player.getUniqueId().toString(), new PlayerNPCCommand(command));
} }
attachment.remove();
} }
}; };
if (command.delay <= 0) { if (command.delay <= 0) {
@ -140,6 +150,11 @@ public class CommandTrait extends Trait {
commands.remove(String.valueOf(id)); commands.remove(String.valueOf(id));
} }
public void setTemporaryPermissions(List<String> permissions) {
temporaryPermissions.clear();
temporaryPermissions.addAll(permissions);
}
public static enum Hand { public static enum Hand {
BOTH, BOTH,
LEFT, LEFT,

View File

@ -62,6 +62,7 @@ public class Messages {
public static final String COMMAND_REQUIREMENTS_INVALID_MOB_TYPE = "citizens.commands.requirements.disallowed-mobtype"; public static final String COMMAND_REQUIREMENTS_INVALID_MOB_TYPE = "citizens.commands.requirements.disallowed-mobtype";
public static final String COMMAND_RIGHT_HAND_HEADER = "citizens.commands.npc.command.right-hand-header"; public static final String COMMAND_RIGHT_HAND_HEADER = "citizens.commands.npc.command.right-hand-header";
public static final String COMMAND_SAVE_HELP = "citizens.commands.citizens.save.help"; public static final String COMMAND_SAVE_HELP = "citizens.commands.citizens.save.help";
public static final String COMMAND_TEMPORARY_PERMISSIONS_SET = "citizens.commands.npc.command.temporary-permissions-set";
public static final String COMMAND_TOO_FEW_ARGUMENTS = "citizens.commands.requirements.too-few-arguments"; public static final String COMMAND_TOO_FEW_ARGUMENTS = "citizens.commands.requirements.too-few-arguments";
public static final String COMMAND_TOO_MANY_ARGUMENTS = "citizens.commands.requirements.too-many-arguments"; public static final String COMMAND_TOO_MANY_ARGUMENTS = "citizens.commands.requirements.too-many-arguments";
public static final String COMMAND_UNKNOWN_COMMAND_ID = "citizens.commands.npc.command.unknown-id"; public static final String COMMAND_UNKNOWN_COMMAND_ID = "citizens.commands.npc.command.unknown-id";

View File

@ -47,6 +47,7 @@ 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-removed=Command [[{0}]] removed.
citizens.commands.npc.command.command-added=Command [[{0}]] added with id [[{1}]]. citizens.commands.npc.command.command-added=Command [[{0}]] added with id [[{1}]].
citizens.commands.npc.command.unknown-id=Unknown command id [[{0}]] for this NPC. 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.controllable.not-controllable=[[{0}]] is not controllable. citizens.commands.npc.controllable.not-controllable=[[{0}]] is not controllable.
citizens.commands.npc.controllable.removed=[[{0}]] can no longer be controlled. citizens.commands.npc.controllable.removed=[[{0}]] can no longer be controlled.
citizens.commands.npc.controllable.set=[[{0}]] can now be controlled. citizens.commands.npc.controllable.set=[[{0}]] can now be controlled.