1
1
package com .datdeveloper .datmoddingapi .permissions ;
2
2
3
- import com .datdeveloper .datmoddingapi .DatConfig ;
4
3
import net .minecraft .commands .CommandSource ;
5
4
import net .minecraft .server .level .ServerPlayer ;
6
5
import net .minecraftforge .server .permission .PermissionAPI ;
7
- import net .minecraftforge .server .permission .nodes .PermissionDynamicContext ;
8
6
import net .minecraftforge .server .permission .nodes .PermissionNode ;
9
- import org .spongepowered .api .entity .living .player .Player ;
10
7
import org .spongepowered .api .service .permission .Subject ;
11
8
9
+ import java .util .Arrays ;
10
+ import java .util .Collection ;
11
+ import java .util .Collections ;
12
+ import java .util .List ;
13
+ import java .util .stream .Stream ;
14
+
12
15
/**
13
16
* A permissionAPI agnostic system that allows for testing permission nodes without knowledge of the underlying PermissionAPI
14
17
* Currently handles Forge's permission api and sponges permission api
@@ -19,26 +22,61 @@ public class DatPermissions {
19
22
*/
20
23
public static boolean spongeLoaded = false ;
21
24
25
+ private static EPermissionSystem getPermissionSystem () {
26
+ // Sponge is not supported currently
27
+ // if (DatConfig.getPermissionSystem() == EPermissionSystem.AUTO) return (spongeLoaded ? EPermissionSystem.SPONGE : EPermissionSystem.FORGE);
28
+ // else return DatConfig.getPermissionSystem();
29
+ return EPermissionSystem .FORGE ;
30
+ }
31
+
22
32
/**
23
33
* Checks if the given CommandSource has the given permission
24
34
* @param source The CommandSource being tested
25
35
* @param permissionNode The permission node to test
26
- * @param context Extra context to the permission
27
36
* @return true if the CommandSource has permission
28
37
*/
29
- public static boolean hasPermission (final CommandSource source , final PermissionNode <Boolean > permissionNode , final PermissionDynamicContext <?>... context ) {
30
- final EPermissionSystem api ;
38
+ public static boolean hasPermission (final CommandSource source , final PermissionNode <Boolean > permissionNode ) {
39
+ return hasAnyPermissions (source , permissionNode );
40
+ }
31
41
32
- if (DatConfig .getPermissionSystem () == EPermissionSystem .AUTO ) api = (spongeLoaded ? EPermissionSystem .SPONGE : EPermissionSystem .FORGE );
33
- else api = DatConfig .getPermissionSystem ();
42
+ /**
43
+ * Checks if the given CommandSource has any of the given permission
44
+ * @param source The CommandSource being tested
45
+ * @param permissionNodes The permission nodes to test
46
+ * @return true if the CommandSource has any of the given permissions
47
+ */
48
+ @ SafeVarargs
49
+ public static boolean hasAnyPermissions (final CommandSource source , final PermissionNode <Boolean >... permissionNodes ) {
50
+ return hasPermissions (source , Arrays .stream (permissionNodes ).toList ())
51
+ .anyMatch (node -> node );
52
+ }
53
+
54
+ /**
55
+ * Checks if the given CommandSource has all the given permission
56
+ * @param source The CommandSource being tested
57
+ * @param permissionNodes The permission nodes to test
58
+ * @return true if the CommandSource has all the given permissions
59
+ */
60
+ @ SafeVarargs
61
+ public static boolean hasAllPermissions (final CommandSource source , final PermissionNode <Boolean >... permissionNodes ) {
62
+ return hasPermissions (source , Arrays .stream (permissionNodes ).toList ())
63
+ .allMatch (node -> node );
64
+ }
65
+
66
+ private static Stream <Boolean > hasPermissions (final CommandSource source , final Collection <PermissionNode <Boolean >> permissionNodes ) {
67
+ final EPermissionSystem api = getPermissionSystem ();
34
68
35
69
if (api == EPermissionSystem .FORGE ) {
36
- if (!(source instanceof ServerPlayer player )) return true ;
37
- return PermissionAPI .getPermission (player , permissionNode , context );
70
+ final ServerPlayer player ;
71
+ if (source instanceof ServerPlayer ) player = (ServerPlayer ) source ;
72
+ else player = null ;
73
+
74
+ return permissionNodes .stream ().map (node -> player == null || PermissionAPI .getPermission (player , node ));
38
75
} else {
39
- if (!(source instanceof Player )) return true ;
40
- final Subject subject = (Subject ) source ;
41
- return subject .hasPermission (permissionNode .getNodeName ());
76
+ final Subject player ;
77
+ if (source instanceof Subject ) player = (Subject ) source ;
78
+ else player = null ;
79
+ return permissionNodes .stream ().map (node -> player == null || player .hasPermission (node .getNodeName ()));
42
80
}
43
81
}
44
82
}
0 commit comments