Fix sign color changes messing with character count
Color changes do not affect the maximum amount of characters allowed on a line.
package org.geysermc.connector.network.translators.world.block.entity;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
+import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.nukkitx.nbt.NbtMapBuilder;
import org.geysermc.connector.network.translators.chat.MessageTranslator;
import org.geysermc.connector.utils.SignUtils;
@@ -35,7 +36,7 @@ public class SignBlockEntityTranslator extends BlockEntityTranslator {
/**
* Maps a color stored in a sign's Color tag to a Bedrock Edition formatting code.
* <br>
- * The color names correspond to dye names, because of this we can't use {@link MessageTranslator#getColor(String)}.
+ * The color names correspond to dye names, because of this we can't use a more global method.
*
* @param javaColor The dye color stored in the sign's Color tag.
* @return A Bedrock Edition formatting code for valid dye colors, otherwise an empty string.
@@ -101,27 +102,34 @@ public class SignBlockEntityTranslator extends BlockEntityTranslator {
String signLine = getOrDefault(tag.getValue().get("Text" + currentLine), "");
signLine = MessageTranslator.convertMessageLenient(signLine);
- // Trim any trailing formatting codes
- if (signLine.length() > 2 && signLine.toCharArray()[signLine.length() - 2] == '\u00a7') {
- signLine = signLine.substring(0, signLine.length() - 2);
- }
-
// Check the character width on the sign to ensure there is no overflow that is usually hidden
// to Java Edition clients but will appear to Bedrock clients
int signWidth = 0;
StringBuilder finalSignLine = new StringBuilder();
+ boolean previousCharacterWasFormatting = false; // Color changes do not count for maximum width
for (char c : signLine.toCharArray()) {
+ if (c == '\u00a7') {
+ // Don't count this character
+ previousCharacterWasFormatting = true;
+ } else if (previousCharacterWasFormatting) {
+ // Don't count this character either
+ previousCharacterWasFormatting = false;
+ } else {
signWidth += SignUtils.getCharacterWidth(c);
+ }
+
if (signWidth <= SignUtils.BEDROCK_CHARACTER_WIDTH_MAX) {
finalSignLine.append(c);
} else {
+ // Adding the character would make Bedrock move to the next line - Java doesn't do that, so we do not want to
break;
}
}
// Java Edition 1.14 added the ability to change the text color of the whole sign using dye
- if (tag.contains("Color")) {
- signText.append(getBedrockSignColor(tag.get("Color").getValue().toString()));
+ Tag color = tag.get("Color");
+ if (color != null) {
+ signText.append(getBedrockSignColor(color.getValue().toString()));
}
signText.append(finalSignLine.toString());
