|
1 | 1 | package pro.cloudnode.smp.cloudnodemsg; |
2 | 2 |
|
| 3 | +import io.papermc.paper.chat.ChatRenderer; |
| 4 | +import me.clip.placeholderapi.PlaceholderAPI; |
| 5 | +import net.kyori.adventure.audience.Audience; |
| 6 | +import net.kyori.adventure.key.Key; |
3 | 7 | import net.kyori.adventure.text.Component; |
| 8 | +import net.kyori.adventure.text.event.ClickEvent; |
| 9 | +import net.kyori.adventure.text.event.HoverEvent; |
| 10 | +import net.kyori.adventure.text.format.TextColor; |
4 | 11 | import net.kyori.adventure.text.minimessage.MiniMessage; |
| 12 | +import net.kyori.adventure.text.minimessage.tag.Tag; |
5 | 13 | import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; |
| 14 | +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; |
| 15 | +import org.bukkit.OfflinePlayer; |
6 | 16 | import org.bukkit.configuration.file.FileConfiguration; |
| 17 | +import org.bukkit.entity.Player; |
7 | 18 | import org.bukkit.scoreboard.Team; |
8 | 19 | import org.jetbrains.annotations.NotNull; |
| 20 | +import org.jetbrains.annotations.Nullable; |
9 | 21 |
|
10 | 22 | import java.util.Objects; |
| 23 | +import java.util.Optional; |
11 | 24 |
|
12 | 25 | public final class PluginConfig { |
13 | 26 | public @NotNull FileConfiguration config; |
@@ -305,6 +318,44 @@ public PluginConfig(final @NotNull FileConfiguration config) { |
305 | 318 | ); |
306 | 319 | } |
307 | 320 |
|
| 321 | + /** |
| 322 | + * Chat format |
| 323 | + */ |
| 324 | + public @NotNull Optional<@NotNull ChatRenderer> chatFormat() { |
| 325 | + final @Nullable String str = config.getString("chat-format"); |
| 326 | + if (str == null || str.equals("null") || str.isBlank()) |
| 327 | + return Optional.empty(); |
| 328 | + return Optional.of((source, sourceDisplayName, message, viewer) -> MiniMessage.miniMessage().deserialize( |
| 329 | + str, |
| 330 | + Placeholder.component("message", message), |
| 331 | + TagResolver.resolver("papi", (args, ctx) -> { |
| 332 | + String placeholder = args.popOr("placeholder expected").value().trim(); |
| 333 | + if (!placeholder.startsWith("%") && !placeholder.endsWith("%")) |
| 334 | + placeholder = "%" + placeholder + "%"; |
| 335 | + if (!CloudnodeMSG.getInstance().getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) |
| 336 | + CloudnodeMSG.getInstance().getLogger().severe("Attempted to use PlaceholderAPI placeholder `" + placeholder |
| 337 | + + "` in chat format, but PlaceholderAPI is not present!"); |
| 338 | + return Tag.inserting(Component.text(PlaceholderAPI.setPlaceholders(source, placeholder))); |
| 339 | + }), |
| 340 | + TagResolver.resolver("has-team", (args, ctx) -> { |
| 341 | + final String text = args.popOr("text expected").value(); |
| 342 | + final Team team = source.getScoreboard().getPlayerTeam(source); |
| 343 | + if (team == null) { |
| 344 | + if (args.hasNext()) |
| 345 | + return Tag.inserting(MiniMessage.miniMessage().deserialize(args.popOr("expected fallback value").value())); |
| 346 | + else return Tag.inserting(Component.empty()); |
| 347 | + } |
| 348 | + return Tag.inserting(ctx.deserialize(text, |
| 349 | + Placeholder.component("team", team.displayName()) |
| 350 | + )); |
| 351 | + }), |
| 352 | + TagResolver.resolver("player", (args, ctx) -> Tag.inserting( |
| 353 | + Component.text(source.getName()) |
| 354 | + .clickEvent(ClickEvent.suggestCommand("/tell " + source.getName() + " ")) |
| 355 | + )) |
| 356 | + )); |
| 357 | + } |
| 358 | + |
308 | 359 | /** |
309 | 360 | * No permission |
310 | 361 | */ |
|
0 commit comments