Skip to content

Commit

Permalink
cleanup some packets (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Jul 24, 2024
1 parent bff18e9 commit 2cd2c1e
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,30 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Configuration;
#pragma warning disable CS1591

/// <summary>
/// Configuration Disconnect packet
/// See https://wiki.vg/Protocol#Disconnect_.28configuration.29
/// </summary>
public class DisconnectPacket : IPacket
{
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="reason"></param>
public DisconnectPacket(Chat reason)
{
Reason = reason;
}

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_Disconnect;

/// <summary>
/// Reason for disconnect
/// </summary>
public Chat Reason { get; set; }

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_Disconnect;
public required Chat Reason { get; init; }

/// <inheritdoc />
public void Write(PacketBuffer buffer, MinecraftData version)
{
buffer.WriteString(Reason.ToJson().ToString());
buffer.WriteChatComponent(Reason);
}

/// <inheritdoc />
public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
return new DisconnectPacket(buffer.ReadChatComponent());
return new DisconnectPacket { Reason = buffer.ReadChatComponent() };
}
}
#pragma warning restore CS1591
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Configuration;
#pragma warning disable CS1591

/// <summary>
/// Feature flags packet
/// See https://wiki.vg/Protocol#Feature_Flags
/// </summary>
public class FeatureFlagsPacket : IPacket
{
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="featureFlags"></param>
public FeatureFlagsPacket(string[] featureFlags)
{
FeatureFlags = featureFlags;
}

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_FeatureFlags;

/// <summary>
/// The enabled feature flags
/// </summary>
public string[] FeatureFlags { get; set; }

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_FeatureFlags;
public required string[] FeatureFlags { get; init; }

/// <inheritdoc />
public void Write(PacketBuffer buffer, MinecraftData version)
Expand All @@ -35,8 +27,7 @@ public void Write(PacketBuffer buffer, MinecraftData version)
/// <inheritdoc />
public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
return new FeatureFlagsPacket(
buffer.ReadVarIntArray(buff => buff.ReadString()));
return new FeatureFlagsPacket { FeatureFlags = buffer.ReadVarIntArray(buff => buff.ReadString()) };
}
}
#pragma warning restore CS1591
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Configuration;
#pragma warning disable CS1591

/// <summary>
/// Finish configuration packet
/// See https://wiki.vg/Protocol#Finish_Configuration
/// </summary>
public class FinishConfigurationPacket : IPacket
{
Expand All @@ -22,4 +23,3 @@ public static IPacket Read(PacketBuffer buffer, MinecraftData version)
return new FinishConfigurationPacket();
}
}
#pragma warning restore CS1591
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Configuration;
#pragma warning disable CS1591

/// <summary>
/// Keep alive packet in Configuration
/// See https://wiki.vg/Protocol#Clientbound_Keep_Alive_.28configuration.29
/// </summary>
public class KeepAlivePacket : IPacket
{
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="keepAliveId"></param>
public KeepAlivePacket(long keepAliveId)
{
KeepAliveId = keepAliveId;
}

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_KeepAlive;

/// <summary>
/// The keep alive id
/// </summary>
public long KeepAliveId { get; set; }

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_KeepAlive;
public required long KeepAliveId { get; init; }

/// <inheritdoc />
public void Write(PacketBuffer buffer, MinecraftData version)
Expand All @@ -35,7 +27,6 @@ public void Write(PacketBuffer buffer, MinecraftData version)
/// <inheritdoc />
public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
return new KeepAlivePacket(buffer.ReadLong());
return new KeepAlivePacket() { KeepAliveId = buffer.ReadLong() };
}
}
#pragma warning restore CS1591
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Configuration;
#pragma warning disable CS1591

/// <summary>
/// Ping packet
/// See https://wiki.vg/Protocol#Ping_.28configuration.29
/// </summary>
public class PingPacket : IPacket
{
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="id"></param>
public PingPacket(int id)
{
Id = id;
}

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_Ping;

/// <summary>
/// The id of the ping
/// </summary>
public int Id { get; set; }

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_Ping;
public required int Id { get; init; }

/// <inheritdoc />
public void Write(PacketBuffer buffer, MinecraftData version)
Expand All @@ -35,8 +27,6 @@ public void Write(PacketBuffer buffer, MinecraftData version)
/// <inheritdoc />
public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
return new PingPacket(
buffer.ReadInt());
return new PingPacket() { Id = buffer.ReadInt() };
}
}
#pragma warning restore CS1591
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,30 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Configuration;
#pragma warning disable CS1591

