Skip to content

Commit

Permalink
Make chat message deduplication local to instances
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Jan 30, 2025
1 parent bfeb8a8 commit 30bd197
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.soulfiremc.server.InstanceManager;
import com.soulfiremc.server.api.InternalPlugin;
import com.soulfiremc.server.api.PluginInfo;
import com.soulfiremc.server.api.event.bot.ChatMessageReceiveEvent;
import com.soulfiremc.server.api.event.lifecycle.InstanceSettingsRegistryInitEvent;
import com.soulfiremc.server.settings.lib.InstanceSettingsSource;
import com.soulfiremc.server.api.metadata.MetadataKey;
import com.soulfiremc.server.settings.lib.SettingsObject;
import com.soulfiremc.server.settings.property.BooleanProperty;
import com.soulfiremc.server.settings.property.ImmutableBooleanProperty;
Expand All @@ -38,14 +39,13 @@
import net.lenni0451.lambdaevents.EventHandler;
import org.pf4j.Extension;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Extension
public class ChatMessageLogger extends InternalPlugin {
private static final Cache<String, Integer> CHAT_MESSAGES = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS)
.build();
private static final MetadataKey<Cache<String, Integer>> CHAT_MESSAGES = MetadataKey.of("chat_message_logger", "chat_messages", Cache.class);

public ChatMessageLogger() {
super(new PluginInfo(
Expand All @@ -67,14 +67,17 @@ public static void onMessage(ChatMessageReceiveEvent event) {

// usage of synchronized method so that the chatMessages set is not modified while being
// iterated
logChatMessage(settingsSource, event.message());
logChatMessage(event.connection().instanceManager(), event.message());
}

private static synchronized void logChatMessage(InstanceSettingsSource settingsSource, Component message) {
private static synchronized void logChatMessage(InstanceManager instanceManager, Component message) {
var chatMessage = instanceManager.metadata().getOrSet(CHAT_MESSAGES, () -> Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS)
.build());
var ansiMessage = SoulFireAdventure.TRUE_COLOR_ANSI_SERIALIZER.serialize(message);

var deduplicateAmount = settingsSource.get(ChatMessageSettings.DEDUPLICATE_AMOUNT);
var messageCount = CHAT_MESSAGES.get(ansiMessage, (key) -> 0);
var deduplicateAmount = instanceManager.settingsSource().get(ChatMessageSettings.DEDUPLICATE_AMOUNT);
int messageCount = Objects.requireNonNull(chatMessage.get(ansiMessage, (key) -> 0));
if (messageCount < deduplicateAmount) {
// Print to remote console (always true color)
log.atInfo()
Expand All @@ -84,7 +87,7 @@ private static synchronized void logChatMessage(InstanceSettingsSource settingsS
log.atInfo()
.addKeyValue(SFLogAppender.SF_SKIP_PUBLISHING, "true")
.log("{}", SoulFireAdventure.ANSI_SERIALIZER.serialize(message));
CHAT_MESSAGES.put(ansiMessage, messageCount + 1);
chatMessage.put(ansiMessage, messageCount + 1);
}
}

Expand Down Expand Up @@ -116,6 +119,4 @@ private static class ChatMessageSettings implements SettingsObject {
.stepValue(1)
.build();
}

private record ChatMessageState(Cache<String, Integer> chatMessages) {}
}

0 comments on commit 30bd197

Please sign in to comment.