Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sign logging rules #83

Merged
merged 1 commit into from
Jan 5, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import dev.pgm.community.utils.Sounds;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
Expand Down Expand Up @@ -417,26 +416,62 @@ private void broadcastPunishment(
});
}

private boolean hasText(String string) {
for (int i = 0; i < string.length(); i++) {
if (Character.isLetterOrDigit(string.charAt(i))) return true;
}
return false;
}

private boolean isAllText(String string) {
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (!(Character.isLetterOrDigit(ch) || ch == ' ')) return false;
}
return true;
}

private void compactDuplicates(StringBuilder builder, String base) {
if (base.isEmpty()) return;
char last = base.charAt(0);
builder.append(last);
for (int i = 1; i < base.length(); i++) {
char next = base.charAt(i);
if (last == next) continue;
builder.append(next);
last = next;
}
}

private void logSign(Player player, String[] lines, Location location) {
if (!getModerationConfig().isSignLoggerEnabled()) return;

int totalChars =
Arrays.stream(lines).mapToInt(line -> line.replace(" ", "").length()).sum();
StringBuilder compactBuilder = new StringBuilder(), fullBuilder = new StringBuilder();
for (String line : lines) {
if ((line = line.trim()).isEmpty()) continue;
fullBuilder.append(line).append('\n');

if (totalChars < 4) return; // Don't track signs with barely any text
if (hasText(line)) compactBuilder.append(line);
else compactDuplicates(compactBuilder, line);
compactBuilder.append(' ');
}
if (!fullBuilder.isEmpty()) {
compactBuilder.deleteCharAt(compactBuilder.length() - 1);
fullBuilder.deleteCharAt(fullBuilder.length() - 1);
}

String oneLineSign = compactBuilder.toString();
if (oneLineSign.length() < 4 && isAllText(oneLineSign))
return; // Don't track signs with barely any text

String locString =
String.format("%d %d %d", location.getBlockX(), location.getBlockY(), location.getBlockZ());

String oneLineSign = Arrays.stream(lines)
.map(line -> line.trim())
.filter(line -> !line.isBlank() && !line.isEmpty())
.collect(Collectors.joining(" "));

Component alert = text()
.append(player(player, NameStyle.FANCY))
.append(text(" placed a sign: \"", NamedTextColor.GRAY))
.append(text(oneLineSign, NamedTextColor.YELLOW))
.append(text(oneLineSign, NamedTextColor.YELLOW)
.hoverEvent(HoverEvent.showText(text(fullBuilder.toString(), NamedTextColor.YELLOW))))
.append(text("\"", NamedTextColor.GRAY))
.clickEvent(ClickEvent.runCommand("/tploc " + locString))
.hoverEvent(HoverEvent.showText(text("Click to teleport to sign", NamedTextColor.GRAY)))
Expand Down
Loading