Allow console to use chat with 'console_sender' channel format

This commit is contained in:
Draycia committed 2023-08-02 18:02:53 -07:00
1 parent 19d17ce004
commit 2d89dc68c4
7 files changed
+290 -11

No files matched your search

@@ -38,6 +38,8 @@ import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.event.CarbonEventHandler;
@@ -53,6 +55,7 @@ import net.draycia.carbon.common.event.events.CarbonReloadEvent;
import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl;
import net.draycia.carbon.common.listeners.ChatListenerInternal;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.common.util.Exceptions;
import net.draycia.carbon.common.util.FileUtil;
import net.kyori.adventure.audience.Audience;
@@ -280,10 +283,31 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
return null;
}
private void sendMessageInChannelAsConsole(
final Audience sender,
final ChatChannel channel,
final String plainMessage
) {
final List<Audience> recipients = CarbonChatProvider.carbonChat().server().players()
.stream().filter(player -> channel.hearingPermitted(player).permitted())
.collect(Collectors.toList());
this.sendMessageInChannel(new ConsoleCarbonPlayer(sender), channel, recipients, plainMessage);
}
private void sendMessageInChannelAsPlayer(
final CarbonPlayer sender,
final ChatChannel channel,
final String plainMessage
) {
this.sendMessageInChannel(sender, channel, channel.recipients(sender), plainMessage);
}
private void sendMessageInChannel(
final CarbonPlayer sender,
final ChatChannel channel,
final List<Audience> recipients,
final String plainMessage
) {
final CarbonChatEvent chatEvent = this.prepareAndEmitChatEvent(sender, plainMessage, null, channel);
@@ -291,7 +315,7 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
return;
}
for (final Audience recipient : chatEvent.recipients()) {
for (final Audience recipient : recipients) {
Component renderedMessage = chatEvent.message();
for (final KeyedRenderer renderer : chatEvent.renderers()) {
@@ -326,27 +350,40 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
final Key channelKey = channel.key();
final Command<Commander> command = builder.senderType(PlayerCommander.class)
final Command<Commander> command = builder.senderType(Commander.class)
.handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
final Commander commander = handler.getSender();
final @Nullable ChatChannel chatChannel = this.channel(channelKey);
if (sender.muted()) {
this.carbonMessages.muteCannotSpeak(sender);
if (!(commander instanceof PlayerCommander playerCommander)) {
if (chatChannel != null && handler.contains("message")) {
final String message = handler.get("message");
// TODO: trigger platform events related to chat
this.sendMessageInChannelAsConsole(commander, chatChannel, message);
}
return;
}
if (sender.leftChannels().contains(channelKey) && chatChannel != null) {
sender.joinChannel(chatChannel);
this.carbonMessages.channelJoined(sender);
final var player = playerCommander.carbonPlayer();
if (player.muted()) {
this.carbonMessages.muteCannotSpeak(player);
return;
}
if (player.leftChannels().contains(channelKey) && chatChannel != null) {
player.joinChannel(chatChannel);
this.carbonMessages.channelJoined(player);
}
if (handler.contains("message")) {
final String message = handler.get("message");
// TODO: trigger platform events related to chat
this.sendMessageInChannelAsPlayer(sender, chatChannel, message);
this.sendMessageInChannelAsPlayer(player, chatChannel, message);
} else {
sender.selectedChannel(chatChannel);
this.carbonMessages.changedChannels(sender, channelKey.value());
player.selectedChannel(chatChannel);
this.carbonMessages.changedChannels(player, channelKey.value());
}
})
.build();
@@ -28,6 +28,7 @@ import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -45,6 +46,9 @@ public class MessagePacketHandler implements Listener {
if (!(event instanceof CarbonChatEventImpl e) || !e.origin) {
return;
}
if (event.sender() instanceof ConsoleCarbonPlayer) {
return;
}
messaging.get().withPacketService(packetService -> {
final CarbonPlayer sender = event.sender();
@@ -0,0 +1,218 @@
package net.draycia.carbon.common.users;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;
@DefaultQualifier(NonNull.class)
public class ConsoleCarbonPlayer implements CarbonPlayer, ForwardingAudience.Single {
private final Audience audience;
public ConsoleCarbonPlayer(final Audience audience) {
this.audience = audience;
}
@Override
public @NotNull Audience audience() {
return this.audience;
}
@Override
public double distanceSquaredFrom(final CarbonPlayer other) {
return 0;
}
@Override
public boolean sameWorldAs(final CarbonPlayer other) {
return true;
}
@Override
public String username() {
return "Console";
}
@Override
public boolean hasCustomDisplayName() {
return false;
}
@Override
public @Nullable Component displayName() {
return null;
}
@Override
public void displayName(final @Nullable Component displayName) {
}
@Override
public UUID uuid() {
return new UUID(0, 0);
}
@Override
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
return null;
}
@Override
public @Nullable Locale locale() {
return null;
}
@Override
public @Nullable ChatChannel selectedChannel() {
return null;
}
@Override
public void selectedChannel(final @Nullable ChatChannel chatChannel) {
}
@Override
public boolean hasPermission(final String permission) {
return true;
}
@Override
public String primaryGroup() {
return "console_sender";
}
@Override
public List<String> groups() {
return List.of("console_sender");
}
@Override
public boolean muted() {
return false;
}
@Override
public void muted(final boolean muted) {
}
@Override
public boolean ignoring(final UUID player) {
return false;
}
@Override
public boolean ignoring(final CarbonPlayer player) {
return false;
}
@Override
public void ignoring(final UUID player, final boolean nowIgnoring) {
}
@Override
public void ignoring(final CarbonPlayer player, final boolean nowIgnoring) {
}
@Override
public boolean deafened() {
return false;
}
@Override
public void deafened(final boolean deafened) {
}
@Override
public boolean spying() {
return true;
}
@Override
public void spying(final boolean spying) {
}
@Override
public void sendMessageAsPlayer(final String message) {
}
@Override
public boolean online() {
return true;
}
@Override
public @Nullable UUID whisperReplyTarget() {
return null;
}
@Override
public void whisperReplyTarget(final @Nullable UUID uuid) {
}
@Override
public @Nullable UUID lastWhisperTarget() {
return null;
}
@Override
public void lastWhisperTarget(final @Nullable UUID uuid) {
}
@Override
public boolean vanished() {
return false;
}
@Override
public boolean awareOf(final CarbonPlayer other) {
return true;
}
@Override
public List<Key> leftChannels() {
return List.of();
}
@Override
public void joinChannel(final ChatChannel channel) {
}
@Override
public void leaveChannel(final ChatChannel channel) {
}
@Override
public @NotNull Identity identity() {
return Identity.nil();
}
@Override
public ChannelMessage channelForMessage(final Component message) {
return new ChannelMessage(message, null);
}
}
@@ -26,6 +26,7 @@ import java.lang.reflect.Type;
import java.util.Map;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.fabric.users.CarbonPlayerFabric;
import net.fabricmc.loader.api.FabricLoader;
import net.kyori.adventure.audience.Audience;
@@ -72,6 +73,9 @@ public class FabricMessageRenderer<T extends Audience> implements IMessageRender
if (sourced.recipient() instanceof CarbonPlayerFabric recipient && recipient.online()) {
tagResolver.resolver(MiniPlaceholders.getRelationalPlaceholders(sender, recipient));
}
} else if (sourced.sender() instanceof ConsoleCarbonPlayer console) {
// I don't know if this will ever actually resolve anything, or if anything supports console audience
tagResolver.resolver(MiniPlaceholders.getAudiencePlaceholders(console));
}
}
}
@@ -34,6 +34,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.users.ConsoleCarbonPlayer;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.draycia.carbon.common.util.ChannelUtils;
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
@@ -69,6 +70,11 @@ public final class DSRVChatHook implements ChatHook {
events.subscribe(CarbonChatEvent.class, event -> {
final ChatChannel chatChannel = event.chatChannel();
final CarbonPlayer carbonPlayer = event.sender();
if (carbonPlayer instanceof ConsoleCarbonPlayer) {
return;
}
final ImmutablePair<CarbonPlayer, ChatChannel> pair = new ImmutablePair<>(carbonPlayer, chatChannel);
Component messageComponent = awaitingEvent.getIfPresent(pair);
awaitingEvent.invalidate(pair);
@@ -29,6 +29,7 @@ import java.util.function.Supplier;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.paper.CarbonChatPaper;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
@@ -92,6 +93,11 @@ public class PaperMessageRenderer<T extends Audience> implements IMessageRendere
return this.miniMessage.deserialize(placeholderResolvedMessage, tagResolver.build());
}
// We can't/shouldn't resolve placeholders for non-players
if (sender instanceof ConsoleCarbonPlayer) {
return this.miniMessage.deserialize(placeholderResolvedMessage, tagResolver.build());
}
final Player senderBukkitPlayer = requireNonNull(Bukkit.getPlayer(sender.uuid()));
if (CarbonChatPaper.miniPlaceholdersLoaded()) {
@@ -27,6 +27,7 @@ import java.lang.reflect.Type;
import java.util.Map;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.draycia.carbon.velocity.users.CarbonPlayerVelocity;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
@@ -74,6 +75,9 @@ public class VelocityMessageRenderer<T extends Audience> implements IMessageRend
if (sourced.recipient() instanceof CarbonPlayerVelocity recipient) {
tagResolver.resolver(MiniPlaceholders.getRelationalGlobalPlaceholders(sender, recipient));
}
} else if (sourced.sender() instanceof ConsoleCarbonPlayer console) {
// I don't know if this will ever actually resolve anything, or if anything supports console audience
tagResolver.resolver(MiniPlaceholders.getAudiencePlaceholders(console));
}
}
}