diff options
Diffstat (limited to 'src/main/java/me/lucko/fabric/api/permissions/v0')
-rw-r--r-- | src/main/java/me/lucko/fabric/api/permissions/v0/PermissionCheckEvent.java | 52 | ||||
-rw-r--r-- | src/main/java/me/lucko/fabric/api/permissions/v0/Permissions.java | 186 |
2 files changed, 238 insertions, 0 deletions
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); + } + +} |