2019-11-28 00:14:38 +00:00
|
|
|
/*
|
2020-04-15 19:37:53 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-11-28 00:14:38 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
package org.geysermc.connector.network.translators.world.block.entity;
|
2019-11-28 00:14:38 +00:00
|
|
|
|
2020-06-19 10:57:34 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
|
2019-11-28 00:14:38 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2020-11-05 21:36:22 +00:00
|
|
|
import com.nukkitx.nbt.NbtMapBuilder;
|
2019-11-28 00:14:38 +00:00
|
|
|
import org.geysermc.connector.utils.MessageUtils;
|
2020-09-02 04:38:36 +00:00
|
|
|
import org.geysermc.connector.utils.SignUtils;
|
2019-11-28 00:14:38 +00:00
|
|
|
|
2020-05-06 21:52:57 +00:00
|
|
|
@BlockEntity(name = "Sign", regex = "sign")
|
2019-11-28 00:14:38 +00:00
|
|
|
public class SignBlockEntityTranslator extends BlockEntityTranslator {
|
2020-08-22 20:39:40 +00:00
|
|
|
/**
|
|
|
|
* 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 MessageUtils#getColor(String)}.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
private static String getBedrockSignColor(String javaColor) {
|
|
|
|
String base = "\u00a7";
|
|
|
|
switch (javaColor) {
|
|
|
|
case "white":
|
|
|
|
base += 'f';
|
|
|
|
break;
|
|
|
|
case "orange":
|
|
|
|
base += '6';
|
|
|
|
break;
|
|
|
|
case "magenta":
|
|
|
|
case "purple":
|
|
|
|
base += '5';
|
|
|
|
break;
|
|
|
|
case "light_blue":
|
|
|
|
base += 'b';
|
|
|
|
break;
|
|
|
|
case "yellow":
|
|
|
|
base += 'e';
|
|
|
|
break;
|
|
|
|
case "lime":
|
|
|
|
base += 'a';
|
|
|
|
break;
|
|
|
|
case "pink":
|
|
|
|
base += 'd';
|
|
|
|
break;
|
|
|
|
case "gray":
|
|
|
|
base += '8';
|
|
|
|
break;
|
|
|
|
case "light_gray":
|
|
|
|
base += '7';
|
|
|
|
break;
|
|
|
|
case "cyan":
|
|
|
|
base += '3';
|
|
|
|
break;
|
|
|
|
case "blue":
|
|
|
|
base += '9';
|
|
|
|
break;
|
|
|
|
case "brown": // Brown does not have a bedrock counterpart.
|
|
|
|
case "red": // In Java Edition light red (&c) can only be applied using commands. Red dye gives &4.
|
|
|
|
base += '4';
|
|
|
|
break;
|
|
|
|
case "green":
|
|
|
|
base += '2';
|
|
|
|
break;
|
|
|
|
case "black":
|
|
|
|
base += '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:36:22 +00:00
|
|
|
@Override
|
|
|
|
public void translateTag(NbtMapBuilder builder, CompoundTag tag, int blockState) {
|
|
|
|
StringBuilder signText = new StringBuilder();
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
int currentLine = i + 1;
|
|
|
|
String signLine = getOrDefault(tag.getValue().get("Text" + currentLine), "");
|
|
|
|
signLine = MessageUtils.getBedrockMessage(MessageSerializer.fromString(signLine));
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
for (char c : signLine.toCharArray()) {
|
|
|
|
signWidth += SignUtils.getCharacterWidth(c);
|
|
|
|
if (signWidth <= SignUtils.BEDROCK_CHARACTER_WIDTH_MAX) {
|
|
|
|
finalSignLine.append(c);
|
|
|
|
} else {
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
signText.append(finalSignLine.toString());
|
|
|
|
signText.append("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.put("Text", MessageUtils.getBedrockMessage(MessageSerializer.fromString(signText.toString())));
|
|
|
|
}
|
2019-11-28 00:14:38 +00:00
|
|
|
}
|