Added optional additional data in Permission#isValidFor

This commit is contained in:
themode 2020-11-02 02:53:12 +01:00
parent 463e1f047f
commit 2e0c5a72b7
6 changed files with 55 additions and 29 deletions

View File

@ -3,6 +3,7 @@ package net.minestom.server.command;
import net.minestom.server.entity.Player;
import net.minestom.server.permission.Permission;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ -63,19 +64,23 @@ public interface CommandSender {
* Simple shortcut to <pre>getAllPermissions().contains(permission) &amp;&amp; permission.isValidFor(this)</pre> for readability.
*
* @param p permission to check against
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender)}
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender, Object)}
*/
default boolean hasPermission(@NotNull Permission p) {
return getAllPermissions().contains(p) && p.isValidFor(this);
return hasPermission(p, null);
}
default <T> boolean hasPermission(@NotNull Permission<T> p, @Nullable T data) {
return getAllPermissions().contains(p) && p.isValidFor(this, data);
}
/**
* Checks if the given {@link Permission} is possessed by this command sender.
* Will call {@link Permission#isValidFor(CommandSender)} on all permissions that are an instance of {@code permissionClass}.
* Will call {@link Permission#isValidFor(CommandSender, Object)} on all permissions that are an instance of {@code permissionClass}.
* If no matching permission is found, this result returns false.
*
* @param permissionClass the permission class to check
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender)}
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender, Object)}
* @see #getAllPermissions()
*/
default boolean hasPermission(@NotNull Class<? extends Permission> permissionClass) {
@ -84,7 +89,21 @@ public interface CommandSender {
for (Permission p : getAllPermissions()) {
if (permissionClass.isInstance(p)) {
foundPerm = true;
result &= p.isValidFor(this);
result &= p.isValidFor(this, null);
}
}
if (!foundPerm)
return false;
return result;
}
default <T> boolean hasPermission(@NotNull Class<? extends Permission<T>> permissionClass, @Nullable T data) {
boolean result = true;
boolean foundPerm = false;
for (Permission p : getAllPermissions()) {
if (permissionClass.isInstance(p)) {
foundPerm = true;
result &= p.isValidFor(this, data);
}
}
if (!foundPerm)

View File

@ -1,14 +1,15 @@
package net.minestom.server.permission;
import net.minestom.server.command.CommandSender;
import org.jetbrains.annotations.NotNull;
/**
* Basic {@link Permission} implementation that only requires the permission to be given to the {@link CommandSender} to be considered applied
* (eg. no arguments)
*/
public class BasicPermission implements Permission {
public class BasicPermission implements Permission<Object> {
@Override
public boolean isValidFor(CommandSender commandSender) {
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
return true;
}
}

View File

@ -6,10 +6,14 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Representation of a permission granted to a {@link CommandSender}
* Representation of a permission granted to a {@link CommandSender}.
*
* @param <T> the type of data that this permission can handle in {@link #isValidFor(CommandSender, Object)}.
* Used if you want to allow passing additional data to check if the permission is valid in a certain situation,
* you can default it to {@link Object} if you do not need it.
*/
@FunctionalInterface
public interface Permission {
public interface Permission<T> {
/**
* Does the given {@link CommandSender} have the permission represented by this object?
@ -18,22 +22,23 @@ public interface Permission {
* have this permission and validate the condition in this method.
*
* @param commandSender the command sender
* @param data the optional data (eg the number of home possible, placing a block at X position)
* @return true if the commandSender possesses this permission
*/
boolean isValidFor(CommandSender commandSender);
boolean isValidFor(@NotNull CommandSender commandSender, @Nullable T data);
/**
* Writes any required data for this permission inside the given destination
* Writes any required data for this permission inside the given destination.
*
* @param destination {@link Data} to write to
* @param destination the {@link Data} to write to
*/
default void write(@NotNull Data destination) {
}
/**
* Reads any required data for this permission from the given destination
* Reads any required data for this permission from the given destination.
*
* @param source {@link Data} to read from
* @param source the {@link Data} to read from
* @return this for chaining
*/
default Permission read(@Nullable Data source) {

View File

@ -3,7 +3,7 @@ package demo;
import demo.blocks.BurningTorchBlock;
import demo.blocks.StoneBlock;
import demo.blocks.UpdatableBlockDemo;
import demo.commands.TestCommand;
import demo.commands.*;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.instance.block.BlockManager;
@ -27,14 +27,14 @@ public class Main {
blockManager.registerBlockPlacementRule(new RedstonePlacementRule());
CommandManager commandManager = MinecraftServer.getCommandManager();
//commandManager.register(new EntitySelectorCommand());
commandManager.register(new EntitySelectorCommand());
commandManager.register(new TestCommand());
/*commandManager.register(new HealthCommand());
commandManager.register(new HealthCommand());
commandManager.register(new SimpleCommand());
commandManager.register(new GamemodeCommand());
commandManager.register(new DimensionCommand());
commandManager.register(new ShutdownCommand());
commandManager.register(new TeleportCommand());*/
commandManager.register(new TeleportCommand());
StorageManager storageManager = MinecraftServer.getStorageManager();

View File

@ -22,6 +22,7 @@ public class TestCommand extends Command {
setDefaultExecutor((source, args) -> {
System.out.println("DEFAULT");
System.gc();
});
addSyntax((source, args) -> {

View File

@ -2,11 +2,9 @@ package permissions;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.data.Data;
import net.minestom.server.entity.Player;
import net.minestom.server.permission.Permission;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -26,7 +24,8 @@ public class TestPermissions {
MinecraftServer.init(); // for entity manager
player = new Player(UUID.randomUUID(), "TestPlayer", null) {
@Override
protected void playerConnectionInit() {}
protected void playerConnectionInit() {
}
@Override
public boolean isOnline() {
@ -40,15 +39,16 @@ public class TestPermissions {
assertFalse(player.hasPermission(Permission.class));
}
class PermTest1 implements Permission {
class PermTest1 implements Permission<Object> {
@Override
public boolean isValidFor(CommandSender commandSender) {
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
return true;
}
}
class PermTest2 implements Permission {
class PermTest2 implements Permission<Object> {
@Override
public boolean isValidFor(CommandSender commandSender) {
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
return true;
}
}
@ -65,7 +65,7 @@ public class TestPermissions {
assertTrue(player.hasPermission(PermTest2.class));
}
class BooleanPerm implements Permission {
class BooleanPerm implements Permission<Object> {
private final boolean value;
BooleanPerm(boolean v) {
@ -73,7 +73,7 @@ public class TestPermissions {
}
@Override
public boolean isValidFor(CommandSender commandSender) {
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
return value;
}
}
@ -88,9 +88,9 @@ public class TestPermissions {
@Test
public void singlePermission() {
Permission p = commandSender -> true;
Permission p = (commandSender, data) -> true;
player.addPermission(p);
assertTrue(p.isValidFor(player));
assertTrue(p.isValidFor(player, null));
assertTrue(player.hasPermission(p));
assertTrue(player.hasPermission(Permission.class));
}