Skip to content

Commit

Permalink
Fix sign color changes messing with character count (GeyserMC#2054)
Browse files Browse the repository at this point in the history
Color changes do not affect the maximum amount of characters allowed on a line.
  • Loading branch information
Camotoy authored Mar 21, 2021
1 parent 930c4c5 commit 10c77a3
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -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.
Expand Down Expand Up @@ -101,27 +102,34 @@ public void translateTag(NbtMapBuilder builder, CompoundTag tag, int blockState)
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()) {
signWidth += SignUtils.getCharacterWidth(c);
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());
Expand Down

0 comments on commit 10c77a3

Please sign in to comment.