3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-16 04:50:07 +01:00

Validate sleep message arguments (#5023)

Dieser Commit ist enthalten in:
AJ Ferguson 2024-09-06 13:48:00 -04:00 committet von GitHub
Ursprung 9dad34d0a8
Commit 34f5d71e58
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194

Datei anzeigen

@ -25,7 +25,10 @@
package org.geysermc.geyser.translator.protocol.java;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.TranslationArgument;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventGenericPacket;
@ -55,8 +58,9 @@ public class JavaSystemChatTranslator extends PacketTranslator<ClientboundSystem
if (component.arguments().size() == 2) {
// Hack FYI, but it allows Bedrock players to easily understand this information
// without it being covered up or saying the night is being slept through.
int numPlayersSleeping = ((Number) component.arguments().get(0).value()).intValue();
int totalPlayersNeeded = ((Number) component.arguments().get(1).value()).intValue();
Integer numPlayersSleeping = convertToInt(component.arguments().get(0));
Integer totalPlayersNeeded = convertToInt(component.arguments().get(1));
if (numPlayersSleeping != null && totalPlayersNeeded != null) {
LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket();
sleepInfoPacket.setType(LevelEvent.SLEEPING_PLAYERS);
sleepInfoPacket.setTag(NbtMap.builder()
@ -66,6 +70,7 @@ public class JavaSystemChatTranslator extends PacketTranslator<ClientboundSystem
.build());
session.sendUpstreamPacket(sleepInfoPacket);
}
}
} else if (component.key().equals("sleep.skipping_night")) {
LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket();
sleepInfoPacket.setType(LevelEvent.SLEEPING_PLAYERS);
@ -97,4 +102,19 @@ public class JavaSystemChatTranslator extends PacketTranslator<ClientboundSystem
session.getUpstream().queuePostStartGamePacket(textPacket);
}
}
private static @Nullable Integer convertToInt(TranslationArgument translationArgument) {
Object value = translationArgument.value();
if (value instanceof Number number) {
return number.intValue();
}
if (value instanceof TextComponent textComponent) {
try {
return Integer.parseInt(textComponent.content());
} catch (NumberFormatException e) {
// Ignore
}
}
return null;
}
}