diff --git a/fabric/build.gradle b/fabric/build.gradle index cf237f921..1809f6193 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -1,5 +1,3 @@ -import net.fabricmc.loom.task.RemapJarTask - plugins { alias(libs.plugins.shadow) alias(libs.plugins.loom) @@ -15,6 +13,12 @@ tasks.withType(JavaCompile).configureEach { repositories { maven { url 'https://maven.fabricmc.net/' } + maven { + url 'https://maven.nucleoid.xyz/' + content { + includeGroup('eu.pb4') + } + } } configurations { @@ -43,9 +47,11 @@ dependencies { } jarInJar 'me.lucko:fabric-permissions-api:0.7.0' + implementation 'eu.pb4:placeholder-api:3.0.0+26.1' shade project(':common') shade project(':common:minecraft') + shade project(':common:placeholders') } loom.nestJars(tasks.named("shadowJar"), configurations.jarInJar) diff --git a/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java b/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java index f70971a7a..ba1f8b2cb 100644 --- a/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java +++ b/fabric/src/main/java/me/lucko/luckperms/fabric/LPFabricPlugin.java @@ -41,7 +41,9 @@ import me.lucko.luckperms.fabric.listeners.FabricOtherListeners; import me.lucko.luckperms.fabric.listeners.FabricPermissionsApiListener; import me.lucko.luckperms.fabric.listeners.FabricPermissionsListener; import me.lucko.luckperms.fabric.messaging.FabricMessagingFactory; +import me.lucko.luckperms.fabric.placeholder.FabricPlaceholderApiIntegration; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.luckperms.api.LuckPerms; import net.minecraft.server.players.ServerOpList; @@ -156,6 +158,15 @@ public class LPFabricPlugin extends MinecraftLuckPermsPlugin + * 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.placeholder; + +import eu.pb4.placeholders.api.PlaceholderResult; +import eu.pb4.placeholders.api.Placeholders; +import eu.pb4.placeholders.api.ServerPlaceholderContext; +import me.lucko.luckperms.common.minecraft.MinecraftSenderFactory; +import me.lucko.luckperms.common.model.User; +import me.lucko.luckperms.common.placeholders.Placeholder; +import me.lucko.luckperms.common.placeholders.PlaceholderContext; +import me.lucko.luckperms.common.placeholders.PlaceholderRegistry; +import me.lucko.luckperms.fabric.LPFabricPlugin; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.luckperms.api.query.QueryOptions; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.Identifier; +import net.minecraft.server.level.ServerPlayer; + +public class FabricPlaceholderApiIntegration { + private final LPFabricPlugin plugin; + + public FabricPlaceholderApiIntegration(LPFabricPlugin plugin) { + this.plugin = plugin; + } + + public void register() { + for (Placeholder placeholder : PlaceholderRegistry.getAll()) { + Placeholders.registerServer( + Identifier.fromNamespaceAndPath("luckperms", placeholder.id()), + new Handler(this.plugin, placeholder) + ); + } + } + + private record Handler(LPFabricPlugin plugin, Placeholder placeholder) implements eu.pb4.placeholders.api.Placeholder.Handler { + @Override + public PlaceholderResult onPlaceholderRequest(ServerPlaceholderContext context, String argument) { + ServerPlayer player = context.serverPlayer(); + User user = this.plugin.getUserManager().getIfLoaded(player.getUUID()); + if (user == null) { + return PlaceholderResult.invalid("Unable to find corresponding user for UUID: " + player.getUUID()); + } + + QueryOptions queryOptions = this.plugin.getContextManager().getQueryOptions(player); + PlaceholderContext ctx = new PlaceholderContext(this.plugin.getApiProvider(), user.getApiProxy(), queryOptions); + + String result; + if (this.placeholder instanceof Placeholder.Basic basic) { + result = basic.resolve(ctx); + } else if (this.placeholder instanceof Placeholder.UsingArgument usingArg) { + result = usingArg.resolve(ctx.withArgument(argument == null ? "" : argument)); + } else { + throw new IllegalStateException("Unknown placeholder type: " + this.placeholder.getClass()); + } + + return toResult(parseText(result)); + } + + private Component parseText(String input) { + if (input == null) { + return null; + } + TextComponent component = LegacyComponentSerializer.legacySection().deserialize(input); + return MinecraftSenderFactory.toNativeText(component); + } + + private static PlaceholderResult toResult(Component component) { + return component == null + ? PlaceholderResult.invalid() + : PlaceholderResult.value(component); + } + } +}