feat: discord format

This commit is contained in:
DaRacci committed 2022-10-23 21:05:42 +11:00
1 parent cb95886a99
commit dac67eaac3
6 files changed
+112 -1

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());
}
}
@@ -44,6 +44,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,6 +52,7 @@ 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="<<username>> <message>"
}
""")
private Map<String, String> defaults = Map.of(
@@ -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();
}
}
@@ -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,96 @@
*/
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.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, carbonPlayer, renderedMessage.component(), renderedMessage.component());
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