123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package me.km.permissions;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.UUID;
- import net.minecraft.command.CommandSource;
- import net.minecraft.entity.Entity;
- import net.minecraftforge.api.distmarker.Dist;
- import net.minecraftforge.api.distmarker.OnlyIn;
- @OnlyIn(Dist.DEDICATED_SERVER)
- public class PermissionManager
- {
- private final HashMap<UUID, ArrayList<Integer>> playerGroups = new HashMap<>();
- private final ArrayList<HashSet<String>> stringGroupPerms = new ArrayList<>();
- private final HashSet<String> otherGroup = new HashSet<>();
- private final boolean debug;
-
- public PermissionManager(boolean debug)
- {
- this.debug = debug;
- addGroups();
- }
- public void clear()
- {
- playerGroups.clear();
- stringGroupPerms.clear();
- addGroups();
- }
-
- private void addGroups()
- {
- stringGroupPerms.add(new HashSet<>());
- stringGroupPerms.add(otherGroup);
- }
-
- public void addOtherGroupPermission(String perm)
- {
- otherGroup.add(perm);
- }
-
-
-
-
-
- private final static UUID MARVINIUS = UUID.fromString("e41b5335-3c74-46e9-a6c5-dafc6334a477");
- private final static UUID KAJETANJOHANNES = UUID.fromString("51e240f9-ab10-4ea6-8a5d-779319f51257");
-
- public boolean hasPermission(Entity ent, String perm)
- {
- return hasPermission(ent.getUniqueID(), perm);
- }
-
- public boolean hasPermission(UUID uuid, String perm)
- {
- if(debug)
- {
- return true;
- }
- if(perm.equals("script"))
- {
- if(uuid.equals(MARVINIUS) || uuid.equals(KAJETANJOHANNES))
- {
- return true;
- }
- }
- ArrayList<Integer> groups = playerGroups.get(uuid);
- if(groups == null)
- {
- if(!stringGroupPerms.isEmpty())
- {
- return stringGroupPerms.get(0).contains(perm);
- }
- else
- {
- return false;
- }
- }
- return groups.stream().anyMatch(i -> stringGroupPerms.get(i).contains(perm));
- }
-
- public boolean hasPermission(CommandSource cs, String perm)
- {
- Entity ent = cs.getEntity();
- if(ent != null)
- {
- return hasPermission(ent.getUniqueID(), perm);
- }
- return true;
- }
-
-
-
-
- public void registerPlayerGroup(UUID uuid, int id)
- {
- if(id < 0 || id >= stringGroupPerms.size())
- {
- throw new IllegalArgumentException(String.format("%d' is no valid group id", id));
- }
- ArrayList<Integer> groups = playerGroups.get(uuid);
- if(groups == null)
- {
- groups = new ArrayList<>();
-
- groups.add(0);
- playerGroups.put(uuid, groups);
- }
- groups.add(id);
- }
-
- public boolean unregisterPlayer(UUID uuid, int id)
- {
- if(id < 0 || id >= stringGroupPerms.size())
- {
- throw new IllegalArgumentException(String.format("%d' is no valid group id", id));
- }
- ArrayList<Integer> groups = playerGroups.get(uuid);
- if(groups == null)
- {
- return false;
- }
- return groups.remove((Integer) id);
- }
-
- public void registerGroupPermission(int id, String perm)
- {
- if(perm.isEmpty())
- {
- throw new IllegalArgumentException("empty permission string");
- }
- if(id == 1)
- {
- throw new IllegalArgumentException("id 1 is reserved for worldedit");
- }
- if(id >= stringGroupPerms.size())
- {
- HashSet<String> set = new HashSet<>();
- set.add(perm);
- stringGroupPerms.add(set);
- return;
- }
- stringGroupPerms.get(id).add(perm);
- }
- }
|