Use implicit permissions checks for classes and arenas.

Removes the dynamic registration of permissions for classes and arenas.

This means that it is no longer enough to just check whether a Player.hasPermission("mobarena.classes.Knight"), but we also need to check if the Player.isPermissionSet("mobarena.classes.Knight"), which it won't be by default.

To make the stringly typed parts of this dynamic permissions checking more focused, ArenaClass and Arena now both have hasPermission(Player) methods, so the actual permissions checking happens on the objects themselves, rather than from the caller of these methods.
This commit is contained in:
Andreas Troelsen 2018-04-26 18:29:25 +02:00
parent 31aa4c15a1
commit 94d198c4d0
6 changed files with 31 additions and 45 deletions

View File

@ -137,6 +137,11 @@ public class ArenaClass
this.armor = armor;
}
public boolean hasPermission(Player p) {
String perm = "mobarena.classes." + configName;
return !p.isPermissionSet(perm) || p.hasPermission(perm);
}
/**
* Grants all of the class items and armor to the given player.
* The normal items will be added to the inventory normally, while the

View File

@ -63,9 +63,9 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.stream.Collectors;
public class ArenaImpl implements Arena
{
@ -657,6 +657,12 @@ public class ArenaImpl implements Arena
cleanup();
}
@Override
public boolean hasPermission(Player p) {
String perm = "mobarena.arenas." + name;
return !p.isPermissionSet(perm) || p.hasPermission(perm);
}
@Override
public boolean playerJoin(Player p, Location loc)
{
@ -1353,21 +1359,19 @@ public class ArenaImpl implements Arena
@Override
public void assignRandomClass(Player p)
{
Random r = new Random();
List<String> classes = new LinkedList<>(this.classes.keySet());
List<ArenaClass> classes = this.classes.values().stream()
.filter(c -> c.hasPermission(p))
.collect(Collectors.toList());
String className = classes.remove(r.nextInt(classes.size()));
while (!plugin.has(p, "mobarena.classes." + className))
{
if (classes.isEmpty())
{
plugin.getLogger().info("Player '" + p.getName() + "' has no class permissions!");
playerLeave(p);
return;
}
className = classes.remove(r.nextInt(classes.size()));
if (classes.isEmpty()) {
plugin.getLogger().info("Player '" + p.getName() + "' has no class permissions!");
playerLeave(p);
return;
}
int index = MobArena.random.nextInt(classes.size());
String className = classes.get(index).getConfigName();
assignClass(p, className);
messenger.tell(p, Msg.LOBBY_CLASS_PICKED, this.classes.get(className).getConfigName());
}
@ -1621,7 +1625,7 @@ public class ArenaImpl implements Arena
messenger.tell(p, Msg.JOIN_ALREADY_PLAYING);
else if (running)
messenger.tell(p, Msg.JOIN_ARENA_IS_RUNNING);
else if (!plugin.has(p, "mobarena.arenas." + configName()))
else if (!hasPermission(p))
messenger.tell(p, Msg.JOIN_ARENA_PERMISSION);
else if (getMaxPlayers() > 0 && lobbyPlayers.size() >= getMaxPlayers())
messenger.tell(p, Msg.JOIN_PLAYER_LIMIT_REACHED);

View File

@ -1061,14 +1061,15 @@ public class ArenaListener
if (!arena.getClasses().containsKey(className) && !className.equals("random"))
return;
ArenaClass newAC = arena.getClasses().get(className);
// Check for permission.
if (!plugin.has(p, "mobarena.classes." + className) && !className.equals("random")) {
if (!newAC.hasPermission(p) && !className.equals("random")) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PERMISSION);
return;
}
ArenaClass oldAC = arena.getArenaPlayer(p).getArenaClass();
ArenaClass newAC = arena.getClasses().get(className);
// Same class, do nothing.
if (newAC.equals(oldAC)) {

View File

@ -19,9 +19,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import java.util.ArrayList;
import java.util.Arrays;
@ -140,7 +137,7 @@ public class ArenaMasterImpl implements ArenaMaster
public List<Arena> getPermittedArenas(Player p) {
List<Arena> result = new ArrayList<>(arenas.size());
for (Arena arena : arenas)
if (plugin.has(p, "mobarena.arenas." + arena.configName()))
if (arena.hasPermission(p))
result.add(arena);
return result;
}
@ -148,7 +145,7 @@ public class ArenaMasterImpl implements ArenaMaster
public List<Arena> getEnabledAndPermittedArenas(Player p) {
List<Arena> result = new ArrayList<>(arenas.size());
for (Arena arena : arenas)
if (arena.isEnabled() && plugin.has(p, "mobarena.arenas." + arena.configName()))
if (arena.isEnabled() && arena.hasPermission(p))
result.add(arena);
return result;
}
@ -338,9 +335,6 @@ public class ArenaMasterImpl implements ArenaMaster
loadClassPermissions(arenaClass, section);
loadClassLobbyPermissions(arenaClass, section);
// Register the permission.
registerPermission("mobarena.classes." + lowercase, PermissionDefault.TRUE).addParent("mobarena.classes", true);
// Check for class chests
Location cc = parseLocation(section, "classchest", null);
arenaClass.setClassChest(cc);
@ -486,8 +480,6 @@ public class ArenaMasterImpl implements ArenaMaster
// Remove the class from the map.
classes.remove(lowercase);
unregisterPermission("mobarena.arenas." + lowercase);
}
public boolean addClassPermission(String classname, String perm) {
@ -621,7 +613,6 @@ public class ArenaMasterImpl implements ArenaMaster
ConfigUtils.addIfEmpty(plugin, "waves.yml", makeSection(section, "waves"));
Arena arena = new ArenaImpl(plugin, section, arenaname, world);
registerPermission("mobarena.arenas." + arenaname.toLowerCase(), PermissionDefault.TRUE);
arenas.add(arena);
plugin.getLogger().info("Loaded arena '" + arenaname + "'");
return arena;
@ -669,7 +660,6 @@ public class ArenaMasterImpl implements ArenaMaster
public void removeArenaNode(Arena arena) {
arenas.remove(arena);
unregisterPermission("mobarena.arenas." + arena.configName());
config.set("arenas." + arena.configName(), null);
plugin.saveConfig();
@ -690,20 +680,4 @@ public class ArenaMasterImpl implements ArenaMaster
public void saveConfig() {
plugin.saveConfig();
}
private Permission registerPermission(String permString, PermissionDefault value) {
PluginManager pm = plugin.getServer().getPluginManager();
Permission perm = pm.getPermission(permString);
if (perm == null) {
perm = new Permission(permString);
perm.setDefault(value);
pm.addPermission(perm);
}
return perm;
}
private void unregisterPermission(String s) {
plugin.getServer().getPluginManager().removePermission(s);
}
}

View File

@ -54,7 +54,7 @@ public class PickClassCommand implements Command
}
// Check for permission.
if (!am.getPlugin().has(p, "mobarena.classes." + lowercase) && !lowercase.equals("random")) {
if (!ac.hasPermission(p) && !lowercase.equals("random")) {
arena.getMessenger().tell(p, Msg.LOBBY_CLASS_PERMISSION);
return true;
}

View File

@ -147,6 +147,8 @@ public interface Arena
void forceStart();
void forceEnd();
boolean hasPermission(Player p);
boolean playerJoin(Player p, Location loc);