123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package me.km.permissions;
- import java.util.ArrayList;
- import java.util.EnumSet;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.UUID;
- import me.km.KajetansMod;
- import me.km.api.Module;
- import net.minecraft.command.ICommandSender;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.server.MinecraftServer;
- import net.minecraft.util.text.TextFormatting;
- public class PermissionManager extends Module
- {
- private final HashMap<UUID, ArrayList<Integer>> playerGroups;
- private final ArrayList<EnumSet<Permissions>> enumGroupPerms;
- private final ArrayList<HashSet<String>> stringGroupPerms;
-
- public PermissionManager(String mname, String prefix, TextFormatting color)
- {
- super(mname, prefix, color);
- playerGroups = new HashMap<>();
- enumGroupPerms = new ArrayList<>();
- stringGroupPerms = new ArrayList<>();
- }
- public void clear()
- {
- playerGroups.clear();
- enumGroupPerms.clear();
- stringGroupPerms.clear();
- }
-
- // -----------------------------------------------------------------------------------
- // Permission-Check
- // -----------------------------------------------------------------------------------
-
- public boolean has(ICommandSender cs, Permissions perm)
- {
- if(KajetansMod.debugMode)
- {
- return true;
- }
- if(cs instanceof EntityPlayer)
- {
- ArrayList<Integer> groups = playerGroups.get(((EntityPlayer) cs).getUniqueID());
- if(groups == null)
- {
- //falling back to default group
- if(!enumGroupPerms.isEmpty())
- {
- return enumGroupPerms.get(0).contains(perm);
- }
- else
- {
- return false;
- }
- }
- // check for valid group id takes place at the registry
- return groups.stream().anyMatch(i -> enumGroupPerms.get(i).contains(perm));
- }
- return cs instanceof MinecraftServer;
- }
-
- public boolean has(ICommandSender cs, String perm)
- {
- if(KajetansMod.debugMode)
- {
- return true;
- }
- if(cs instanceof EntityPlayer)
- {
- ArrayList<Integer> groups = playerGroups.get(((EntityPlayer) cs).getUniqueID());
- if(groups == null)
- {
- //falling back to default group
- if(!stringGroupPerms.isEmpty())
- {
- return stringGroupPerms.get(0).contains(perm);
- }
- else
- {
- return false;
- }
- }
- // check for valid group id is done at the registry
- return groups.stream().anyMatch(i -> stringGroupPerms.get(i).contains(perm));
- }
- return cs instanceof MinecraftServer;
- }
-
- // -----------------------------------------------------------------------------------
- // Permission-Registry
- // -----------------------------------------------------------------------------------
-
- public void registerPlayerGroup(UUID uuid, int id)
- {
- if(id < 0 || id >= stringGroupPerms.size())
- {
- throw new IllegalArgumentException("'" + id + "' is no valid group id");
- }
- ArrayList<Integer> groups = playerGroups.get(uuid);
- if(groups == null)
- {
- groups = new ArrayList<>();
- // adding default group
- groups.add(0);
- playerGroups.put(uuid, groups);
- }
- groups.add(id);
- }
-
- public void registerGroupPermission(int id, String perm)
- {
- if(perm.isEmpty())
- {
- throw new IllegalArgumentException("empty permission string");
- }
- else if(Character.isUpperCase(perm.charAt(0)))
- {
- // guess it's an enum ...
- Permissions permission = Permissions.valueOf(perm);
- if(id >= enumGroupPerms.size())
- {
- EnumSet<Permissions> set = EnumSet.of(permission);
- enumGroupPerms.add(set);
- // keep other permissions at same id level
- stringGroupPerms.add(new HashSet<>());
- return;
- }
- enumGroupPerms.get(id).add(permission);
- }
- else
- {
- // just a string ...
- if(id >= stringGroupPerms.size())
- {
- HashSet<String> set = new HashSet<>();
- set.add(perm);
- stringGroupPerms.add(set);
- // keep other permissions at same id level
- enumGroupPerms.add(EnumSet.noneOf(Permissions.class));
- return;
- }
- stringGroupPerms.get(id).add(perm);
- }
- }
- }
|