Validate sleep message arguments (#5023)

This commit is contained in:
AJ Ferguson 2024-09-06 13:48:00 -04:00 committed by GitHub
parent 9dad34d0a8
commit 34f5d71e58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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;
}
}