From 6fe4fde7e13e6ee8bcdffa3424ed41e67223b6d9 Mon Sep 17 00:00:00 2001 From: codestech Date: Sat, 6 Jan 2024 18:05:46 +0100 Subject: [PATCH 1/2] Custom permission handlers --- .../server/command/CommandSender.java | 26 +++- .../server/command/ConsoleSender.java | 25 ++-- .../minestom/server/command/ServerSender.java | 24 ++-- .../net/minestom/server/entity/Entity.java | 11 +- .../net/minestom/server/entity/Player.java | 17 ++- .../permission/DefaultPermissionHandler.java | 13 ++ .../server/permission/Permission.java | 74 ----------- .../server/permission/PermissionHandler.java | 122 +----------------- .../server/permission/PermissionVerifier.java | 20 --- .../server/command/CommandConditionTest.java | 17 ++- .../server/command/CommandSenderTest.java | 57 +++++--- .../server/permission/TestPermissions.java | 116 +++++------------ 12 files changed, 163 insertions(+), 359 deletions(-) create mode 100644 src/main/java/net/minestom/server/permission/DefaultPermissionHandler.java delete mode 100644 src/main/java/net/minestom/server/permission/Permission.java delete mode 100644 src/main/java/net/minestom/server/permission/PermissionVerifier.java diff --git a/src/main/java/net/minestom/server/command/CommandSender.java b/src/main/java/net/minestom/server/command/CommandSender.java index 20a22bac01f..646f560592f 100644 --- a/src/main/java/net/minestom/server/command/CommandSender.java +++ b/src/main/java/net/minestom/server/command/CommandSender.java @@ -13,7 +13,31 @@ *

