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
@@ -20,14 +17,9 @@
*/
public class ServerSender implements CommandSender {
- private final Set
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..b5413629220 100644
--- a/src/main/java/net/minestom/server/permission/PermissionHandler.java
+++ b/src/main/java/net/minestom/server/permission/PermissionHandler.java
@@ -1,138 +1,17 @@
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()}.
*/
+@FunctionalInterface
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
- * 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