mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2026-11-20 06:29:40 +00:00
Implement fabric-permission-api v1 (#4248)
This commit is contained in:
1 parent
d90b3dbf11
commit
a97bdba0a0
5 files changed
+147
-6
No files matched your search
+3
-2
@@ -39,11 +39,12 @@ dependencies {
|
||||
'fabric-command-api-v2',
|
||||
'fabric-lifecycle-events-v1',
|
||||
'fabric-networking-api-v1',
|
||||
'fabric-entity-events-v1'
|
||||
'fabric-entity-events-v1',
|
||||
'fabric-permission-api-v1',
|
||||
]
|
||||
|
||||
apiModules.forEach {
|
||||
implementation(fabricApi.module(it, '0.144.0+26.1'))
|
||||
implementation(fabricApi.module(it, '0.149.0+26.1.2'))
|
||||
}
|
||||
|
||||
jarInJar 'me.lucko:fabric-permissions-api:0.7.0'
|
||||
|
||||
@@ -38,7 +38,8 @@ import me.lucko.luckperms.fabric.context.FabricContextManager;
|
||||
import me.lucko.luckperms.fabric.context.FabricPlayerCalculator;
|
||||
import me.lucko.luckperms.fabric.listeners.FabricConnectionListener;
|
||||
import me.lucko.luckperms.fabric.listeners.FabricOtherListeners;
|
||||
import me.lucko.luckperms.fabric.listeners.FabricPermissionsApiListener;
|
||||
import me.lucko.luckperms.fabric.listeners.FabricPermissionsApiV0Listener;
|
||||
import me.lucko.luckperms.fabric.listeners.FabricPermissionsApiV1Listener;
|
||||
import me.lucko.luckperms.fabric.listeners.FabricPermissionsListener;
|
||||
import me.lucko.luckperms.fabric.messaging.FabricMessagingFactory;
|
||||
import me.lucko.luckperms.fabric.placeholder.FabricPlaceholderApiIntegration;
|
||||
@@ -66,7 +67,8 @@ public class LPFabricPlugin extends MinecraftLuckPermsPlugin<LPFabricPlugin, LPF
|
||||
this.connectionListener = new FabricConnectionListener(this);
|
||||
this.connectionListener.registerListeners();
|
||||
|
||||
new FabricPermissionsApiListener(this).registerListeners();
|
||||
new FabricPermissionsApiV0Listener(this).registerListeners();
|
||||
new FabricPermissionsApiV1Listener(this).registerListeners();
|
||||
new FabricPermissionsListener().registerListeners();
|
||||
|
||||
// Command registration also need to occur early, and will persist across game states as well.
|
||||
|
||||
+2
-2
@@ -54,10 +54,10 @@ import java.util.concurrent.CompletableFuture;
|
||||
/**
|
||||
* Listener to route permission checks made via fabric-permissions-api to LuckPerms.
|
||||
*/
|
||||
public class FabricPermissionsApiListener {
|
||||
public class FabricPermissionsApiV0Listener {
|
||||
private final LPFabricPlugin plugin;
|
||||
|
||||
public FabricPermissionsApiListener(LPFabricPlugin plugin) {
|
||||
public FabricPermissionsApiV0Listener(LPFabricPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, 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.luckperms.fabric.listeners;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
import me.lucko.luckperms.common.verbose.event.CheckOrigin;
|
||||
import me.lucko.luckperms.fabric.LPFabricPlugin;
|
||||
import me.lucko.luckperms.fabric.model.MixinUser;
|
||||
import net.fabricmc.fabric.api.permission.v1.MutablePermissionContext;
|
||||
import net.fabricmc.fabric.api.permission.v1.PermissionContext;
|
||||
import net.fabricmc.fabric.api.permission.v1.PermissionEvents;
|
||||
import net.fabricmc.fabric.api.permission.v1.PermissionNode;
|
||||
import net.fabricmc.fabric.impl.permission.PermissionContextKey;
|
||||
import net.minecraft.resources.Identifier;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Listener to route permission checks made via fabric-permission-api v1 to LuckPerms.
|
||||
*/
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public class FabricPermissionsApiV1Listener {
|
||||
private static final PermissionContext.Key<User> USER_KEY = new PermissionContextKey<>(Identifier.fromNamespaceAndPath("luckperms", "user"));
|
||||
|
||||
private final LPFabricPlugin plugin;
|
||||
|
||||
public FabricPermissionsApiV1Listener(LPFabricPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void registerListeners() {
|
||||
PermissionEvents.ON_REQUEST.register(this::onPermissionRequest);
|
||||
PermissionEvents.PREPARE_OFFLINE_PLAYER.register(this::onPrepareOfflinePlayer);
|
||||
}
|
||||
|
||||
private <T> T onPermissionRequest(PermissionContext ctx, PermissionNode<T> node) {
|
||||
Codec<T> codec = node.codec();
|
||||
|
||||
if (codec == Codec.BOOL) {
|
||||
// permission check
|
||||
String permissionString = node.key().getNamespace() + '.' + node.key().getPath();
|
||||
Boolean permissionValue = permissionCheck(ctx, permissionString);
|
||||
return node.cast(permissionValue);
|
||||
}
|
||||
|
||||
// assume anything non-boolean is an option lookup
|
||||
String optionKey = node.key().toString();
|
||||
String optionValue = optionCheck(ctx, optionKey);
|
||||
if (optionValue == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (codec == Codec.STRING) {
|
||||
return node.cast(optionValue);
|
||||
}
|
||||
if (codec == Codec.INT) {
|
||||
return node.cast(Integer.parseInt(optionValue));
|
||||
}
|
||||
|
||||
// attempt parse with JsonParser & JsonOps
|
||||
JsonElement element = JsonParser.parseString(optionValue);
|
||||
return codec.parse(JsonOps.INSTANCE, element).getOrThrow();
|
||||
}
|
||||
|
||||
private CompletableFuture<Consumer<MutablePermissionContext>> onPrepareOfflinePlayer(PermissionContext ctx, MinecraftServer server) {
|
||||
UUID uniqueId = ctx.uuid();
|
||||
String username = ctx.get(PermissionContextKey.NAME);
|
||||
|
||||
return this.plugin.getStorage().loadUser(uniqueId, username).thenApply(user -> {
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
return mutableCtx -> mutableCtx.set(USER_KEY, user);
|
||||
});
|
||||
}
|
||||
|
||||
private static Boolean permissionCheck(PermissionContext ctx, String permission) {
|
||||
Entity entity = ctx.get(PermissionContextKey.ENTITY);
|
||||
if (entity instanceof MixinUser user) {
|
||||
return user.luckperms$hasPermission(permission).asBoolean();
|
||||
}
|
||||
|
||||
User user = ctx.get(USER_KEY);
|
||||
if (user != null) {
|
||||
return user.getCachedData().getPermissionData().checkPermission(permission, CheckOrigin.PLATFORM_API_HAS_PERMISSION).result().asBoolean();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String optionCheck(PermissionContext ctx, String optionKey) {
|
||||
Entity entity = ctx.get(PermissionContextKey.ENTITY);
|
||||
if (entity instanceof MixinUser user) {
|
||||
return user.luckperms$getOption(optionKey);
|
||||
}
|
||||
|
||||
User user = ctx.get(USER_KEY);
|
||||
if (user != null) {
|
||||
return user.getCachedData().getMetaData().getMetaOrChatMetaValue(optionKey, CheckOrigin.PLATFORM_API);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,6 +36,8 @@
|
||||
"fabric-command-api-v2": "*",
|
||||
"fabric-lifecycle-events-v1": "*",
|
||||
"fabric-networking-api-v1": "*",
|
||||
"fabric-entity-events-v1": "*",
|
||||
"fabric-permission-api-v1": "*",
|
||||
"fabric-permissions-api-v0": ">=0.7.0"
|
||||
},
|
||||
"custom": {
|
||||
|
||||
Reference in new issue
Block a user