mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 06:28:54 +00:00
Add channel quick message prefixes
This commit is contained in:
1 parent
ff40025fe5
commit
745ccd86bd
3 files changed
+43
-8
No files matched your search
@@ -9,6 +9,7 @@ import net.kyori.adventure.audience.Audience;
|
|||||||
import net.kyori.adventure.key.Keyed;
|
import net.kyori.adventure.key.Keyed;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
import static net.kyori.adventure.text.Component.empty;
|
import static net.kyori.adventure.text.Component.empty;
|
||||||
@@ -59,6 +60,14 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
|
|||||||
*/
|
*/
|
||||||
Set<Audience> filterRecipients(final CarbonPlayer sender, final Set<Audience> recipients);
|
Set<Audience> filterRecipients(final CarbonPlayer sender, final Set<Audience> recipients);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Messages will be sent in this channel if they start with this prefix.
|
||||||
|
*
|
||||||
|
* @return the message prefix that sends messages in this channel
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@Nullable String quickPrefix();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the result of a channel permission check.
|
* Represents the result of a channel permission check.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import net.draycia.carbon.api.users.ComponentPlayerResult;
|
|||||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
@@ -36,15 +37,28 @@ public final class BukkitChatListener implements Listener {
|
|||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
||||||
final var playerResult = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
final var playerResult = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||||
|
final var sender = playerResult.player();
|
||||||
|
|
||||||
if (playerResult.player() == null) {
|
if (sender == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final var sender = playerResult.player();
|
var channel = requireNonNullElse(sender.selectedChannel(),
|
||||||
final var channel = requireNonNullElse(playerResult.player().selectedChannel(),
|
|
||||||
this.registry.defaultValue());
|
this.registry.defaultValue());
|
||||||
|
|
||||||
|
var originalMessage = PlainComponentSerializer.plain().serialize(event.originalMessage());
|
||||||
|
|
||||||
|
for (var chatChannel : this.registry) {
|
||||||
|
if (chatChannel.quickPrefix() == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (originalMessage.startsWith(chatChannel.quickPrefix()) && chatChannel.speechPermitted(sender).permitted()) {
|
||||||
|
channel = chatChannel;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: option to specify if the channel should invoke ChatChannel#recipients
|
// TODO: option to specify if the channel should invoke ChatChannel#recipients
|
||||||
// or ChatChannel#filterRecipients
|
// or ChatChannel#filterRecipients
|
||||||
// for now we will just always invoke ChatChannel#recipients
|
// for now we will just always invoke ChatChannel#recipients
|
||||||
@@ -80,9 +94,9 @@ public final class BukkitChatListener implements Listener {
|
|||||||
try {
|
try {
|
||||||
if (viewer instanceof Player player) {
|
if (viewer instanceof Player player) {
|
||||||
final ComponentPlayerResult targetPlayer = this.carbonChat.server().player(player).join();
|
final ComponentPlayerResult targetPlayer = this.carbonChat.server().player(player).join();
|
||||||
component = renderer.render(playerResult.player(), targetPlayer.player(), component, message);
|
component = renderer.render(sender, targetPlayer.player(), component, message);
|
||||||
} else {
|
} else {
|
||||||
component = renderer.render(playerResult.player(), viewer, component, message);
|
component = renderer.render(sender, viewer, component, message);
|
||||||
}
|
}
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public final class ConfigChatChannel implements ChatChannel {
|
|||||||
The key can stay "carbon".
|
The key can stay "carbon".
|
||||||
The value is what's used in commands, this is probably what you want to change.
|
The value is what's used in commands, this is probably what you want to change.
|
||||||
""")
|
""")
|
||||||
private final Key key = Key.key("carbon", "basic");
|
private Key key = Key.key("carbon", "basic");
|
||||||
|
|
||||||
@Comment("""
|
@Comment("""
|
||||||
The permission required to use the channel.
|
The permission required to use the channel.
|
||||||
@@ -53,14 +53,26 @@ public final class ConfigChatChannel implements ChatChannel {
|
|||||||
To send messages you must have the permission carbon.channel.basic.speak
|
To send messages you must have the permission carbon.channel.basic.speak
|
||||||
If you want to give both, grant carbon.channel.basic or carbon.channel.basic.*
|
If you want to give both, grant carbon.channel.basic or carbon.channel.basic.*
|
||||||
""")
|
""")
|
||||||
private final String permission = "carbon.channel.basic";
|
private String permission = "carbon.channel.basic";
|
||||||
|
|
||||||
@Setting("format")
|
@Setting("format")
|
||||||
@Comment("The chat formats for this channel.")
|
@Comment("The chat formats for this channel.")
|
||||||
private final ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource();
|
private ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource();
|
||||||
|
|
||||||
|
@Comment("Messages will be sent in this channel if they start with this prefix.")
|
||||||
|
private @Nullable String quickPrefix = "";
|
||||||
|
|
||||||
private transient @Nullable ConfigChannelMessageService messageService = null;
|
private transient @Nullable ConfigChannelMessageService messageService = null;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable String quickPrefix() {
|
||||||
|
if (this.quickPrefix == null || this.quickPrefix.isBlank()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.quickPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Component render(
|
public @NotNull Component render(
|
||||||
final CarbonPlayer sender,
|
final CarbonPlayer sender,
|
||||||
|
|||||||
Reference in new issue
Block a user