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; package org.geysermc.geyser.translator.protocol.java;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent; 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.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent; import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventGenericPacket; import org.cloudburstmc.protocol.bedrock.packet.LevelEventGenericPacket;
@ -55,16 +58,18 @@ public class JavaSystemChatTranslator extends PacketTranslator<ClientboundSystem
if (component.arguments().size() == 2) { if (component.arguments().size() == 2) {
// Hack FYI, but it allows Bedrock players to easily understand this information // 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. // without it being covered up or saying the night is being slept through.
int numPlayersSleeping = ((Number) component.arguments().get(0).value()).intValue(); Integer numPlayersSleeping = convertToInt(component.arguments().get(0));
int totalPlayersNeeded = ((Number) component.arguments().get(1).value()).intValue(); Integer totalPlayersNeeded = convertToInt(component.arguments().get(1));
LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket(); if (numPlayersSleeping != null && totalPlayersNeeded != null) {
sleepInfoPacket.setType(LevelEvent.SLEEPING_PLAYERS); LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket();
sleepInfoPacket.setTag(NbtMap.builder() sleepInfoPacket.setType(LevelEvent.SLEEPING_PLAYERS);
.putInt("ableToSleep", totalPlayersNeeded) sleepInfoPacket.setTag(NbtMap.builder()
.putInt("overworldPlayerCount", totalPlayersNeeded) .putInt("ableToSleep", totalPlayersNeeded)
.putInt("sleepingPlayerCount", numPlayersSleeping) .putInt("overworldPlayerCount", totalPlayersNeeded)
.build()); .putInt("sleepingPlayerCount", numPlayersSleeping)
session.sendUpstreamPacket(sleepInfoPacket); .build());
session.sendUpstreamPacket(sleepInfoPacket);
}
} }
} else if (component.key().equals("sleep.skipping_night")) { } else if (component.key().equals("sleep.skipping_night")) {
LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket(); LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket();
@ -97,4 +102,19 @@ public class JavaSystemChatTranslator extends PacketTranslator<ClientboundSystem
session.getUpstream().queuePostStartGamePacket(textPacket); 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;
}
} }