Merge branch 'Minestom:master' into main

This commit is contained in:
Matt Worzala 2023-05-11 21:31:15 +03:00 committed by GitHub
commit bbffbabe9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 6 deletions

View File

@ -6,6 +6,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBT;
import java.util.Map;
public class PlayerMeta extends LivingEntityMeta {
public static final byte OFFSET = LivingEntityMeta.MAX_OFFSET;
public static final byte MAX_OFFSET = OFFSET + 1;
@ -108,6 +110,8 @@ public class PlayerMeta extends LivingEntityMeta {
}
public void setLeftShoulderEntityData(@Nullable NBT value) {
if (value == null) value = NBT.Compound(Map.of());
super.metadata.setIndex(OFFSET + 4, Metadata.NBT(value));
}
@ -117,6 +121,8 @@ public class PlayerMeta extends LivingEntityMeta {
}
public void setRightShoulderEntityData(@Nullable NBT value) {
if (value == null) value = NBT.Compound(Map.of());
super.metadata.setIndex(OFFSET + 5, Metadata.NBT(value));
}

View File

@ -6,6 +6,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.parser.SNBTParser;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Represents an object which can have permissions.
@ -56,6 +57,8 @@ public interface PermissionHandler {
/**
* Gets if this handler has the permission {@code permission}.
* This method will also pattern match for wildcards. For example, if this handler has the permission {@code "*"}, this method will always return true.
* However, if this handler has the permission {@code "foo.b*r.baz"}, this method will return true if {@code permission} is {@code "foo.baaar.baz"} or {@code "foo.br.baz}, but not {@code "foo.bar.bz"}.
* <p>
* Uses {@link Permission#equals(Object)} internally.
*
@ -67,6 +70,15 @@ public interface PermissionHandler {
if (permissionLoop.equals(permission)) {
return true;
}
String permissionLoopName = permissionLoop.getPermissionName();
if (permissionLoopName.contains("*")) {
// Sanitize permissionLoopName
String regexSanitized = Pattern.quote(permissionLoopName).replace("*", "\\E(.*)\\Q"); // Replace * with regex
// pattern matching for wildcards, where foo.b*r.baz matches foo.baaaar.baz or foo.bar.baz
if (permission.getPermissionName().matches(regexSanitized)) {
return true;
}
}
}
return false;
}
@ -100,13 +112,17 @@ public interface PermissionHandler {
* @return true if the handler has the permission, false otherwise
*/
default boolean hasPermission(@NotNull String permissionName, @Nullable PermissionVerifier permissionVerifier) {
final Permission permission = getPermission(permissionName);
Permission permission = getPermission(permissionName);
if (permission != null) {
// Verify using the permission verifier
return permissionVerifier == null || permissionVerifier.isValid(permission.getNBTData());
if (permission == null && permissionVerifier == null) {
permission = new Permission(permissionName, null);
} else if (permission == null) {
return false;
}
return false;
// If no permission verifier, hand off to no-verifier hasPermission for wildcard support
if(permissionVerifier == null) { return hasPermission(permission); }
// Verify using the permission verifier
return permissionVerifier.isValid(permission.getNBTData());
}
/**

View File

@ -18,7 +18,7 @@ public class TestPermissions {
private Player player;
private Permission permission1, permission2;
private Permission permission1, permission2, permission3, wildcard;
@BeforeEach
public void init() {
@ -42,6 +42,10 @@ public class TestPermissions {
);
permission2 = new Permission("perm.name2");
permission3 = new Permission("perm.name2.sub.sub2");
wildcard = new Permission("*");
}
@Test
@ -76,6 +80,63 @@ public class TestPermissions {
assertFalse(player.hasPermission("perm.name2", Objects::nonNull));
}
@Test
public void hasPatternMatchingWildcard() {
Permission permission = new Permission("foo.b*r.baz");
Permission match = new Permission("foo.baaar.baz");
Permission match2 = new Permission("foo.br.baz");
String match3 = "foo.br.baz";
String match4 = "foo.baaar.baz";
Permission nomatch = new Permission("foo.br.bz");
Permission nomatch2 = new Permission("foo.b.baz");
assertFalse(player.hasPermission(match));
assertFalse(player.hasPermission(match2));
assertFalse(player.hasPermission(nomatch));
assertFalse(player.hasPermission(nomatch2));
player.addPermission(permission);
assertTrue(player.hasPermission(match));
assertTrue(player.hasPermission(match2));
assertTrue(player.hasPermission(match3));
assertTrue(player.hasPermission(match4));
assertFalse(player.hasPermission(nomatch));
assertFalse(player.hasPermission(nomatch2));
}
@Test
public void hasPermissionWildcard() {
Permission permission = new Permission("foo.b*");
Permission match = new Permission("foo.baaar.baz");
Permission match2 = new Permission("foo.b");
String match3 = "foo.b";
String match4 = "foo.baaar.baz";
Permission nomatch = new Permission("foo.");
Permission nomatch2 = new Permission("foo/b");
assertFalse(player.hasPermission(match));
assertFalse(player.hasPermission(match2));
assertFalse(player.hasPermission(nomatch));
assertFalse(player.hasPermission(nomatch2));
player.addPermission(permission);
assertTrue(player.hasPermission(match));
assertTrue(player.hasPermission(match2));
assertTrue(player.hasPermission(match3));
assertTrue(player.hasPermission(match4));
assertFalse(player.hasPermission(nomatch));
assertFalse(player.hasPermission(nomatch2));
}
@Test
public void hasAllPermissionsWithWildcard() {
assertFalse(player.hasPermission(permission2));
assertFalse(player.hasPermission(permission3));
player.addPermission(wildcard);
assertTrue(player.hasPermission(permission2));
assertTrue(player.hasPermission(permission3));
}
@AfterEach
public void cleanup() {