Skip to content

Commit

Permalink
Ensure players aren't floating in their beds; improve Bedrock sleepin…
Browse files Browse the repository at this point in the history
…g notifications
  • Loading branch information
Camotoy committed Aug 31, 2024
1 parent 63e60bc commit 8935b34
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityLinkData;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.cloudburstmc.protocol.bedrock.packet.*;
import org.cloudburstmc.protocol.bedrock.packet.AddPlayerPacket;
import org.cloudburstmc.protocol.bedrock.packet.MovePlayerPacket;
import org.cloudburstmc.protocol.bedrock.packet.SetEntityDataPacket;
import org.cloudburstmc.protocol.bedrock.packet.SetEntityLinkPacket;
import org.cloudburstmc.protocol.bedrock.packet.UpdateAttributesPacket;
import org.geysermc.geyser.api.entity.type.player.GeyserPlayerEntity;
import org.geysermc.geyser.entity.EntityDefinitions;
import org.geysermc.geyser.entity.attribute.GeyserAttributeType;
Expand Down Expand Up @@ -278,7 +282,13 @@ public void updateRotation(float yaw, float pitch, float headYaw, boolean isOnGr

@Override
public void setPosition(Vector3f position) {
super.setPosition(position.add(0, definition.offset(), 0));
if (this.bedPosition != null) {
// As of Bedrock 1.21.22 and Fabric 1.21.1
// Messes with Bedrock if we send this to the client itself, though.
super.setPosition(position.up(0.2f));
} else {
super.setPosition(position.add(0, definition.offset(), 0));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void setPosition(Vector3f position) {
if (valid) { // Don't update during session init
session.getCollisionManager().updatePlayerBoundingBox(position);
}
super.setPosition(position);
this.position = position.add(0, definition.offset(), 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.cloudburstmc.protocol.bedrock.BedrockDisconnectReasons;
import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;
import org.cloudburstmc.protocol.bedrock.codec.compat.BedrockCompat;
import org.cloudburstmc.protocol.bedrock.codec.v622.Bedrock_v622;
import org.cloudburstmc.protocol.bedrock.data.ExperimentData;
import org.cloudburstmc.protocol.bedrock.data.PacketCompressionAlgorithm;
import org.cloudburstmc.protocol.bedrock.data.ResourcePackType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void translate(GeyserSession session, MovePlayerPacket packet) {

if (entity.getBedPosition() != null) {
// https://github.com/GeyserMC/Geyser/issues/5001
// Bedrock 1.20.22 started sending a MovePlayerPacket as soon as it got into a bed.
// Bedrock 1.21.22 started sending a MovePlayerPacket as soon as it got into a bed.
// This trips up Fabric.
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,56 @@

package org.geysermc.geyser.translator.protocol.java;

import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;
import net.kyori.adventure.text.TranslatableComponent;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventGenericPacket;
import org.cloudburstmc.protocol.bedrock.packet.TextPacket;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket;

@Translator(packet = ClientboundSystemChatPacket.class)
public class JavaSystemChatTranslator extends PacketTranslator<ClientboundSystemChatPacket> {

@Override
public void translate(GeyserSession session, ClientboundSystemChatPacket packet) {
if (packet.getContent() instanceof TranslatableComponent component && component.key().equals("chat.disabled.missingProfileKey")) {
// We likely got this message as a response to a player trying to chat
// As there SHOULD be no false flags for this, print every time it shows up in chat.
if (Boolean.parseBoolean(System.getProperty("Geyser.PrintSecureChatInformation", "true"))) {
session.sendMessage(GeyserLocale.getPlayerLocaleString("geyser.chat.secure_info_1", session.locale()));
session.sendMessage(GeyserLocale.getPlayerLocaleString("geyser.chat.secure_info_2", session.locale(), "https://geysermc.link/secure-chat"));
if (packet.getContent() instanceof TranslatableComponent component) {
if (component.key().equals("chat.disabled.missingProfileKey")) {
// We likely got this message as a response to a player trying to chat
// As there SHOULD be no false flags for this, print every time it shows up in chat.
if (Boolean.parseBoolean(System.getProperty("Geyser.PrintSecureChatInformation", "true"))) {
session.sendMessage(GeyserLocale.getPlayerLocaleString("geyser.chat.secure_info_1", session.locale()));
session.sendMessage(GeyserLocale.getPlayerLocaleString("geyser.chat.secure_info_2", session.locale(), "https://geysermc.link/secure-chat"));
}
} else if (component.key().equals("sleep.players_sleeping")) {
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();
LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket();
sleepInfoPacket.setType(LevelEvent.SLEEPING_PLAYERS);
sleepInfoPacket.setTag(NbtMap.builder()
.putInt("ableToSleep", totalPlayersNeeded)
.putInt("overworldPlayerCount", totalPlayersNeeded)
.putInt("sleepingPlayerCount", numPlayersSleeping)
.build());
session.sendUpstreamPacket(sleepInfoPacket);
}
} else if (component.key().equals("sleep.skipping_night")) {
LevelEventGenericPacket sleepInfoPacket = new LevelEventGenericPacket();
sleepInfoPacket.setType(LevelEvent.SLEEPING_PLAYERS);
sleepInfoPacket.setTag(NbtMap.builder()
.putInt("ableToSleep", 1)
.putInt("overworldPlayerCount", 1)
.putInt("sleepingPlayerCount", 1)
.build());
session.sendUpstreamPacket(sleepInfoPacket);
}
}

Expand Down

0 comments on commit 8935b34

Please sign in to comment.