From cf117ff13be0998f64d95f7e735323a69929bbdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 3 Jan 2024 11:50:15 +0100 Subject: [PATCH] feature: add a miniplaceholders expansion --- Bukkit/build.gradle.kts | 1 + .../bukkit/listener/ServerListener.java | 6 ++ .../bukkit/placeholder/MiniPlaceholders.java | 69 +++++++++++++++++++ .../core/configuration/Settings.java | 4 ++ Core/src/main/resources/lang/messages_en.json | 1 + gradle/libs.versions.toml | 2 + 6 files changed, 83 insertions(+) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MiniPlaceholders.java diff --git a/Bukkit/build.gradle.kts b/Bukkit/build.gradle.kts index 19e48b218..472792b72 100644 --- a/Bukkit/build.gradle.kts +++ b/Bukkit/build.gradle.kts @@ -41,6 +41,7 @@ dependencies { compileOnly(libs.luckperms) compileOnly(libs.essentialsx) compileOnly(libs.mvdwapi) { isTransitive = false } + compileOnly(libs.miniplaceholders) { isTransitive = false } // Other libraries implementation(libs.squirrelid) { isTransitive = false } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java index 7ad0337ae..bf05fc505 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java @@ -21,6 +21,7 @@ package com.plotsquared.bukkit.listener; import com.google.inject.Inject; import com.plotsquared.bukkit.BukkitPlatform; import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; +import com.plotsquared.bukkit.placeholder.MiniPlaceholders; import com.plotsquared.bukkit.util.BukkitEconHandler; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Settings; @@ -52,6 +53,11 @@ public class ServerListener implements Listener { new MVdWPlaceholders(this.plugin, this.plugin.placeholderRegistry()); ConsolePlayer.getConsole().sendMessage(TranslatableCaption.of("placeholder.hooked")); } + if (Bukkit.getPluginManager().getPlugin("MiniPlaceholders") != null + && Settings.Enabled_Components.USE_MINIPLACEHOLDERS) { + new MiniPlaceholders(this.plugin.placeholderRegistry()); + ConsolePlayer.getConsole().sendMessage(TranslatableCaption.of("placeholder.miniplaceholders.hooked")); + } if (Settings.Enabled_Components.ECONOMY && Bukkit.getPluginManager().isPluginEnabled("Vault")) { EconHandler econHandler = new BukkitEconHandler(); try { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MiniPlaceholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MiniPlaceholders.java new file mode 100644 index 000000000..5cad0332a --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MiniPlaceholders.java @@ -0,0 +1,69 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.placeholder; + +import com.google.common.eventbus.Subscribe; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.player.ConsolePlayer; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.util.placeholders.Placeholder; +import com.plotsquared.core.util.placeholders.PlaceholderRegistry; +import io.github.miniplaceholders.api.Expansion; +import io.github.miniplaceholders.api.utils.TagsUtils; +import org.bukkit.entity.Player; +import org.checkerframework.checker.nullness.qual.NonNull; + +public final class MiniPlaceholders { + + private Expansion expansion = null; + private final PlaceholderRegistry registry; + + public MiniPlaceholders(final @NonNull PlaceholderRegistry registry) { + this.registry = registry; + this.createExpansion(); + PlotSquared.get().getEventDispatcher().registerListener(this); + } + + @Subscribe + public void onNewPlaceholder(final PlaceholderRegistry.@NonNull PlaceholderAddedEvent event) { + // We cannot register placeholders on the fly, so we have to replace the expansion. + this.createExpansion(); + } + + private synchronized void createExpansion() { + if (this.expansion != null && this.expansion.registered()) { + this.expansion.unregister(); + } + final Expansion.Builder builder = Expansion.builder("plotsquared"); + for (final Placeholder placeholder : this.registry.getPlaceholders()) { + builder.audiencePlaceholder(placeholder.getKey(), (audience, argumentQueue, context) -> { + final PlotPlayer plotPlayer; + if (audience instanceof Player player) { + plotPlayer = BukkitUtil.adapt(player); + } else { + plotPlayer = ConsolePlayer.getConsole(); + } + return TagsUtils.staticTag(placeholder.getValue(plotPlayer)); + }); + } + this.expansion = builder.build(); + this.expansion.register(); + } +} diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java index 03ac33e78..57a1aa756 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -810,6 +810,10 @@ public class Settings extends Config { ); @Comment("Whether PlotSquared should hook into MvDWPlaceholderAPI or not") public static boolean USE_MVDWAPI = true; + + @Comment("Whether PlotSquared should hook into MiniPlaceholders") + public static boolean USE_MINIPLACEHOLDERS = true; + @Comment("Prevent cross plot beacon effects") public static boolean DISABLE_BEACON_EFFECT_OVERFLOW = true; diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index 278efc25e..67400f2ae 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -190,6 +190,7 @@ "core.prefix": "[P2] ", "core.enabled": " is now enabled.", "placeholder.hooked": "PlotSquared hooked into MVdWPlaceholderAPI", + "placeholder.miniplaceholders.hooked": "PlotSquared hooked into MiniPlaceholders", "placeholder.nan": "Not a number", "reload.reloaded_configs": "Translations and world settings have been reloaded successfully.", "reload.reload_failed": "Failed to reload file configurations.", diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f891c19d7..9437b8099 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,6 +18,7 @@ placeholderapi = "2.11.5" luckperms = "5.4" essentialsx = "2.20.1" mvdwapi = "3.1.1" +miniplaceholders = "2.2.3" # Third party prtree = "2.0.1" @@ -68,6 +69,7 @@ prtree = { group = "com.intellectualsites.prtree", name = "PRTree", version.ref aopalliance = { group = "aopalliance", name = "aopalliance", version.ref = "aopalliance" } cloudServices = { group = "cloud.commandframework", name = "cloud-services", version.ref = "cloud-services" } mvdwapi = { group = "com.intellectualsites.mvdwplaceholderapi", name = "MVdWPlaceholderAPI", version.ref = "mvdwapi" } +miniplaceholders = { group = "io.github.miniplaceholders", name = "miniplaceholders-api", version.ref = "miniplaceholders" } squirrelid = { group = "org.enginehub", name = "squirrelid", version.ref = "squirrelid" } arkitektonika = { group = "com.intellectualsites.arkitektonika", name = "Arkitektonika-Client", version.ref = "arkitektonika" } paster = { group = "com.intellectualsites.paster", name = "Paster", version.ref = "paster" }