Allow someone to modify the event before the renderer is set. This allows someone to edit the message before processing

This commit is contained in:
Taah 2023-08-23 19:34:01 -07:00
parent d54325304e
commit d7df923e5c

View File

@ -18,12 +18,12 @@ import org.bukkit.event.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Supplier; import java.util.function.Supplier;
@Toggleable("chat.enabled") @Toggleable("chat.enabled")
public class ChatListener extends PlexListener public class ChatListener extends PlexListener
{ {
public static final TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig public static final TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig
.builder() .builder()
.match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]") .match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
@ -32,6 +32,7 @@ public class ChatListener extends PlexListener
.clickEvent(ClickEvent.openUrl( .clickEvent(ClickEvent.openUrl(
matchResult.group() matchResult.group()
))).build(); ))).build();
public static BiConsumer<AsyncChatEvent, PlexPlayer> PRE_RENDERER = ChatListener::defaultChatProcessing;
private final PlexChatRenderer renderer = new PlexChatRenderer(); private final PlexChatRenderer renderer = new PlexChatRenderer();
@EventHandler @EventHandler
@ -58,6 +59,8 @@ public class ChatListener extends PlexListener
renderer.prefix = null; renderer.prefix = null;
} }
PRE_RENDERER.accept(event, plexPlayer);
event.renderer(renderer); event.renderer(renderer);
} }
@ -70,8 +73,6 @@ public class ChatListener extends PlexListener
@Override @Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer)
{ {
String text = PlexUtils.cleanString(PlexUtils.getTextFromComponent(message));
Component component = Component.empty(); Component component = Component.empty();
if (before != null) if (before != null)
@ -92,8 +93,14 @@ public class ChatListener extends PlexListener
.append(Component.space()) .append(Component.space())
.append(Component.text("»").color(NamedTextColor.GRAY)) .append(Component.text("»").color(NamedTextColor.GRAY))
.append(Component.space()) .append(Component.space())
.append(SafeMiniMessage.mmDeserializeWithoutEvents(text)) .append(message)
.replaceText(URL_REPLACEMENT_CONFIG); .replaceText(URL_REPLACEMENT_CONFIG);
} }
} }
private static void defaultChatProcessing(AsyncChatEvent event, PlexPlayer plexPlayer)
{
String text = PlexUtils.cleanString(PlexUtils.getTextFromComponent(event.message()));
event.message(SafeMiniMessage.mmDeserializeWithoutEvents(text));
}
} }