* Main implementations are {@link Player} and {@link ConsoleSender}. */ -public interface CommandSender extends PermissionHandler, Audience, Taggable, Identified { +public interface CommandSender extends Audience, Taggable, Identified { + + /** + * Gets a permission handler of the sender. + * + * @return a permission handler of the sender + */ + @NotNull PermissionHandler getPermissionHandler(); + + /** + * Sets a permission handler for the sender. + * + * @param handler the permission handler + */ + void setPermissionHandler(@NotNull PermissionHandler handler); + + /** + * Gets if this sender has the permission {@code permission}. + * + * @param permission the permission to check + * @return true if the sender has the permission, false otherwise + */ + default boolean hasPermission(@NotNull String permission) { + return getPermissionHandler().hasPermission(permission); + } /** * Sends a raw string message. diff --git a/src/main/java/net/minestom/server/command/ConsoleSender.java b/src/main/java/net/minestom/server/command/ConsoleSender.java index d97bc82ae1c..b370a63e9b4 100644 --- a/src/main/java/net/minestom/server/command/ConsoleSender.java +++ b/src/main/java/net/minestom/server/command/ConsoleSender.java @@ -1,14 +1,12 @@ package net.minestom.server.command; -import java.util.UUID; - import net.kyori.adventure.audience.MessageType; -import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.logger.slf4j.ComponentLogger; -import net.minestom.server.permission.Permission; +import net.minestom.server.permission.DefaultPermissionHandler; +import net.minestom.server.permission.PermissionHandler; import net.minestom.server.tag.TagHandler; import org.jetbrains.annotations.NotNull; @@ -21,7 +19,6 @@ public class ConsoleSender implements CommandSender { private static final ComponentLogger LOGGER = ComponentLogger.logger(ConsoleSender.class); - private final Set permissions = new CopyOnWriteArraySet<>(); private final TagHandler tagHandler = TagHandler.newHandler(); private final Identity identity = Identity.nil(); @@ -29,20 +26,26 @@ public class ConsoleSender implements CommandSender { .withStatic(Identity.UUID, this.identity.uuid()) .build(); + private PermissionHandler permissionHandler = new DefaultPermissionHandler(); + @Override - public void sendMessage(@NotNull String message) { - LOGGER.info(message); + public @NotNull PermissionHandler getPermissionHandler() { + return permissionHandler; } @Override - public void sendMessage(@NotNull Identity source, @NotNull Component message, @NotNull MessageType type) { + public void setPermissionHandler(@NotNull PermissionHandler handler) { + this.permissionHandler = handler; + } + + @Override + public void sendMessage(@NotNull String message) { LOGGER.info(message); } - @NotNull @Override - public Set getAllPermissions() { - return permissions; + public void sendMessage(@NotNull Identity source, @NotNull Component message, @NotNull MessageType type) { + LOGGER.info(message); } @Override diff --git a/src/main/java/net/minestom/server/command/ServerSender.java b/src/main/java/net/minestom/server/command/ServerSender.java index ee0496e01a0..aca75619b91 100644 --- a/src/main/java/net/minestom/server/command/ServerSender.java +++ b/src/main/java/net/minestom/server/command/ServerSender.java @@ -3,14 +3,11 @@ import net.kyori.adventure.audience.Audience; import net.kyori.adventure.identity.Identity; import net.minestom.server.command.builder.CommandContext; -import net.minestom.server.permission.Permission; +import net.minestom.server.permission.DefaultPermissionHandler; +import net.minestom.server.permission.PermissionHandler; import net.minestom.server.tag.TagHandler; import org.jetbrains.annotations.NotNull; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - /** * Sender used in {@link CommandManager#executeServerCommand(String)}. *

@@ -20,14 +17,9 @@ */ public class ServerSender implements CommandSender { - private final Set permissions = Collections.unmodifiableSet(new HashSet<>()); private final TagHandler tagHandler = TagHandler.newHandler(); - @NotNull - @Override - public Set getAllPermissions() { - return permissions; - } + private PermissionHandler permissionHandler = new DefaultPermissionHandler(); @Override public @NotNull TagHandler tagHandler() { @@ -38,4 +30,14 @@ public Set getAllPermissions() { public @NotNull Identity identity() { return Identity.nil(); } + + @Override + public @NotNull PermissionHandler getPermissionHandler() { + return permissionHandler; + } + + @Override + public void setPermissionHandler(@NotNull PermissionHandler handler) { + this.permissionHandler = handler; + } } diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 19591c42863..10edbf452b3 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -34,8 +34,6 @@ import net.minestom.server.network.packet.server.LazyPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.play.*; -import net.minestom.server.permission.Permission; -import net.minestom.server.permission.PermissionHandler; import net.minestom.server.potion.Potion; import net.minestom.server.potion.PotionEffect; import net.minestom.server.potion.TimedPotion; @@ -84,7 +82,7 @@ * To create your own entity you probably want to extends {@link LivingEntity} or {@link EntityCreature} instead. */ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, EventHandler, Taggable, - PermissionHandler, HoverEventSource, Sound.Emitter, Shape { + HoverEventSource, Sound.Emitter, Shape { private static final int VELOCITY_UPDATE_INTERVAL = 1; @@ -156,7 +154,6 @@ public void referenceUpdate(@NotNull Point point, @Nullable EntityTracker tracke private final TagHandler tagHandler = TagHandler.newHandler(); private final Scheduler scheduler = Scheduler.newScheduler(); private final EventNode eventNode; - private final Set permissions = new CopyOnWriteArraySet<>(); protected UUID uuid; private boolean isActive; // False if entity has only been instanced without being added somewhere @@ -528,12 +525,6 @@ public synchronized void switchEntityType(@NotNull EntityType entityType) { viewers.forEach(this::updateNewViewer); } - @NotNull - @Override - public Set getAllPermissions() { - return permissions; - } - /** * Updates the entity, called every tick. *

diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 0cdf342de06..de52d914c97 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -22,8 +22,6 @@ import net.minestom.server.adventure.audience.Audiences; import net.minestom.server.attribute.Attribute; import net.minestom.server.collision.BoundingBox; -import net.minestom.server.collision.CollisionUtils; -import net.minestom.server.collision.PhysicsResult; import net.minestom.server.command.CommandSender; import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Pos; @@ -31,7 +29,6 @@ import net.minestom.server.effects.Effects; import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.fakeplayer.FakePlayer; -import net.minestom.server.entity.metadata.LivingEntityMeta; import net.minestom.server.entity.metadata.PlayerMeta; import net.minestom.server.entity.vehicle.PlayerVehicleInformation; import net.minestom.server.event.EventDispatcher; @@ -65,6 +62,8 @@ import net.minestom.server.network.player.GameProfile; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerSocketConnection; +import net.minestom.server.permission.DefaultPermissionHandler; +import net.minestom.server.permission.PermissionHandler; import net.minestom.server.recipe.Recipe; import net.minestom.server.recipe.RecipeManager; import net.minestom.server.registry.Registry; @@ -216,6 +215,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable, private Identity identity; private final Pointers pointers; + private PermissionHandler permissionHandler = new DefaultPermissionHandler(); + public Player(@NotNull UUID uuid, @NotNull String username, @NotNull PlayerConnection playerConnection) { super(EntityType.PLAYER, uuid); this.username = username; @@ -2220,6 +2221,16 @@ public void setUuid(@NotNull UUID uuid) { this.identity = Identity.identity(uuid); } + @Override + public @NotNull PermissionHandler getPermissionHandler() { + return permissionHandler; + } + + @Override + public void setPermissionHandler(@NotNull PermissionHandler handler) { + this.permissionHandler = handler; + } + @Override public boolean isPlayer() { return true; diff --git a/src/main/java/net/minestom/server/permission/DefaultPermissionHandler.java b/src/main/java/net/minestom/server/permission/DefaultPermissionHandler.java new file mode 100644 index 00000000000..44b51f67917 --- /dev/null +++ b/src/main/java/net/minestom/server/permission/DefaultPermissionHandler.java @@ -0,0 +1,13 @@ +package net.minestom.server.permission; + +import org.jetbrains.annotations.NotNull; + +/** + * A default permission handler. Used when no custom permission handler is set. + */ +public final class DefaultPermissionHandler implements PermissionHandler { + @Override + public boolean hasPermission(@NotNull String permission) { + return false; + } +} diff --git a/src/main/java/net/minestom/server/permission/Permission.java b/src/main/java/net/minestom/server/permission/Permission.java deleted file mode 100644 index 1959e256919..00000000000 --- a/src/main/java/net/minestom/server/permission/Permission.java +++ /dev/null @@ -1,74 +0,0 @@ -package net.minestom.server.permission; - -import net.minestom.server.command.CommandSender; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jglrxavpok.hephaistos.nbt.NBTCompound; - -import java.util.Objects; - -/** - * Representation of a permission granted to a {@link CommandSender}. - * Each permission has a string representation used as an identifier, and an optional - * {@link NBTCompound} used to store additional data. - *

- * The class is immutable. - */ -public class Permission { - - private final String permissionName; - private final NBTCompound data; - - /** - * Creates a new permission object with optional data. - * - * @param permissionName the name of the permission - * @param data the optional data of the permission - */ - public Permission(@NotNull String permissionName, @Nullable NBTCompound data) { - this.permissionName = permissionName; - this.data = data; - } - - /** - * Creates a new permission object without additional data - * - * @param permissionName the name of the permission - */ - public Permission(@NotNull String permissionName) { - this(permissionName, null); - } - - /** - * Gets the name of the permission. - * - * @return the permission name - */ - @NotNull - public String getPermissionName() { - return permissionName; - } - - /** - * Gets the data associated to this permission. - * - * @return the nbt data of this permission, can be null if not any - */ - @Nullable - public NBTCompound getNBTData() { - return data; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Permission that = (Permission) o; - return permissionName.equals(that.permissionName) && Objects.equals(data, that.data); - } - - @Override - public int hashCode() { - return Objects.hash(permissionName, data); - } -} diff --git a/src/main/java/net/minestom/server/permission/PermissionHandler.java b/src/main/java/net/minestom/server/permission/PermissionHandler.java index 0683e34977d..b9ced3a1481 100644 --- a/src/main/java/net/minestom/server/permission/PermissionHandler.java +++ b/src/main/java/net/minestom/server/permission/PermissionHandler.java @@ -1,138 +1,18 @@ package net.minestom.server.permission; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -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. *

* Permissions are in-memory only by default. - * You have however the capacity to store them persistently as the {@link Permission} object - * is serializer-friendly, {@link Permission#getPermissionName()} being a {@link String} - * and {@link Permission#getNBTData()} serializable into a string using {@link NBTCompound#toSNBT()} - * and deserialized back with {@link SNBTParser#parse()}. */ public interface PermissionHandler { - - /** - * Returns all permissions associated to this handler. - * The returned collection should be modified only by subclasses. - * - * @return the permissions of this handler. - */ - @NotNull - Set getAllPermissions(); - - /** - * Adds a {@link Permission} to this handler. - * - * @param permission the permission to add - */ - default void addPermission(@NotNull Permission permission) { - getAllPermissions().add(permission); - } - - /** - * Removes a {@link Permission} from this handler. - * - * @param permission the permission to remove - */ - default void removePermission(@NotNull Permission permission) { - getAllPermissions().remove(permission); - } - - /** - * Removes a {@link Permission} based on its string identifier. - * - * @param permissionName the permission name - */ - default void removePermission(@NotNull String permissionName) { - getAllPermissions().removeIf(permission -> permission.getPermissionName().equals(permissionName)); - } - /** * 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"}. - *

- * Uses {@link Permission#equals(Object)} internally. * * @param permission the permission to check * @return true if the handler has the permission, false otherwise */ - default boolean hasPermission(@NotNull Permission permission) { - for (Permission permissionLoop : getAllPermissions()) { - 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; - } - - /** - * Gets the {@link Permission} with the name {@code permissionName}. - *

- * Useful if you want to retrieve the permission data. - * - * @param permissionName the permission name - * @return the permission from its name, null if not found - */ - @Nullable - default Permission getPermission(@NotNull String permissionName) { - for (Permission permission : getAllPermissions()) { - // Verify permission name equality - if (permission.getPermissionName().equals(permissionName)) { - return permission; - } - } - return null; - } - - /** - * Gets if this handler has the permission with the name {@code permissionName} and which verify the optional - * {@link PermissionVerifier}. - * - * @param permissionName the permission name - * @param permissionVerifier the optional verifier, - * null means that only the permission name will be used - * @return true if the handler has the permission, false otherwise - */ - default boolean hasPermission(@NotNull String permissionName, @Nullable PermissionVerifier permissionVerifier) { - Permission permission = getPermission(permissionName); - - if (permission == null && permissionVerifier == null) { - permission = new Permission(permissionName, null); - } else if (permission == null) { - 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()); - } - - /** - * Gets if this handler has the permission with the name {@code permissionName}. - * - * @param permissionName the permission name - * @return true if the handler has the permission, false otherwise - */ - default boolean hasPermission(@NotNull String permissionName) { - return hasPermission(permissionName, null); - } - + boolean hasPermission(@NotNull String permission); } diff --git a/src/main/java/net/minestom/server/permission/PermissionVerifier.java b/src/main/java/net/minestom/server/permission/PermissionVerifier.java deleted file mode 100644 index 51323b5d887..00000000000 --- a/src/main/java/net/minestom/server/permission/PermissionVerifier.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.minestom.server.permission; - -import org.jetbrains.annotations.Nullable; -import org.jglrxavpok.hephaistos.nbt.NBTCompound; - -/** - * Interface used to check if the {@link NBTCompound nbt data} of a {@link Permission} is correct. - */ -@FunctionalInterface -public interface PermissionVerifier { - - /** - * Called when using {@link PermissionHandler#hasPermission(String, PermissionVerifier)}. - * - * @param nbtCompound the data of the permission, can be null if not any - * @return true if {@link PermissionHandler#hasPermission(String, PermissionVerifier)} - * should return true, false otherwise - */ - boolean isValid(@Nullable NBTCompound nbtCompound); -} diff --git a/src/test/java/net/minestom/server/command/CommandConditionTest.java b/src/test/java/net/minestom/server/command/CommandConditionTest.java index f7450b020c9..7f77579e1ce 100644 --- a/src/test/java/net/minestom/server/command/CommandConditionTest.java +++ b/src/test/java/net/minestom/server/command/CommandConditionTest.java @@ -3,12 +3,11 @@ import net.kyori.adventure.identity.Identity; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandDispatcher; -import net.minestom.server.permission.Permission; +import net.minestom.server.permission.PermissionHandler; import net.minestom.server.tag.TagHandler; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; -import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.jupiter.api.Assertions.*; @@ -126,10 +125,6 @@ public void subConditionOverride() { } private static final class Sender implements CommandSender { - @Override - public @NotNull Set getAllPermissions() { - return null; - } @Override public @NotNull TagHandler tagHandler() { @@ -140,5 +135,15 @@ private static final class Sender implements CommandSender { public @NotNull Identity identity() { return Identity.nil(); } + + @Override + public @NotNull PermissionHandler getPermissionHandler() { + return null; + } + + @Override + public void setPermissionHandler(@NotNull PermissionHandler handler) { + + } } } diff --git a/src/test/java/net/minestom/server/command/CommandSenderTest.java b/src/test/java/net/minestom/server/command/CommandSenderTest.java index fe2b5186f3b..8be0f246946 100644 --- a/src/test/java/net/minestom/server/command/CommandSenderTest.java +++ b/src/test/java/net/minestom/server/command/CommandSenderTest.java @@ -4,11 +4,10 @@ import net.kyori.adventure.identity.Identity; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import net.minestom.server.permission.Permission; +import net.minestom.server.permission.PermissionHandler; import net.minestom.server.tag.TagHandler; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.junit.jupiter.api.Test; import java.util.HashSet; @@ -20,19 +19,17 @@ public class CommandSenderTest { @Test public void testSenderPermissions() { + TestPermissionHandler handler = new TestPermissionHandler(); - CommandSender sender = new SenderTest(); + String permission = "permission.test"; - Permission permission = new Permission("permission.test", new NBTCompound()); + assertEquals(handler.getAllPermissions(), Set.of()); - assertEquals(sender.getAllPermissions(), Set.of()); + handler.addPermission(permission); + assertEquals(handler.getAllPermissions(), Set.of(permission)); - sender.addPermission(permission); - assertEquals(sender.getPermission(permission.getPermissionName()), permission); - assertEquals(sender.getAllPermissions(), Set.of(permission)); - - sender.removePermission(permission.getPermissionName()); - assertEquals(sender.getAllPermissions(), Set.of()); + handler.removePermission(permission); + assertEquals(handler.getAllPermissions(), Set.of()); } @Test @@ -53,16 +50,10 @@ public void testMessageSending() { private static final class SenderTest implements CommandSender { - private final Set permissions = new HashSet<>(); private final TagHandler handler = TagHandler.newHandler(); private Component mostRecentMessage = null; - @Override - public @NotNull Set getAllPermissions() { - return permissions; - } - @Override public @NotNull TagHandler tagHandler() { return handler; @@ -81,5 +72,37 @@ public void sendMessage(@NotNull Identity source, @NotNull Component message, @N public @NotNull Identity identity() { return Identity.nil(); } + + @Override + public @NotNull PermissionHandler getPermissionHandler() { + return null; + } + + @Override + public void setPermissionHandler(@NotNull PermissionHandler handler) { + + } + } + + private static final class TestPermissionHandler implements PermissionHandler { + + private final Set permissions = new HashSet<>(); + + @Override + public boolean hasPermission(@NotNull String permission) { + return permissions.contains(permission); + } + + public @NotNull Set getAllPermissions() { + return Set.copyOf(this.permissions); + } + + public void addPermission(@NotNull String permission) { + this.permissions.add(permission); + } + + public void removePermission(@NotNull String permission) { + this.permissions.remove(permission); + } } } diff --git a/src/test/java/net/minestom/server/permission/TestPermissions.java b/src/test/java/net/minestom/server/permission/TestPermissions.java index c5604540d92..3abc391b811 100644 --- a/src/test/java/net/minestom/server/permission/TestPermissions.java +++ b/src/test/java/net/minestom/server/permission/TestPermissions.java @@ -2,12 +2,13 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.entity.Player; -import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.Objects; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -17,8 +18,9 @@ public class TestPermissions { private Player player; + private TestPermissionHandler handler; - private Permission permission1, permission2, permission3, wildcard; + private String permission1, permission2, permission3; @BeforeEach public void init() { @@ -34,18 +36,12 @@ public boolean isOnline() { } }; - permission1 = new Permission("perm.name", - NBT.Compound(nbt -> { - nbt.setString("name", "Minestom"); - nbt.setInt("amount", 5); - }) - ); + handler = new TestPermissionHandler(); + player.setPermissionHandler(handler); - permission2 = new Permission("perm.name2"); - - permission3 = new Permission("perm.name2.sub.sub2"); - - wildcard = new Permission("*"); + permission1 = "perm.name"; + permission2 = "perm.name2"; + permission3 = "perm.name2.sub.sub2"; } @Test @@ -56,89 +52,39 @@ public void noPermission() { @Test public void hasPermissionClass() { - assertFalse(player.hasPermission(permission1)); - player.addPermission(permission1); + handler.addPermission(permission1); assertTrue(player.hasPermission(permission1)); assertFalse(player.hasPermission(permission2)); - player.addPermission(permission2); + handler.addPermission(permission2); assertTrue(player.hasPermission(permission2)); } - @Test - public void hasPermissionNameNbt() { - player.addPermission(permission1); - assertTrue(player.hasPermission("perm.name")); - assertTrue(player.hasPermission("perm.name", - nbtCompound -> { - final String name = nbtCompound != null ? nbtCompound.getString("name") : null; - return Objects.equals(name, "Minestom"); - })); - - player.addPermission(permission2); - assertFalse(player.hasPermission("perm.name2", Objects::nonNull)); - } + @AfterEach + public void cleanup() { - @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)); - } + private static final class TestPermissionHandler implements PermissionHandler { - @Test - public void hasAllPermissionsWithWildcard() { - assertFalse(player.hasPermission(permission2)); - assertFalse(player.hasPermission(permission3)); - player.addPermission(wildcard); - assertTrue(player.hasPermission(permission2)); - assertTrue(player.hasPermission(permission3)); - } + private final Set permissions = new HashSet<>(); - @AfterEach - public void cleanup() { + @Override + public boolean hasPermission(@NotNull String permission) { + return permissions.contains(permission); + } + + public @NotNull Set getAllPermissions() { + return Set.copyOf(this.permissions); + } + + public void addPermission(@NotNull String permission) { + this.permissions.add(permission); + } + public void removePermission(@NotNull String permission) { + this.permissions.remove(permission); + } } } From 336a952b8c0ab27f2e4936f63a0cec795186aacc Mon Sep 17 00:00:00 2001 From: codestech Date: Sat, 6 Jan 2024 18:18:54 +0100 Subject: [PATCH 2/2] Make a permission handler a functional interface --- .../java/net/minestom/server/permission/PermissionHandler.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/permission/PermissionHandler.java b/src/main/java/net/minestom/server/permission/PermissionHandler.java index b9ced3a1481..b5413629220 100644 --- a/src/main/java/net/minestom/server/permission/PermissionHandler.java +++ b/src/main/java/net/minestom/server/permission/PermissionHandler.java @@ -4,9 +4,8 @@ /** * Represents an object which can have permissions. - *

- * Permissions are in-memory only by default. */ +@FunctionalInterface public interface PermissionHandler { /** * Gets if this handler has the permission {@code permission}.