|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.translators.bedrock; |
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; |
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientUpdateJigsawBlockPacket; |
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientUpdateSignPacket; |
|
|
import com.nukkitx.nbt.NbtMap; |
|
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket; |
|
|
import org.geysermc.connector.network.session.GeyserSession; |
|
|
import org.geysermc.connector.network.translators.PacketTranslator; |
|
|
import org.geysermc.connector.network.translators.Translator; |
|
|
import org.geysermc.connector.utils.SignUtils; |
|
|
|
|
|
@Translator(packet = BlockEntityDataPacket.class) |
|
|
public class BedrockBlockEntityDataTranslator extends PacketTranslator<BlockEntityDataPacket> { |
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
|
|
|
|
public void translate(GeyserSession session, BlockEntityDataPacket packet) { |
|
|
NbtMap tag = packet.getData(); |
|
|
if (tag.getString("id").equals("Sign")) { |
|
|
|
|
|
|
|
|
|
|
|
if (!tag.getString("Text").equals(session.getLastSignMessage())) { |
|
|
session.setLastSignMessage(tag.getString("Text")); |
|
|
return; |
|
|
} |
|
|
|
|
|
StringBuilder newMessage = new StringBuilder(); |
|
|
|
|
|
|
|
|
String[] lines = new String[] {"", "", "", ""}; |
|
|
int iterator = 0; |
|
|
|
|
|
|
|
|
int widthCount = 0; |
|
|
|
|
|
for (char character : tag.getString("Text").toCharArray()) { |
|
|
widthCount += SignUtils.getCharacterWidth(character); |
|
|
|
|
|
if (character == '\n' || widthCount > SignUtils.JAVA_CHARACTER_WIDTH_MAX) { |
|
|
|
|
|
boolean wentOverMax = widthCount > SignUtils.JAVA_CHARACTER_WIDTH_MAX && character != '\n'; |
|
|
widthCount = 0; |
|
|
|
|
|
String word = null; |
|
|
if (wentOverMax && iterator < lines.length - 1) { |
|
|
|
|
|
|
|
|
int index = newMessage.lastIndexOf(" "); |
|
|
if (index != -1) { |
|
|
|
|
|
word = newMessage.substring(index + 1); |
|
|
|
|
|
newMessage.delete(index, newMessage.length()); |
|
|
} |
|
|
} |
|
|
lines[iterator] = newMessage.toString(); |
|
|
iterator++; |
|
|
|
|
|
|
|
|
if (iterator > lines.length - 1) { |
|
|
break; |
|
|
} |
|
|
newMessage = new StringBuilder(); |
|
|
if (wentOverMax) { |
|
|
|
|
|
if (word != null) { |
|
|
newMessage.append(word); |
|
|
|
|
|
for (char wordCharacter : word.toCharArray()) { |
|
|
widthCount += SignUtils.getCharacterWidth(wordCharacter); |
|
|
} |
|
|
} |
|
|
|
|
|
newMessage.append(character); |
|
|
widthCount += SignUtils.getCharacterWidth(character); |
|
|
} |
|
|
} else newMessage.append(character); |
|
|
} |
|
|
|
|
|
if (iterator < lines.length) lines[iterator] = newMessage.toString(); |
|
|
Position pos = new Position(tag.getInt("x"), tag.getInt("y"), tag.getInt("z")); |
|
|
ClientUpdateSignPacket clientUpdateSignPacket = new ClientUpdateSignPacket(pos, lines); |
|
|
session.sendDownstreamPacket(clientUpdateSignPacket); |
|
|
|
|
|
|
|
|
session.setLastSignMessage(null); |
|
|
|
|
|
} else if (tag.getString("id").equals("JigsawBlock")) { |
|
|
|
|
|
Position pos = new Position(tag.getInt("x"), tag.getInt("y"), tag.getInt("z")); |
|
|
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"); |
|
|
ClientUpdateJigsawBlockPacket jigsawPacket = new ClientUpdateJigsawBlockPacket(pos, name, target, pool, |
|
|
finalState, joint); |
|
|
session.sendDownstreamPacket(jigsawPacket); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|