mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
Add permission to allow PAPI placeholders in messages
Fix chat placeholders not being properly used
This commit is contained in:
1 parent
f459d47bdb
commit
83e5439de2
9 files changed
+199
-68
No files matched your search
@@ -56,6 +56,7 @@ import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.event.events.CarbonReloadEvent;
|
||||
import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.key.Key;
|
||||
@@ -314,7 +315,13 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(Key.key("carbon", "default"), channel));
|
||||
|
||||
final Component eventMessage = ConfigChatChannel.parseMessageTags(sender, plainMessage);
|
||||
final Component eventMessage;
|
||||
|
||||
if (sender instanceof WrappedCarbonPlayer wrapped) {
|
||||
eventMessage = wrapped.parseMessageTags(plainMessage);
|
||||
} else {
|
||||
eventMessage = WrappedCarbonPlayer.parseMessageTags(plainMessage, sender::hasPermission);
|
||||
}
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, eventMessage, recipients, renderers, channel, null);
|
||||
this.eventHandler.emit(chatEvent);
|
||||
|
||||
@@ -24,18 +24,15 @@ import io.leangen.geantyref.TypeToken;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.SourcedAudience;
|
||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessages;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.messages.SourcedMessageSender;
|
||||
import net.draycia.carbon.common.messages.SourcedReceiverResolver;
|
||||
import net.draycia.carbon.common.messages.placeholders.BooleanPlaceholderResolver;
|
||||
@@ -46,10 +43,6 @@ import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
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 net.kyori.moonshine.Moonshine;
|
||||
import net.kyori.moonshine.exception.scan.UnscannableMethodException;
|
||||
import net.kyori.moonshine.strategy.StandardPlaceholderResolverStrategy;
|
||||
@@ -159,56 +152,6 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
);
|
||||
}
|
||||
|
||||
public static final Map<String, TagResolver> 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())
|
||||
);
|
||||
|
||||
public static Component parseMessageTags(final Commander sender, final String message) {
|
||||
return parseMessageTags(message, sender::hasPermission);
|
||||
}
|
||||
|
||||
public static Component parseMessageTags(final CarbonPlayer sender, final String message) {
|
||||
return parseMessageTags(message, sender::hasPermission);
|
||||
}
|
||||
|
||||
public static Component parseMessageTags(final String message, final Predicate<String> permission) {
|
||||
if (!permission.test("carbon.messagetags")) {
|
||||
return Component.text(message);
|
||||
}
|
||||
|
||||
final TagResolver.Builder resolver = TagResolver.builder();
|
||||
|
||||
for (final Map.Entry<String, TagResolver> 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer) {
|
||||
return ChannelPermissionResult.allowedIf(text("Insufficient permissions!"), () ->
|
||||
|
||||
+2
-2
@@ -27,7 +27,6 @@ import com.google.common.base.Suppliers;
|
||||
import com.google.inject.Inject;
|
||||
import java.util.function.Supplier;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.channels.ConfigChatChannel;
|
||||
import net.draycia.carbon.common.command.ArgumentFactory;
|
||||
import net.draycia.carbon.common.command.CarbonCommand;
|
||||
import net.draycia.carbon.common.command.CommandSettings;
|
||||
@@ -35,6 +34,7 @@ import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
import net.draycia.carbon.common.util.CloudUtils;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
@@ -158,7 +158,7 @@ public 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 ConfigChatChannel.parseMessageTags(sender, trimQuotes(nick));
|
||||
return WrappedCarbonPlayer.parseMessageTags(trimQuotes(nick), sender::hasPermission);
|
||||
}
|
||||
|
||||
private static String trimQuotes(final String string) {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.event.events;
|
||||
|
||||
import net.draycia.carbon.api.event.CarbonEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
|
||||
public class CarbonEarlyChatEvent implements CarbonEvent {
|
||||
|
||||
public final CarbonPlayer sender;
|
||||
public String message;
|
||||
|
||||
public CarbonEarlyChatEvent(final CarbonPlayer sender, final String message) {
|
||||
this.sender = sender;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public CarbonPlayer sender() {
|
||||
return this.sender;
|
||||
}
|
||||
|
||||
public String message() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void message(final String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,15 +21,15 @@ package net.draycia.carbon.common.listeners;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.event.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.channels.ConfigChatChannel;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.event.events.CarbonEarlyChatEvent;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.chat.SignedMessage;
|
||||
import net.kyori.adventure.key.Key;
|
||||
@@ -53,7 +53,19 @@ public abstract class ChatListenerInternal {
|
||||
|
||||
protected CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage) {
|
||||
String content = this.configFactory.primaryConfig().applyChatPlaceholders(messageContent);
|
||||
Component message = ConfigChatChannel.parseMessageTags(sender, content);
|
||||
|
||||
final CarbonEarlyChatEvent earlyChatEvent = new CarbonEarlyChatEvent(sender, content);
|
||||
this.carbonChat.eventHandler().emit(earlyChatEvent);
|
||||
|
||||
content = earlyChatEvent.message();
|
||||
|
||||
Component message;
|
||||
|
||||
if (sender instanceof WrappedCarbonPlayer wrapped) {
|
||||
message = wrapped.parseMessageTags(content);
|
||||
} else {
|
||||
message = WrappedCarbonPlayer.parseMessageTags(content, sender::hasPermission);
|
||||
}
|
||||
|
||||
final CarbonPlayer.ChannelMessage channelMessage = sender.channelForMessage(message);
|
||||
final ChatChannel channel = channelMessage.channel();
|
||||
|
||||
@@ -22,7 +22,9 @@ package net.draycia.carbon.common.users;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.InventorySlot;
|
||||
@@ -30,6 +32,10 @@ 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.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;
|
||||
@@ -45,6 +51,22 @@ import static java.util.Objects.requireNonNullElse;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
||||
|
||||
public static final Map<String, TagResolver> 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) {
|
||||
@@ -59,6 +81,36 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
||||
return LuckPermsProvider.get().getUserManager().getUser(this.uuid());
|
||||
}
|
||||
|
||||
public Component parseMessageTags(final String message) {
|
||||
return parseMessageTags(message, this::hasPermission);
|
||||
}
|
||||
|
||||
public static Component parseMessageTags(final String message, final Predicate<String> permission) {
|
||||
if (!permission.test("carbon.messagetags")) {
|
||||
return Component.text(message);
|
||||
}
|
||||
|
||||
final TagResolver.Builder resolver = TagResolver.builder();
|
||||
|
||||
for (final Map.Entry<String, TagResolver> 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean awareOf(final CarbonPlayer other) {
|
||||
if (other.vanished()) {
|
||||
|
||||
Reference in new issue
Block a user