Permissions.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package me.km.permissions;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.UUID;
  6. import net.minecraft.command.CommandSource;
  7. import net.minecraft.entity.Entity;
  8. import net.minecraftforge.api.distmarker.Dist;
  9. import net.minecraftforge.api.distmarker.OnlyIn;
  10. @OnlyIn(Dist.DEDICATED_SERVER)
  11. public class Permissions {
  12. private final static UUID MARVINIUS = UUID.fromString("e41b5335-3c74-46e9-a6c5-dafc6334a477");
  13. private final static UUID KAJETANJOHANNES = UUID.fromString("51e240f9-ab10-4ea6-8a5d-779319f51257");
  14. private final HashMap<UUID, ArrayList<Integer>> playerGroups = new HashMap<>();
  15. private final ArrayList<HashSet<String>> stringGroupPerms = new ArrayList<>();
  16. private final HashSet<String> otherGroup = new HashSet<>();
  17. private final boolean debug;
  18. public Permissions(boolean debug) {
  19. this.debug = debug;
  20. addGroups();
  21. }
  22. public void clear() {
  23. playerGroups.clear();
  24. stringGroupPerms.clear();
  25. addGroups();
  26. }
  27. private void addGroups() {
  28. stringGroupPerms.add(new HashSet<>());
  29. stringGroupPerms.add(otherGroup);
  30. }
  31. public void addOtherGroupPermission(String perm) {
  32. otherGroup.add(perm);
  33. }
  34. public boolean has(Entity ent, String perm) {
  35. return has(ent.getUniqueID(), perm);
  36. }
  37. public boolean has(UUID uuid, String perm) {
  38. if(debug) {
  39. return true;
  40. }
  41. if(perm.equals("script") && (uuid.equals(MARVINIUS) || uuid.equals(KAJETANJOHANNES))) {
  42. return true;
  43. }
  44. ArrayList<Integer> groups = playerGroups.get(uuid);
  45. if(groups == null) {
  46. if(!stringGroupPerms.isEmpty()) {
  47. return stringGroupPerms.get(0).contains(perm);
  48. } else {
  49. return false;
  50. }
  51. }
  52. return groups.stream().anyMatch(i -> stringGroupPerms.get(i).contains(perm));
  53. }
  54. public boolean has(CommandSource cs, String perm) {
  55. Entity ent = cs.getEntity();
  56. if(ent != null) {
  57. return has(ent.getUniqueID(), perm);
  58. }
  59. return true;
  60. }
  61. public void register(UUID uuid, int groupId) {
  62. if(groupId < 0 || groupId >= stringGroupPerms.size()) {
  63. throw new IllegalArgumentException(String.format("%d' is no valid group id", groupId));
  64. }
  65. ArrayList<Integer> groups = playerGroups.get(uuid);
  66. if(groups == null) {
  67. groups = new ArrayList<>();
  68. // adding default group
  69. groups.add(0);
  70. playerGroups.put(uuid, groups);
  71. }
  72. groups.add(groupId);
  73. }
  74. public boolean unregister(UUID uuid, int groupId) {
  75. if(groupId < 0 || groupId >= stringGroupPerms.size()) {
  76. throw new IllegalArgumentException(String.format("%d' is no valid group id", groupId));
  77. }
  78. ArrayList<Integer> groups = playerGroups.get(uuid);
  79. if(groups == null) {
  80. return false;
  81. }
  82. return groups.remove((Integer) groupId);
  83. }
  84. public void register(int groupId, String perm) {
  85. if(perm.isEmpty()) {
  86. throw new IllegalArgumentException("empty permission string");
  87. }
  88. if(groupId == 1) {
  89. throw new IllegalArgumentException("id 1 is reserved for worldedit");
  90. }
  91. if(groupId >= stringGroupPerms.size()) {
  92. HashSet<String> set = new HashSet<>();
  93. set.add(perm);
  94. stringGroupPerms.add(set);
  95. return;
  96. }
  97. stringGroupPerms.get(groupId).add(perm);
  98. }
  99. }