mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
perf: Cache URL_REPLACEMENT_CONFIG creation
And move it to the common module
This commit is contained in:
4 files changed
+19
-25
No files matched your search
@@ -19,6 +19,11 @@
|
||||
*/
|
||||
package net.draycia.carbon.common.util;
|
||||
|
||||
import com.google.common.base.Suppliers;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -26,6 +31,14 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class Strings {
|
||||
|
||||
private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
|
||||
public static final Supplier<TextReplacementConfig> URL_REPLACEMENT_CONFIG = Suppliers.memoize(
|
||||
() -> TextReplacementConfig.builder()
|
||||
.match(DEFAULT_URL_PATTERN)
|
||||
.replacement(builder -> builder.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, builder.content())))
|
||||
.build()
|
||||
);
|
||||
|
||||
private Strings() {
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
@@ -33,13 +32,13 @@ import net.draycia.carbon.fabric.callback.ChatCallback;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static java.util.Objects.requireNonNullElse;
|
||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||
import static net.draycia.carbon.common.util.Strings.URL_REPLACEMENT_CONFIG;
|
||||
import static net.kyori.adventure.key.Key.key;
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
@@ -50,8 +49,6 @@ public class FabricChatListener implements Consumer<ChatCallback.Chat> {
|
||||
private final CarbonChatFabric carbonChatFabric;
|
||||
private final ChannelRegistry channelRegistry;
|
||||
|
||||
private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
|
||||
|
||||
public FabricChatListener(final CarbonChatFabric carbonChatFabric, final ChannelRegistry channelRegistry) {
|
||||
this.carbonChatFabric = carbonChatFabric;
|
||||
this.channelRegistry = channelRegistry;
|
||||
@@ -66,10 +63,7 @@ public class FabricChatListener implements Consumer<ChatCallback.Chat> {
|
||||
Component eventMessage = text(chat.message());
|
||||
|
||||
if (sender.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());
|
||||
eventMessage = eventMessage.replaceText(URL_REPLACEMENT_CONFIG.get());
|
||||
}
|
||||
|
||||
for (final var chatChannel : this.channelRegistry) {
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.google.inject.Inject;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
@@ -37,7 +36,6 @@ import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
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 net.kyori.event.EventSubscriber;
|
||||
import ninja.egg82.messenger.services.PacketService;
|
||||
@@ -50,6 +48,7 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static java.util.Objects.requireNonNullElse;
|
||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||
import static net.draycia.carbon.common.util.Strings.URL_REPLACEMENT_CONFIG;
|
||||
import static net.kyori.adventure.key.Key.key;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
@@ -60,8 +59,6 @@ public final class PaperChatListener implements Listener {
|
||||
private final ChannelRegistry registry;
|
||||
private final CarbonMessages carbonMessages;
|
||||
|
||||
private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
|
||||
|
||||
@Inject
|
||||
public PaperChatListener(final CarbonChat carbonChat, final ChannelRegistry registry, final CarbonMessages carbonMessages) {
|
||||
this.carbonChat = (CarbonChatPaper) carbonChat;
|
||||
@@ -82,10 +79,7 @@ public final class PaperChatListener implements Listener {
|
||||
Component eventMessage = ConfigChatChannel.parseMessageTags(sender, messageContents);
|
||||
|
||||
if (sender.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());
|
||||
eventMessage = eventMessage.replaceText(URL_REPLACEMENT_CONFIG.get());
|
||||
}
|
||||
|
||||
for (final var chatChannel : this.registry) {
|
||||
|
||||
+2
-9
@@ -23,7 +23,6 @@ import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.regex.Pattern;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
@@ -33,12 +32,11 @@ import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.velocity.CarbonChatVelocity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static java.util.Objects.requireNonNullElse;
|
||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||
import static net.draycia.carbon.common.util.Strings.URL_REPLACEMENT_CONFIG;
|
||||
import static net.kyori.adventure.key.Key.key;
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
@@ -50,8 +48,6 @@ public final class VelocityChatListener {
|
||||
private final ChannelRegistry registry;
|
||||
private final UserManager<?> userManager;
|
||||
|
||||
private static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
|
||||
|
||||
@Inject
|
||||
private VelocityChatListener(final CarbonChat carbonChat, final ChannelRegistry registry, final UserManager<?> userManager) {
|
||||
this.carbonChat = (CarbonChatVelocity) carbonChat;
|
||||
@@ -73,10 +69,7 @@ public final class VelocityChatListener {
|
||||
Component eventMessage = text(originalMessage);
|
||||
|
||||
if (sender.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());
|
||||
eventMessage = eventMessage.replaceText(URL_REPLACEMENT_CONFIG.get());
|
||||
}
|
||||
|
||||
for (final var chatChannel : this.registry) {
|
||||
|
||||
Reference in new issue
Block a user