Add some more permissions and properly set command permission

This commit is contained in:
Phoenix616 2019-04-27 02:48:49 +01:00
parent 9ebc4eb64d
commit 5b5450ad08
5 changed files with 59 additions and 10 deletions

View File

@ -44,6 +44,8 @@ import org.bukkit.block.Biome;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.AbstractMap;
@ -116,7 +118,7 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI {
private void initOptionParsers() {
addOptionParser(new SimpleOptionParser(array("p", "player"), (searcher, args) -> {
if (args.length > 0) {
if (args.length > 0 && searcher.getInitiator().hasPermission("randomteleport.tpothers")) {
List<Player> players = new ArrayList<>();
for (String arg : args) {
for (String s : arg.split(",")) {
@ -249,7 +251,7 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI {
}
public boolean sendMessage(CommandSender sender, String key, String... replacements) {
String message = lang.getConfig(sender).get(key, replacements);
String message = getMessage(sender, key, replacements);
if (message != null && !message.isEmpty()) {
sender.sendMessage(message);
return true;
@ -257,6 +259,10 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI {
return false;
}
public String getMessage(CommandSender sender, String key, String... replacements) {
return lang.getConfig(sender).get(key, replacements);
}
public HookManager getHookManager() {
return hookManager;
}
@ -279,6 +285,16 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI {
*/
public void addOptionParser(OptionParser parser) {
optionParsers.add(parser);
if (parser instanceof SimpleOptionParser) {
Permission parent = getServer().getPluginManager().getPermission("randomteleport.manual.option.*");
for (String alias : ((SimpleOptionParser) parser).getAliases()) {
Permission perm = new Permission("randomteleport.manual.option." + alias, PermissionDefault.OP);
perm.addParent(parent, true);
try {
getServer().getPluginManager().addPermission(perm);
} catch (IllegalArgumentException ignored) {} // duplicate
}
}
}
/**

View File

@ -45,11 +45,11 @@ public class RandomTeleportCommand implements CommandExecutor {
return true;
}
} else if (args.length == 1) {
if ("--reload".equalsIgnoreCase(args[0])) {
if ("--reload".equalsIgnoreCase(args[0]) && sender.hasPermission("randomteleport.reload")) {
plugin.loadConfig();
plugin.sendMessage(sender, "reloaded");
return true;
} else if ("--stat".equalsIgnoreCase(args[0])) {
} else if ("--stat".equalsIgnoreCase(args[0]) && sender.hasPermission("randomteleport.stat")) {
//TODO: teleporter and searcher statistics
} else if (sender instanceof Player) {
runPreset(args[0].toLowerCase(), sender, (Player) sender, ((Player) sender).getLocation());
@ -57,8 +57,13 @@ public class RandomTeleportCommand implements CommandExecutor {
}
} else {
try {
if (sender.hasPermission("randomteleport.manual")){
plugin.parseAndRun(sender, getLocation(sender), args);
return true;
} else {
plugin.sendMessage(sender, "error.no-permission.general", "perm", "randomteleport.manual");
return true;
}
} catch (IllegalArgumentException e) {
if (args.length == 2) {
Player target = plugin.getServer().getPlayer(args[1]);

View File

@ -20,6 +20,7 @@ package de.themoep.randomteleport.searcher.options;
import de.themoep.randomteleport.searcher.RandomSearcher;
import org.apache.commons.lang.Validate;
import org.bukkit.command.CommandSender;
import java.util.Arrays;
import java.util.Set;
@ -51,9 +52,28 @@ public class SimpleOptionParser implements OptionParser {
parts[0] = parts[0].substring(1);
}
if (aliases.contains(parts[0].toLowerCase())) {
if (!hasAccess(searcher.getInitiator())) {
throw new IllegalArgumentException(searcher.getPlugin().getMessage(
searcher.getInitiator(), "error.no-permission.option",
"option", parts[0],
"perm", "randomteleport.manual.option." + aliases.iterator().next()));
}
ret |= parser.apply(searcher, Arrays.copyOfRange(parts, 1, parts.length));
}
}
return ret;
}
private boolean hasAccess(CommandSender initiator) {
for (String alias : aliases) {
if (initiator.hasPermission("randomteleport.manual.option." + alias)) {
return true;
}
}
return false;
}
public Set<String> getAliases() {
return aliases;
}
}

View File

@ -21,5 +21,6 @@ error:
already-searching: "&cA search is already in progress&c!"
no-permission:
general: "&cYou don't have permission to do that! &o({perm})"
option: "&cYou don't have permission to use the option {option}! &o({perm})"
preset: "&cYou don't have permission to use the preset &6{preset}&c! &o({perm})"
tp-others: "&cYou don't have permission to teleport other players! &o({perm})"

View File

@ -9,6 +9,7 @@ commands:
randomteleport:
aliases: [randomtp, rtp]
description: RandomTeleport command.
permission: randomteleport.use
usage: |
/<command> - uses the default preset
/<command> <preset> [<playername>] - uses a specific preset
@ -34,6 +35,12 @@ permissions:
randomteleport.use:
description: Gives permission to the command
default: op
randomteleport.manual:
description: Gives permission to manually specify parameters in the command
default: op
randomteleport.manual.option.*:
description: Gives permission to use certain options in the command
default: op
randomteleport.tpothers:
description: Gives permission to teleport other players
default: op