|
| 1 | +package packet |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "go.minekube.com/gate/pkg/edition/java/proto/version" |
| 9 | + "go.minekube.com/gate/pkg/gate/proto" |
| 10 | + "go.minekube.com/gate/pkg/util/uuid" |
| 11 | +) |
| 12 | + |
| 13 | +func TestJoinGameMinecraft262OnlineModeRoundTrip(t *testing.T) { |
| 14 | + levelName := "minecraft:overworld" |
| 15 | + original := &JoinGame{ |
| 16 | + EntityID: 1, |
| 17 | + Hardcore: true, |
| 18 | + LevelNames: []string{"minecraft:overworld"}, |
| 19 | + MaxPlayers: 100, |
| 20 | + ViewDistance: 12, |
| 21 | + SimulationDistance: 10, |
| 22 | + ReducedDebugInfo: true, |
| 23 | + ShowRespawnScreen: true, |
| 24 | + DoLimitedCrafting: true, |
| 25 | + Dimension: 0, |
| 26 | + DimensionInfo: &DimensionInfo{ |
| 27 | + LevelName: &levelName, |
| 28 | + }, |
| 29 | + PartialHashedSeed: 1234, |
| 30 | + Gamemode: 1, |
| 31 | + PreviousGamemode: -1, |
| 32 | + LastDeathPosition: &DeathPosition{Key: "minecraft:overworld", Value: 42}, |
| 33 | + SeaLevel: 63, |
| 34 | + PortalCooldown: 5, |
| 35 | + OnlineMode: true, |
| 36 | + EnforcesSecureChat: true, |
| 37 | + } |
| 38 | + ctx := &proto.PacketContext{Direction: proto.ClientBound, Protocol: version.Minecraft_26_2.Protocol} |
| 39 | + |
| 40 | + var buf bytes.Buffer |
| 41 | + require.NoError(t, original.Encode(ctx, &buf)) |
| 42 | + require.True(t, bytes.HasSuffix(buf.Bytes(), []byte{ |
| 43 | + 0x05, // portal cooldown |
| 44 | + 0x3f, // sea level |
| 45 | + 0x01, // online mode |
| 46 | + 0x01, // enforces secure chat |
| 47 | + })) |
| 48 | + |
| 49 | + var decoded JoinGame |
| 50 | + require.NoError(t, decoded.Decode(ctx, &buf)) |
| 51 | + require.True(t, decoded.OnlineMode) |
| 52 | + require.True(t, decoded.EnforcesSecureChat) |
| 53 | + require.Equal(t, 0, buf.Len()) |
| 54 | +} |
| 55 | + |
| 56 | +func TestServerLoginSuccessMinecraft262SessionIDRoundTrip(t *testing.T) { |
| 57 | + sessionID := uuid.New() |
| 58 | + original := &ServerLoginSuccess{ |
| 59 | + UUID: testUUID, |
| 60 | + Username: "Robin", |
| 61 | + SessionID: sessionID, |
| 62 | + } |
| 63 | + ctx := &proto.PacketContext{Direction: proto.ClientBound, Protocol: version.Minecraft_26_2.Protocol} |
| 64 | + |
| 65 | + var buf bytes.Buffer |
| 66 | + require.NoError(t, original.Encode(ctx, &buf)) |
| 67 | + |
| 68 | + var decoded ServerLoginSuccess |
| 69 | + require.NoError(t, decoded.Decode(ctx, &buf)) |
| 70 | + require.Equal(t, sessionID, decoded.SessionID) |
| 71 | + require.Equal(t, 0, buf.Len()) |
| 72 | +} |
0 commit comments