Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You can find the documentation for FairyChat [here](https://github.com/rexlManu/
| `/fairychat` | | Show information about FairyChat |
| `/fairychat reload` | `fairychat.command.fairychat.reload` | Reload the configuration |
| | `fairychat.bypass.similarity` | Bypass the similarity check for last message |
| | `fairychat.bypass.capitals` | Bypass the capital letters check |
| | `fairychat.bypass.cooldown` | Bypass the chat cooldown |
| | `fairychat.feature.displayitem` | Allow players to show the item in and with <item> |
| | `fairychat.messages.join.ignore` | The join message will not be sent |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public class PluginConfiguration {
@Comment("How similar the messages have to be to be considered a duplicate.")
private double similarityPercentage = 0.75;

@Comment("Prevent excessive capital letters being used.")
private boolean preventExcessiveCapitals = false;

@Comment("How much of the message has to be in capital letters to be considered excessive.")
private double capitalPercentage = 0.75;

@Comment({
"The similarity algorithm that should be used.",
"Supported algorithms:",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package de.rexlmanu.fairychat.plugin.core.capitals;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import de.rexlmanu.fairychat.plugin.configuration.PluginConfiguration;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;

@Singleton
@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class CapitalsService {
private final Provider<PluginConfiguration> configurationProvider;
private final MiniMessage miniMessage;

public Component checkCapitals(Component message) {

if (!this.configurationProvider.get().preventExcessiveCapitals()) {
return message;
}
String miniTextMessage = this.miniMessage.serialize(message);
int count = 0;
for (int i = 0; i < miniTextMessage.length(); i++) {
char c = miniTextMessage.charAt(i);
if (c >= 'A' && c <= 'Z')
count++;
}
double percentageCapitals = (double) count / miniTextMessage.length();

if (percentageCapitals < this.configurationProvider.get().capitalPercentage()) {
return message;
}

StringBuilder sb = new StringBuilder(miniTextMessage.length());
for (int i = 0; i < miniTextMessage.length(); i++) {
char c = miniTextMessage.charAt(i);
if (c >= 'A' && c <= 'Z') {
sb.append((char)(c + 32));
} else {
sb.append(c);
}
}

return this.miniMessage.deserialize(sb.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import de.rexlmanu.fairychat.plugin.configuration.PluginConfiguration;
import de.rexlmanu.fairychat.plugin.core.capitals.CapitalsService;
import de.rexlmanu.fairychat.plugin.core.ignore.UserIgnoreService;
import de.rexlmanu.fairychat.plugin.core.mentions.MentionService;
import de.rexlmanu.fairychat.plugin.integration.IntegrationRegistry;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class PlayerChatFormatRenderer implements DefaultChatRenderer {
private final MiniMessage miniMessage;
private final PermissionProvider permissionProvider;
private final MentionService mentionService;
private final CapitalsService capitalsService;
private final IntegrationRegistry registry;
private final UserIgnoreService userIgnoreService;

Expand Down Expand Up @@ -69,7 +71,9 @@ public Component formatMessage(@NotNull Player source, @NotNull Component messag
if (this.configurationProvider.get().legacyColorSupport()) {
serializer = LegacyComponentSerializer.legacyAmpersand()::serialize;
}
final String textMessage = this.resolveMessageModifiers(source, serializer.apply(message));
final String textMessage = this.resolveMessageModifiers(source, serializer.apply(
source.hasPermission("fairychat.bypass.capitals") ? message : this.capitalsService.checkCapitals(message))
);

// Check if the player has the permission to use mini message
if (source.hasPermission("fairychat.feature.minimessage")) {
Expand Down