mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Add message prefix feature
This commit is contained in:
1 parent
d328b09c36
commit
f64ae319ed
4 files changed
+41
-4
No files matched your search
@@ -4,6 +4,7 @@ import net.draycia.carbon.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -39,6 +40,9 @@ public abstract class ChatChannel {
|
||||
|
||||
public abstract String getKey();
|
||||
|
||||
@Nullable
|
||||
public abstract String getPrefix();
|
||||
|
||||
public abstract String getAliases();
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -257,6 +258,7 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
|
||||
@Override
|
||||
public String getFormat(String group) {
|
||||
// TODO: investigate this resorting to vanilla formatting when primaryGroupOnly is true and certain conditions are met
|
||||
return getFormats().getOrDefault(group, getFormats().getOrDefault(getDefaultFormatName(),
|
||||
"<white><%player_displayname%<white>> <message>"));
|
||||
}
|
||||
@@ -286,6 +288,16 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
return name == null ? key : name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getPrefix() {
|
||||
if (config != null && config.contains("prefix")) {
|
||||
return config.getString("prefix");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwitchMessage() {
|
||||
return getString("switch-message");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.draycia.carbon.listeners;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -22,17 +23,33 @@ public class BukkitChatListener implements Listener {
|
||||
event.getRecipients().clear();
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
|
||||
ChatChannel channel = user.getSelectedChannel();
|
||||
|
||||
// TODO: ChatUser#canUseChannel(ChatChannel channel)
|
||||
if (!user.getSelectedChannel().canPlayerUse(user)) {
|
||||
for (ChatChannel entry : carbonChat.getChannelManager().getRegistry().values()) {
|
||||
if (entry.getPrefix() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (event.getMessage().startsWith(entry.getPrefix())) {
|
||||
if (entry.canPlayerUse(user)) {
|
||||
event.setMessage(event.getMessage().substring(entry.getPrefix().length()));
|
||||
channel = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final ChatChannel selectedChannel = channel;
|
||||
|
||||
if (!selectedChannel.canPlayerUse(user)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.isAsynchronous()) {
|
||||
user.getSelectedChannel().sendMessage(user, event.getMessage(), false);
|
||||
selectedChannel.sendMessage(user, event.getMessage(), false);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
user.getSelectedChannel().sendMessage(user, event.getMessage(), false);
|
||||
selectedChannel.sendMessage(user, event.getMessage(), false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,9 @@ channels:
|
||||
aliases: 'global|g'
|
||||
# If this channel is the default channel
|
||||
default: true
|
||||
# If the player speaks in chat, and their message starts with the following, their message
|
||||
# is automatically sent in this channel. This setting is entirely optional.
|
||||
prefix: '#'
|
||||
announcements:
|
||||
name: 'Announcements'
|
||||
formats:
|
||||
@@ -59,6 +62,7 @@ channels:
|
||||
staff: '<gold>Broadcast: <yellow><message>'
|
||||
ignorable: false
|
||||
should-bungee: false
|
||||
prefix: '!'
|
||||
|
||||
# Options:
|
||||
# 'MiniMessage': https://github.com/KyoriPowered/adventure-docs/blob/feature/minimessage/source/minimessage.rst#the-components
|
||||
|
||||
Reference in new issue
Block a user