diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java index 76a4aa7b..d7a59602 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/NicknameCommand.java @@ -32,7 +32,7 @@ import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.config.ConfigManager; import net.draycia.carbon.common.messages.CarbonMessages; -import net.draycia.carbon.common.users.WrappedCarbonPlayer; +import net.draycia.carbon.common.messages.TagPermissions; import net.draycia.carbon.common.util.CloudUtils; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.key.Key; @@ -166,7 +166,7 @@ public final class NicknameCommand extends CarbonCommand { private static Component parseNickname(final Commander sender, final String nick) { // trim one level of quotes, to allow for nicknames which collide with command literals - return WrappedCarbonPlayer.parseMessageTags(trimQuotes(nick), sender::hasPermission); + return TagPermissions.parseTags(TagPermissions.NICKNAME, trimQuotes(nick), sender::hasPermission); } private static String trimQuotes(final String string) { diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java b/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java index f44b1151..b45136cc 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java @@ -41,10 +41,10 @@ import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.config.ConfigManager; import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.messages.Option; +import net.draycia.carbon.common.messages.TagPermissions; import net.draycia.carbon.common.users.NetworkUsers; import net.draycia.carbon.common.users.PartyInvites; import net.draycia.carbon.common.users.UserManagerInternal; -import net.draycia.carbon.common.users.WrappedCarbonPlayer; import net.draycia.carbon.common.util.Pagination; import net.draycia.carbon.common.util.PaginationHelper; import net.kyori.adventure.key.Key; @@ -181,7 +181,7 @@ public final class PartyCommands extends CarbonCommand { return; } final String name = ctx.getOrDefault("name", player.username() + "'s party"); - final Component component = ((WrappedCarbonPlayer) player).parseMessageTags(name); + final Component component = TagPermissions.parseTags(TagPermissions.PARTY_NAME, name, player::hasPermission); final Party party; try { party = this.userManager.createParty(component); diff --git a/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java b/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java index 969aa037..28d970b6 100644 --- a/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java +++ b/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java @@ -30,6 +30,7 @@ import net.draycia.carbon.common.config.ConfigManager; import net.draycia.carbon.common.event.events.CarbonChatEventImpl; import net.draycia.carbon.common.event.events.CarbonEarlyChatEvent; import net.draycia.carbon.common.messages.CarbonMessages; +import net.draycia.carbon.common.messages.TagPermissions; import net.draycia.carbon.common.users.WrappedCarbonPlayer; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.chat.SignedMessage; @@ -81,7 +82,7 @@ public abstract class ChatListenerInternal { if (sender instanceof WrappedCarbonPlayer wrapped) { message = wrapped.parseMessageTags(content); } else { - message = WrappedCarbonPlayer.parseMessageTags(content, sender::hasPermission); + message = TagPermissions.parseTags(TagPermissions.MESSAGE, content, sender::hasPermission); } if (probablyBlank(message)) { return null; diff --git a/common/src/main/java/net/draycia/carbon/common/messages/TagPermissions.java b/common/src/main/java/net/draycia/carbon/common/messages/TagPermissions.java new file mode 100644 index 00000000..d6782708 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/messages/TagPermissions.java @@ -0,0 +1,89 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 Josua Parks (Vicarious) + * 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 net.draycia.carbon.common.messages; + +import java.util.Map; +import java.util.function.Predicate; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + +@DefaultQualifier(NonNull.class) +public final class TagPermissions { + + public static final String NICKNAME = "carbon.nickname.tags"; + public static final String MESSAGE = "carbon.messagetags"; + public static final String PARTY_NAME = "carbon.parties.name.tags"; + private static final Map DEFAULT_TAGS = Map.ofEntries( + Map.entry("hover", StandardTags.hoverEvent()), + Map.entry("click", StandardTags.clickEvent()), + Map.entry("color", StandardTags.color()), + Map.entry("keybind", StandardTags.keybind()), + Map.entry("translatable", StandardTags.translatable()), + Map.entry("insertion", StandardTags.insertion()), + Map.entry("font", StandardTags.font()), + Map.entry("decorations", StandardTags.decorations()), + Map.entry("gradient", StandardTags.gradient()), + Map.entry("rainbow", StandardTags.rainbow()), + Map.entry("reset", StandardTags.reset()), + Map.entry("newline", StandardTags.newline()) + ); + + private TagPermissions() { + } + + public static Component parseTags(final String basePermission, final String message, final Predicate permission, final TagResolver.Builder resolver) { + if (!permission.test(basePermission)) { + return Component.text(message); + } + + boolean hasAllDecorations = false; + for (final Map.Entry entry : DEFAULT_TAGS.entrySet()) { + if (permission.test(basePermission + '.' + entry.getKey())) { + resolver.resolver(entry.getValue()); + if (entry.getKey().equals("decorations")) { + hasAllDecorations = true; + } + } + } + + if (!hasAllDecorations) { + for (final TextDecoration decoration : TextDecoration.values()) { + if (!permission.test(basePermission + '.' + decoration.name())) { + continue; + } + + resolver.resolver(StandardTags.decorations(decoration)); + } + } + + final MiniMessage miniMessage = MiniMessage.builder().tags(resolver.build()).build(); + + return miniMessage.deserialize(message); + } + + public static Component parseTags(final String basePermission, final String message, final Predicate permission) { + return parseTags(basePermission, message, permission, TagResolver.builder()); + } +} diff --git a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java index 2d26dad8..e90f1be9 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java +++ b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java @@ -28,22 +28,19 @@ import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; -import java.util.function.Predicate; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.Party; import net.draycia.carbon.api.util.InventorySlot; import net.draycia.carbon.common.config.PrimaryConfig; import net.draycia.carbon.common.messages.SourcedAudience; +import net.draycia.carbon.common.messages.TagPermissions; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextReplacementConfig; -import net.kyori.adventure.text.format.TextDecoration; -import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; -import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.model.user.User; @@ -58,22 +55,6 @@ import static java.util.Objects.requireNonNullElse; @DefaultQualifier(NonNull.class) public abstract class WrappedCarbonPlayer implements CarbonPlayer { - public static final Map DEFAULT_TAGS = Map.ofEntries( - Map.entry("hover", StandardTags.hoverEvent()), - Map.entry("click", StandardTags.clickEvent()), - Map.entry("color", StandardTags.color()), - Map.entry("keybind", StandardTags.keybind()), - Map.entry("translatable", StandardTags.translatable()), - Map.entry("insertion", StandardTags.insertion()), - Map.entry("font", StandardTags.font()), - // Decoration tags are handled separately - //Map.entry("decoration", StandardTags.decoration()), - Map.entry("gradient", StandardTags.gradient()), - Map.entry("rainbow", StandardTags.rainbow()), - Map.entry("reset", StandardTags.reset()), - Map.entry("newline", StandardTags.newline()) - ); - protected final CarbonPlayerCommon carbonPlayerCommon; protected WrappedCarbonPlayer(final CarbonPlayerCommon carbonPlayerCommon) { @@ -107,35 +88,7 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer { resolver.resolver(MiniPlaceholders.getAudiencePlaceholders(this)); } - return parseMessageTags(message, this::hasPermission, resolver); - } - - protected static Component parseMessageTags(final String message, final Predicate permission, final TagResolver.Builder resolver) { - if (!permission.test("carbon.messagetags")) { - return Component.text(message); - } - - for (final Map.Entry entry : DEFAULT_TAGS.entrySet()) { - if (permission.test("carbon.messagetags." + entry.getKey())) { - resolver.resolver(entry.getValue()); - } - } - - for (final TextDecoration decoration : TextDecoration.values()) { - if (!permission.test("carbon.messagetags." + decoration.name())) { - continue; - } - - resolver.resolver(StandardTags.decorations(decoration)); - } - - final MiniMessage miniMessage = MiniMessage.builder().tags(resolver.build()).build(); - - return miniMessage.deserialize(message); - } - - public static Component parseMessageTags(final String message, final Predicate permission) { - return parseMessageTags(message, permission, TagResolver.builder()); + return TagPermissions.parseTags(TagPermissions.MESSAGE, message, this::hasPermission, resolver); } @Override diff --git a/common/src/main/resources/carbon-permissions.yml b/common/src/main/resources/carbon-permissions.yml index 07ff0a15..2c561daf 100644 --- a/common/src/main/resources/carbon-permissions.yml +++ b/common/src/main/resources/carbon-permissions.yml @@ -22,16 +22,56 @@ carbon.nickname: "Checks your nickname." carbon.nickname.set: "Set or remove your nickname." carbon.nickname.others: "Checks other player's nicknames." carbon.nickname.others.set: "Set or remove other player's nicknames." -carbon.nickname.style.color: "Allows the use of colors in nicknames." -carbon.nickname.style.gradient: "Allows the use of gradients in nicknames." -carbon.nickname.style.decorations: "Allows the use of decorations in nicknames." -carbon.nickname.style.hoverEvent: "Allows the use of hover events in nicknames." -carbon.nickname.style.clickEvent: "Allows the use of click events in nicknames." -carbon.nickname.style.insertion: "Allows the use of insertions in nicknames." -carbon.nickname.style.rainbow: "Allows the use of the rainbow tag in nicknames." -carbon.nickname.style.reset: "Allows the use of the reset tag in nicknames." carbon.reload: "Reloads Carbon's config, channel settings, and translations." carbon.whisper: "Sends private messages to other players." carbon.whisper.continue: "Sends a message to the last player you whispered." carbon.whisper.reply: "Sends a message to the last player who messaged you." carbon.whisper.vanished: "Allows the player to send messages to vanished players." + +# Nickname tag permissions +carbon.nickname.tags.color: "Allows the use of colors in nicknames." +carbon.nickname.tags.gradient: "Allows the use of gradients in nicknames." +carbon.nickname.tags.rainbow: "Allows the use of the rainbow tag in nicknames." +carbon.nickname.tags.decorations: "Allows the use of obfuscated, bold, strikethrough, underlined, and italic in nicknames. If a player has this permission, the specific decoration permissions won't be checked." +carbon.nickname.tags.obfuscated: "Allows the use of obfuscated in nicknames." +carbon.nickname.tags.bold: "Allows the use of bold in nicknames." +carbon.nickname.tags.strikethrough: "Allows the use of strikethrough in nicknames." +carbon.nickname.tags.underlined: "Allows the use of underline in nicknames." +carbon.nickname.tags.italic: "Allows the use of italics in nicknames." +carbon.nickname.tags.hover: "Allows the use of hover events in nicknames." +carbon.nickname.tags.click: "Allows the use of click events in nicknames." +carbon.nickname.tags.insertion: "Allows the use of insertions in nicknames." +carbon.nickname.tags.reset: "Allows the use of the reset tag in nicknames." +carbon.nickname.tags.newline: "Allows the use of the newline tag in nicknames." + +# Party name tag permissions +carbon.parties.name.tags.color: "Allows the use of colors in party names." +carbon.parties.name.tags.gradient: "Allows the use of gradients in party names." +carbon.parties.name.tags.rainbow: "Allows the use of the rainbow tag in party names." +carbon.parties.name.tags.decorations: "Allows the use of obfuscated, bold, strikethrough, underlined, and italic in party names. If a player has this permission, the specific decoration permissions won't be checked." +carbon.parties.name.tags.obfuscated: "Allows the use of obfuscated in party names." +carbon.parties.name.tags.bold: "Allows the use of bold in party names." +carbon.parties.name.tags.strikethrough: "Allows the use of strikethrough in party names." +carbon.parties.name.tags.underlined: "Allows the use of underline in party names." +carbon.parties.name.tags.italic: "Allows the use of italics in party names." +carbon.parties.name.tags.hover: "Allows the use of hover events in party names." +carbon.parties.name.tags.click: "Allows the use of click events in party names." +carbon.parties.name.tags.insertion: "Allows the use of insertions in party names." +carbon.parties.name.tags.reset: "Allows the use of the reset tag in party names." +carbon.parties.name.tags.newline: "Allows the use of the newline tag in party names." + +# Message tag permissions +carbon.messagetags.color: "Allows the use of colors in messages." +carbon.messagetags.gradient: "Allows the use of gradients in messages." +carbon.messagetags.rainbow: "Allows the use of the rainbow tag in messages." +carbon.messagetags.decorations: "Allows the use of obfuscated, bold, strikethrough, underlined, and italic in messages. If a player has this permission, the specific decoration permissions won't be checked." +carbon.messagetags.obfuscated: "Allows the use of obfuscated in messages." +carbon.messagetags.bold: "Allows the use of bold in messages." +carbon.messagetags.strikethrough: "Allows the use of strikethrough in messages." +carbon.messagetags.underlined: "Allows the use of underline in messages." +carbon.messagetags.italic: "Allows the use of italics in messages." +carbon.messagetags.hover: "Allows the use of hover events in messages." +carbon.messagetags.click: "Allows the use of click events in messages." +carbon.messagetags.insertion: "Allows the use of insertions in messages." +carbon.messagetags.reset: "Allows the use of the reset tag in messages." +carbon.messagetags.newline: "Allows the use of the newline tag in messages." diff --git a/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java b/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java index 3dd14b4e..60de0d15 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java +++ b/paper/src/main/java/net/draycia/carbon/paper/hooks/DSRVChatHook.java @@ -33,6 +33,7 @@ import net.draycia.carbon.api.event.CarbonEventHandler; import net.draycia.carbon.api.event.events.CarbonChatEvent; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.channels.CarbonChannelRegistry; +import net.draycia.carbon.common.messages.TagPermissions; import net.draycia.carbon.common.users.ConsoleCarbonPlayer; import net.draycia.carbon.common.users.WrappedCarbonPlayer; import net.draycia.carbon.common.util.ChannelUtils; @@ -89,7 +90,7 @@ public final class DSRVChatHook implements ChatHook { if (carbonPlayer instanceof WrappedCarbonPlayer wrapped) { eventMessage = wrapped.parseMessageTags(messageContents); } else { - eventMessage = WrappedCarbonPlayer.parseMessageTags(messageContents, carbonPlayer::hasPermission); + eventMessage = TagPermissions.parseTags(TagPermissions.MESSAGE, messageContents, carbonPlayer::hasPermission); } DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Received a CarbonChatEvent (player: " + carbonPlayer.username() + ")");