Skip to content

Commit

Permalink
added ClientSettings.cs (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Apr 3, 2024
1 parent 16ca614 commit 2e0ce8d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 17 deletions.
98 changes: 98 additions & 0 deletions Components/MineSharp.Protocol/ClientSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using MineSharp.Core.Common;

namespace MineSharp.Protocol;

/// <summary>
/// Describes client settings
/// </summary>
public record ClientSettings
{
/// <summary>
/// Default client settings
/// </summary>
public static readonly ClientSettings Default = new ClientSettings(
"en_GB",
24,
ChatMode.Enabled,
true,
0x7F,
PlayerHand.MainHand,
false,
true);

/// <summary>
/// Locale (e.g. 'en_GB')
/// </summary>
public string Locale { get; }

/// <summary>
/// The client's view distance
/// </summary>
public byte ViewDistance { get; }

/// <summary>
/// What the client want's to see in chat (currently ignored)
/// </summary>
public ChatMode ChatMode { get; } // TODO: #31

/// <summary>
/// Whether the client allows colored chat (currently ignored)
/// </summary>
public bool ColoredChat { get; } // TODO: #31

/// <summary>
/// Bitmask of skin parts displayed by the client (not used)
/// </summary>
public byte DisplayedSkinParts { get; }

/// <summary>
/// The clients main hand
/// </summary>
public PlayerHand MainHand { get; }

/// <summary>
/// Whether to filter chat messages (currently ignored)
/// </summary>
public bool EnableTextFiltering { get; } // TODO: #31

/// <summary>
/// Whether you want to show up in a server's online players list
/// </summary>
public bool AllowServerListings { get; }

/// <summary>
/// Constructor
/// </summary>
public ClientSettings(string locale, byte viewDistance, ChatMode chatMode, bool coloredChat, byte displayedSkinParts, PlayerHand mainHand, bool enableTextFiltering, bool allowServerListings)
{
Locale = locale;
ViewDistance = viewDistance;
ChatMode = chatMode;
ColoredChat = coloredChat;
DisplayedSkinParts = displayedSkinParts;
MainHand = mainHand;
EnableTextFiltering = enableTextFiltering;
AllowServerListings = allowServerListings;
}
}

/// <summary>
/// Specifies the chat mode
/// </summary>
public enum ChatMode
{
/// <summary>
/// Show all chat messages
/// </summary>
Enabled = 0,

/// <summary>
/// Only command messages
/// </summary>
CommandsOnly = 1,

/// <summary>
/// No player messages nor command messages
/// </summary>
Hidden = 2
}
35 changes: 19 additions & 16 deletions Components/MineSharp.Protocol/MinecraftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,22 @@ public sealed class MinecraftClient : IDisposable
/// </summary>
public readonly ushort Port;

/// <summary>
/// The clients settings
/// </summary>
public readonly ClientSettings Settings;

/// <summary>
/// Create a new MinecraftClient
/// </summary>
/// <param name="data">The data used by the client</param>
/// <param name="session">The session object</param>
/// <param name="hostnameOrIp">Hostname or ip of the server</param>
/// <param name="port">Port of the server</param>
/// <param name="api">Optional: instance of MinecraftApi</param>
/// <param name="tcpFactory">Optional: TcpClient factory</param>
public MinecraftClient(
MinecraftData data,
Session session,
string hostnameOrIp,
ushort port = 25565,
MinecraftApi? api = null,
ITcpClientFactory? tcpFactory = null)
ITcpClientFactory? tcpFactory = null,
ClientSettings? settings = null)
{
this.Data = data;
this._packetQueue = new ConcurrentQueue<PacketSendTask>();
Expand All @@ -126,7 +126,7 @@ public MinecraftClient(
this._useAnonymousNbt = this.Data.Version.Protocol >= ProtocolVersion.V_1_20_2;
this._tcpTcpFactory = tcpFactory;
this.ip = IPHelper.ResolveHostname(hostnameOrIp, ref port);

if (session.OnlineSession)
api ??= new MinecraftApi();

Expand All @@ -135,6 +135,7 @@ public MinecraftClient(
this.Port = port;
this.Hostname = hostnameOrIp;
this.gameState = GameState.Handshaking;
this.Settings = settings ?? ClientSettings.Default;
}

/// <summary>
Expand Down Expand Up @@ -278,15 +279,17 @@ internal void UpdateGameState(GameState next)
this._gameJoinedTsc.TrySetResult();

if (next == GameState.Configuration)
{
this.SendPacket(new ClientInformationPacket(
"en_pt",
24,
0,
true,
0x7F,
1,
false,
true)); // TODO: Add a settings object #31
this.Settings.Locale,
this.Settings.ViewDistance,
(int)this.Settings.ChatMode,
this.Settings.ColoredChat,
this.Settings.DisplayedSkinParts,
(int)this.Settings.MainHand,
this.Settings.EnableTextFiltering,
this.Settings.AllowServerListings));
}
}

internal void EnableEncryption(byte[] key)
Expand Down
16 changes: 15 additions & 1 deletion MineSharp.Bot/BotBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class BotBuilder
// proxy
private ProxyFactory? proxyProvider = null;

private ClientSettings? settings = null;

private bool autoConnect;

/// <summary>
Expand Down Expand Up @@ -113,6 +115,15 @@ public BotBuilder WithPlugin<T>() where T : Plugin
return this;
}

/// <summary>
/// Set the client settings
/// </summary>
public BotBuilder WithSettings(ClientSettings settings)
{
this.settings = settings;
return this;
}

/// <summary>
/// Do not load the default plugins
/// </summary>
Expand Down Expand Up @@ -229,14 +240,17 @@ public async Task<MineSharpBot> CreateAsync()
else
throw new ArgumentNullException(nameof(this.session),
"No session provided. Set either Session(), OfflineSession() or OnlineSession()");

this.settings ??= ClientSettings.Default;

var client = new MinecraftClient(
data,
session,
this.hostname,
this.port,
api,
this.proxyProvider);
this.proxyProvider,
this.settings);

var bot = new MineSharpBot(client);

Expand Down

0 comments on commit 2e0ce8d

Please sign in to comment.