Implement predicate permissions (#210)

Co-authored-by: Josh Taylor <me@broccol.ai>
This commit is contained in:
Alexander Söderberg 2021-01-13 13:23:30 +01:00
parent 78b081ccc2
commit 0b6a554946
7 changed files with 212 additions and 0 deletions

View file

@ -26,6 +26,8 @@ package cloud.commandframework.examples.bukkit;
import cloud.commandframework.ArgumentDescription;
import cloud.commandframework.Command;
import cloud.commandframework.CommandTree;
import cloud.commandframework.Description;
import cloud.commandframework.keys.SimpleCloudKey;
import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler;
import cloud.commandframework.minecraft.extras.MinecraftHelp;
import cloud.commandframework.annotations.AnnotationParser;
@ -59,6 +61,7 @@ import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.minecraft.extras.RichDescription;
import cloud.commandframework.minecraft.extras.TextColorArgument;
import cloud.commandframework.paper.PaperCommandManager;
import cloud.commandframework.permission.PredicatePermission;
import cloud.commandframework.tasks.TaskConsumer;
import cloud.commandframework.types.tuples.Triplet;
import io.leangen.geantyref.TypeToken;
@ -70,6 +73,7 @@ import net.kyori.adventure.text.format.TextDecoration;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -84,6 +88,10 @@ import org.bukkit.util.Vector;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -213,6 +221,15 @@ public final class ExamplePlugin extends JavaPlugin {
}
private void constructCommands() {
// Add a custom permission checker
this.annotationParser.registerBuilderModifier(
GameModeRequirement.class,
(requirement, builder) -> builder.permission(
PredicatePermission.of(SimpleCloudKey.of("gamemode"), sender ->
!(sender instanceof Player) || ((Player) sender).getGameMode() == requirement.value()
)
)
);
//
// Parse all @CommandMethod-annotated methods
//
@ -470,4 +487,21 @@ public final class ExamplePlugin extends JavaPlugin {
.execute(() -> sender.sendMessage("You have been teleported!"));
}
/**
* Command must have the given game mode
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface GameModeRequirement {
/**
* The required game mode
*
* @return Required game mode
*/
GameMode value();
}
}