/// <summary>
/// Registry data packet
/// See https://wiki.vg/Protocol#Registry_Data
/// </summary>
public class RegistryDataPacket : IPacket
{
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="registryData"></param>
public RegistryDataPacket(NbtCompound registryData)
{
RegistryData = registryData;
}

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_RegistryData;

/// <summary>
/// The registry data
/// </summary>
public NbtCompound RegistryData { get; set; }

/// <inheritdoc />
public PacketType Type => PacketType.CB_Configuration_RegistryData;

public required NbtCompound RegistryData { get; init; }

/// <inheritdoc />
public void Write(PacketBuffer buffer, MinecraftData version)
{
buffer.WriteNbt(RegistryData);
}

/// <inheritdoc />
public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
return new RegistryDataPacket(
buffer.ReadNbtCompound());
return new RegistryDataPacket() { RegistryData = buffer.ReadNbtCompound() };
}
}
#pragma warning restore CS1591
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,32 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Login;
#pragma warning disable CS1591

/// <summary>
/// Disconnect packet for login
/// See https://wiki.vg/Protocol#Disconnect_.28login.29
/// </summary>
public class DisconnectPacket : IPacket
{
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="reason"></param>
public DisconnectPacket(Chat reason)
{
Reason = reason;
}

/// <inheritdoc />
public PacketType Type => PacketType.CB_Login_Disconnect;

/// <summary>
/// The reason for being disconnected
/// </summary>
public Chat Reason { get; set; }

/// <inheritdoc />
public PacketType Type => PacketType.CB_Login_Disconnect;

public required Chat Reason { get; init; }

/// <inheritdoc />
public void Write(PacketBuffer buffer, MinecraftData version)
{
// Disconnect (login) packet is always sent as JSON text component according to wiki.vg
buffer.WriteString(Reason.ToJson().ToString());
}

/// <inheritdoc />
public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
var reason = buffer.ReadString();
return new DisconnectPacket(Chat.Parse(reason));
return new DisconnectPacket() { Reason = Chat.Parse(reason) };
}
}
#pragma warning restore CS1591
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,30 @@
using MineSharp.Data.Protocol;

namespace MineSharp.Protocol.Packets.Clientbound.Login;
#pragma warning disable CS1591

/// <summary>
/// Encryption request packet
/// See https://wiki.vg/Protocol#Encryption_Request
/// </summary>
public class EncryptionRequestPacket : IPacket
{
/// <summary>
/// Create a new instance
/// </summary>
/// <param name="serverId"></param>
/// <param name="publicKey"></param>
/// <param name="verifyToken"></param>
public EncryptionRequestPacket(string serverId, byte[] publicKey, byte[] verifyToken)
{
ServerId = serverId;
PublicKey = publicKey;
VerifyToken = verifyToken;
}

/// <inheritdoc />
public PacketType Type => PacketType.CB_Login_EncryptionBegin;

/// <summary>
/// The hashed server id
/// </summary>
public string ServerId { get; set; }
public required string ServerId { get; init; }

/// <summary>
/// The public key of the server
/// </summary>
public byte[] PublicKey { get; set; }
public required byte[] PublicKey { get; init; }

/// <summary>
/// Verify token
/// </summary>
public byte[] VerifyToken { get; set; }

/// <inheritdoc />
public PacketType Type => PacketType.CB_Login_EncryptionBegin;
public required byte[] VerifyToken { get; init; }

/// <inheritdoc />
public void Write(PacketBuffer buffer, MinecraftData version)
Expand All @@ -54,12 +42,16 @@ public void Write(PacketBuffer buffer, MinecraftData version)
public static IPacket Read(PacketBuffer buffer, MinecraftData version)
{
var serverId = buffer.ReadString();
Span<byte> publicKey = stackalloc byte[buffer.ReadVarInt()];
var publicKey = new byte[buffer.ReadVarInt()];
buffer.ReadBytes(publicKey);
Span<byte> verifyToken = stackalloc byte[buffer.ReadVarInt()];
var verifyToken = new byte[buffer.ReadVarInt()];
buffer.ReadBytes(verifyToken);

return new EncryptionRequestPacket(serverId, publicKey.ToArray(), verifyToken.ToArray());
return new EncryptionRequestPacket()
{
ServerId = serverId,
PublicKey = publicKey,
VerifyToken = verifyToken
};
}
}
#pragma warning restore CS1591
Loading

0 comments on commit 2cd2c1e

Please sign in to comment.