Skip to content

Commit c27129f

Browse files
committed
Added permission testing for having one or all of a list of permissions, and disallowed sponge as it's not compatible
1 parent 5bddb90 commit c27129f

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77

88
group = 'com.datdeveloper'
9-
version = "${minecraftVersion}-1.4.1"
9+
version = "${minecraftVersion}-1.4.2"
1010

1111
java {
1212
archivesBaseName = 'datmoddingapi'
Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.datdeveloper.datmoddingapi.permissions;
22

3-
import com.datdeveloper.datmoddingapi.DatConfig;
43
import net.minecraft.commands.CommandSource;
54
import net.minecraft.server.level.ServerPlayer;
65
import net.minecraftforge.server.permission.PermissionAPI;
7-
import net.minecraftforge.server.permission.nodes.PermissionDynamicContext;
86
import net.minecraftforge.server.permission.nodes.PermissionNode;
9-
import org.spongepowered.api.entity.living.player.Player;
107
import 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
}

src/main/java/com/datdeveloper/datmoddingapi/permissions/EPermissionSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
public enum EPermissionSystem {
44
AUTO,
55
FORGE,
6-
SPONGE
6+
// SPONGE
77
}

0 commit comments

Comments
 (0)