mirror of
https://github.com/garbagemule/MobArena.git
synced 2025-02-16 04:21:29 +01:00
Use the Things API for per-class permissions.
This replaces the Map<String, Boolean> approach in ArenaClass with a List<Thing> approach. This greatly simplifies how per-class permissions are handled.
This commit is contained in:
parent
c1d1728144
commit
a9d0ca0828
@ -10,16 +10,11 @@ import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ArenaClass
|
||||
@ -29,8 +24,8 @@ public class ArenaClass
|
||||
private List<Thing> armor;
|
||||
private List<Thing> items;
|
||||
private List<Thing> effects;
|
||||
private Map<String,Boolean> perms;
|
||||
private Map<String,Boolean> lobbyperms;
|
||||
private List<Thing> perms;
|
||||
private List<Thing> lobbyperms;
|
||||
private boolean unbreakableWeapons, unbreakableArmor;
|
||||
private Thing price;
|
||||
private Location classchest;
|
||||
@ -46,8 +41,8 @@ public class ArenaClass
|
||||
this.items = new ArrayList<>();
|
||||
this.armor = new ArrayList<>(4);
|
||||
this.effects = new ArrayList<>();
|
||||
this.perms = new HashMap<>();
|
||||
this.lobbyperms = new HashMap<>();
|
||||
this.perms = new ArrayList<>();
|
||||
this.lobbyperms = new ArrayList<>();
|
||||
|
||||
this.unbreakableWeapons = unbreakableWeapons;
|
||||
this.unbreakableArmor = unbreakableArmor;
|
||||
@ -178,66 +173,35 @@ public class ArenaClass
|
||||
|
||||
/**
|
||||
* Add a permission value to the class.
|
||||
* @param perm the permission
|
||||
* @param value the value
|
||||
*/
|
||||
public void addPermission(String perm, boolean value) {
|
||||
perms.put(perm, value);
|
||||
public void addPermission(Thing permission) {
|
||||
perms.add(permission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an unmodifiable map of permissions and values for the class.
|
||||
* @return a map of permissions and values
|
||||
*/
|
||||
public Map<String,Boolean> getPermissions() {
|
||||
return Collections.unmodifiableMap(perms);
|
||||
public List<Thing> getPermissions() {
|
||||
return Collections.unmodifiableList(perms);
|
||||
}
|
||||
|
||||
public void addLobbyPermission(String perm, boolean value) {
|
||||
lobbyperms.put(perm, value);
|
||||
public void addLobbyPermission(Thing permission) {
|
||||
lobbyperms.add(permission);
|
||||
}
|
||||
|
||||
public Map<String,Boolean> getLobbyPermissions() {
|
||||
return Collections.unmodifiableMap(lobbyperms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grant the given player all the permissions of the class.
|
||||
* All permissions will be attached to a PermissionAttachment object, which
|
||||
* will be returned to the caller.
|
||||
* @param plugin a MobArena instance
|
||||
* @param p a player
|
||||
* @return the PermissionAttachment with all the permissions
|
||||
*/
|
||||
public PermissionAttachment grantPermissions(MobArena plugin, Player p) {
|
||||
if (perms.isEmpty()) return null;
|
||||
|
||||
PermissionAttachment pa = p.addAttachment(plugin);
|
||||
grantPerms(pa, perms, p);
|
||||
return pa;
|
||||
public void grantPermissions(Player p) {
|
||||
perms.forEach(perm -> perm.giveTo(p));
|
||||
}
|
||||
|
||||
public PermissionAttachment grantLobbyPermissions(MobArena plugin, Player p) {
|
||||
if (lobbyperms.isEmpty()) return null;
|
||||
|
||||
PermissionAttachment pa = p.addAttachment(plugin);
|
||||
grantPerms(pa, lobbyperms, p);
|
||||
return pa;
|
||||
}
|
||||
|
||||
private void grantPerms(PermissionAttachment pa, Map<String,Boolean> map, Player p) {
|
||||
for (Entry<String,Boolean> entry : map.entrySet()) {
|
||||
try {
|
||||
pa.setPermission(entry.getKey(), entry.getValue());
|
||||
}
|
||||
catch (Exception e) {
|
||||
String perm = entry.getKey() + ":" + entry.getValue();
|
||||
String player = p.getName();
|
||||
|
||||
pa.getPlugin().getLogger().warning("[PERM00] Failed to attach permission '" + perm + "' to player '" + player + " with class " + this.configName
|
||||
+ "'.\nPlease verify that your class permissions are well-formed.");
|
||||
}
|
||||
}
|
||||
public void grantLobbyPermissions(Player p) {
|
||||
lobbyperms.forEach(perm -> perm.giveTo(p));
|
||||
}
|
||||
|
||||
public Location getClassChest() {
|
||||
|
@ -1166,7 +1166,7 @@ public class ArenaImpl implements Arena
|
||||
removePotionEffects(p);
|
||||
arenaPlayer.setArenaClass(arenaClass);
|
||||
arenaClass.grantItems(p);
|
||||
arenaClass.grantLobbyPermissions(plugin, p);
|
||||
arenaClass.grantLobbyPermissions(p);
|
||||
|
||||
autoReady(p);
|
||||
}
|
||||
@ -1240,7 +1240,7 @@ public class ArenaImpl implements Arena
|
||||
inv.setBoots(boots);
|
||||
inv.setItemInOffHand(offhand);
|
||||
|
||||
arenaClass.grantLobbyPermissions(plugin, p);
|
||||
arenaClass.grantLobbyPermissions(p);
|
||||
|
||||
autoReady(p);
|
||||
}
|
||||
@ -1290,7 +1290,7 @@ public class ArenaImpl implements Arena
|
||||
return;
|
||||
}
|
||||
removePermissionAttachments(player);
|
||||
arenaClass.grantPermissions(plugin, player);
|
||||
arenaClass.grantPermissions(player);
|
||||
}
|
||||
|
||||
private void removePermissionAttachments(Player player) {
|
||||
|
@ -425,33 +425,19 @@ public class ArenaMasterImpl implements ArenaMaster
|
||||
}
|
||||
|
||||
private void loadClassPermissions(ArenaClass arenaClass, ConfigurationSection section) {
|
||||
List<String> perms = section.getStringList("permissions");
|
||||
if (perms.isEmpty()) return;
|
||||
|
||||
for (String perm : perms) {
|
||||
// If the permission starts with - or ^, it must be revoked.
|
||||
boolean value = true;
|
||||
if (perm.startsWith("-") || perm.startsWith("^")) {
|
||||
perm = perm.substring(1).trim();
|
||||
value = false;
|
||||
}
|
||||
arenaClass.addPermission(perm, value);
|
||||
}
|
||||
section.getStringList("permissions").stream()
|
||||
.map(perm -> "perm:" + perm)
|
||||
.map(plugin.getThingManager()::parse)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(arenaClass::addPermission);
|
||||
}
|
||||
|
||||
private void loadClassLobbyPermissions(ArenaClass arenaClass, ConfigurationSection section) {
|
||||
List<String> perms = section.getStringList("lobby-permissions");
|
||||
if (perms.isEmpty()) return;
|
||||
|
||||
for (String perm : perms) {
|
||||
// If the permission starts with - or ^, it must be revoked.
|
||||
boolean value = true;
|
||||
if (perm.startsWith("-") || perm.startsWith("^")) {
|
||||
perm = perm.substring(1).trim();
|
||||
value = false;
|
||||
}
|
||||
arenaClass.addLobbyPermission(perm, value);
|
||||
}
|
||||
section.getStringList("lobby-permissions").stream()
|
||||
.map(perm -> "perm:" + perm)
|
||||
.map(plugin.getThingManager()::parse)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(arenaClass::addLobbyPermission);
|
||||
}
|
||||
|
||||
public ArenaClass createClassNode(String classname, PlayerInventory inv, boolean safe) {
|
||||
|
@ -4,11 +4,11 @@ import com.garbagemule.MobArena.ArenaClass;
|
||||
import com.garbagemule.MobArena.commands.Command;
|
||||
import com.garbagemule.MobArena.commands.CommandInfo;
|
||||
import com.garbagemule.MobArena.framework.ArenaMaster;
|
||||
import com.garbagemule.MobArena.things.Thing;
|
||||
import com.garbagemule.MobArena.util.TextUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.List;
|
||||
|
||||
@CommandInfo(
|
||||
name = "listclassperms",
|
||||
@ -33,18 +33,14 @@ public class ListClassPermsCommand implements Command
|
||||
}
|
||||
|
||||
am.getGlobalMessenger().tell(sender, "Permissions for '" + className + "':");
|
||||
Map<String,Boolean> perms = arenaClass.getPermissions();
|
||||
List<Thing> perms = arenaClass.getPermissions();
|
||||
if (perms.isEmpty()) {
|
||||
am.getGlobalMessenger().tell(sender, "<none>");
|
||||
return true;
|
||||
}
|
||||
|
||||
for (Entry<String,Boolean> entry : arenaClass.getPermissions().entrySet()) {
|
||||
String perm = entry.getKey();
|
||||
if (!entry.getValue()) {
|
||||
perm = "^" + perm;
|
||||
}
|
||||
am.getGlobalMessenger().tell(sender, "- " + perm);
|
||||
for (Thing perm : perms) {
|
||||
am.getGlobalMessenger().tell(sender, perm.toString());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user