mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2026-11-20 23:52:17 +00:00
Add Fabric placeholder-api integration
This commit is contained in:
3 files changed
+117
-2
No files matched your search
+8
-2
@@ -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)
|
||||
|
||||
@@ -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<LPFabricPlugin, LPF
|
||||
if (getConfiguration().get(ConfigKeys.UPDATE_CLIENT_COMMAND_LIST)) {
|
||||
getApiProvider().getEventBus().subscribe(new MinecraftCommandListUpdater(this));
|
||||
}
|
||||
|
||||
// hook with placeholder api, if present
|
||||
if (FabricLoader.getInstance().isModLoaded("placeholder-api")) {
|
||||
try {
|
||||
new FabricPlaceholderApiIntegration(this).register();
|
||||
} catch (LinkageError e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FabricSenderFactory getSenderFactory() {
|
||||
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.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<ServerPlaceholderContext, String> {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user