mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Add channel whisper functionality (#91)
* ChatChannel -> TextChannel, WhisperChannel, GroupChannel * Add basic whisper channel support * Move WhisperPings into WhisperOptions * Add defaults to WhisperOptions * Various deprecation and refactor fixes * Test and fux bugs
This commit is contained in:
1 parent
14dfcfbf38
commit
f9cac355b6
44 files changed
+797
-304
No files matched your search
+8
-3
@@ -1,10 +1,11 @@
|
||||
package net.draycia.carbon.common.adventure;
|
||||
|
||||
import de.themoep.minedown.adventure.MineDown;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.adventure.MessageProcessor;
|
||||
import net.kyori.adventure.platform.AudienceProvider;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -14,18 +15,22 @@ public class AdventureManager implements MessageProcessor {
|
||||
@NonNull
|
||||
private final AudienceProvider provider;
|
||||
|
||||
@NonNull
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
@NonNull
|
||||
private final FormatType formatType;
|
||||
|
||||
public AdventureManager(final @NonNull AudienceProvider provider, final @NonNull FormatType formatType) {
|
||||
this.provider = provider;
|
||||
this.formatType = formatType;
|
||||
this.carbonChat = CarbonChatProvider.carbonChat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component processMessage(@Nullable final String input, final @NonNull String @NonNull ... placeholders) {
|
||||
if (input == null || input.trim().isEmpty()) {
|
||||
return TextComponent.empty();
|
||||
return Component.empty();
|
||||
}
|
||||
|
||||
switch (this.formatType) {
|
||||
@@ -49,7 +54,7 @@ public class AdventureManager implements MessageProcessor {
|
||||
input = input.replace("<" + placeholder + ">", replacement);
|
||||
}
|
||||
|
||||
return this.audiences().gsonSerializer().deserialize(input);
|
||||
return this.carbonChat.gsonSerializer().deserialize(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+9
-9
@@ -1,10 +1,10 @@
|
||||
package net.draycia.carbon.common.channels;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.config.ChannelOptions;
|
||||
import net.draycia.carbon.api.users.ConsoleUser;
|
||||
import net.draycia.carbon.common.utils.ColorUtils;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.api.events.ChatFormatEvent;
|
||||
@@ -31,7 +31,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CarbonChatChannel implements ChatChannel {
|
||||
public class CarbonChatChannel implements TextChannel {
|
||||
|
||||
@NonNull
|
||||
private final CarbonChat carbonChat;
|
||||
@@ -205,7 +205,7 @@ public class CarbonChatChannel implements ChatChannel {
|
||||
@Override
|
||||
public void sendComponents(final @NonNull Map<ChatUser, Component> components) {
|
||||
for (final Map.Entry<ChatUser, Component> entry : components.entrySet()) {
|
||||
if (entry.getValue().equals(TextComponent.empty())) {
|
||||
if (entry.getValue().equals(Component.empty())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -216,7 +216,7 @@ public class CarbonChatChannel implements ChatChannel {
|
||||
@Override
|
||||
public void sendComponentsAndLog(final @NonNull Map<ChatUser, Component> components) {
|
||||
for (final Map.Entry<ChatUser, Component> entry : components.entrySet()) {
|
||||
if (entry.getValue().equals(TextComponent.empty())) {
|
||||
if (entry.getValue().equals(Component.empty())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -327,7 +327,7 @@ public class CarbonChatChannel implements ChatChannel {
|
||||
public void sendMessageToBungee(final @NonNull UUID uuid, final @NonNull Component component) {
|
||||
this.carbonChat.messageService().sendMessage("channel-component", uuid, byteArray -> {
|
||||
byteArray.writeUTF(this.key());
|
||||
byteArray.writeUTF(this.carbonChat.messageProcessor().audiences().gsonSerializer().serialize(component));
|
||||
byteArray.writeUTF(this.carbonChat.gsonSerializer().serialize(component));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -495,10 +495,10 @@ public class CarbonChatChannel implements ChatChannel {
|
||||
return this.options().primaryGroupOnly();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldCancelChatEvent() {
|
||||
return this.options().shouldCancelChatEvent();
|
||||
}
|
||||
// @Override
|
||||
// public boolean shouldCancelChatEvent() {
|
||||
// return this.options().shouldCancelChatEvent();
|
||||
// }
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
|
||||
+278
@@ -0,0 +1,278 @@
|
||||
package net.draycia.carbon.common.channels;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.WhisperChannel;
|
||||
import net.draycia.carbon.api.events.ChatComponentEvent;
|
||||
import net.draycia.carbon.api.events.ChatFormatEvent;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.users.ChatUser;
|
||||
import net.draycia.carbon.api.users.ConsoleUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CarbonWhisperChannel implements WhisperChannel {
|
||||
|
||||
private final @NonNull CarbonChat carbonChat;
|
||||
|
||||
private final @NonNull ChatUser sender;
|
||||
private final @NonNull ChatUser audience;
|
||||
|
||||
public CarbonWhisperChannel(final @NonNull ChatUser sender, final @NonNull ChatUser audience) {
|
||||
this.carbonChat = CarbonChatProvider.carbonChat();
|
||||
this.sender = sender;
|
||||
this.audience = audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ChatUser sender() {
|
||||
return this.sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ChatUser audience() {
|
||||
return this.audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable TextColor channelColor(@NonNull final ChatUser user) {
|
||||
return NamedTextColor.WHITE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Map<ChatUser, Component> parseMessage(@NonNull final ChatUser user, @NonNull final String message,
|
||||
final boolean fromRemote) {
|
||||
return this.parseMessage(user, Collections.singleton(this.audience()), message, fromRemote);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Map<ChatUser, Component> parseMessage(@NonNull final ChatUser user,
|
||||
@NonNull final Collection<@NonNull ChatUser> recipients,
|
||||
@NonNull final String message, final boolean fromRemote) {
|
||||
// TODO: extract to method, this is just the same thing but twice
|
||||
final Map<ChatUser, Component> result = new HashMap<>();
|
||||
|
||||
// Formats
|
||||
final String senderFormat = this.senderFormat();
|
||||
final String receiverFormat = this.receiverFormat();
|
||||
|
||||
final ChatFormatEvent senderFormatEvent = new ChatFormatEvent(this.sender, this.sender, this, senderFormat, message);
|
||||
|
||||
CarbonEvents.post(senderFormatEvent);
|
||||
|
||||
if (senderFormatEvent.cancelled() || senderFormatEvent.message().trim().isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
final ChatFormatEvent receiverFormatEvent = new ChatFormatEvent(this.sender, this.audience, this, receiverFormat, message);
|
||||
|
||||
CarbonEvents.post(receiverFormatEvent);
|
||||
|
||||
if (receiverFormatEvent.cancelled() || receiverFormatEvent.message().trim().isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// Display names
|
||||
final String senderName;
|
||||
|
||||
if (this.sender.nickname() != null) {
|
||||
senderName = this.sender.nickname();
|
||||
} else {
|
||||
senderName = this.sender.displayName();
|
||||
}
|
||||
|
||||
final String receiverName;
|
||||
|
||||
if (this.audience.nickname() != null) {
|
||||
receiverName = this.audience.nickname();
|
||||
} else {
|
||||
receiverName = this.audience.displayName();
|
||||
}
|
||||
|
||||
// Components
|
||||
final TextComponent senderComponent = (TextComponent) this.carbonChat.messageProcessor().processMessage(
|
||||
senderFormatEvent.format(),
|
||||
"br", "\n",
|
||||
"senderdisplayname", senderName,
|
||||
"sender", this.sender.name(),
|
||||
"receiverdisplayname", receiverName,
|
||||
"receiver", this.audience.name(),
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", this.carbonChat.carbonSettings().serverName(),
|
||||
"message", senderFormatEvent.message());
|
||||
|
||||
final ChatComponentEvent senderComponentEvent = new ChatComponentEvent(this.sender, this.sender, this,
|
||||
senderComponent, senderFormatEvent.message());
|
||||
|
||||
CarbonEvents.post(senderComponentEvent);
|
||||
|
||||
result.put(this.sender, senderComponentEvent.component());
|
||||
|
||||
final TextComponent receiverComponent = (TextComponent) this.carbonChat.messageProcessor().processMessage(
|
||||
receiverFormatEvent.format(),
|
||||
"br", "\n",
|
||||
"senderdisplayname", senderName,
|
||||
"sender", this.sender.name(),
|
||||
"receiverdisplayname", receiverName,
|
||||
"receiver", this.audience.name(),
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", this.carbonChat.carbonSettings().serverName(),
|
||||
"message", senderFormatEvent.message());
|
||||
|
||||
final ChatComponentEvent receiverComponentEvent = new ChatComponentEvent(this.sender, this.audience, this,
|
||||
receiverComponent, receiverFormatEvent.message());
|
||||
|
||||
CarbonEvents.post(receiverComponentEvent);
|
||||
|
||||
result.put(this.audience, receiverComponentEvent.component());
|
||||
|
||||
System.out.println(this.sender.equals(this.audience));
|
||||
|
||||
final ChatFormatEvent consoleFormatEvent = new ChatFormatEvent(this.sender, null, this,
|
||||
this.consoleFormat(), message);
|
||||
|
||||
CarbonEvents.post(consoleFormatEvent);
|
||||
|
||||
final TextComponent consoleFormat = (TextComponent) this.carbonChat.messageProcessor().processMessage(
|
||||
consoleFormatEvent.format(),
|
||||
"br", "\n",
|
||||
"senderdisplayname", senderName,
|
||||
"sender", this.sender.name(),
|
||||
"receiverdisplayname", receiverName,
|
||||
"receiver", this.audience.name(),
|
||||
"phase", Long.toString(System.currentTimeMillis() % 25),
|
||||
"server", this.carbonChat.carbonSettings().serverName(),
|
||||
"message", senderFormatEvent.message());
|
||||
|
||||
// TODO: add ConsoleUser to users map
|
||||
final ChatComponentEvent consoleEvent = new ChatComponentEvent(this.sender, null, this, consoleFormat,
|
||||
consoleFormatEvent.message());
|
||||
|
||||
CarbonEvents.post(consoleEvent);
|
||||
|
||||
System.out.println(result.size());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(@NonNull final ChatUser user) {
|
||||
return user.equals(this.sender);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(@NonNull final ChatUser sender, @NonNull final ChatUser target, final boolean checkSpying) {
|
||||
return (checkSpying && target.spyingWhispers()) || (sender.equals(this.sender) && target.equals(this.audience));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerSee(@NonNull final ChatUser target, final boolean checkSpying) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendComponents(final @NonNull Map<ChatUser, Component> components) {
|
||||
for (final Map.Entry<ChatUser, Component> entry : components.entrySet()) {
|
||||
entry.getKey().sendMessage(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendComponentsAndLog(final @NonNull Map<ChatUser, Component> components) {
|
||||
for (final Map.Entry<ChatUser, Component> entry : components.entrySet()) {
|
||||
entry.getKey().sendMessage(entry.getValue());
|
||||
|
||||
if (entry.getKey() instanceof ConsoleUser) {
|
||||
this.carbonChat.messageProcessor().audiences().console().sendMessage(entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String name() {
|
||||
return "Whisper";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull String key() {
|
||||
return "whisper";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignorable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull List<@NonNull Pattern> itemLinkPatterns() {
|
||||
return this.carbonChat.carbonSettings().itemLinkPatterns();
|
||||
}
|
||||
|
||||
public @Nullable String senderFormat() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().senderFormat();
|
||||
}
|
||||
|
||||
public @Nullable String receiverFormat() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().receiverFormat();
|
||||
}
|
||||
|
||||
public @Nullable String consoleFormat() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().consoleFormat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String switchMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().switchMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String switchOtherMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().switchOtherMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String switchFailureMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().switchFailureMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String cannotIgnoreMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().cannotIgnoreMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String toggleOffMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().toggleOffMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String toggleOnMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().toggleOnMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String toggleOtherOnMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().toggleOtherOnMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String toggleOtherOffMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().toggleOtherOffMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String cannotUseMessage() {
|
||||
return this.carbonChat.carbonSettings().whisperOptions().cannotUseMessage();
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -1,6 +1,7 @@
|
||||
package net.draycia.carbon.common.channels;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.config.ChannelOptions;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
@@ -50,9 +51,11 @@ public class ChannelManager {
|
||||
public void registerChannel(final @NonNull ChatChannel channel) {
|
||||
this.registry().register(channel.key(), channel);
|
||||
|
||||
if (channel.isDefault() && this.defaultChannelKey == null) {
|
||||
this.carbonChat.logger().info("Default channel registered: " + channel.name());
|
||||
this.defaultChannelKey = channel.key();
|
||||
if (channel instanceof TextChannel) {
|
||||
if (((TextChannel) channel).isDefault() && this.defaultChannelKey == null) {
|
||||
this.carbonChat.logger().info("Default channel registered: " + channel.name());
|
||||
this.defaultChannelKey = channel.key();
|
||||
}
|
||||
}
|
||||
|
||||
CarbonEvents.post(new ChannelRegisterEvent(Collections.singletonList(channel), this.registry()));
|
||||
@@ -95,7 +98,11 @@ public class ChannelManager {
|
||||
|
||||
if (channel != null) {
|
||||
this.registerChannel(channel);
|
||||
String prefix = channel.messagePrefix();
|
||||
String prefix = null;
|
||||
|
||||
if (channel instanceof TextChannel) {
|
||||
prefix = ((TextChannel) channel).messagePrefix();
|
||||
}
|
||||
|
||||
if (prefix != null && !prefix.trim().isEmpty()) {
|
||||
prefix = "(" + prefix + ") ";
|
||||
|
||||
+4
-4
@@ -5,7 +5,7 @@ import com.intellectualsites.commands.arguments.standard.StringArgument;
|
||||
import com.intellectualsites.commands.context.CommandContext;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.users.ChatUser;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
@@ -13,11 +13,11 @@ public class AliasedChannelCommand {
|
||||
|
||||
private final @NonNull CarbonChat carbonChat;
|
||||
|
||||
private final @NonNull ChatChannel chatChannel;
|
||||
private final @NonNull TextChannel chatChannel;
|
||||
|
||||
private final @NonNull String commandName;
|
||||
|
||||
public AliasedChannelCommand(final @NonNull CommandManager<ChatUser> commandManager, final @NonNull ChatChannel chatChannel) {
|
||||
public AliasedChannelCommand(final @NonNull CommandManager<ChatUser> commandManager, final @NonNull TextChannel chatChannel) {
|
||||
this.carbonChat = CarbonChatProvider.carbonChat();
|
||||
this.chatChannel = chatChannel;
|
||||
this.commandName = chatChannel.key();
|
||||
@@ -62,7 +62,7 @@ public class AliasedChannelCommand {
|
||||
});
|
||||
}
|
||||
|
||||
public @NonNull ChatChannel chatChannel() {
|
||||
public @NonNull TextChannel chatChannel() {
|
||||
return this.chatChannel;
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ import com.intellectualsites.commands.arguments.standard.StringArgument;
|
||||
import com.intellectualsites.commands.context.CommandContext;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.commands.settings.CommandSettings;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.ChatUser;
|
||||
@@ -45,7 +46,7 @@ public class ChannelCommand {
|
||||
|
||||
private void channel(final @NonNull CommandContext<ChatUser> context) {
|
||||
final ChatUser user = context.getSender();
|
||||
final ChatChannel channel = context.getRequired("channel");
|
||||
final TextChannel channel = context.getRequired("channel");
|
||||
|
||||
if (user.channelSettings(channel).ignored()) {
|
||||
user.sendMessage(this.carbonChat.messageProcessor().processMessage(channel.cannotUseMessage(),
|
||||
|
||||
+4
-4
@@ -83,7 +83,7 @@ public class ChannelListCommand {
|
||||
}
|
||||
|
||||
final Iterator<ChatChannel> visibleChannels = canSee.iterator();
|
||||
final TextComponent.Builder availableList = TextComponent.builder("");
|
||||
final TextComponent.Builder availableList = Component.text();
|
||||
|
||||
this.makeList(visibleChannels, availableList);
|
||||
|
||||
@@ -95,7 +95,7 @@ public class ChannelListCommand {
|
||||
|
||||
if (sender.hasPermission("carbonchat.channellist.bypass") && !cannotSee.isEmpty()) {
|
||||
final Iterator<ChatChannel> invisibleChannels = cannotSee.iterator();
|
||||
final TextComponent.Builder unavailableList = TextComponent.builder("");
|
||||
final TextComponent.Builder unavailableList = Component.text();
|
||||
|
||||
this.makeList(invisibleChannels, unavailableList);
|
||||
|
||||
@@ -110,10 +110,10 @@ public class ChannelListCommand {
|
||||
private void makeList(final @NonNull Iterator<@NonNull ChatChannel> iterator, final TextComponent.@NonNull Builder list) {
|
||||
final String listSeparator = this.carbonChat.translations().channelListSeparator();
|
||||
// TODO: Larry, why did you double assign the listSeparatorComponent?
|
||||
final Component listSeparatorComponent = TextComponent.of(listSeparator);
|
||||
final Component listSeparatorComponent = Component.text(listSeparator);
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
list.append(TextComponent.of(iterator.next().name()));
|
||||
list.append(Component.text(iterator.next().name()));
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
list.append(listSeparatorComponent);
|
||||
|
||||
+21
-3
@@ -7,9 +7,12 @@ import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.commands.settings.CommandSettings;
|
||||
import net.draycia.carbon.api.users.ChatUser;
|
||||
import net.draycia.carbon.common.channels.CarbonWhisperChannel;
|
||||
import net.draycia.carbon.common.utils.CommandUtils;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MessageCommand {
|
||||
|
||||
@NonNull
|
||||
@@ -31,15 +34,30 @@ public class MessageCommand {
|
||||
.withSenderType(ChatUser.class) // player
|
||||
.withPermission("carbonchat.message")
|
||||
.argument(CommandUtils.chatUserArgument())
|
||||
.argument(StringArgument.<ChatUser>newBuilder("message").greedy().build())
|
||||
.argument(StringArgument.<ChatUser>newBuilder("message").greedy().asOptional().build())
|
||||
.handler(this::sendMessage)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
private void sendMessage(final @NonNull CommandContext<ChatUser> context) {
|
||||
context.<ChatUser>getRequired("target")
|
||||
.sendMessage(context.getSender(), context.getRequired("message"));
|
||||
final ChatUser sender = context.getSender();
|
||||
final ChatUser receiver = context.getRequired("user");
|
||||
|
||||
if (sender.equals(receiver)) {
|
||||
final String message = this.carbonChat.translations().cannotWhisperSelf();
|
||||
sender.sendMessage(this.carbonChat.messageProcessor().processMessage(message));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
final Optional<String> message = context.get("message");
|
||||
|
||||
if (message.isPresent()) {
|
||||
receiver.sendMessage(sender, message.get());
|
||||
} else {
|
||||
sender.selectedChannel(new CarbonWhisperChannel(sender, receiver));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -85,7 +85,7 @@ public class SpyChannelCommand {
|
||||
|
||||
final String message;
|
||||
|
||||
if (user.spyingwhispers()) {
|
||||
if (user.spyingWhispers()) {
|
||||
user.spyingWhispers(false);
|
||||
message = this.carbonChat.translations().spyWhispersOff();
|
||||
} else {
|
||||
|
||||
@@ -44,7 +44,7 @@ public final class KeySerializer extends ScalarSerializer<Key> {
|
||||
}
|
||||
|
||||
try {
|
||||
return Key.of(obj.toString());
|
||||
return Key.key(obj.toString());
|
||||
} catch(final InvalidKeyException ex) {
|
||||
throw new ObjectMappingException(ex);
|
||||
}
|
||||
|
||||
+3
-2
@@ -63,7 +63,7 @@ public final class SoundSerializer implements TypeSerializer<Sound> {
|
||||
throw new ObjectMappingException("A name and source are required to deserialize a Sound");
|
||||
}
|
||||
|
||||
return Sound.of(name, source, volume, pitch);
|
||||
return Sound.sound(name, source, volume, pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,10 +72,11 @@ public final class SoundSerializer implements TypeSerializer<Sound> {
|
||||
value.setValue(null);
|
||||
return;
|
||||
}
|
||||
|
||||
value.getNode(NAME).setValue(KeySerializer.INSTANCE.type(), obj.name());
|
||||
value.getNode(SOURCE).setValue(SOURCE_TYPE, obj.source());
|
||||
value.getNode(VOLUME).setValue(obj.volume());
|
||||
value.getNode(PITCH).setValue(obj.pitch());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package net.draycia.carbon.common.contexts;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.events.PreChatFormatEvent;
|
||||
import net.draycia.carbon.api.Context;
|
||||
import net.kyori.event.PostOrders;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class FilterContext {
|
||||
|
||||
@NonNull
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public FilterContext(final @NonNull CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
CarbonEvents.register(PreChatFormatEvent.class, PostOrders.FIRST, false, event -> {
|
||||
if (!carbonChat.moderationSettings().filters().enabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.user().hasPermission("carbonchat.filter.exempt")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.channelUsesFilter(event.channel())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String message = event.message();
|
||||
|
||||
for (final Map.Entry<String, List<Pattern>> entry :
|
||||
this.carbonChat.moderationSettings().filters().replacements().entrySet()) {
|
||||
|
||||
for (final Pattern pattern : entry.getValue()) {
|
||||
final Matcher matcher = pattern.matcher(message);
|
||||
|
||||
if (entry.getKey().equals("_")) {
|
||||
message = matcher.replaceAll("");
|
||||
} else {
|
||||
message = matcher.replaceAll(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
event.message(message);
|
||||
|
||||
for (final Pattern blockedWord : this.carbonChat.moderationSettings().filters().blockedPatterns()) {
|
||||
if (blockedWord.matcher(event.message()).find()) {
|
||||
event.cancelled(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean channelUsesFilter(final @NonNull ChatChannel chatChannel) {
|
||||
if (chatChannel instanceof TextChannel) {
|
||||
final Context context = ((TextChannel) chatChannel).context("filter");
|
||||
|
||||
if (context == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return context.isBoolean() && context.asBoolean();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -41,7 +41,7 @@ public class PingHandler {
|
||||
final float volume = carbonChat.carbonSettings().channelPings().volume();
|
||||
final float pitch = carbonChat.carbonSettings().channelPings().pitch();
|
||||
|
||||
event.target().playSound(Sound.of(key, source, volume, pitch));
|
||||
event.target().playSound(Sound.sound(key, source, volume, pitch));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+2
-11
@@ -4,8 +4,6 @@ import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.events.misc.CarbonEvents;
|
||||
import net.draycia.carbon.api.events.PrivateMessageEvent;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.event.PostOrders;
|
||||
|
||||
public class WhisperPingHandler {
|
||||
@@ -22,16 +20,9 @@ public class WhisperPingHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!carbonChat.carbonSettings().whisperPings().enabled()) {
|
||||
return;
|
||||
if (carbonChat.carbonSettings().whisperOptions().pings().enabled()) {
|
||||
event.target().playSound(carbonChat.carbonSettings().whisperOptions().pings().sound());
|
||||
}
|
||||
|
||||
final Key key = carbonChat.carbonSettings().whisperPings().sound();
|
||||
final Sound.Source source = carbonChat.carbonSettings().whisperPings().source();
|
||||
final float volume = carbonChat.carbonSettings().whisperPings().volume();
|
||||
final float pitch = carbonChat.carbonSettings().whisperPings().pitch();
|
||||
|
||||
event.sender().playSound(Sound.of(key, source, volume, pitch));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+5
-9
@@ -1,13 +1,13 @@
|
||||
package net.draycia.carbon.common.messaging;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.messaging.MessageService;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.UUID;
|
||||
@@ -21,13 +21,9 @@ public class MessageManager {
|
||||
@NonNull
|
||||
private final MessageService messageService;
|
||||
|
||||
@NonNull
|
||||
private final GsonComponentSerializer gsonSerializer;
|
||||
|
||||
public MessageManager(final @NonNull CarbonChat carbonChat, final @NonNull MessageService messageService) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.messageService = messageService;
|
||||
this.gsonSerializer = this.carbonChat.messageProcessor().audiences().gsonSerializer();
|
||||
|
||||
this.registerDefaultListeners();
|
||||
}
|
||||
@@ -96,10 +92,10 @@ public class MessageManager {
|
||||
final ChatChannel channel = this.carbonChat.channelRegistry().get(byteArray.readUTF());
|
||||
final ChatUser user = this.carbonChat.userService().wrap(uuid);
|
||||
|
||||
if (channel != null) {
|
||||
final Component component = this.gsonSerializer.deserialize(byteArray.readUTF());
|
||||
if (channel instanceof TextChannel) {
|
||||
final Component component = this.carbonChat.gsonSerializer().deserialize(byteArray.readUTF());
|
||||
|
||||
channel.sendComponent(user, component);
|
||||
((TextChannel) channel).sendComponent(user, component);
|
||||
|
||||
this.carbonChat.messageProcessor().audiences().console().sendMessage(component);
|
||||
}
|
||||
@@ -113,7 +109,7 @@ public class MessageManager {
|
||||
|
||||
if (!target.ignoringUser(uuid)) {
|
||||
target.replyTarget(uuid);
|
||||
target.sendMessage(this.gsonSerializer.deserialize(message));
|
||||
target.sendMessage(this.carbonChat.gsonSerializer().deserialize(message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+2
-2
@@ -218,8 +218,8 @@ public class MySQLUserService<T extends ChatUser> implements UserService<T> {
|
||||
this.carbonChat.logger().info("Saving user data!");
|
||||
stm.executeUpdateQuery("INSERT INTO sc_users (uuid, channel, muted, shadowmuted, spyingwhispers, nickname) VALUES (?, ?, ?, ?, ?, ?) " +
|
||||
"ON DUPLICATE KEY UPDATE channel = ?, muted = ?, shadowmuted = ?, spyingwhispers = ?, nickname =?",
|
||||
user.uuid().toString(), selectedName, user.muted(), user.shadowMuted(), user.spyingwhispers(), user.nickname(),
|
||||
selectedName, user.muted(), user.shadowMuted(), user.spyingwhispers(), user.nickname());
|
||||
user.uuid().toString(), selectedName, user.muted(), user.shadowMuted(), user.spyingWhispers(), user.nickname(),
|
||||
selectedName, user.muted(), user.shadowMuted(), user.spyingWhispers(), user.nickname());
|
||||
|
||||
this.carbonChat.logger().info("Saving user channel settings!");
|
||||
// Save user channel settings
|
||||
|
||||
@@ -19,8 +19,8 @@ public final class ColorUtils {
|
||||
|
||||
@Nullable
|
||||
public static TextColor parseColor(@Nullable final ChatUser user, @Nullable String input) {
|
||||
if (input == null) {
|
||||
input = "white";
|
||||
if (input == null || input.isEmpty()) {
|
||||
return NamedTextColor.WHITE;
|
||||
}
|
||||
|
||||
// TODO: find out way to do this
|
||||
|
||||
@@ -2,10 +2,15 @@ package net.draycia.carbon.common.utils;
|
||||
|
||||
import com.intellectualsites.commands.arguments.CommandArgument;
|
||||
import com.intellectualsites.commands.arguments.parser.ArgumentParseResult;
|
||||
import com.intellectualsites.commands.context.CommandContext;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.channels.TextChannel;
|
||||
import net.draycia.carbon.api.users.ChatUser;
|
||||
import net.draycia.carbon.api.users.UserService;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.Queue;
|
||||
|
||||
public final class CommandUtils {
|
||||
|
||||
@@ -14,21 +19,24 @@ public final class CommandUtils {
|
||||
}
|
||||
|
||||
// TODO: turn this into a proper class that handles suggestions
|
||||
public static CommandArgument<ChatUser, ChatChannel> channelArgument() {
|
||||
return CommandArgument.<ChatUser, ChatChannel>ofType(ChatChannel.class, "channel")
|
||||
public static CommandArgument<ChatUser, TextChannel> channelArgument() {
|
||||
return CommandArgument.<ChatUser, TextChannel>ofType(TextChannel.class, "channel")
|
||||
.asRequired()
|
||||
.withParser((c, i) -> {
|
||||
final String input = i.peek();
|
||||
final String input = i.poll();
|
||||
|
||||
if (input == null) {
|
||||
// TODO: make exception messages configurable
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Channel cannot be null"));
|
||||
}
|
||||
|
||||
final ChatChannel channel = CarbonChatProvider.carbonChat()
|
||||
.channelRegistry().get(input);
|
||||
|
||||
if (channel != null) {
|
||||
return ArgumentParseResult.success(channel);
|
||||
if (channel instanceof TextChannel) {
|
||||
return ArgumentParseResult.success((TextChannel) channel);
|
||||
} else if (channel != null) {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Channel is not public!"));
|
||||
} else {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Channel does not exist"));
|
||||
}
|
||||
@@ -39,22 +47,7 @@ public final class CommandUtils {
|
||||
public static CommandArgument<ChatUser, ChatUser> optionalChatUserArgument() {
|
||||
return CommandArgument.<ChatUser, ChatUser>ofType(ChatUser.class, "user")
|
||||
.asOptional()
|
||||
.withParser((c, i) -> {
|
||||
final String input = i.peek();
|
||||
|
||||
if (input == null) {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Player cannot be null"));
|
||||
}
|
||||
|
||||
final UserService<?> userService = CarbonChatProvider.carbonChat().userService();
|
||||
final ChatUser user = userService.wrap(userService.resolve(input));
|
||||
|
||||
if (user != null) {
|
||||
return ArgumentParseResult.success(user);
|
||||
} else {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Player does not exist"));
|
||||
}
|
||||
})
|
||||
.withParser(CommandUtils::parse)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -62,23 +55,24 @@ public final class CommandUtils {
|
||||
public static CommandArgument<ChatUser, ChatUser> chatUserArgument() {
|
||||
return CommandArgument.<ChatUser, ChatUser>ofType(ChatUser.class, "user")
|
||||
.asRequired()
|
||||
.withParser((c, i) -> {
|
||||
final String input = i.peek();
|
||||
|
||||
if (input == null) {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Player cannot be null"));
|
||||
}
|
||||
|
||||
final UserService<?> userService = CarbonChatProvider.carbonChat().userService();
|
||||
final ChatUser user = userService.wrap(userService.resolve(input));
|
||||
|
||||
if (user != null) {
|
||||
return ArgumentParseResult.success(user);
|
||||
} else {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Player does not exist"));
|
||||
}
|
||||
})
|
||||
.withParser(CommandUtils::parse)
|
||||
.build();
|
||||
}
|
||||
|
||||
private static @NonNull ArgumentParseResult<ChatUser> parse(final CommandContext<ChatUser> c, final Queue<String> i) {
|
||||
final String input = i.poll();
|
||||
|
||||
if (input == null) {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Player cannot be null"));
|
||||
}
|
||||
|
||||
final UserService<?> userService = CarbonChatProvider.carbonChat().userService();
|
||||
final ChatUser user = userService.wrap(userService.resolve(input));
|
||||
|
||||
if (user != null) {
|
||||
return ArgumentParseResult.success(user);
|
||||
} else {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException("Player does not exist"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user