mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-15 20:51:34 +01:00
Added optional additional data in Permission#isValidFor
This commit is contained in:
parent
463e1f047f
commit
2e0c5a72b7
@ -3,6 +3,7 @@ package net.minestom.server.command;
|
|||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.permission.Permission;
|
import net.minestom.server.permission.Permission;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
@ -63,19 +64,23 @@ public interface CommandSender {
|
|||||||
* Simple shortcut to <pre>getAllPermissions().contains(permission) && permission.isValidFor(this)</pre> for readability.
|
* Simple shortcut to <pre>getAllPermissions().contains(permission) && permission.isValidFor(this)</pre> for readability.
|
||||||
*
|
*
|
||||||
* @param p permission to check against
|
* @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) {
|
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.
|
* 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.
|
* If no matching permission is found, this result returns false.
|
||||||
*
|
*
|
||||||
* @param permissionClass the permission class to check
|
* @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()
|
* @see #getAllPermissions()
|
||||||
*/
|
*/
|
||||||
default boolean hasPermission(@NotNull Class<? extends Permission> permissionClass) {
|
default boolean hasPermission(@NotNull Class<? extends Permission> permissionClass) {
|
||||||
@ -84,7 +89,21 @@ public interface CommandSender {
|
|||||||
for (Permission p : getAllPermissions()) {
|
for (Permission p : getAllPermissions()) {
|
||||||
if (permissionClass.isInstance(p)) {
|
if (permissionClass.isInstance(p)) {
|
||||||
foundPerm = true;
|
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)
|
if (!foundPerm)
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package net.minestom.server.permission;
|
package net.minestom.server.permission;
|
||||||
|
|
||||||
import net.minestom.server.command.CommandSender;
|
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
|
* Basic {@link Permission} implementation that only requires the permission to be given to the {@link CommandSender} to be considered applied
|
||||||
* (eg. no arguments)
|
* (eg. no arguments)
|
||||||
*/
|
*/
|
||||||
public class BasicPermission implements Permission {
|
public class BasicPermission implements Permission<Object> {
|
||||||
@Override
|
@Override
|
||||||
public boolean isValidFor(CommandSender commandSender) {
|
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,14 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
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
|
@FunctionalInterface
|
||||||
public interface Permission {
|
public interface Permission<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does the given {@link CommandSender} have the permission represented by this object?
|
* 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.
|
* have this permission and validate the condition in this method.
|
||||||
*
|
*
|
||||||
* @param commandSender the command sender
|
* @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
|
* @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) {
|
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
|
* @return this for chaining
|
||||||
*/
|
*/
|
||||||
default Permission read(@Nullable Data source) {
|
default Permission read(@Nullable Data source) {
|
||||||
|
@ -3,7 +3,7 @@ package demo;
|
|||||||
import demo.blocks.BurningTorchBlock;
|
import demo.blocks.BurningTorchBlock;
|
||||||
import demo.blocks.StoneBlock;
|
import demo.blocks.StoneBlock;
|
||||||
import demo.blocks.UpdatableBlockDemo;
|
import demo.blocks.UpdatableBlockDemo;
|
||||||
import demo.commands.TestCommand;
|
import demo.commands.*;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.CommandManager;
|
import net.minestom.server.command.CommandManager;
|
||||||
import net.minestom.server.instance.block.BlockManager;
|
import net.minestom.server.instance.block.BlockManager;
|
||||||
@ -27,14 +27,14 @@ public class Main {
|
|||||||
blockManager.registerBlockPlacementRule(new RedstonePlacementRule());
|
blockManager.registerBlockPlacementRule(new RedstonePlacementRule());
|
||||||
|
|
||||||
CommandManager commandManager = MinecraftServer.getCommandManager();
|
CommandManager commandManager = MinecraftServer.getCommandManager();
|
||||||
//commandManager.register(new EntitySelectorCommand());
|
commandManager.register(new EntitySelectorCommand());
|
||||||
commandManager.register(new TestCommand());
|
commandManager.register(new TestCommand());
|
||||||
/*commandManager.register(new HealthCommand());
|
commandManager.register(new HealthCommand());
|
||||||
commandManager.register(new SimpleCommand());
|
commandManager.register(new SimpleCommand());
|
||||||
commandManager.register(new GamemodeCommand());
|
commandManager.register(new GamemodeCommand());
|
||||||
commandManager.register(new DimensionCommand());
|
commandManager.register(new DimensionCommand());
|
||||||
commandManager.register(new ShutdownCommand());
|
commandManager.register(new ShutdownCommand());
|
||||||
commandManager.register(new TeleportCommand());*/
|
commandManager.register(new TeleportCommand());
|
||||||
|
|
||||||
|
|
||||||
StorageManager storageManager = MinecraftServer.getStorageManager();
|
StorageManager storageManager = MinecraftServer.getStorageManager();
|
||||||
|
@ -22,6 +22,7 @@ public class TestCommand extends Command {
|
|||||||
|
|
||||||
setDefaultExecutor((source, args) -> {
|
setDefaultExecutor((source, args) -> {
|
||||||
System.out.println("DEFAULT");
|
System.out.println("DEFAULT");
|
||||||
|
System.gc();
|
||||||
});
|
});
|
||||||
|
|
||||||
addSyntax((source, args) -> {
|
addSyntax((source, args) -> {
|
||||||
|
@ -2,11 +2,9 @@ package permissions;
|
|||||||
|
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.data.Data;
|
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.permission.Permission;
|
import net.minestom.server.permission.Permission;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -26,7 +24,8 @@ public class TestPermissions {
|
|||||||
MinecraftServer.init(); // for entity manager
|
MinecraftServer.init(); // for entity manager
|
||||||
player = new Player(UUID.randomUUID(), "TestPlayer", null) {
|
player = new Player(UUID.randomUUID(), "TestPlayer", null) {
|
||||||
@Override
|
@Override
|
||||||
protected void playerConnectionInit() {}
|
protected void playerConnectionInit() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isOnline() {
|
public boolean isOnline() {
|
||||||
@ -40,15 +39,16 @@ public class TestPermissions {
|
|||||||
assertFalse(player.hasPermission(Permission.class));
|
assertFalse(player.hasPermission(Permission.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
class PermTest1 implements Permission {
|
class PermTest1 implements Permission<Object> {
|
||||||
@Override
|
@Override
|
||||||
public boolean isValidFor(CommandSender commandSender) {
|
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class PermTest2 implements Permission {
|
|
||||||
|
class PermTest2 implements Permission<Object> {
|
||||||
@Override
|
@Override
|
||||||
public boolean isValidFor(CommandSender commandSender) {
|
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ public class TestPermissions {
|
|||||||
assertTrue(player.hasPermission(PermTest2.class));
|
assertTrue(player.hasPermission(PermTest2.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
class BooleanPerm implements Permission {
|
class BooleanPerm implements Permission<Object> {
|
||||||
private final boolean value;
|
private final boolean value;
|
||||||
|
|
||||||
BooleanPerm(boolean v) {
|
BooleanPerm(boolean v) {
|
||||||
@ -73,7 +73,7 @@ public class TestPermissions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValidFor(CommandSender commandSender) {
|
public boolean isValidFor(@NotNull CommandSender commandSender, Object data) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,9 +88,9 @@ public class TestPermissions {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void singlePermission() {
|
public void singlePermission() {
|
||||||
Permission p = commandSender -> true;
|
Permission p = (commandSender, data) -> true;
|
||||||
player.addPermission(p);
|
player.addPermission(p);
|
||||||
assertTrue(p.isValidFor(player));
|
assertTrue(p.isValidFor(player, null));
|
||||||
assertTrue(player.hasPermission(p));
|
assertTrue(player.hasPermission(p));
|
||||||
assertTrue(player.hasPermission(Permission.class));
|
assertTrue(player.hasPermission(Permission.class));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user