Merge pull request #215 from DaRacci/feat/discord-format

Feat/discord format
This commit is contained in:
Josua Parks authored and GitHub committed 2022-12-08 15:12:28 -08:00
commit 4cad0b47c1
7 files changed
+130 -3

No files matched your search

@@ -24,6 +24,7 @@ import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel;
@@ -140,4 +141,19 @@ public final class BasicChatChannel implements ChatChannel {
return this.key;
}
@Override
public boolean equals(final Object other) {
if (!(other instanceof BasicChatChannel otherChannel)) return false;
if (!(otherChannel.commandName().equals(this.commandName()))) return false;
if (!(Objects.equals(otherChannel.quickPrefix(), this.quickPrefix()))) return false;
if (!(Objects.equals(otherChannel.permission(), this.permission()))) return false;
if (otherChannel.radius() != this.radius()) return false;
return otherChannel.key().equals(this.key());
}
@Override
public int hashCode() {
return Objects.hash(this.commandName(), this.quickPrefix(), this.permission(), this.radius(), this.key());
}
}
@@ -290,4 +290,19 @@ public final class ConfigChatChannel implements ChatChannel {
return this.radius;
}
@Override
public boolean equals(final Object other) {
if (!(other instanceof BasicChatChannel otherChannel)) return false;
if (!(otherChannel.commandName().equals(this.commandName()))) return false;
if (!(Objects.equals(otherChannel.quickPrefix(), this.quickPrefix()))) return false;
if (!(Objects.equals(otherChannel.permission(), this.permission()))) return false;
if (otherChannel.radius() != this.radius()) return false;
return otherChannel.key().equals(this.key());
}
@Override
public int hashCode() {
return Objects.hash(this.commandName(), this.quickPrefix(), this.permission(), this.radius(), this.key());
}
}
@@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Objects;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.util.DiscordRecipient;
import net.kyori.adventure.audience.Audience;
import net.kyori.moonshine.message.IMessageSource;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -44,6 +45,7 @@ public class ConfigChannelMessageSource implements IMessageSource<SourcedAudienc
Basic chat formats.
The "default_format" format is the main one you want to edit.
The "console" format is what's shown to console.
The "discord" format is what's shown to supported discord integrations.
If PlaceholderAPI is installed, PAPI placeholders (with %) are supported.
The keys are group names, the values are chat formats (MiniMessage).
For example:
@@ -51,11 +53,13 @@ public class ConfigChannelMessageSource implements IMessageSource<SourcedAudienc
default_format="<<username>> <message>"
vip="[VIP] <<username>> <message>"
admin="<white>[</white>Prefix<white>]</white> <display_name><white>: <message></white>"
discord="<message>"
}
""")
private Map<String, String> defaults = Map.of(
"default_format", "<display_name>: <message>",
"console", "[<channel>] <username> - <uuid>: <message>"
"console", "[<channel>] <username> - <uuid>: <message>",
"discord", "<message>"
);
// TODO: Move the default to the advanced config?
@@ -70,12 +74,15 @@ public class ConfigChannelMessageSource implements IMessageSource<SourcedAudienc
private static final String FALLBACK_FORMAT = "<red><</red><username><red>></red> <message>";
// TODO: Remove DiscordRecipient and use key instead (Couldn't figure out how to do it)
@Override
public String messageOf(final SourcedAudience sourcedAudience, final String ignored) {
if (sourcedAudience.recipient() instanceof CarbonPlayer) {
return this.forPlayer(sourcedAudience);
} else if (sourcedAudience.recipient() instanceof DiscordRecipient) {
return this.defaults.getOrDefault("discord", FALLBACK_FORMAT);
} else {
return this.forAudience(sourcedAudience.recipient());
return this.defaults.getOrDefault("console", FALLBACK_FORMAT);
}
}
@@ -351,4 +351,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
return this.uuid;
}
@Override
public boolean equals(final @Nullable Object other) {
if (other == null || this.getClass() != other.getClass()) return false;
return this.uuid.equals(((CarbonPlayerCommon) other).uuid);
}
@Override
public int hashCode() {
return this.uuid.hashCode();
}
}
@@ -0,0 +1,9 @@
package net.draycia.carbon.common.util;
import net.kyori.adventure.audience.Audience;
public class DiscordRecipient implements Audience {
public static final DiscordRecipient INSTANCE = new DiscordRecipient();
private DiscordRecipient() {}
}
@@ -195,4 +195,16 @@ public final class CarbonPlayerPaper extends WrappedCarbonPlayer implements Forw
return Bukkit.getPlayer(this.uuid());
}
@Override
public boolean equals(final @Nullable Object other) {
if (other == null || this.getClass() != other.getClass()) return false;
final CarbonPlayerPaper that = (CarbonPlayerPaper) other;
return this.carbonPlayerCommon.equals(that.carbonPlayerCommon);
}
@Override
public int hashCode() {
return this.carbonPlayerCommon.hashCode();
}
}
@@ -19,42 +19,98 @@
*/
package net.draycia.carbon.paper.util;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import github.scarsz.discordsrv.Debug;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.api.Subscribe;
import github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent;
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.minimessage.MiniMessage;
import github.scarsz.discordsrv.hooks.chat.ChatHook;
import github.scarsz.discordsrv.util.PluginUtil;
import java.time.Duration;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.RenderedMessage;
import net.draycia.carbon.common.channels.ConfigChatChannel;
import net.draycia.carbon.common.util.ChannelUtils;
import net.draycia.carbon.common.util.DiscordRecipient;
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.Nullable;
import static github.scarsz.discordsrv.util.MessageUtil.DEFAULT_URL_PATTERN;
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
import static net.kyori.adventure.key.Key.key;
public class DSRVChatHook implements ChatHook {
public DSRVChatHook() {
final Cache<ImmutablePair<CarbonPlayer, ChatChannel>, Component> awaitingEvent = Caffeine.newBuilder()
.expireAfterWrite(Duration.ofMillis(25))
.build();
CarbonChatProvider.carbonChat().eventHandler().subscribe(CarbonChatEvent.class, event -> {
if (event.previewing()) {
final var pair = new ImmutablePair<>(event.sender(), event.chatChannel());
awaitingEvent.put(pair, event.message());
return;
}
final ChatChannel chatChannel = event.chatChannel();
final CarbonPlayer carbonPlayer = event.sender();
final ImmutablePair<CarbonPlayer, ChatChannel> pair = new ImmutablePair<>(carbonPlayer, chatChannel);
final Component messageComponent = awaitingEvent.getIfPresent(pair);
awaitingEvent.invalidate(pair);
if (messageComponent == null) {
return;
}
var renderedMessage = new RenderedMessage(messageComponent, MessageType.CHAT);
renderedMessage = keyedRenderer(key("carbon", "discord"), chatChannel).render(carbonPlayer, DiscordRecipient.INSTANCE, renderedMessage.component(), renderedMessage.component());
// TODO: Should we bother with any of these renders?
for (final var renderer : event.renderers()) {
if (renderer.key().asString().equals("carbon:default")) continue;
renderedMessage = renderer.render(carbonPlayer, carbonPlayer, renderedMessage.component(), renderedMessage.component());
}
final var messageContents = PlainTextComponentSerializer.plainText().serialize(renderedMessage.component());
Component eventMessage = ConfigChatChannel.parseMessageTags(carbonPlayer, messageContents);
if (carbonPlayer.hasPermission("carbon.chatlinks")) {
eventMessage = eventMessage.replaceText(TextReplacementConfig.builder()
.match(DEFAULT_URL_PATTERN)
.replacement(builder -> builder.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, builder.content())))
.build());
}
DiscordSRV.debug(Debug.MINECRAFT_TO_DISCORD, "Received a CarbonChatEvent (player: " + carbonPlayer.username() + ")");
final @Nullable Player player = ((CarbonPlayerPaper) carbonPlayer).bukkitPlayer();
final String message = PlainTextComponentSerializer.plainText().serialize(event.message());
final String message = PlainTextComponentSerializer.plainText().serialize(eventMessage);
if (player != null) {
DiscordSRV.getPlugin().processChatMessage(player, message, chatChannel.commandName(), event.result().cancelled());
}
});
DiscordSRV.api.subscribe(new Object() {
@Subscribe
public void handle(final GameChatMessagePreProcessEvent event) {
if (event.getTriggeringBukkitEvent() == null) return;
event.setCancelled(true);
}
});
}
@Override