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> playerGroups = new HashMap<>(); private final ArrayList> stringGroupPerms = new ArrayList<>(); private final HashSet 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); } // ------------------------------------------------------------------------- // Permission-Check // ------------------------------------------------------------------------- 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 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; } // ------------------------------------------------------------------------- // Permission-Registry // ------------------------------------------------------------------------- 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 groups = playerGroups.get(uuid); if(groups == null) { groups = new ArrayList<>(); // adding default group 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 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 set = new HashSet<>(); set.add(perm); stringGroupPerms.add(set); return; } stringGroupPerms.get(id).add(perm); } }