summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java68
-rw-r--r--src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java52
-rw-r--r--src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java186
3 files changed, 291 insertions, 15 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java
index a27753f..5ec0239 100644
--- a/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java
+++ b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java
@@ -2,9 +2,9 @@ package com.keuin.kbackupfabric;
import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider;
import com.keuin.kbackupfabric.ui.KBCommands;
-import com.keuin.kbackupfabric.util.PermissionValidator;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
+import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
@@ -13,36 +13,74 @@ public final class KBCommandsRegister {
public static void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
// register /kb and /kb help for help menu
- dispatcher.register(CommandManager.literal("kb").executes(KBCommands::kb));
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("help").executes(KBCommands::help)));
+ dispatcher.register(CommandManager.literal("kb")
+ .requires(Permissions.require("kb.root", true))
+ .executes(KBCommands::kb));
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("help")
+ .requires(Permissions.require("kb.help", true))
+ .executes(KBCommands::help)));
// register /kb list for showing the backup list. OP is required.
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("list").requires(PermissionValidator::op).executes(KBCommands::list)));
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("list")
+ .requires(Permissions.require("kb.list", 4))
+ .executes(KBCommands::list)));
// register /kb backup [name] for performing backup. OP is required.
dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("backup").then(
- CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::primitiveBackup)
- ).requires(PermissionValidator::op).executes(KBCommands::primitiveBackupWithDefaultName)));
+ CommandManager.argument("backupName", StringArgumentType.greedyString())
+ .requires(Permissions.require("kb.backup", 4))
+ .executes(KBCommands::primitiveBackup)
+ ).requires(Permissions.require("kb.backup", 4))
+ .executes(KBCommands::primitiveBackupWithDefaultName)));
// register /kb incbak [name] for performing incremental backup. OP is required.
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("incbak").then(
- CommandManager.argument("backupName", StringArgumentType.greedyString()).requires(PermissionValidator::op).executes(KBCommands::incrementalBackup)
- ).requires(PermissionValidator::op).executes(KBCommands::incrementalBackupWithDefaultName)));
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("incbak")
+ .then(CommandManager.argument("backupName", StringArgumentType.greedyString())
+ .requires(Permissions.require("kb.incbak", 4))
+ .executes(KBCommands::incrementalBackup))
+ .requires(Permissions.require("kb.incbak", 4))
+ .executes(KBCommands::incrementalBackupWithDefaultName)
+ ));
// register /kb restore <name> for performing restore. OP is required.
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("restore").then(CommandManager.argument("backupName", StringArgumentType.greedyString()).suggests(BackupNameSuggestionProvider.getProvider()).requires(PermissionValidator::op).executes(KBCommands::restore)).executes(KBCommands::list)));
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("restore")
+ .then(CommandManager.argument("backupName", StringArgumentType.greedyString())
+ .suggests(BackupNameSuggestionProvider.getProvider())
+ .requires(Permissions.require("kb.restore", 4))
+ .executes(KBCommands::restore))
+ .requires(Permissions.require("kb.list", 4))
+ .executes(KBCommands::list)));
// register /kb delete [name] for deleting an existing backup. OP is required.
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("delete").then(CommandManager.argument("backupName", StringArgumentType.greedyString()).suggests(BackupNameSuggestionProvider.getProvider()).requires(PermissionValidator::op).executes(KBCommands::delete))));
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("delete")
+ .then(CommandManager.argument("backupName", StringArgumentType.greedyString())
+ .suggests(BackupNameSuggestionProvider.getProvider())
+// .requires(Permissions.require("kb.delete", 4))
+ .executes(KBCommands::delete))
+ .requires(Permissions.require("kb.delete", 4))));
// register /kb confirm for confirming the execution. OP is required.
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("confirm").requires(PermissionValidator::op).executes(KBCommands::confirm)));
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("confirm")
+ .requires(Permissions.require("kb.confirm", 4))
+ .executes(KBCommands::confirm)));
// register /kb cancel for cancelling the execution to be confirmed. OP is required.
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("cancel").requires(PermissionValidator::op).executes(KBCommands::cancel)));
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("cancel")
+ .requires(Permissions.require("kb.cancel", 4))
+ .executes(KBCommands::cancel)));
- // register /kb prev for showing the latest backup.
- dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("prev").requires(PermissionValidator::op).executes(KBCommands::prev)));
+ // register /kb prev for showing the latest backup. OP is required.
+ dispatcher.register(CommandManager.literal("kb")
+ .then(CommandManager.literal("prev")
+ .requires(Permissions.require("kb.prev", 4))
+ .executes(KBCommands::prev)));
// // register /kb setMethod for selecting backup method (zip, incremental)
// dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("setMethod").then(CommandManager.argument("backupMethod", StringArgumentType.string()).suggests(BackupMethodSuggestionProvider.getProvider()).requires(PermissionValidator::op).executes(KBCommands::setMethod))));
diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java b/src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java
new file mode 100644
index 0000000..42b5b63
--- /dev/null
+++ b/src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java
@@ -0,0 +1,52 @@
+/*
+ * This file is part of fabric-permissions-api, licensed under the MIT License.
+ *
+ * Copyright (c) lucko (Luck) <[email protected]>
+ * Copyright (c) contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.lucko.fabric.api.permissions.v0;
+
+import net.fabricmc.fabric.api.event.Event;
+import net.fabricmc.fabric.api.event.EventFactory;
+import net.fabricmc.fabric.api.util.TriState;
+import net.minecraft.command.CommandSource;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Simple permissions check event for {@link CommandSource}s.
+ */
+public interface PermissionCheckEvent {
+
+ Event<PermissionCheckEvent> EVENT = EventFactory.createArrayBacked(PermissionCheckEvent.class, (callbacks) -> (source, permission) -> {
+ for (PermissionCheckEvent callback : callbacks) {
+ TriState state = callback.onPermissionCheck(source, permission);
+ if (state != TriState.DEFAULT) {
+ return state;
+ }
+ }
+ return TriState.DEFAULT;
+ });
+
+ @NotNull TriState onPermissionCheck(@NotNull CommandSource source, @NotNull String permission);
+
+}
diff --git a/src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java b/src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java
new file mode 100644
index 0000000..9fab02b
--- /dev/null
+++ b/src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java
@@ -0,0 +1,186 @@
+/*
+ * This file is part of fabric-permissions-api, licensed under the MIT License.
+ *
+ * Copyright (c) lucko (Luck) <[email protected]>
+ * Copyright (c) contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.lucko.fabric.api.permissions.v0;
+
+import net.fabricmc.fabric.api.util.TriState;
+import net.minecraft.command.CommandSource;
+import net.minecraft.entity.Entity;
+import net.minecraft.server.command.ServerCommandSource;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Objects;
+import java.util.function.Predicate;
+
+/**
+ * A simple permissions API.
+ */
+public interface Permissions {
+
+ /**
+ * Gets the {@link TriState state} of a {@code permission} for the given source.
+ *
+ * @param source the source
+ * @param permission the permission
+ * @return the state of the permission
+ */
+ static @NotNull TriState getPermissionValue(@NotNull CommandSource source, @NotNull String permission) {
+ Objects.requireNonNull(source, "source");
+ Objects.requireNonNull(permission, "permission");
+ return PermissionCheckEvent.EVENT.invoker().onPermissionCheck(source, permission);
+ }
+
+ /**
+ * Performs a permission check, falling back to the {@code defaultValue} if the resultant
+ * state is {@link TriState#DEFAULT}.
+ *
+ * @param source the source to perform the check for
+ * @param permission the permission to check
+ * @param defaultValue the default value to use if nothing has been set
+ * @return the result of the permission check
+ */
+ static boolean check(@NotNull CommandSource source, @NotNull String permission, boolean defaultValue) {
+ return getPermissionValue(source, permission).orElse(defaultValue);
+ }
+
+ /**
+ * Performs a permission check, falling back to requiring the {@code defaultRequiredLevel}
+ * if the resultant state is {@link TriState#DEFAULT}.
+ *
+ * @param source the source to perform the check for
+ * @param permission the permission to check
+ * @param defaultRequiredLevel the required permission level to check for as a fallback
+ * @return the result of the permission check
+ */
+ static boolean check(@NotNull CommandSource source, @NotNull String permission, int defaultRequiredLevel) {
+ return getPermissionValue(source, permission).orElseGet(() -> source.hasPermissionLevel(defaultRequiredLevel));
+ }
+
+ /**
+ * Performs a permission check, falling back to {@code false} if the resultant state
+ * is {@link TriState#DEFAULT}.
+ *
+ * @param source the source to perform the check for
+ * @param permission the permission to check
+ * @return the result of the permission check
+ */
+ static boolean check(@NotNull CommandSource source, @NotNull String permission) {
+ return getPermissionValue(source, permission).orElse(false);
+ }
+
+ /**
+ * Creates a predicate which returns the result of performing a permission check,
+ * falling back to the {@code defaultValue} if the resultant state is {@link TriState#DEFAULT}.
+ *
+ * @param permission the permission to check
+ * @param defaultValue the default value to use if nothing has been set
+ * @return a predicate that will perform the permission check
+ */
+ static @NotNull Predicate<ServerCommandSource> require(@NotNull String permission, boolean defaultValue) {
+ Objects.requireNonNull(permission, "permission");
+ return player -> check(player, permission, defaultValue);
+ }
+
+ /**
+ * Creates a predicate which returns the result of performing a permission check,
+ * falling back to requiring the {@code defaultRequiredLevel} if the resultant state is
+ * {@link TriState#DEFAULT}.
+ *
+ * @param permission the permission to check
+ * @param defaultRequiredLevel the required permission level to check for as a fallback
+ * @return a predicate that will perform the permission check
+ */
+ static @NotNull Predicate<ServerCommandSource> require(@NotNull String permission, int defaultRequiredLevel) {
+ Objects.requireNonNull(permission, "permission");
+ return player -> check(player, permission, defaultRequiredLevel);
+ }
+
+ /**
+ * Creates a predicate which returns the result of performing a permission check,
+ * falling back to {@code false} if the resultant state is {@link TriState#DEFAULT}.
+ *
+ * @param permission the permission to check
+ * @return a predicate that will perform the permission check
+ */
+ static @NotNull Predicate<ServerCommandSource> require(@NotNull String permission) {
+ Objects.requireNonNull(permission, "permission");
+ return player -> check(player, permission);
+ }
+
+ /**
+ * Gets the {@link TriState state} of a {@code permission} for the given entity.
+ *
+ * @param entity the entity
+ * @param permission the permission
+ * @return the state of the permission
+ */
+ static @NotNull TriState getPermissionValue(@NotNull Entity entity, @NotNull String permission) {
+ Objects.requireNonNull(entity, "entity");
+ return getPermissionValue(entity.getCommandSource(), permission);
+ }
+
+ /**
+ * Performs a permission check, falling back to the {@code defaultValue} if the resultant
+ * state is {@link TriState#DEFAULT}.
+ *
+ * @param entity the entity to perform the check for
+ * @param permission the permission to check
+ * @param defaultValue the default value to use if nothing has been set
+ * @return the result of the permission check
+ */
+ static boolean check(@NotNull Entity entity, @NotNull String permission, boolean defaultValue) {
+ Objects.requireNonNull(entity, "entity");
+ return check(entity.getCommandSource(), permission, defaultValue);
+ }
+
+ /**
+ * Performs a permission check, falling back to requiring the {@code defaultRequiredLevel}
+ * if the resultant state is {@link TriState#DEFAULT}.
+ *
+ * @param entity the entity to perform the check for
+ * @param permission the permission to check
+ * @param defaultRequiredLevel the required permission level to check for as a fallback
+ * @return the result of the permission check
+ */
+ static boolean check(@NotNull Entity entity, @NotNull String permission, int defaultRequiredLevel) {
+ Objects.requireNonNull(entity, "entity");
+ return check(entity.getCommandSource(), permission, defaultRequiredLevel);
+ }
+
+ /**
+ * Performs a permission check, falling back to {@code false} if the resultant state
+ * is {@link TriState#DEFAULT}.
+ *
+ * @param entity the entity to perform the check for
+ * @param permission the permission to check
+ * @return the result of the permission check
+ */
+ static boolean check(@NotNull Entity entity, @NotNull String permission) {
+ Objects.requireNonNull(entity, "entity");
+ return check(entity.getCommandSource(), permission);
+ }
+
+}