Permissions.java 3.6 KB

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