Merge remote-tracking branch 'upstream/master' into dev

This commit is contained in:
onebeastchris 2024-06-03 20:54:10 +02:00
commit aaad10b8c9
15 changed files with 117 additions and 89 deletions

View file

@ -770,6 +770,7 @@ public class GeyserImpl implements GeyserApi {
return 0;
}
//noinspection DataFlowIssue
return Integer.parseInt(BUILD_NUMBER);
}

View file

@ -148,9 +148,9 @@ public class GeyserLocale {
} catch (IOException ignored) {}
}
} else {
if (GeyserImpl.getInstance() != null && !validLocalLanguage) {
if (!validLocalLanguage) {
// Don't warn on missing locales if a local file has been found
GeyserImpl.getInstance().getLogger().warning("Missing locale: " + locale);
bootstrap.getGeyserLogger().warning("Missing locale: " + locale);
}
}
@ -162,12 +162,7 @@ public class GeyserLocale {
localeProp.load(stream);
} catch (IOException e) {
String message = "Unable to load custom language override!";
if (GeyserImpl.getInstance() != null) {
GeyserImpl.getInstance().getLogger().error(message, e);
} else {
System.err.println(message);
e.printStackTrace();
}
bootstrap.getGeyserLogger().error(message, e);
}
LOCALE_MAPPINGS.putIfAbsent(locale, localeProp);

View file

@ -26,8 +26,8 @@
package org.geysermc.geyser.translator.protocol.bedrock;
import org.cloudburstmc.protocol.bedrock.packet.CommandRequestPacket;
import org.geysermc.geyser.api.util.PlatformType;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.util.PlatformType;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
@ -39,15 +39,17 @@ public class BedrockCommandRequestTranslator extends PacketTranslator<CommandReq
@Override
public void translate(GeyserSession session, CommandRequestPacket packet) {
String command = MessageTranslator.convertToPlainText(packet.getCommand());
handleCommand(session, MessageTranslator.normalizeSpace(command).substring(1));
}
static void handleCommand(GeyserSession session, String command) {
if (!(session.getGeyser().getPlatformType() == PlatformType.STANDALONE
&& GeyserImpl.getInstance().commandManager().runCommand(session, command.substring(1)))) {
&& GeyserImpl.getInstance().commandManager().runCommand(session, command))) {
if (MessageTranslator.isTooLong(command, session)) {
return;
}
// running commands via Bedrock's command select menu adds a trailing whitespace which Java doesn't like
// https://github.com/GeyserMC/Geyser/issues/3877
session.sendCommand(command.substring(1).stripTrailing());
session.sendCommand(command);
}
}
}

View file

@ -36,13 +36,22 @@ public class BedrockTextTranslator extends PacketTranslator<TextPacket> {
@Override
public void translate(GeyserSession session, TextPacket packet) {
String message = MessageTranslator.convertToPlainText(packet.getMessage());
// Java trims all messages, and then checks for the leading slash
String message = MessageTranslator.convertToPlainText(
MessageTranslator.normalizeSpace(packet.getMessage())
);
if (message.isBlank()) {
// Java Edition (as of 1.17.1) just doesn't pass on these messages, so... we won't either!
return;
}
if (message.startsWith("/")) {
// Yes, Java actually allows whitespaces before commands and will still see those as valid
BedrockCommandRequestTranslator.handleCommand(session, message.substring(1));
return;
}
if (MessageTranslator.isTooLong(message, session)) {
return;
}

View file

@ -387,6 +387,39 @@ public class MessageTranslator {
return false;
}
/**
* Normalizes whitespaces - a thing a vanilla client apparently does with commands and chat messages.
*/
public static String normalizeSpace(String string) {
if (string == null || string.isEmpty()) {
return string;
}
final int size = string.length();
final char[] newChars = new char[size];
int count = 0;
int whitespacesCount = 0;
boolean startWhitespaces = true;
for (int i = 0; i < size; i++) {
final char actualChar = string.charAt(i);
final boolean isWhitespace = Character.isWhitespace(actualChar);
if (isWhitespace) {
if (whitespacesCount == 0 && !startWhitespaces) {
newChars[count++] = ' ';
}
whitespacesCount++;
} else {
startWhitespaces = false;
// Replace non-breaking spaces with regular spaces for normalization
newChars[count++] = (actualChar == '\u00A0' ? ' ' : actualChar);
whitespacesCount = 0;
}
}
if (startWhitespaces) {
return "";
}
return new String(newChars, 0, count - (whitespacesCount > 0 ? 1 : 0)).trim();
}
public static void init() {
// no-op
}