NucleoidMC / leukocyte

Region-based protection controllable by API or commands
GNU Lesser General Public License v3.0
39 stars 13 forks source link

1.20.1 LuckPerms support #71

Open MJRamon opened 4 months ago

MJRamon commented 4 months ago

Does it support LuckPerms? If not, can you add support for it? Thank you.

JustusJG commented 1 month ago

I've made a LuckPerms support but it's for 1.21. Maybe it works in 1.20.1 too. It's not perfect, it works for me.

diff --git a/build.gradle b/build.gradle
index ca86766..b616c53 100644
--- a/build.gradle
+++ b/build.gradle
@@ -9,10 +9,10 @@ version = project.mod_version + "+" + project.minecraft_version
 group = project.maven_group

 repositories {
+  mavenCentral()
   maven { url = "https://maven.nucleoid.xyz" }
   maven { url = "https://maven.gegy.dev/" }
   maven { url = "https://jitpack.io" }
   maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
 }

 dependencies {
@@ -26,6 +26,8 @@ dependencies {

   modCompileOnly "dev.gegy:player-roles-api:1.6.8"
   modCompileOnly "me.lucko:fabric-permissions-api:0.2-SNAPSHOT"
+
+  compileOnly 'net.luckperms:api:5.4'
 }

 processResources {
diff --git a/src/main/java/xyz/nucleoid/leukocyte/roles/RoleAccessor.java b/src/main/java/xyz/nucleoid/leukocyte/roles/RoleAccessor.java
index 62d2703..5e58843 100644
--- a/src/main/java/xyz/nucleoid/leukocyte/roles/RoleAccessor.java
+++ b/src/main/java/xyz/nucleoid/leukocyte/roles/RoleAccessor.java
@@ -3,12 +3,18 @@ package xyz.nucleoid.leukocyte.roles;
 import dev.gegy.roles.api.PlayerRolesApi;
 import dev.gegy.roles.api.Role;
 import net.fabricmc.loader.api.FabricLoader;
+import net.luckperms.api.LuckPerms;
+import net.luckperms.api.LuckPermsProvider;
+import net.luckperms.api.model.group.Group;
 import net.minecraft.server.network.ServerPlayerEntity;

+import java.util.Collection;
+import java.util.Comparator;
 import java.util.stream.Stream;

 public interface RoleAccessor {
-    RoleAccessor INSTANCE = FabricLoader.getInstance().isModLoaded("player_roles") ? new PlayerRoles() : new None();
+    RoleAccessor INSTANCE = FabricLoader.getInstance().isModLoaded("player_roles") ? new PlayerRoles() :
+            FabricLoader.getInstance().isModLoaded("luckperms") ? new LPGroups() : new None();

     Stream<String> getAllRoles();

@@ -48,4 +54,23 @@ public interface RoleAccessor {
             }
         }
     }
+
+    final class LPGroups implements RoleAccessor {
+        LPGroups() {
+        }
+
+        @Override
+        public Stream<String> getAllRoles() {
+            Collection<Group> groups = LuckPermsProvider.get().getGroupManager().getLoadedGroups();
+
+            return groups.stream()
+                    .sorted(Comparator.comparingInt(group -> group.getWeight().orElse(0)))
+                    .map(Group::getName);
+        }
+
+        @Override
+        public boolean hasRole(ServerPlayerEntity player, String roleId) {
+            return PermissionAccessor.INSTANCE.hasPermission(player, "group." + roleId);
+        }
+    }
 }