Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed May 22, 2024
1 parent 99a17ab commit 86de2d0
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 28 deletions.
2 changes: 0 additions & 2 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ plugins {

dependencies {
implementation(projects.pistonmotdKyori)

implementation("net.kyori:adventure-text-minimessage:4.17.0")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.pistonmaster.pistonmotd.api;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.pistonmaster.pistonmotd.kyori.PistonSerializersRelocated;
import org.apiguardian.api.API;
Expand Down Expand Up @@ -57,7 +56,7 @@ private static Component parseTextToComponent(final String text) {
}

// Initially parse the text via MiniMessage
Component component = MiniMessage.miniMessage().deserialize(parsedText);
Component component = PistonSerializersRelocated.miniMessage.deserialize(parsedText);

// Parse it to an ampersand RGB string
String ampersandRGB = PistonSerializersRelocated.ampersandRGB.serialize(component);
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/pm.kyori-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
implementation("net.kyori:adventure-api:4.17.0")
implementation("net.kyori:adventure-text-serializer-legacy:4.17.0")
implementation("net.kyori:adventure-text-serializer-gson:4.17.0")
implementation("net.kyori:adventure-text-minimessage:4.17.0")
}

tasks.named<ShadowJar>("shadowJar").get().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void addSample(UUID uuid, String name) throws UnsupportedOperationExcepti

@Override
public boolean supportsHex() {
return event.getClient().getProtocolVersion() >= PistonConstants.MINECRAFT_1_16;
return getClientProtocol() == -1 || getClientProtocol() >= PistonConstants.MINECRAFT_1_16;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void addSample(UUID uuid, String name) throws UnsupportedOperationExcepti

@Override
public boolean supportsHex() {
return event.getConnection().getVersion() >= PistonConstants.MINECRAFT_1_16;
return getClientProtocol() == -1 || getClientProtocol() >= PistonConstants.MINECRAFT_1_16;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.pistonmaster.pistonmotd.kyori;

import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

public class PistonSerializersRelocated {
public static final MiniMessage miniMessage = MiniMessage.miniMessage();
public static final LegacyComponentSerializer section = LegacyComponentSerializer.builder().character('§').build();
public static final LegacyComponentSerializer ampersandRGB = LegacyComponentSerializer.builder().character('&').hexCharacter('#').hexColors().build();
public static final GsonComponentSerializer gsonSerializer = GsonComponentSerializer.gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

@RequiredArgsConstructor
public class CenterPlaceholder implements PlaceholderParser {
private static final ThreadLocal<boolean[]> CENTERED_LINES = ThreadLocal.withInitial(() -> new boolean[2]);

private static final int LINE_LENGTH = 45;
private static final String PLACEHOLDER = "%center%";
private static final String SPACE = " ";
Expand All @@ -24,23 +26,23 @@ public class CenterPlaceholder implements PlaceholderParser {

@Override
public String parseString(String text) {
boolean[] centeredLines = CENTERED_LINES.get();
AtomicReference<String> colorCode = new AtomicReference<>();
Set<Character> formatModifiers = new HashSet<>();
String[] lines = text.split("\n");
String[] lines = text.split("\n", 2);
for (int i = 0; i < lines.length; i++) {
lines[i] = centerText(lines[i], colorCode, formatModifiers);
// We need to keep track of colors even if the line is not centered
String parsed = centerText(lines[i], colorCode, formatModifiers);
if (centeredLines[i]) {
lines[i] = parsed;
}
}

CENTERED_LINES.remove();
return String.join("\n", lines);
}

private String centerText(String text, AtomicReference<String> colorCode, Set<Character> formatModifiers) {
if (!text.startsWith(PLACEHOLDER)) {
return text;
}

text = text.substring(PLACEHOLDER.length());

float textWidthCounter = 0;
int textChars = text.length();

Expand All @@ -53,8 +55,8 @@ private String centerText(String text, AtomicReference<String> colorCode, Set<Ch
colorCode.set(null);
formatModifiers.clear();
} else if (next == HEX_CODE) {
colorCode.set(HEX_CODE + text.substring(i + 1, i + 8));
i += 7;
colorCode.set(text.substring(i + 1, i + 8));
i += 6;
} else if (isValidColorChar(next)) {
colorCode.set(String.valueOf(next));
} else {
Expand Down Expand Up @@ -159,4 +161,21 @@ private static int[] findClosestExponents(float x, float y, float z, int range)

return new int[]{bestN, bestM};
}

public static class PreProcessorCenterPlaceholder implements PlaceholderParser {
@Override
public String parseString(String text) {
boolean[] centeredLines = CENTERED_LINES.get();

String[] lines = text.split("\\n|<newline>", 2);
for (int i = 0; i < lines.length; i++) {
if (lines[i].startsWith(PLACEHOLDER)) {
centeredLines[i] = true;
lines[i] = lines[i].substring(PLACEHOLDER.length());
}
}

return String.join("\n", lines);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void loadConfig() {
public void registerCommonPlaceholder() {
platform.startup("Registering placeholders");
PlaceholderUtil.registerParser(new CommonPlaceholder(platform));
PlaceholderUtil.registerParser(new CenterPlaceholder.PreProcessorCenterPlaceholder());
PlaceholderUtil.registerPostParser(new CenterPlaceholder());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ default void handle(PistonStatusPing ping) {

if (config.isExtensionVanishHideCount()) {
try {
ping.setOnline(ping.getOnline() - vanished.size());
ping.setOnline(Math.max(ping.getOnline() - vanished.size(), 0));
} catch (UnsupportedOperationException e) {
logUnsupportedConfig("extensions.vanish.hideCount");
}
Expand Down
1 change: 1 addition & 0 deletions shared/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# %max% (Server max slots)
# %newline% adds a newline to your MOTD.
# %tps% shows the tps (Paper only)
# %center% centers the text of a line. Needs to be at the exact start of a line.
description:
activated: true
text:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.net.InetSocketAddress;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.UUID;

@Getter
Expand Down Expand Up @@ -98,13 +97,7 @@ public void addSample(UUID uuid, String name) throws UnsupportedOperationExcepti

@Override
public boolean supportsHex() {
OptionalInt version = event.client().version().dataVersion();

if (version.isPresent()) {
return version.getAsInt() >= PistonConstants.MINECRAFT_1_16;
} else {
return false;
}
return getClientProtocol() == -1 || getClientProtocol() >= PistonConstants.MINECRAFT_1_16;
}

@Override
Expand All @@ -114,7 +107,7 @@ public void setFavicon(StatusFavicon favicon) {

@Override
public int getClientProtocol() throws UnsupportedOperationException {
return event.client().version().dataVersion().orElse(0);
return event.client().version().dataVersion().orElse(-1);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ java {

tasks {
runVelocity {
version("3.2.0-SNAPSHOT")
version("3.1.1")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void addSample(UUID uuid, String name) throws UnsupportedOperationExcepti

@Override
public boolean supportsHex() {
return getClientProtocol() >= PistonConstants.MINECRAFT_1_16;
return getClientProtocol() == -1 || getClientProtocol() >= PistonConstants.MINECRAFT_1_16;
}

@Override
Expand Down

0 comments on commit 86de2d0

Please sign in to comment.