2020-04-08 21:42:36 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-04-08 21:42:36 +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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.protocol.bedrock;
|
2020-04-08 21:42:36 +00:00
|
|
|
|
2021-11-14 05:07:24 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetJigsawBlockPacket;
|
2021-11-13 03:44:15 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundSignUpdatePacket;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.math.vector.Vector3i;
|
2022-12-21 00:47:45 +00:00
|
|
|
import org.cloudburstmc.nbt.NbtMap;
|
2022-10-30 00:23:21 +00:00
|
|
|
import org.cloudburstmc.protocol.bedrock.packet.BlockEntityDataPacket;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.translator.protocol.PacketTranslator;
|
|
|
|
import org.geysermc.geyser.translator.protocol.Translator;
|
2023-04-30 01:41:14 +00:00
|
|
|
import org.geysermc.geyser.translator.text.MessageTranslator;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.util.SignUtils;
|
2020-04-08 21:44:34 +00:00
|
|
|
|
2020-04-08 21:42:36 +00:00
|
|
|
@Translator(packet = BlockEntityDataPacket.class)
|
|
|
|
public class BedrockBlockEntityDataTranslator extends PacketTranslator<BlockEntityDataPacket> {
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void translate(GeyserSession session, BlockEntityDataPacket packet) {
|
2020-07-05 20:58:43 +00:00
|
|
|
NbtMap tag = packet.getData();
|
2022-01-20 00:30:54 +00:00
|
|
|
String id = tag.getString("id");
|
2023-06-22 22:05:07 +00:00
|
|
|
if (id.endsWith("Sign")) {
|
2023-05-18 04:58:27 +00:00
|
|
|
// Hanging signs are narrower
|
2023-06-22 22:05:07 +00:00
|
|
|
int widthMax = SignUtils.getSignWidthMax(id.startsWith("Hanging"));
|
2023-05-18 04:58:27 +00:00
|
|
|
|
2023-05-11 16:38:49 +00:00
|
|
|
String text = MessageTranslator.convertToPlainText(
|
2023-05-18 00:11:14 +00:00
|
|
|
tag.getCompound(session.getWorldCache().isEditingSignOnFront() ? "FrontText" : "BackText").getString("Text"));
|
2022-05-30 00:31:54 +00:00
|
|
|
// Note: as of 1.18.30, only one packet is sent from Bedrock when the sign is finished.
|
|
|
|
// Previous versions did not have this behavior.
|
2020-07-05 20:58:43 +00:00
|
|
|
StringBuilder newMessage = new StringBuilder();
|
|
|
|
// While Bedrock's sign lines are one string, Java's is an array of each line
|
|
|
|
// (Initialized all with empty strings because it complains about null)
|
|
|
|
String[] lines = new String[] {"", "", "", ""};
|
|
|
|
int iterator = 0;
|
2020-09-02 04:38:36 +00:00
|
|
|
// Keep track of the width of each character
|
|
|
|
// If it goes over the maximum, we need to start a new line to match Java
|
|
|
|
int widthCount = 0;
|
2020-07-05 20:58:43 +00:00
|
|
|
// This converts the message into the array'd message Java wants
|
2022-01-20 00:30:54 +00:00
|
|
|
for (char character : text.toCharArray()) {
|
2020-09-02 04:38:36 +00:00
|
|
|
widthCount += SignUtils.getCharacterWidth(character);
|
2022-12-04 23:12:07 +00:00
|
|
|
|
2020-09-02 04:38:36 +00:00
|
|
|
// If we get a return in Bedrock, or go over the character width max, that signals to use the next line.
|
2023-05-18 04:58:27 +00:00
|
|
|
if (character == '\n' || widthCount > widthMax) {
|
2020-09-02 04:38:36 +00:00
|
|
|
// We need to apply some more logic if we went over the character width max
|
2023-05-18 04:58:27 +00:00
|
|
|
boolean wentOverMax = widthCount > widthMax && character != '\n';
|
2020-09-02 04:38:36 +00:00
|
|
|
widthCount = 0;
|
|
|
|
// Saves if we're moving a word to the next line
|
|
|
|
String word = null;
|
|
|
|
if (wentOverMax && iterator < lines.length - 1) {
|
|
|
|
// If we went over the max, we want to try to wrap properly like Bedrock does.
|
|
|
|
// So we look for a space in the Bedrock user's text to imply a word.
|
|
|
|
int index = newMessage.lastIndexOf(" ");
|
|
|
|
if (index != -1) {
|
|
|
|
// There is indeed a space in this line; let's get it
|
|
|
|
word = newMessage.substring(index + 1);
|
|
|
|
// 'Delete' that word from the string builder
|
|
|
|
newMessage.delete(index, newMessage.length());
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 20:58:43 +00:00
|
|
|
lines[iterator] = newMessage.toString();
|
|
|
|
iterator++;
|
2020-09-02 04:38:36 +00:00
|
|
|
// Bedrock, for whatever reason, can hold a message out of the bounds of the four lines
|
2020-07-05 20:58:43 +00:00
|
|
|
// We don't care about that so we discard that
|
|
|
|
if (iterator > lines.length - 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newMessage = new StringBuilder();
|
2020-09-02 04:38:36 +00:00
|
|
|
if (wentOverMax) {
|
|
|
|
// Apply the wrapped word to the new line
|
|
|
|
if (word != null) {
|
|
|
|
newMessage.append(word);
|
|
|
|
// And apply the width count
|
|
|
|
for (char wordCharacter : word.toCharArray()) {
|
|
|
|
widthCount += SignUtils.getCharacterWidth(wordCharacter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we went over the max, we want to append the character to the new line.
|
|
|
|
newMessage.append(character);
|
|
|
|
widthCount += SignUtils.getCharacterWidth(character);
|
|
|
|
}
|
2020-07-05 20:58:43 +00:00
|
|
|
} else newMessage.append(character);
|
2020-04-08 21:42:36 +00:00
|
|
|
}
|
2020-07-05 20:58:43 +00:00
|
|
|
// Put the final line on since it isn't done in the for loop
|
|
|
|
if (iterator < lines.length) lines[iterator] = newMessage.toString();
|
2022-05-25 23:17:49 +00:00
|
|
|
Vector3i pos = Vector3i.from(tag.getInt("x"), tag.getInt("y"), tag.getInt("z"));
|
2023-05-18 00:11:14 +00:00
|
|
|
ServerboundSignUpdatePacket signUpdatePacket = new ServerboundSignUpdatePacket(pos, lines, session.getWorldCache().isEditingSignOnFront());
|
2021-11-13 03:44:15 +00:00
|
|
|
session.sendDownstreamPacket(signUpdatePacket);
|
2020-07-05 20:58:43 +00:00
|
|
|
|
2022-01-20 00:30:54 +00:00
|
|
|
} else if (id.equals("JigsawBlock")) {
|
2020-09-15 00:54:19 +00:00
|
|
|
// Client has just sent a jigsaw block update
|
2022-05-25 23:17:49 +00:00
|
|
|
Vector3i pos = Vector3i.from(tag.getInt("x"), tag.getInt("y"), tag.getInt("z"));
|
2020-09-15 00:54:19 +00:00
|
|
|
String name = tag.getString("name");
|
|
|
|
String target = tag.getString("target");
|
|
|
|
String pool = tag.getString("target_pool");
|
|
|
|
String finalState = tag.getString("final_state");
|
|
|
|
String joint = tag.getString("joint");
|
2021-11-13 03:44:15 +00:00
|
|
|
ServerboundSetJigsawBlockPacket jigsawPacket = new ServerboundSetJigsawBlockPacket(pos, name, target, pool,
|
2020-09-15 00:54:19 +00:00
|
|
|
finalState, joint);
|
|
|
|
session.sendDownstreamPacket(jigsawPacket);
|
2020-04-08 21:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|