11package com .datdeveloper .datmoddingapi .permissions ;
22
3- import com .datdeveloper .datmoddingapi .DatConfig ;
43import net .minecraft .commands .CommandSource ;
54import net .minecraft .server .level .ServerPlayer ;
65import net .minecraftforge .server .permission .PermissionAPI ;
7- import net .minecraftforge .server .permission .nodes .PermissionDynamicContext ;
86import net .minecraftforge .server .permission .nodes .PermissionNode ;
9- import org .spongepowered .api .entity .living .player .Player ;
107import org .spongepowered .api .service .permission .Subject ;
118
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+
1215/**
1316 * A permissionAPI agnostic system that allows for testing permission nodes without knowledge of the underlying PermissionAPI
1417 * Currently handles Forge's permission api and sponges permission api
@@ -19,26 +22,61 @@ public class DatPermissions {
1922 */
2023 public static boolean spongeLoaded = false ;
2124
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+
2232 /**
2333 * Checks if the given CommandSource has the given permission
2434 * @param source The CommandSource being tested
2535 * @param permissionNode The permission node to test
26- * @param context Extra context to the permission
2736 * @return true if the CommandSource has permission
2837 */
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+ }
3141
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 ();
3468
3569 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 ));
3875 } 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 ()));
4280 }
4381 }
4482}
0 commit comments