Minecraft-Console-Client/MinecraftClient/Protocol/Handlers/Protocol18.cs

3084 lines
171 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
2022-10-04 11:53:07 +08:00
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using MinecraftClient.Commands;
using MinecraftClient.Crypto;
using MinecraftClient.Inventory;
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Logger;
using MinecraftClient.Mapping;
using MinecraftClient.Mapping.BlockPalettes;
using MinecraftClient.Mapping.EntityPalettes;
using MinecraftClient.Protocol.Handlers.Forge;
2022-10-14 20:34:45 +08:00
using MinecraftClient.Protocol.Handlers.packet.s2c;
using MinecraftClient.Protocol.Handlers.PacketPalettes;
2022-08-27 02:10:44 +08:00
using MinecraftClient.Protocol.Message;
using MinecraftClient.Protocol.PacketPipeline;
using MinecraftClient.Protocol.ProfileKey;
using MinecraftClient.Protocol.Session;
using MinecraftClient.Proxy;
using MinecraftClient.Scripting;
2022-10-05 15:02:30 +08:00
using static MinecraftClient.Settings;
namespace MinecraftClient.Protocol.Handlers
{
/// <summary>
/// Implementation for Minecraft 1.8.X+ Protocols
/// </summary>
2020-06-29 21:52:17 +02:00
/// <remarks>
/// Typical update steps for implementing protocol changes for a new Minecraft version:
/// - Perform a diff between latest supported version in MCC and new stable version to support on https://wiki.vg/Protocol
/// - If there are any changes in packets implemented by MCC, add MCXXXVersion field below and implement new packet layouts
/// - Add the packet type palette for that Minecraft version. Please see PacketTypePalette.cs for more information
2020-06-29 21:52:17 +02:00
/// - Also see Material.cs and ItemType.cs for updating block and item data inside MCC
/// </remarks>
partial class Protocol18Handler : IMinecraftCom
{
internal const int MC_1_8_Version = 47;
internal const int MC_1_9_Version = 107;
internal const int MC_1_9_1_Version = 108;
internal const int MC_1_10_Version = 210;
internal const int MC_1_11_2_Version = 316;
internal const int MC_1_12_Version = 335;
internal const int MC_1_12_2_Version = 340;
internal const int MC_1_13_Version = 393;
internal const int MC_1_13_2_Version = 404;
internal const int MC_1_14_Version = 477;
internal const int MC_1_15_Version = 573;
internal const int MC_1_15_2_Version = 578;
internal const int MC_1_16_Version = 735;
internal const int MC_1_16_1_Version = 736;
internal const int MC_1_16_2_Version = 751;
internal const int MC_1_16_3_Version = 753;
internal const int MC_1_16_5_Version = 754;
internal const int MC_1_17_Version = 755;
internal const int MC_1_17_1_Version = 756;
internal const int MC_1_18_1_Version = 757;
internal const int MC_1_18_2_Version = 758;
internal const int MC_1_19_Version = 759;
2022-08-24 12:37:22 +08:00
internal const int MC_1_19_2_Version = 760;
2020-06-29 21:52:17 +02:00
private int autocomplete_transaction_id = 0;
private readonly Dictionary<int, short> window_actions = new();
2020-06-29 21:52:17 +02:00
private bool login_phase = true;
private readonly int protocolVersion;
2020-06-29 21:52:17 +02:00
private int currentDimension;
2022-09-02 20:22:33 +08:00
private bool isOnlineMode = false;
2022-10-16 18:42:11 +08:00
private float LastYaw, LastPitch;
2020-06-29 21:52:17 +02:00
2022-08-27 02:10:44 +08:00
private int pendingAcknowledgments = 0;
private readonly LastSeenMessagesCollector lastSeenMessagesCollector = new(5);
2022-08-27 02:10:44 +08:00
private LastSeenMessageList.Entry? lastReceivedMessage = null;
readonly Protocol18Forge pForge;
readonly Protocol18Terrain pTerrain;
readonly IMinecraftComHandler handler;
readonly EntityPalette entityPalette;
readonly ItemPalette itemPalette;
readonly PacketTypePalette packetPalette;
readonly SocketWrapper socketWrapper;
readonly DataTypes dataTypes;
readonly ILogger log;
readonly RandomNumberGenerator randomGen;
2020-06-29 21:52:17 +02:00
private readonly CancellationToken CancelToken;
public Protocol18Handler(CancellationToken cancelToken, TcpClient Client, int protocolVersion, IMinecraftComHandler handler, ForgeInfo? forgeInfo)
{
CancelToken = cancelToken;
2020-06-29 21:52:17 +02:00
ConsoleIO.SetAutoCompleteEngine(this);
ChatParser.InitTranslations();
socketWrapper = new SocketWrapper(Client);
dataTypes = new DataTypes(protocolVersion);
2022-09-02 09:29:24 +08:00
this.protocolVersion = protocolVersion;
2020-06-29 21:52:17 +02:00
this.handler = handler;
pForge = new Protocol18Forge(forgeInfo, protocolVersion, dataTypes, this, handler);
pTerrain = new Protocol18Terrain(protocolVersion, dataTypes, handler);
packetPalette = new PacketTypeHandler(protocolVersion, forgeInfo != null).GetTypeHandler();
log = handler.GetLogger();
randomGen = RandomNumberGenerator.Create();
2020-06-29 21:52:17 +02:00
2022-09-02 21:38:43 +08:00
if (handler.GetTerrainEnabled() && protocolVersion > MC_1_19_2_Version)
2020-06-29 21:52:17 +02:00
{
2022-11-30 16:08:55 +08:00
log.Error("§c" + Translations.extra_terrainandmovement_disabled);
2020-06-29 21:52:17 +02:00
handler.SetTerrainEnabled(false);
}
2022-09-02 21:38:43 +08:00
if (handler.GetInventoryEnabled() && (protocolVersion < MC_1_10_Version || protocolVersion > MC_1_19_2_Version))
2020-06-29 21:52:17 +02:00
{
2022-11-30 16:08:55 +08:00
log.Error("§c" + Translations.extra_inventory_disabled);
2020-06-29 21:52:17 +02:00
handler.SetInventoryEnabled(false);
}
2022-09-02 21:38:43 +08:00
if (handler.GetEntityHandlingEnabled() && (protocolVersion < MC_1_10_Version || protocolVersion > MC_1_19_2_Version))
2020-06-29 21:52:17 +02:00
{
2022-11-30 16:08:55 +08:00
log.Error("§c" + Translations.extra_entity_disabled);
2020-06-29 21:52:17 +02:00
handler.SetEntityHandlingEnabled(false);
}
// Block palette
2022-08-28 23:04:55 +08:00
if (protocolVersion > MC_1_19_2_Version && handler.GetTerrainEnabled())
throw new NotImplementedException(Translations.exception_palette_block);
2022-08-28 23:04:55 +08:00
if (protocolVersion >= MC_1_19_Version)
Block.Palette = new Palette119();
else if (protocolVersion >= MC_1_17_Version)
Block.Palette = new Palette117();
else if (protocolVersion >= MC_1_16_Version)
Block.Palette = new Palette116();
else if (protocolVersion >= MC_1_15_Version)
Block.Palette = new Palette115();
else if (protocolVersion >= MC_1_14_Version)
Block.Palette = new Palette114();
2022-09-02 21:38:43 +08:00
else if (protocolVersion >= MC_1_13_Version)
2022-08-28 23:04:55 +08:00
Block.Palette = new Palette113();
else
Block.Palette = new Palette112();
// Entity palette
2022-09-02 21:38:43 +08:00
if (protocolVersion > MC_1_19_2_Version && handler.GetEntityHandlingEnabled())
throw new NotImplementedException(Translations.exception_palette_entity);
2022-09-02 21:38:43 +08:00
if (protocolVersion >= MC_1_19_Version)
entityPalette = new EntityPalette119();
else if (protocolVersion >= MC_1_17_Version)
entityPalette = new EntityPalette117();
else if (protocolVersion >= MC_1_16_2_Version)
entityPalette = new EntityPalette1162();
else if (protocolVersion >= MC_1_16_Version)
entityPalette = new EntityPalette1161();
else if (protocolVersion >= MC_1_15_Version)
entityPalette = new EntityPalette115();
else if (protocolVersion >= MC_1_14_Version)
entityPalette = new EntityPalette114();
else if (protocolVersion >= MC_1_13_Version)
entityPalette = new EntityPalette113();
2022-09-18 01:15:57 +02:00
else
2022-09-02 21:38:43 +08:00
entityPalette = new EntityPalette112();
// Item palette
2022-09-02 21:38:43 +08:00
if (protocolVersion > MC_1_19_2_Version && handler.GetInventoryEnabled())
throw new NotImplementedException(Translations.exception_palette_item);
2022-09-02 21:38:43 +08:00
if (protocolVersion >= MC_1_19_Version)
itemPalette = new ItemPalette119();
else if (protocolVersion >= MC_1_18_1_Version)
itemPalette = new ItemPalette118();
else if (protocolVersion >= MC_1_17_Version)
itemPalette = new ItemPalette117();
else if (protocolVersion >= MC_1_16_2_Version)
itemPalette = new ItemPalette1162();
else if (protocolVersion >= MC_1_16_1_Version)
itemPalette = new ItemPalette1161();
2022-09-18 01:15:57 +02:00
else
2022-09-02 21:38:43 +08:00
itemPalette = new ItemPalette115();
2022-08-27 02:10:44 +08:00
// MessageType
// You can find it in https://wiki.vg/Protocol#Player_Chat_Message or /net/minecraft/network/message/MessageType.java
2022-08-28 18:39:59 +08:00
if (this.protocolVersion >= MC_1_19_2_Version)
2022-08-27 02:10:44 +08:00
ChatParser.ChatId2Type = new()
{
{ 0, ChatParser.MessageType.CHAT },
{ 1, ChatParser.MessageType.SAY_COMMAND },
{ 2, ChatParser.MessageType.MSG_COMMAND_INCOMING },
{ 3, ChatParser.MessageType.MSG_COMMAND_OUTGOING },
{ 4, ChatParser.MessageType.TEAM_MSG_COMMAND_INCOMING },
{ 5, ChatParser.MessageType.TEAM_MSG_COMMAND_OUTGOING },
{ 6, ChatParser.MessageType.EMOTE_COMMAND },
};
2022-08-28 18:39:59 +08:00
else if (this.protocolVersion == MC_1_19_Version)
2022-08-27 02:10:44 +08:00
ChatParser.ChatId2Type = new()
{
{ 0, ChatParser.MessageType.CHAT },
{ 1, ChatParser.MessageType.RAW_MSG },
{ 2, ChatParser.MessageType.RAW_MSG },
{ 3, ChatParser.MessageType.SAY_COMMAND },
{ 4, ChatParser.MessageType.MSG_COMMAND_INCOMING },
{ 5, ChatParser.MessageType.TEAM_MSG_COMMAND_INCOMING },
{ 6, ChatParser.MessageType.EMOTE_COMMAND },
{ 7, ChatParser.MessageType.RAW_MSG },
};
2020-06-29 21:52:17 +02:00
}
private async Task MainTicker()
{
var periodicTimer = new PeriodicTimer(TimeSpan.FromMilliseconds(1000 / 20));
while (await periodicTimer.WaitForNextTickAsync(CancelToken) && !CancelToken.IsCancellationRequested)
2020-06-29 21:52:17 +02:00
{
try
{
await handler.OnUpdate();
}
catch (Exception e)
{
if (Config.Logging.DebugMessages)
2022-08-31 22:52:05 +08:00
{
ConsoleIO.WriteLine($"{e.GetType().Name} when ticking: {e.Message}");
if (e.StackTrace != null)
ConsoleIO.WriteLine(e.StackTrace);
2022-08-31 22:52:05 +08:00
}
}
}
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Start the updating thread. Should be called after login success.
2020-06-29 21:52:17 +02:00
/// </summary>
public async Task StartUpdating()
2020-06-29 21:52:17 +02:00
{
Task MainTickerTask = Task.Run(MainTicker);
2020-06-29 21:52:17 +02:00
while (!CancelToken.IsCancellationRequested)
2020-06-29 21:52:17 +02:00
{
try
{
(int packetID, PacketStream packetStream) = await socketWrapper.GetNextPacket(handleCompress: protocolVersion >= MC_1_8_Version);
// ConsoleIO.WriteLine("Len: " + packetData.Count + ", Packet: " + packetPalette.GetIncommingTypeById(packetID).ToString());
await HandlePacket(packetID, packetStream);
}
catch (IOException) { break; }
catch (SocketException) { break; }
catch (ObjectDisposedException) { break; }
catch (OperationCanceledException) { break; }
catch (NullReferenceException) { break; }
2020-06-29 21:52:17 +02:00
}
if (!CancelToken.IsCancellationRequested)
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, string.Empty);
2022-08-28 14:57:44 +08:00
await MainTickerTask;
2020-06-29 21:52:17 +02:00
}
/// <summary>
/// Handle the given packet
/// </summary>
/// <param name="packetID">Packet ID</param>
/// <param name="packetData">Packet contents</param>
/// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns>
internal async Task<bool> HandlePacket(int packetID, PacketStream packetData)
2020-06-29 21:52:17 +02:00
{
try
{
if (login_phase)
{
switch (packetID) // Packet IDs are different while logging in
2020-06-29 21:52:17 +02:00
{
case 0x03:
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
socketWrapper.CompressionThreshold = await dataTypes.ReadNextVarIntAsync(packetData);
2020-06-29 21:52:17 +02:00
break;
case 0x04:
int messageId = await dataTypes.ReadNextVarIntAsync(packetData);
string channel = await dataTypes.ReadNextStringAsync(packetData);
(bool understood, List<byte> responseData) = await pForge.HandleLoginPluginRequest(channel, packetData);
await SendLoginPluginResponse(messageId, understood, responseData.ToArray());
return understood;
2020-06-29 21:52:17 +02:00
default:
return false; // Ignored packet
2020-06-29 21:52:17 +02:00
}
}
2020-06-29 21:52:17 +02:00
// Regular in-game packets
else switch (packetPalette.GetIncommingTypeById(packetID))
{
case PacketTypesIn.KeepAlive:
handler.OnServerKeepAlive();
await SendPacket(PacketTypesOut.KeepAlive, packetData);
break;
2022-08-31 21:25:54 +08:00
case PacketTypesIn.Ping:
handler.OnServerKeepAlive();
await SendPacket(PacketTypesOut.Pong, packetData);
2022-08-31 21:25:54 +08:00
break;
case PacketTypesIn.JoinGame:
Task OnGameJoinedTask = handler.OnGameJoined();
int playerEntityID = await dataTypes.ReadNextIntAsync(packetData);
handler.OnReceivePlayerEntityID(playerEntityID);
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_2_Version)
await dataTypes.ReadNextBoolAsync(packetData); // Is hardcore - 1.16.2 and above
Task OnGamemodeUpdateTask = handler.OnGamemodeUpdate(Guid.Empty, await dataTypes.ReadNextByteAsync(packetData));
2020-06-29 22:15:56 +02:00
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
{
await dataTypes.ReadNextByteAsync(packetData); // Previous Gamemode - 1.16 and above
int worldCount = await dataTypes.ReadNextVarIntAsync(packetData); // Dimension Count (World Count) - 1.16 and above
for (int i = 0; i < worldCount; i++)
await dataTypes.SkipNextStringAsync(packetData); // Dimension Names (World Names) - 1.16 and above
var registryCodec = await dataTypes.ReadNextNbtAsync(packetData); // Registry Codec (Dimension Codec) - 1.16 and above
2022-09-07 00:02:09 +08:00
if (handler.GetTerrainEnabled())
World.StoreDimensionList(registryCodec);
}
2020-06-29 22:01:29 +02:00
// Current dimension
2022-08-28 23:04:55 +08:00
// String: 1.19 and above
// NBT Tag Compound: [1.16.2 to 1.18.2]
// String identifier: 1.16 and 1.16.1
// varInt: [1.9.1 to 1.15.2]
// byte: below 1.9.1
2022-09-15 12:48:38 +08:00
string? dimensionTypeName = null;
2022-09-07 00:02:09 +08:00
Dictionary<string, object>? dimensionType = null;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
{
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
dimensionTypeName = await dataTypes.ReadNextStringAsync(packetData); // Dimension Type: Identifier
2022-09-02 09:29:24 +08:00
else if (protocolVersion >= MC_1_16_2_Version)
dimensionType = await dataTypes.ReadNextNbtAsync(packetData); // Dimension Type: NBT Tag Compound
else
await dataTypes.SkipNextStringAsync(packetData);
currentDimension = 0;
}
2022-09-02 09:29:24 +08:00
else if (protocolVersion >= MC_1_9_1_Version)
currentDimension = await dataTypes.ReadNextIntAsync(packetData);
else
currentDimension = (sbyte)await dataTypes.ReadNextByteAsync(packetData);
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_14_Version)
await dataTypes.ReadNextByteAsync(packetData); // Difficulty - 1.13 and below
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
2022-08-28 22:27:21 +08:00
{
string dimensionName = await dataTypes.ReadNextStringAsync(packetData); // Dimension Name (World Name) - 1.16 and above
2022-09-07 00:02:09 +08:00
if (handler.GetTerrainEnabled())
{
2022-09-15 12:48:38 +08:00
if (protocolVersion >= MC_1_16_2_Version && protocolVersion <= MC_1_18_2_Version)
{
2022-09-07 00:08:31 +08:00
World.StoreOneDimension(dimensionName, dimensionType!);
2022-09-15 12:48:38 +08:00
World.SetDimension(dimensionName);
}
else if (protocolVersion >= MC_1_19_Version)
{
World.SetDimension(dimensionTypeName!);
}
2022-09-07 00:02:09 +08:00
}
2022-08-28 22:27:21 +08:00
}
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_15_Version)
await dataTypes.SkipNextLongAsync(packetData); // Hashed world seed - 1.15 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_2_Version)
await dataTypes.SkipNextVarIntAsync(packetData); // Max Players - 1.16.2 and above
2021-12-11 21:06:17 +08:00
else
await dataTypes.ReadNextByteAsync(packetData); // Max Players - 1.16.1 and below
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_16_Version)
await dataTypes.SkipNextStringAsync(packetData); // Level Type - 1.15 and below
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_14_Version)
await dataTypes.SkipNextVarIntAsync(packetData); // View distance - 1.14 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_18_1_Version)
await dataTypes.SkipNextVarIntAsync(packetData); // Simulation Distance - 1.18 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
await dataTypes.ReadNextBoolAsync(packetData); // Reduced debug info - 1.8 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_15_Version)
await dataTypes.ReadNextBoolAsync(packetData); // Enable respawn screen - 1.15 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
2020-06-29 21:52:17 +02:00
{
await dataTypes.ReadNextBoolAsync(packetData); // Is Debug - 1.16 and above
await dataTypes.ReadNextBoolAsync(packetData); // Is Flat - 1.16 and above
2020-06-29 21:52:17 +02:00
}
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
{
bool hasDeathLocation = await dataTypes.ReadNextBoolAsync(packetData); // Has death location
if (hasDeathLocation)
{
await dataTypes.SkipNextStringAsync(packetData); // Death dimension name: Identifier
await dataTypes.SkipNextLocationAsync(packetData); // Death location
}
}
await OnGameJoinedTask;
await OnGamemodeUpdateTask;
break;
2022-10-14 20:34:45 +08:00
case PacketTypesIn.DeclareCommands:
if (protocolVersion >= MC_1_19_Version)
await DeclareCommands.Read(dataTypes, packetData);
2022-10-14 20:34:45 +08:00
break;
case PacketTypesIn.ChatMessage:
int messageType = 0;
2020-06-29 21:54:12 +02:00
2022-09-02 09:29:24 +08:00
if (protocolVersion <= MC_1_18_2_Version) // 1.18 and bellow
{
string message = await dataTypes.ReadNextStringAsync(packetData);
Guid senderUUID;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
{
//Hide system messages or xp bar messages?
messageType = await dataTypes.ReadNextByteAsync(packetData);
if (messageType == 1 && !Config.Main.Advanced.ShowSystemMessages
|| messageType == 2 && !Config.Main.Advanced.ShowSystemMessages)
break;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_5_Version)
senderUUID = await dataTypes.ReadNextUUIDAsync(packetData);
else senderUUID = Guid.Empty;
}
else
senderUUID = Guid.Empty;
handler.OnTextReceived(new(message, true, messageType, senderUUID));
}
2022-08-28 18:39:59 +08:00
else if (protocolVersion == MC_1_19_Version) // 1.19
2020-06-29 21:52:17 +02:00
{
string signedChat = await dataTypes.ReadNextStringAsync(packetData);
bool hasUnsignedChatContent = await dataTypes.ReadNextBoolAsync(packetData);
string? unsignedChatContent = hasUnsignedChatContent ? await dataTypes.ReadNextStringAsync(packetData) : null;
messageType = await dataTypes.ReadNextVarIntAsync(packetData);
if (messageType == 1 && !Config.Main.Advanced.ShowSystemMessages
|| messageType == 2 && !Config.Main.Advanced.ShowXPBarMessages)
break;
Guid senderUUID = await dataTypes.ReadNextUUIDAsync(packetData);
string senderDisplayName = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
bool hasSenderTeamName = await dataTypes.ReadNextBoolAsync(packetData);
string? senderTeamName = hasSenderTeamName ? ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData)) : null;
long timestamp = await dataTypes.ReadNextLongAsync(packetData);
long salt = await dataTypes.ReadNextLongAsync(packetData);
byte[] messageSignature = await dataTypes.ReadNextByteArrayAsync(packetData);
2022-08-27 02:10:44 +08:00
bool verifyResult;
2022-09-02 20:22:33 +08:00
if (!isOnlineMode)
verifyResult = false;
else if (senderUUID == handler.GetUserUuid())
2022-08-27 02:10:44 +08:00
verifyResult = true;
else
{
PlayerInfo? player = handler.GetPlayerInfo(senderUUID);
verifyResult = player != null && player.VerifyMessage(signedChat, timestamp, salt, ref messageSignature);
2022-08-27 02:10:44 +08:00
}
2022-09-03 13:39:45 +08:00
ChatMessage chat = new(signedChat, true, messageType, senderUUID, unsignedChatContent, senderDisplayName, senderTeamName, timestamp, messageSignature, verifyResult);
handler.OnTextReceived(chat);
}
2022-08-24 12:37:22 +08:00
else // 1.19.1 +
{
byte[]? precedingSignature = await dataTypes.ReadNextBoolAsync(packetData) ? await dataTypes.ReadNextByteArrayAsync(packetData) : null;
Guid senderUUID = await dataTypes.ReadNextUUIDAsync(packetData);
byte[] headerSignature = await dataTypes.ReadNextByteArrayAsync(packetData);
2022-08-27 02:10:44 +08:00
string signedChat = await dataTypes.ReadNextStringAsync(packetData);
string? decorated = await dataTypes.ReadNextBoolAsync(packetData) ? await dataTypes.ReadNextStringAsync(packetData) : null;
long timestamp = await dataTypes.ReadNextLongAsync(packetData);
long salt = await dataTypes.ReadNextLongAsync(packetData);
2022-08-27 02:10:44 +08:00
int lastSeenMessageListLen = await dataTypes.ReadNextVarIntAsync(packetData);
2022-08-27 02:10:44 +08:00
LastSeenMessageList.Entry[] lastSeenMessageList = new LastSeenMessageList.Entry[lastSeenMessageListLen];
for (int i = 0; i < lastSeenMessageListLen; ++i)
{
Guid user = await dataTypes.ReadNextUUIDAsync(packetData);
byte[] lastSignature = await dataTypes.ReadNextByteArrayAsync(packetData);
2022-08-27 02:10:44 +08:00
lastSeenMessageList[i] = new(user, lastSignature);
}
LastSeenMessageList lastSeenMessages = new(lastSeenMessageList);
string? unsignedChatContent = await dataTypes.ReadNextBoolAsync(packetData) ? await dataTypes.ReadNextStringAsync(packetData) : null;
2022-08-27 02:10:44 +08:00
int filterEnum = await dataTypes.ReadNextVarIntAsync(packetData);
2022-08-27 02:10:44 +08:00
if (filterEnum == 2) // PARTIALLY_FILTERED
await dataTypes.SkipNextULongArray(packetData);
2022-08-27 02:10:44 +08:00
int chatTypeId = await dataTypes.ReadNextVarIntAsync(packetData);
string chatName = await dataTypes.ReadNextStringAsync(packetData);
string? targetName = await dataTypes.ReadNextBoolAsync(packetData) ? await dataTypes.ReadNextStringAsync(packetData) : null;
2022-08-27 02:10:44 +08:00
2022-09-02 20:22:33 +08:00
Dictionary<string, Json.JSONData> chatInfo = Json.ParseJson(chatName).Properties;
string senderDisplayName = (chatInfo.TryGetValue("insertion", out Json.JSONData? insertion) ? insertion : chatInfo["text"]).StringValue;
2022-09-02 20:22:33 +08:00
string? senderTeamName = null;
2022-09-03 13:39:45 +08:00
ChatParser.MessageType messageTypeEnum = ChatParser.ChatId2Type!.GetValueOrDefault(chatTypeId, ChatParser.MessageType.CHAT);
2022-09-02 21:38:43 +08:00
if (targetName != null &&
2022-09-02 20:22:33 +08:00
(messageTypeEnum == ChatParser.MessageType.TEAM_MSG_COMMAND_INCOMING || messageTypeEnum == ChatParser.MessageType.TEAM_MSG_COMMAND_OUTGOING))
senderTeamName = Json.ParseJson(targetName).Properties["with"].DataArray[0].Properties["text"].StringValue;
2022-10-14 10:33:09 +08:00
if (string.IsNullOrWhiteSpace(senderDisplayName))
{
PlayerInfo? player = handler.GetPlayerInfo(senderUUID);
if (player != null && (player.DisplayName != null || player.Name != null) && string.IsNullOrWhiteSpace(senderDisplayName))
{
senderDisplayName = ChatParser.ParseText(player.DisplayName ?? player.Name);
if (string.IsNullOrWhiteSpace(senderDisplayName))
senderDisplayName = player.DisplayName ?? player.Name;
else
senderDisplayName += "§r";
}
}
2022-08-27 02:10:44 +08:00
bool verifyResult;
2022-09-02 20:22:33 +08:00
if (!isOnlineMode)
verifyResult = false;
else if (senderUUID == handler.GetUserUuid())
2022-08-27 02:10:44 +08:00
verifyResult = true;
else
{
PlayerInfo? player = handler.GetPlayerInfo(senderUUID);
2022-08-27 02:32:59 +08:00
if (player == null || !player.IsMessageChainLegal())
2022-08-27 02:10:44 +08:00
verifyResult = false;
else
{
bool lastVerifyResult = player.IsMessageChainLegal();
verifyResult = player.VerifyMessage(signedChat, timestamp, salt, ref headerSignature, ref precedingSignature, lastSeenMessages);
if (lastVerifyResult && !verifyResult)
log.Warn(string.Format(Translations.chat_message_chain_broken, senderDisplayName));
2022-08-27 02:10:44 +08:00
}
}
ChatMessage chat = new(signedChat, false, chatTypeId, senderUUID, unsignedChatContent, senderDisplayName, senderTeamName, timestamp, headerSignature, verifyResult);
if (isOnlineMode && !chat.LacksSender())
await Acknowledge(chat);
2022-08-27 02:10:44 +08:00
handler.OnTextReceived(chat);
}
break;
case PacketTypesIn.CombatEvent:
// 1.8 - 1.16.5
if (protocolVersion >= MC_1_8_Version && protocolVersion <= MC_1_16_5_Version)
{
CombatEventType eventType = (CombatEventType)await dataTypes.ReadNextVarIntAsync(packetData);
if (eventType == CombatEventType.EntityDead)
{
await dataTypes.SkipNextVarIntAsync(packetData);
handler.OnPlayerKilled(
await dataTypes.ReadNextIntAsync(packetData),
ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData))
);
}
}
break;
case PacketTypesIn.DeathCombatEvent:
await dataTypes.SkipNextVarIntAsync(packetData);
handler.OnPlayerKilled(
await dataTypes.ReadNextIntAsync(packetData),
ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData))
);
2022-08-27 02:10:44 +08:00
break;
case PacketTypesIn.MessageHeader:
2022-08-28 18:39:59 +08:00
if (protocolVersion >= MC_1_19_2_Version)
2022-08-27 02:10:44 +08:00
{
byte[]? precedingSignature = await dataTypes.ReadNextBoolAsync(packetData) ? await dataTypes.ReadNextByteArrayAsync(packetData) : null;
Guid senderUUID = await dataTypes.ReadNextUUIDAsync(packetData);
byte[] headerSignature = await dataTypes.ReadNextByteArrayAsync(packetData);
byte[] bodyDigest = await dataTypes.ReadNextByteArrayAsync(packetData);
2022-08-27 02:10:44 +08:00
bool verifyResult;
2022-09-18 01:15:57 +02:00
2022-09-02 20:22:33 +08:00
if (!isOnlineMode)
verifyResult = false;
else if (senderUUID == handler.GetUserUuid())
2022-08-27 02:10:44 +08:00
verifyResult = true;
else
{
PlayerInfo? player = handler.GetPlayerInfo(senderUUID);
2022-09-18 01:15:57 +02:00
2022-08-27 02:32:59 +08:00
if (player == null || !player.IsMessageChainLegal())
2022-08-27 02:10:44 +08:00
verifyResult = false;
else
{
bool lastVerifyResult = player.IsMessageChainLegal();
verifyResult = player.VerifyMessageHead(ref precedingSignature, ref headerSignature, ref bodyDigest);
if (lastVerifyResult && !verifyResult)
log.Warn(string.Format(Translations.chat_message_chain_broken, player.Name));
2022-08-27 02:10:44 +08:00
}
}
}
break;
case PacketTypesIn.Respawn:
2022-09-15 12:54:43 +08:00
string? dimensionTypeNameRespawn = null;
2022-09-07 00:02:09 +08:00
Dictionary<string, object>? dimensionTypeRespawn = null;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
{
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
dimensionTypeNameRespawn = await dataTypes.ReadNextStringAsync(packetData); // Dimension Type: Identifier
2022-09-02 09:29:24 +08:00
else if (protocolVersion >= MC_1_16_2_Version)
dimensionTypeRespawn = await dataTypes.ReadNextNbtAsync(packetData); // Dimension Type: NBT Tag Compound
else
await dataTypes.SkipNextStringAsync(packetData);
currentDimension = 0;
2020-06-29 21:52:17 +02:00
}
2020-06-29 21:54:12 +02:00
else
2022-08-28 22:27:21 +08:00
{ // 1.15 and below
currentDimension = await dataTypes.ReadNextIntAsync(packetData);
}
2022-08-28 22:27:21 +08:00
if (protocolVersion >= MC_1_16_Version)
{
string dimensionName = await dataTypes.ReadNextStringAsync(packetData); // Dimension Name (World Name) - 1.16 and above
2022-09-07 00:02:09 +08:00
if (handler.GetTerrainEnabled())
{
2022-09-15 12:54:43 +08:00
if (protocolVersion >= MC_1_16_2_Version && protocolVersion <= MC_1_18_2_Version)
{
2022-09-07 00:08:31 +08:00
World.StoreOneDimension(dimensionName, dimensionTypeRespawn!);
2022-09-15 12:54:43 +08:00
World.SetDimension(dimensionName);
}
else if (protocolVersion >= MC_1_19_Version)
{
World.SetDimension(dimensionTypeNameRespawn!);
}
2022-09-07 00:02:09 +08:00
}
2022-08-28 22:27:21 +08:00
}
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_14_Version)
await dataTypes.ReadNextByteAsync(packetData); // Difficulty - 1.13 and below
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_15_Version)
await dataTypes.SkipNextLongAsync(packetData); // Hashed world seed - 1.15 and above
await dataTypes.ReadNextByteAsync(packetData); // Gamemode
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
await dataTypes.ReadNextByteAsync(packetData); // Previous Game mode - 1.16 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_16_Version)
await dataTypes.SkipNextStringAsync(packetData); // Level Type - 1.15 and below
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
{
await dataTypes.ReadNextBoolAsync(packetData); // Is Debug - 1.16 and above
await dataTypes.ReadNextBoolAsync(packetData); // Is Flat - 1.16 and above
await dataTypes.ReadNextBoolAsync(packetData); // Copy metadata - 1.16 and above
}
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
{
bool hasDeathLocation = await dataTypes.ReadNextBoolAsync(packetData); // Has death location
if (hasDeathLocation)
{
await dataTypes.SkipNextStringAsync(packetData); // Death dimension name: Identifier
await dataTypes.SkipNextLocationAsync(packetData); // Death location
}
}
handler.OnRespawn();
break;
case PacketTypesIn.PlayerPositionAndLook:
{
2022-10-16 18:42:11 +08:00
// These always need to be read, since we need the field after them for teleport confirm
double x = await dataTypes.ReadNextDoubleAsync(packetData);
double y = await dataTypes.ReadNextDoubleAsync(packetData);
double z = await dataTypes.ReadNextDoubleAsync(packetData);
2022-10-16 18:42:11 +08:00
Location location = new(x, y, z);
float yaw = await dataTypes.ReadNextFloatAsync(packetData);
float pitch = await dataTypes.ReadNextFloatAsync(packetData);
byte locMask = await dataTypes.ReadNextByteAsync(packetData);
2022-10-16 18:42:11 +08:00
// entity handling require player pos for distance calculating
if (handler.GetTerrainEnabled() || handler.GetEntityHandlingEnabled())
{
if (protocolVersion >= MC_1_8_Version)
{
Location current = handler.GetCurrentLocation();
location.X = (locMask & 1 << 0) != 0 ? current.X + x : x;
location.Y = (locMask & 1 << 1) != 0 ? current.Y + y : y;
location.Z = (locMask & 1 << 2) != 0 ? current.Z + z : z;
}
}
if (protocolVersion >= MC_1_9_Version)
{
int teleportID = await dataTypes.ReadNextVarIntAsync(packetData);
2022-10-16 18:42:11 +08:00
if (teleportID < 0) { yaw = LastYaw; pitch = LastPitch; }
else { LastYaw = yaw; LastPitch = pitch; }
handler.UpdateLocation(location, yaw, pitch);
2022-10-16 18:42:11 +08:00
// Teleport confirm packet
await SendPacket(PacketTypesOut.TeleportConfirm, dataTypes.GetVarInt(teleportID));
2022-11-01 16:37:50 +08:00
if (Config.Main.Advanced.TemporaryFixBadpacket)
{
await SendLocationUpdate(location, true, yaw, pitch, true);
2022-11-01 16:37:50 +08:00
if (teleportID == 1)
await SendLocationUpdate(location, true, yaw, pitch, true);
2022-11-01 16:37:50 +08:00
}
2022-10-16 18:42:11 +08:00
}
else
{
handler.UpdateLocation(location, yaw, pitch);
LastYaw = yaw; LastPitch = pitch;
}
2022-10-16 18:42:11 +08:00
if (protocolVersion >= MC_1_17_Version)
await dataTypes.ReadNextBoolAsync(packetData); // Dismount Vehicle - 1.17 and above
}
break;
case PacketTypesIn.ChunkData:
if (handler.GetTerrainEnabled())
{
Interlocked.Increment(ref handler.GetWorld().chunkCnt);
Interlocked.Increment(ref handler.GetWorld().chunkLoadNotCompleted);
int chunkX = await dataTypes.ReadNextIntAsync(packetData);
int chunkZ = await dataTypes.ReadNextIntAsync(packetData);
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_17_Version)
{
ulong[]? verticalStripBitmask = null;
2022-09-02 09:29:24 +08:00
if (protocolVersion == MC_1_17_Version || protocolVersion == MC_1_17_1_Version)
verticalStripBitmask = await dataTypes.ReadNextULongArrayAsync(packetData); // Bit Mask Length and Primary Bit Mask
await dataTypes.ReadNextNbtAsync(packetData); // Heightmaps
2022-09-02 09:29:24 +08:00
if (protocolVersion == MC_1_17_Version || protocolVersion == MC_1_17_1_Version)
{
int biomesLength = await dataTypes.ReadNextVarIntAsync(packetData); // Biomes length
for (int i = 0; i < biomesLength; i++)
await dataTypes.SkipNextVarIntAsync(packetData); // Biomes
}
int dataSize = await dataTypes.ReadNextVarIntAsync(packetData); // Size
await pTerrain.ProcessChunkColumnData(chunkX, chunkZ, verticalStripBitmask, packetData);
2022-08-30 19:09:07 +08:00
Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
// Block Entity data: ignored
// Light data: ignored
}
else
{
bool chunksContinuous = await dataTypes.ReadNextBoolAsync(packetData);
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version && protocolVersion <= MC_1_16_1_Version)
await dataTypes.ReadNextBoolAsync(packetData); // Ignore old data - 1.16 to 1.16.1 only
2022-09-02 09:29:24 +08:00
ushort chunkMask = protocolVersion >= MC_1_9_Version
? (ushort)await dataTypes.ReadNextVarIntAsync(packetData)
: await dataTypes.ReadNextUShortAsync(packetData);
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
{
ushort addBitmap = await dataTypes.ReadNextUShortAsync(packetData);
int compressedDataSize = await dataTypes.ReadNextIntAsync(packetData);
byte[] compressed = await dataTypes.ReadDataAsync(compressedDataSize, packetData);
ZLibStream zlibStream = new(new MemoryStream(compressed, false), CompressionMode.Decompress, leaveOpen: false);
PacketStream chunkDataStream = new(zlibStream, compressedDataSize);
await pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, currentDimension == 0, chunksContinuous, currentDimension, chunkDataStream);
await chunkDataStream.DisposeAsync();
2022-08-30 19:09:07 +08:00
Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
}
else
{
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_14_Version)
await dataTypes.ReadNextNbtAsync(packetData); // Heightmaps - 1.14 and above
int biomesLength = 0;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_2_Version)
if (chunksContinuous)
biomesLength = await dataTypes.ReadNextVarIntAsync(packetData); // Biomes length - 1.16.2 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_15_Version && chunksContinuous)
{
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_2_Version)
{
for (int i = 0; i < biomesLength; i++)
{
// Biomes - 1.16.2 and above
// Don't use ReadNextVarInt because it cost too much time
await dataTypes.SkipNextVarIntAsync(packetData);
}
}
else
await dataTypes.DropDataAsync(1024 * 4, packetData); // Biomes - 1.15 and above
}
int dataSize = await dataTypes.ReadNextVarIntAsync(packetData);
2022-08-29 23:39:03 +08:00
await pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData);
2022-08-30 19:09:07 +08:00
Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
}
}
2020-06-29 21:52:17 +02:00
}
break;
case PacketTypesIn.MapData:
if (protocolVersion < MC_1_8_Version)
break;
int mapid = await dataTypes.ReadNextVarIntAsync(packetData);
byte scale = await dataTypes.ReadNextByteAsync(packetData);
// 1.9 +
bool trackingPosition = true;
// 1.14+
bool locked = false;
// 1.17+ (locked and trackingPosition switched places)
if (protocolVersion >= MC_1_17_Version)
{
if (protocolVersion >= MC_1_14_Version)
locked = await dataTypes.ReadNextBoolAsync(packetData);
if (protocolVersion >= MC_1_9_Version)
trackingPosition = await dataTypes.ReadNextBoolAsync(packetData);
}
else
{
if (protocolVersion >= MC_1_9_Version)
trackingPosition = await dataTypes.ReadNextBoolAsync(packetData);
if (protocolVersion >= MC_1_14_Version)
locked = await dataTypes.ReadNextBoolAsync(packetData);
}
2022-09-18 00:18:27 +02:00
int iconcount = 0;
List<MapIcon> icons = new();
// 1,9 + = needs tracking position to be true to get the icons
2022-10-04 12:00:10 +08:00
if (protocolVersion <= MC_1_16_5_Version || trackingPosition)
2022-09-18 00:18:27 +02:00
{
iconcount = await dataTypes.ReadNextVarIntAsync(packetData);
2022-09-18 00:18:27 +02:00
for (int i = 0; i < iconcount; i++)
{
MapIcon mapIcon = new();
// 1.8 - 1.13
if (protocolVersion < MC_1_13_2_Version)
{
byte directionAndtype = await dataTypes.ReadNextByteAsync(packetData);
byte direction, type;
// 1.12.2+
if (protocolVersion >= MC_1_12_2_Version)
{
direction = (byte)(directionAndtype & 0xF);
type = (byte)(directionAndtype >> 4 & 0xF);
}
else // 1.8 - 1.12
{
direction = (byte)(directionAndtype >> 4 & 0xF);
type = (byte)(directionAndtype & 0xF);
}
mapIcon.Type = (MapIconType)type;
mapIcon.Direction = direction;
}
// 1.13.2+
if (protocolVersion >= MC_1_13_2_Version)
mapIcon.Type = (MapIconType)await dataTypes.ReadNextVarIntAsync(packetData);
mapIcon.X = await dataTypes.ReadNextByteAsync(packetData);
mapIcon.Z = await dataTypes.ReadNextByteAsync(packetData);
2022-09-18 00:18:27 +02:00
// 1.13.2+
if (protocolVersion >= MC_1_13_2_Version)
{
mapIcon.Direction = await dataTypes.ReadNextByteAsync(packetData);
if (await dataTypes.ReadNextBoolAsync(packetData)) // Has Display Name?
mapIcon.DisplayName = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
}
2022-09-18 00:18:27 +02:00
icons.Add(mapIcon);
}
}
byte columnsUpdated = await dataTypes.ReadNextByteAsync(packetData); // width
2022-09-18 00:18:27 +02:00
byte rowsUpdated = 0; // height
byte mapCoulmnX = 0;
byte mapRowZ = 0;
byte[]? colors = null;
if (columnsUpdated > 0)
{
rowsUpdated = await dataTypes.ReadNextByteAsync(packetData); // height
mapCoulmnX = await dataTypes.ReadNextByteAsync(packetData);
mapRowZ = await dataTypes.ReadNextByteAsync(packetData);
colors = await dataTypes.ReadNextByteArrayAsync(packetData);
}
2022-09-18 00:18:27 +02:00
handler.OnMapData(mapid, scale, trackingPosition, locked, icons, columnsUpdated, rowsUpdated, mapCoulmnX, mapRowZ, colors);
break;
case PacketTypesIn.TradeList:
if (protocolVersion >= MC_1_14_Version && handler.GetInventoryEnabled())
2020-06-29 21:52:17 +02:00
{
// MC 1.14 or greater
int windowID = await dataTypes.ReadNextVarIntAsync(packetData);
int size = await dataTypes.ReadNextByteAsync(packetData);
List<VillagerTrade> trades = new();
for (int tradeId = 0; tradeId < size; tradeId++)
2020-06-30 23:05:00 +02:00
{
VillagerTrade trade = await dataTypes.ReadNextTradeAsync(packetData, itemPalette);
trades.Add(trade);
2020-06-30 23:05:00 +02:00
}
VillagerInfo villagerInfo = new()
2020-06-30 23:05:00 +02:00
{
Level = await dataTypes.ReadNextVarIntAsync(packetData),
Experience = await dataTypes.ReadNextVarIntAsync(packetData),
IsRegularVillager = await dataTypes.ReadNextBoolAsync(packetData),
CanRestock = await dataTypes.ReadNextBoolAsync(packetData)
};
handler.OnTradeList(windowID, trades, villagerInfo);
}
break;
case PacketTypesIn.Title:
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
{
int action2 = await dataTypes.ReadNextVarIntAsync(packetData);
string titletext = string.Empty;
string subtitletext = string.Empty;
string actionbartext = string.Empty;
string json = string.Empty;
int fadein = -1;
int stay = -1;
int fadeout = -1;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_10_Version)
2020-06-30 23:05:00 +02:00
{
if (action2 == 0)
{
json = titletext;
titletext = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
}
else if (action2 == 1)
{
json = subtitletext;
subtitletext = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
}
else if (action2 == 2)
{
json = actionbartext;
actionbartext = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
}
else if (action2 == 3)
{
fadein = await dataTypes.ReadNextIntAsync(packetData);
stay = await dataTypes.ReadNextIntAsync(packetData);
fadeout = await dataTypes.ReadNextIntAsync(packetData);
}
2020-06-30 23:05:00 +02:00
}
else
2020-06-30 23:05:00 +02:00
{
if (action2 == 0)
{
json = titletext;
titletext = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
}
else if (action2 == 1)
{
json = subtitletext;
subtitletext = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
}
else if (action2 == 2)
{
fadein = await dataTypes.ReadNextIntAsync(packetData);
stay = await dataTypes.ReadNextIntAsync(packetData);
fadeout = await dataTypes.ReadNextIntAsync(packetData);
}
2020-06-30 23:05:00 +02:00
}
handler.OnTitle(action2, titletext, subtitletext, actionbartext, fadein, stay, fadeout, json);
2020-06-29 21:52:17 +02:00
}
break;
case PacketTypesIn.MultiBlockChange:
if (handler.GetTerrainEnabled())
2020-06-29 21:52:17 +02:00
{
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_2_Version)
2020-06-29 21:52:17 +02:00
{
long chunkSection = await dataTypes.ReadNextLongAsync(packetData);
int sectionX = (int)(chunkSection >> 42);
int sectionY = (int)(chunkSection << 44 >> 44);
int sectionZ = (int)(chunkSection << 22 >> 42);
await dataTypes.ReadNextBoolAsync(packetData); // Useless boolean (Related to light update)
int blocksSize = await dataTypes.ReadNextVarIntAsync(packetData);
for (int i = 0; i < blocksSize; i++)
{
2022-10-08 17:56:32 +08:00
ulong chunkSectionPosition = (ulong)dataTypes.ReadNextVarLong(packetData);
int blockId = (int)(chunkSectionPosition >> 12);
int localX = (int)(chunkSectionPosition >> 8 & 0x0F);
int localZ = (int)(chunkSectionPosition >> 4 & 0x0F);
2022-10-08 17:56:32 +08:00
int localY = (int)(chunkSectionPosition & 0x0F);
2022-10-08 17:56:32 +08:00
Block block = new((ushort)blockId);
int blockX = sectionX * 16 + localX;
int blockY = sectionY * 16 + localY;
int blockZ = sectionZ * 16 + localZ;
2022-10-08 17:56:32 +08:00
Location location = new(blockX, blockY, blockZ);
handler.OnBlockChange(location, block);
}
2020-06-29 21:54:12 +02:00
}
else
{
int chunkX = await dataTypes.ReadNextIntAsync(packetData);
int chunkZ = await dataTypes.ReadNextIntAsync(packetData);
2022-09-02 09:29:24 +08:00
int recordCount = protocolVersion < MC_1_8_Version
? await dataTypes.ReadNextShortAsync(packetData)
: await dataTypes.ReadNextVarIntAsync(packetData);
for (int i = 0; i < recordCount; i++)
{
byte locationXZ;
ushort blockIdMeta;
int blockY;
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
{
blockIdMeta = await dataTypes.ReadNextUShortAsync(packetData);
blockY = await dataTypes.ReadNextByteAsync(packetData);
locationXZ = await dataTypes.ReadNextByteAsync(packetData);
}
else
{
locationXZ = await dataTypes.ReadNextByteAsync(packetData);
blockY = await dataTypes.ReadNextByteAsync(packetData);
blockIdMeta = (ushort)await dataTypes.ReadNextVarIntAsync(packetData);
}
int blockX = locationXZ >> 4;
int blockZ = locationXZ & 0x0F;
2022-10-08 17:56:32 +08:00
Location location = new(chunkX, chunkZ, blockX, blockY, blockZ);
Block block = new(blockIdMeta);
2022-10-08 17:56:32 +08:00
handler.OnBlockChange(location, block);
}
}
}
break;
case PacketTypesIn.ServerData:
string motd = "-";
bool hasMotd = await dataTypes.ReadNextBoolAsync(packetData);
if (hasMotd)
motd = ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData));
string iconBase64 = "-";
bool hasIcon = await dataTypes.ReadNextBoolAsync(packetData);
if (hasIcon)
iconBase64 = await dataTypes.ReadNextStringAsync(packetData);
bool previewsChat = await dataTypes.ReadNextBoolAsync(packetData);
handler.OnServerDataRecived(hasMotd, motd, hasIcon, iconBase64, previewsChat);
break;
case PacketTypesIn.BlockChange:
if (handler.GetTerrainEnabled())
{
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
2020-06-30 23:05:00 +02:00
{
int blockX = await dataTypes.ReadNextIntAsync(packetData);
int blockY = await dataTypes.ReadNextByteAsync(packetData);
int blockZ = await dataTypes.ReadNextIntAsync(packetData);
short blockId = (short)await dataTypes.ReadNextVarIntAsync(packetData);
byte blockMeta = await dataTypes.ReadNextByteAsync(packetData);
2022-10-08 17:56:32 +08:00
Location location = new(blockX, blockY, blockZ);
Block block = new(blockId, blockMeta);
handler.OnBlockChange(location, block);
2020-06-30 23:05:00 +02:00
}
else
2020-06-29 21:54:12 +02:00
{
Location location = await dataTypes.ReadNextLocationAsync(packetData);
Block block = new((ushort)await dataTypes.ReadNextVarIntAsync(packetData));
2022-10-08 17:56:32 +08:00
handler.OnBlockChange(location, block);
2020-06-29 21:52:17 +02:00
}
}
break;
case PacketTypesIn.SetDisplayChatPreview:
bool previewsChatSetting = await dataTypes.ReadNextBoolAsync(packetData);
handler.OnChatPreviewSettingUpdate(previewsChatSetting);
break;
case PacketTypesIn.ChatPreview:
int queryID = await dataTypes.ReadNextIntAsync(packetData);
bool componentIsPresent = await dataTypes.ReadNextBoolAsync(packetData);
// Currently noy implemented
log.Debug("New chat preview: ");
log.Debug(">> Query ID: " + queryID);
log.Debug(">> Component is present: " + componentIsPresent);
if (componentIsPresent)
2020-06-29 21:52:17 +02:00
{
string message = await dataTypes.ReadNextStringAsync(packetData);
log.Debug(">> Component: " + ChatParser.ParseText(message));
//handler.OnTextReceived(message, true);
}
2022-08-27 02:10:44 +08:00
break;
case PacketTypesIn.ChatSuggestions:
break;
case PacketTypesIn.MapChunkBulk:
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_9_Version && handler.GetTerrainEnabled())
{
int chunkCount;
bool hasSkyLight;
bool needDispose = false;
PacketStream chunkDataStream = packetData;
//Read global fields
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
2020-06-29 21:52:17 +02:00
{
chunkCount = await dataTypes.ReadNextShortAsync(packetData);
int compressedDataSize = await dataTypes.ReadNextIntAsync(packetData);
hasSkyLight = await dataTypes.ReadNextBoolAsync(packetData);
byte[] compressed = await dataTypes.ReadDataAsync(compressedDataSize, packetData);
ZLibStream zlibStream = new(new MemoryStream(compressed, false), CompressionMode.Decompress, leaveOpen: false);
chunkDataStream = new PacketStream(zlibStream, compressedDataSize);
needDispose = true;
2020-06-29 21:52:17 +02:00
}
else
{
hasSkyLight = await dataTypes.ReadNextBoolAsync(packetData);
chunkCount = await dataTypes.ReadNextVarIntAsync(packetData);
}
//Read chunk records
int[] chunkXs = new int[chunkCount];
int[] chunkZs = new int[chunkCount];
ushort[] chunkMasks = new ushort[chunkCount];
ushort[] addBitmaps = new ushort[chunkCount];
for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++)
{
chunkXs[chunkColumnNo] = await dataTypes.ReadNextIntAsync(packetData);
chunkZs[chunkColumnNo] = await dataTypes.ReadNextIntAsync(packetData);
chunkMasks[chunkColumnNo] = await dataTypes.ReadNextUShortAsync(packetData);
2022-09-02 09:29:24 +08:00
addBitmaps[chunkColumnNo] = protocolVersion < MC_1_8_Version
? await dataTypes.ReadNextUShortAsync(packetData)
: (ushort)0;
}
//Process chunk records
2022-08-30 19:09:07 +08:00
for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++)
{
await pTerrain.ProcessChunkColumnData(chunkXs[chunkColumnNo], chunkZs[chunkColumnNo], chunkMasks[chunkColumnNo], addBitmaps[chunkColumnNo], hasSkyLight, true, currentDimension, chunkDataStream);
2022-08-30 19:09:07 +08:00
Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted);
}
if (needDispose)
await chunkDataStream.DisposeAsync();
}
break;
case PacketTypesIn.UnloadChunk:
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_9_Version && handler.GetTerrainEnabled())
{
int chunkX = await dataTypes.ReadNextIntAsync(packetData);
int chunkZ = await dataTypes.ReadNextIntAsync(packetData);
2022-07-25 03:19:24 +08:00
// Warning: It is legal to include unloaded chunks in the UnloadChunk packet.
// Since chunks that have not been loaded are not recorded, this may result
// in loading chunks that should be unloaded and inaccurate statistics.
2022-07-25 03:19:24 +08:00
if (handler.GetWorld()[chunkX, chunkZ] != null)
Interlocked.Decrement(ref handler.GetWorld().chunkCnt);
2022-07-25 03:19:24 +08:00
handler.GetWorld()[chunkX, chunkZ] = null;
}
break;
case PacketTypesIn.ChangeGameState:
if (protocolVersion >= MC_1_15_2_Version)
{
byte reason = await dataTypes.ReadNextByteAsync(packetData);
float state = await dataTypes.ReadNextFloatAsync(packetData);
handler.OnGameEvent(reason, state);
}
break;
case PacketTypesIn.PlayerInfo:
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
{
int action = await dataTypes.ReadNextVarIntAsync(packetData); // Action Name
int numberOfPlayers = await dataTypes.ReadNextVarIntAsync(packetData); // Number Of Players
for (int i = 0; i < numberOfPlayers; i++)
2020-06-29 21:52:17 +02:00
{
Guid uuid = await dataTypes.ReadNextUUIDAsync(packetData); // Player UUID
switch (action)
{
case 0x00: //Player Join (Add player since 1.19)
string name = await dataTypes.ReadNextStringAsync(packetData); // Player name
int propNum = await dataTypes.ReadNextVarIntAsync(packetData); // Number of properties in the following array
2022-08-28 23:34:28 +08:00
// Property: Tuple<Name, Value, Signature(empty if there is no signature)
// The Property field looks as in the response of https://wiki.vg/Mojang_API#UUID_to_Profile_and_Skin.2FCape
const bool useProperty = false;
#pragma warning disable CS0162 // Unreachable code detected
2022-09-02 21:38:43 +08:00
Tuple<string, string, string?>[]? properties = useProperty ?
new Tuple<string, string, string?>[propNum] : null;
for (int p = 0; p < propNum; p++)
{
string propertyName = await dataTypes.ReadNextStringAsync(packetData); // Name: String (32767)
string val = await dataTypes.ReadNextStringAsync(packetData); // Value: String (32767)
2022-08-28 23:34:28 +08:00
string? propertySignature = null;
if (await dataTypes.ReadNextBoolAsync(packetData)) // Is Signed
propertySignature = await dataTypes.ReadNextStringAsync(packetData); // Signature: String (32767)
2022-08-28 23:34:28 +08:00
if (useProperty)
properties![p] = new(propertyName, val, propertySignature);
}
#pragma warning restore CS0162 // Unreachable code detected
int gameMode = await dataTypes.ReadNextVarIntAsync(packetData); // Gamemode
await handler.OnGamemodeUpdate(uuid, gameMode);
int ping = await dataTypes.ReadNextVarIntAsync(packetData); // Ping
string? displayName = null;
if (await dataTypes.ReadNextBoolAsync(packetData)) // Has display name
displayName = await dataTypes.ReadNextStringAsync(packetData); // Display name
// 1.19 Additions
long? keyExpiration = null;
byte[]? publicKey = null, signature = null;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
{
if (await dataTypes.ReadNextBoolAsync(packetData)) // Has Sig Data (if true, red the following fields)
{
keyExpiration = await dataTypes.ReadNextLongAsync(packetData); // Timestamp
int publicKeyLength = await dataTypes.ReadNextVarIntAsync(packetData); // Public Key Length
if (publicKeyLength > 0)
publicKey = await dataTypes.ReadDataAsync(publicKeyLength, packetData); // Public key
int signatureLength = await dataTypes.ReadNextVarIntAsync(packetData); // Signature Length
if (signatureLength > 0)
signature = await dataTypes.ReadDataAsync(signatureLength, packetData); // Public key
}
}
2022-08-28 23:34:28 +08:00
handler.OnPlayerJoin(new PlayerInfo(uuid, name, properties, gameMode, ping, displayName, keyExpiration, publicKey, signature));
break;
case 0x01: //Update gamemode
await handler.OnGamemodeUpdate(uuid, await dataTypes.ReadNextVarIntAsync(packetData));
break;
case 0x02: //Update latency
int latency = await dataTypes.ReadNextVarIntAsync(packetData);
handler.OnLatencyUpdate(uuid, latency); //Update latency;
break;
case 0x03: //Update display name
if (await dataTypes.ReadNextBoolAsync(packetData))
2022-08-28 16:18:29 +08:00
{
PlayerInfo? player = handler.GetPlayerInfo(uuid);
if (player != null)
player.DisplayName = await dataTypes.ReadNextStringAsync(packetData);
2022-08-28 16:18:29 +08:00
else
await dataTypes.SkipNextStringAsync(packetData);
2022-08-28 16:18:29 +08:00
}
break;
case 0x04: //Player Leave
handler.OnPlayerLeave(uuid);
break;
default:
//Unknown player list item type
break;
}
}
2020-06-29 21:52:17 +02:00
}
else //MC 1.7.X does not provide UUID in tab-list updates
2020-06-29 21:52:17 +02:00
{
string name = await dataTypes.ReadNextStringAsync(packetData);
bool online = await dataTypes.ReadNextBoolAsync(packetData);
short ping = await dataTypes.ReadNextShortAsync(packetData);
Guid FakeUUID = new(MD5.HashData(Encoding.UTF8.GetBytes(name)).Take(16).ToArray());
if (online)
handler.OnPlayerJoin(new PlayerInfo(name, FakeUUID));
else handler.OnPlayerLeave(FakeUUID);
2020-06-29 21:52:17 +02:00
}
break;
case PacketTypesIn.TabComplete:
int old_transaction_id = autocomplete_transaction_id;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_13_Version)
{
autocomplete_transaction_id = await dataTypes.ReadNextVarIntAsync(packetData);
await dataTypes.SkipNextVarIntAsync(packetData); // Start of text to replace
await dataTypes.SkipNextVarIntAsync(packetData); // Length of text to replace
}
2020-06-29 21:54:12 +02:00
int autocomplete_count = await dataTypes.ReadNextVarIntAsync(packetData);
2020-06-29 21:52:17 +02:00
string[] autocomplete_result = new string[autocomplete_count];
for (int i = 0; i < autocomplete_count; i++)
2020-06-29 21:52:17 +02:00
{
autocomplete_result[i] = await dataTypes.ReadNextStringAsync(packetData);
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_13_Version)
{
// Skip optional tooltip for each tab-complete resul`t
if (await dataTypes.ReadNextBoolAsync(packetData))
await dataTypes.SkipNextStringAsync(packetData);
}
2020-06-29 21:52:17 +02:00
}
handler.OnAutoCompleteDone(old_transaction_id, autocomplete_result);
break;
case PacketTypesIn.PluginMessage:
string channel = await dataTypes.ReadNextStringAsync(packetData);
// Length is unneeded as the whole remaining packetData is the entire payload of the packet.
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
pForge.ReadNextVarShort(packetData);
byte[] pluginMessage = await packetData.ReadFullPacket();
handler.OnPluginChannelMessage(channel, pluginMessage);
(bool handleForgePlusinMsgStatus, currentDimension) = await pForge.HandlePluginMessage(channel, pluginMessage, currentDimension);
return handleForgePlusinMsgStatus;
case PacketTypesIn.Disconnect:
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData)));
return false;
case PacketTypesIn.SetCompression:
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version && protocolVersion < MC_1_9_Version)
socketWrapper.CompressionThreshold = await dataTypes.ReadNextVarIntAsync(packetData);
break;
case PacketTypesIn.OpenWindow:
if (handler.GetInventoryEnabled())
2020-06-29 21:52:17 +02:00
{
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_14_Version)
2020-06-29 21:52:17 +02:00
{
// MC 1.13 or lower
byte windowID = await dataTypes.ReadNextByteAsync(packetData);
string type = (await dataTypes.ReadNextStringAsync(packetData)).Replace("minecraft:", "").ToUpper();
ContainerTypeOld inventoryType = (ContainerTypeOld)Enum.Parse(typeof(ContainerTypeOld), type);
string title = await dataTypes.ReadNextStringAsync(packetData);
byte slots = await dataTypes.ReadNextByteAsync(packetData);
Container inventory = new(windowID, inventoryType, ChatParser.ParseText(title));
handler.OnInventoryOpen(windowID, inventory);
}
else
{
// MC 1.14 or greater
int windowID = await dataTypes.ReadNextVarIntAsync(packetData);
int windowType = await dataTypes.ReadNextVarIntAsync(packetData);
string title = await dataTypes.ReadNextStringAsync(packetData);
Container inventory = new(windowID, windowType, ChatParser.ParseText(title));
handler.OnInventoryOpen(windowID, inventory);
2020-06-29 21:52:17 +02:00
}
}
break;
case PacketTypesIn.CloseWindow:
if (handler.GetInventoryEnabled())
2020-06-29 21:52:17 +02:00
{
byte windowID = await dataTypes.ReadNextByteAsync(packetData);
lock (window_actions) { window_actions[windowID] = 0; }
handler.OnInventoryClose(windowID);
2020-06-29 21:52:17 +02:00
}
break;
case PacketTypesIn.WindowItems:
if (handler.GetInventoryEnabled())
{
byte windowId = await dataTypes.ReadNextByteAsync(packetData);
int stateId = -1;
int elements = 0;
2022-02-13 15:00:32 +01:00
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_17_1_Version)
{
// State ID and Elements as VarInt - 1.17.1 and above
stateId = await dataTypes.ReadNextVarIntAsync(packetData);
elements = await dataTypes.ReadNextVarIntAsync(packetData);
}
else
{
// Elements as Short - 1.17.0 and below
await dataTypes.ReadNextShortAsync(packetData);
}
2022-02-13 15:00:32 +01:00
Dictionary<int, Item> inventorySlots = new();
for (int slotId = 0; slotId < elements; slotId++)
{
Item? item = await dataTypes.ReadNextItemSlotAsync(packetData, itemPalette);
if (item != null)
inventorySlots[slotId] = item;
}
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_17_1_Version) // Carried Item - 1.17.1 and above
await dataTypes.ReadNextItemSlotAsync(packetData, itemPalette);
handler.OnWindowItems(windowId, inventorySlots, stateId);
}
break;
2022-10-12 19:51:01 +02:00
case PacketTypesIn.WindowProperty:
byte containerId = await dataTypes.ReadNextByteAsync(packetData);
short propertyId = await dataTypes.ReadNextShortAsync(packetData);
short propertyValue = await dataTypes.ReadNextShortAsync(packetData);
2022-10-12 19:51:01 +02:00
handler.OnWindowProperties(containerId, propertyId, propertyValue);
break;
case PacketTypesIn.SetSlot:
if (handler.GetInventoryEnabled())
2020-06-29 21:52:17 +02:00
{
byte windowID = await dataTypes.ReadNextByteAsync(packetData);
int stateId = -1;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_17_1_Version)
stateId = await dataTypes.ReadNextVarIntAsync(packetData); // State ID - 1.17.1 and above
short slotID = await dataTypes.ReadNextShortAsync(packetData);
Item? item = await dataTypes.ReadNextItemSlotAsync(packetData, itemPalette);
2022-07-25 01:13:41 +08:00
handler.OnSetSlot(windowID, slotID, item, stateId);
2020-06-29 21:52:17 +02:00
}
break;
case PacketTypesIn.WindowConfirmation:
if (handler.GetInventoryEnabled())
2020-06-29 21:52:17 +02:00
{
byte windowID = await dataTypes.ReadNextByteAsync(packetData);
short actionID = await dataTypes.ReadNextShortAsync(packetData);
bool accepted = await dataTypes.ReadNextBoolAsync(packetData);
2022-10-14 10:33:09 +08:00
if (!accepted)
await SendWindowConfirmation(windowID, actionID, true);
2020-06-29 21:52:17 +02:00
}
break;
case PacketTypesIn.ResourcePackSend:
string url = await dataTypes.ReadNextStringAsync(packetData);
string hash = await dataTypes.ReadNextStringAsync(packetData);
bool forced = true; // Assume forced for MC 1.16 and below
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_17_Version)
2020-06-29 21:52:17 +02:00
{
forced = await dataTypes.ReadNextBoolAsync(packetData);
bool hasPromptMessage = await dataTypes.ReadNextBoolAsync(packetData); // Has Prompt Message (Boolean) - 1.17 and above
if (hasPromptMessage)
await dataTypes.SkipNextStringAsync(packetData); // Prompt Message (Optional Chat) - 1.17 and above
2020-06-29 21:52:17 +02:00
}
// Some server plugins may send invalid resource packs to probe the client and we need to ignore them (issue #1056)
if (!url.StartsWith("http") && hash.Length != 40) // Some server may have null hash value
break;
//Send back "accepted" and "successfully loaded" responses for plugins or server config making use of resource pack mandatory
byte[] responseHeader = Array.Empty<byte>();
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_10_Version) //MC 1.10 does not include resource pack hash in responses
responseHeader = dataTypes.ConcatBytes(dataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash));
await SendPacket(PacketTypesOut.ResourcePackStatus, dataTypes.ConcatBytes(responseHeader, dataTypes.GetVarInt(3))); //Accepted pack
await SendPacket(PacketTypesOut.ResourcePackStatus, dataTypes.ConcatBytes(responseHeader, dataTypes.GetVarInt(0))); //Successfully loaded
break;
case PacketTypesIn.SpawnEntity:
if (handler.GetEntityHandlingEnabled())
2020-07-31 16:34:16 +08:00
{
Entity entity = await dataTypes.ReadNextEntity(packetData, entityPalette, false);
handler.OnSpawnEntity(entity);
2020-07-31 16:34:16 +08:00
}
2020-06-29 21:54:12 +02:00
break;
case PacketTypesIn.EntityEquipment:
if (handler.GetEntityHandlingEnabled())
{
int entityid = await dataTypes.ReadNextVarIntAsync(packetData);
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
{
bool hasNext;
do
{
byte bitsData = await dataTypes.ReadNextByteAsync(packetData);
// Top bit set if another entry follows, and otherwise unset if this is the last item in the array
hasNext = bitsData >> 7 == 1;
int slot2 = bitsData >> 1;
Item? item = await dataTypes.ReadNextItemSlotAsync(packetData, itemPalette);
handler.OnEntityEquipment(entityid, slot2, item);
} while (hasNext);
}
else
{
int slot2 = await dataTypes.ReadNextVarIntAsync(packetData);
Item? item = await dataTypes.ReadNextItemSlotAsync(packetData, itemPalette);
handler.OnEntityEquipment(entityid, slot2, item);
}
}
break;
case PacketTypesIn.SpawnLivingEntity:
if (handler.GetEntityHandlingEnabled())
{
Entity entity = await dataTypes.ReadNextEntity(packetData, entityPalette, true);
// packet before 1.15 has metadata at the end
// this is not handled in dataTypes.ReadNextEntity()
// we are simply ignoring leftover data in packet
handler.OnSpawnEntity(entity);
}
break;
case PacketTypesIn.SpawnPlayer:
if (handler.GetEntityHandlingEnabled())
{
int EntityID = await dataTypes.ReadNextVarIntAsync(packetData);
Guid UUID = await dataTypes.ReadNextUUIDAsync(packetData);
double X = await dataTypes.ReadNextDoubleAsync(packetData);
double Y = await dataTypes.ReadNextDoubleAsync(packetData);
double Z = await dataTypes.ReadNextDoubleAsync(packetData);
byte Yaw = await dataTypes.ReadNextByteAsync(packetData);
byte Pitch = await dataTypes.ReadNextByteAsync(packetData);
2020-06-29 21:54:12 +02:00
Location EntityLocation = new(X, Y, Z);
2020-06-29 21:54:12 +02:00
handler.OnSpawnPlayer(EntityID, UUID, EntityLocation, Yaw, Pitch);
}
break;
case PacketTypesIn.EntityEffect:
if (handler.GetEntityHandlingEnabled())
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
{
int entityid = await dataTypes.ReadNextVarIntAsync(packetData);
Effects effect = Effects.Speed;
2022-09-18 01:15:57 +02:00
int effectId = protocolVersion >= MC_1_18_2_Version ?
await dataTypes.ReadNextVarIntAsync(packetData) : await dataTypes.ReadNextByteAsync(packetData);
2022-09-04 18:12:47 +08:00
if (Enum.TryParse(effectId.ToString(), out effect))
{
int amplifier = await dataTypes.ReadNextByteAsync(packetData);
int duration = await dataTypes.ReadNextVarIntAsync(packetData);
byte flags = await dataTypes.ReadNextByteAsync(packetData);
bool hasFactorData = false;
Dictionary<string, object>? factorCodec = null;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
{
hasFactorData = await dataTypes.ReadNextBoolAsync(packetData);
2022-09-04 18:12:47 +08:00
if (hasFactorData)
factorCodec = await dataTypes.ReadNextNbtAsync(packetData);
}
handler.OnEntityEffect(entityid, effect, amplifier, duration, flags, hasFactorData, factorCodec);
}
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
}
break;
case PacketTypesIn.DestroyEntities:
if (handler.GetEntityHandlingEnabled())
2020-06-29 21:52:17 +02:00
{
int entityCount = 1; // 1.17.0 has only one entity per packet
2022-09-02 09:29:24 +08:00
if (protocolVersion != MC_1_17_Version)
entityCount = await dataTypes.ReadNextVarIntAsync(packetData); // All other versions have a "count" field
int[] entityList = new int[entityCount];
for (int i = 0; i < entityCount; i++)
{
entityList[i] = await dataTypes.ReadNextVarIntAsync(packetData);
}
handler.OnDestroyEntities(entityList);
}
break;
case PacketTypesIn.EntityPosition:
if (handler.GetEntityHandlingEnabled())
{
int EntityID = await dataTypes.ReadNextVarIntAsync(packetData);
double DeltaX = Convert.ToDouble(await dataTypes.ReadNextShortAsync(packetData));
double DeltaY = Convert.ToDouble(await dataTypes.ReadNextShortAsync(packetData));
double DeltaZ = Convert.ToDouble(await dataTypes.ReadNextShortAsync(packetData));
bool OnGround = await dataTypes.ReadNextBoolAsync(packetData);
DeltaX /= 128 * 32;
DeltaY /= 128 * 32;
DeltaZ /= 128 * 32;
handler.OnEntityPosition(EntityID, DeltaX, DeltaY, DeltaZ, OnGround);
}
break;
case PacketTypesIn.EntityPositionAndRotation:
if (handler.GetEntityHandlingEnabled())
2020-06-29 21:52:17 +02:00
{
int EntityID = await dataTypes.ReadNextVarIntAsync(packetData);
double DeltaX = Convert.ToDouble(await dataTypes.ReadNextShortAsync(packetData));
double DeltaY = Convert.ToDouble(await dataTypes.ReadNextShortAsync(packetData));
double DeltaZ = Convert.ToDouble(await dataTypes.ReadNextShortAsync(packetData));
byte _yaw = await dataTypes.ReadNextByteAsync(packetData);
byte _pitch = await dataTypes.ReadNextByteAsync(packetData);
bool OnGround = await dataTypes.ReadNextBoolAsync(packetData);
DeltaX /= 128 * 32;
DeltaY /= 128 * 32;
DeltaZ /= 128 * 32;
handler.OnEntityPosition(EntityID, DeltaX, DeltaY, DeltaZ, OnGround);
}
break;
case PacketTypesIn.EntityProperties:
if (handler.GetEntityHandlingEnabled())
{
int EntityID = await dataTypes.ReadNextVarIntAsync(packetData);
int NumberOfProperties = protocolVersion >= MC_1_17_Version ? await dataTypes.ReadNextVarIntAsync(packetData) : await dataTypes.ReadNextIntAsync(packetData);
Dictionary<string, double> keys = new();
for (int i = 0; i < NumberOfProperties; i++)
2020-06-29 21:52:17 +02:00
{
string _key = await dataTypes.ReadNextStringAsync(packetData);
double _value = await dataTypes.ReadNextDoubleAsync(packetData);
List<double> op0 = new();
List<double> op1 = new();
List<double> op2 = new();
int NumberOfModifiers = await dataTypes.ReadNextVarIntAsync(packetData);
for (int j = 0; j < NumberOfModifiers; j++)
2020-06-29 21:52:17 +02:00
{
await dataTypes.SkipNextUUIDAsync(packetData);
double amount = await dataTypes.ReadNextDoubleAsync(packetData);
byte operation = await dataTypes.ReadNextByteAsync(packetData);
switch (operation)
{
case 0: op0.Add(amount); break;
case 1: op1.Add(amount); break;
case 2: op2.Add(amount + 1); break;
}
2020-06-29 21:52:17 +02:00
}
if (op0.Count > 0) _value += op0.Sum();
if (op1.Count > 0) _value *= 1 + op1.Sum();
if (op2.Count > 0) _value *= op2.Aggregate((a, _x) => a * _x);
keys.Add(_key, _value);
2020-06-29 21:52:17 +02:00
}
handler.OnEntityProperties(EntityID, keys);
2020-06-29 21:52:17 +02:00
}
break;
case PacketTypesIn.EntityMetadata:
if (handler.GetEntityHandlingEnabled())
{
int EntityID = await dataTypes.ReadNextVarIntAsync(packetData);
Dictionary<int, object?> metadata = await dataTypes.ReadNextMetadataAsync(packetData, itemPalette);
2022-09-02 22:38:59 +08:00
int healthField; // See https://wiki.vg/Entity_metadata#Living_Entity
2022-09-02 21:38:43 +08:00
if (protocolVersion > MC_1_19_2_Version)
throw new NotImplementedException(Translations.exception_palette_healthfield);
2022-09-02 22:38:59 +08:00
else if (protocolVersion >= MC_1_17_Version) // 1.17 and above
healthField = 9;
else if (protocolVersion >= MC_1_14_Version) // 1.14 and above
healthField = 8;
else if (protocolVersion >= MC_1_10_Version) // 1.10 and above
healthField = 7;
else
throw new NotImplementedException(Translations.exception_palette_healthfield);
2022-09-15 21:11:47 +08:00
if (metadata.TryGetValue(healthField, out object? healthObj) && healthObj != null && healthObj.GetType() == typeof(float))
handler.OnEntityHealth(EntityID, (float)healthObj);
handler.OnEntityMetadata(EntityID, metadata);
}
break;
case PacketTypesIn.EntityStatus:
if (handler.GetEntityHandlingEnabled())
{
int entityId = await dataTypes.ReadNextIntAsync(packetData);
byte status = await dataTypes.ReadNextByteAsync(packetData);
handler.OnEntityStatus(entityId, status);
}
break;
case PacketTypesIn.TimeUpdate:
long WorldAge = await dataTypes.ReadNextLongAsync(packetData);
long TimeOfday = await dataTypes.ReadNextLongAsync(packetData);
handler.OnTimeUpdate(WorldAge, TimeOfday);
break;
case PacketTypesIn.SystemChat:
string systemMessage = await dataTypes.ReadNextStringAsync(packetData);
int msgType = await dataTypes.ReadNextVarIntAsync(packetData);
if (msgType == 1 && !Config.Main.Advanced.ShowSystemMessages)
break;
handler.OnTextReceived(new(systemMessage, true, msgType, Guid.Empty, true));
break;
case PacketTypesIn.EntityTeleport:
if (handler.GetEntityHandlingEnabled())
{
int EntityID = await dataTypes.ReadNextVarIntAsync(packetData);
double X = await dataTypes.ReadNextDoubleAsync(packetData);
double Y = await dataTypes.ReadNextDoubleAsync(packetData);
double Z = await dataTypes.ReadNextDoubleAsync(packetData);
byte EntityYaw = await dataTypes.ReadNextByteAsync(packetData);
byte EntityPitch = await dataTypes.ReadNextByteAsync(packetData);
bool OnGround = await dataTypes.ReadNextBoolAsync(packetData);
handler.OnEntityTeleport(EntityID, X, Y, Z, OnGround);
}
break;
case PacketTypesIn.UpdateHealth:
float health = await dataTypes.ReadNextFloatAsync(packetData);
int food;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
food = await dataTypes.ReadNextVarIntAsync(packetData);
else
food = await dataTypes.ReadNextShortAsync(packetData);
await dataTypes.SkipNextFloatAsync(packetData); // Food Saturation
handler.OnUpdateHealth(health, food);
break;
case PacketTypesIn.SetExperience:
float experiencebar = await dataTypes.ReadNextFloatAsync(packetData);
int level = await dataTypes.ReadNextVarIntAsync(packetData);
int totalexperience = await dataTypes.ReadNextVarIntAsync(packetData);
handler.OnSetExperience(experiencebar, level, totalexperience);
break;
case PacketTypesIn.Explosion:
Location explosionLocation = new(await dataTypes.ReadNextFloatAsync(packetData), await dataTypes.ReadNextFloatAsync(packetData), await dataTypes.ReadNextFloatAsync(packetData));
2022-09-18 01:15:57 +02:00
float explosionStrength = await dataTypes.ReadNextFloatAsync(packetData);
2022-09-02 09:29:24 +08:00
int explosionBlockCount = protocolVersion >= MC_1_17_Version
? await dataTypes.ReadNextVarIntAsync(packetData)
: await dataTypes.ReadNextIntAsync(packetData);
2022-09-18 01:15:57 +02:00
for (int i = 0; i < explosionBlockCount; i++)
await dataTypes.DropDataAsync(3, packetData);
2022-09-18 01:15:57 +02:00
float playerVelocityX = await dataTypes.ReadNextFloatAsync(packetData);
float playerVelocityY = await dataTypes.ReadNextFloatAsync(packetData);
float playerVelocityZ = await dataTypes.ReadNextFloatAsync(packetData);
2022-09-18 01:15:57 +02:00
handler.OnExplosion(explosionLocation, explosionStrength, explosionBlockCount);
break;
case PacketTypesIn.HeldItemChange:
byte slot = await dataTypes.ReadNextByteAsync(packetData);
handler.OnHeldItemChange(slot);
break;
case PacketTypesIn.ScoreboardObjective:
string objectivename = await dataTypes.ReadNextStringAsync(packetData);
byte mode = await dataTypes.ReadNextByteAsync(packetData);
string objectivevalue = string.Empty;
int type2 = -1;
if (mode == 0 || mode == 2)
{
objectivevalue = await dataTypes.ReadNextStringAsync(packetData);
type2 = await dataTypes.ReadNextVarIntAsync(packetData);
}
handler.OnScoreboardObjective(objectivename, mode, objectivevalue, type2);
break;
case PacketTypesIn.UpdateScore:
string entityname = await dataTypes.ReadNextStringAsync(packetData);
2022-09-02 09:29:24 +08:00
int action3 = protocolVersion >= MC_1_18_2_Version
? await dataTypes.ReadNextVarIntAsync(packetData)
: await dataTypes.ReadNextByteAsync(packetData);
string objectivename2 = string.Empty;
int value = -1;
2022-09-02 09:29:24 +08:00
if (action3 != 1 || protocolVersion >= MC_1_8_Version)
objectivename2 = await dataTypes.ReadNextStringAsync(packetData);
if (action3 != 1)
value = await dataTypes.ReadNextVarIntAsync(packetData);
handler.OnUpdateScore(entityname, action3, objectivename2, value);
break;
case PacketTypesIn.BlockChangedAck:
handler.OnBlockChangeAck(await dataTypes.ReadNextVarIntAsync(packetData));
break;
case PacketTypesIn.BlockBreakAnimation:
if (handler.GetEntityHandlingEnabled() && handler.GetTerrainEnabled())
{
int playerId = await dataTypes.ReadNextVarIntAsync(packetData);
Location blockLocation = await dataTypes.ReadNextLocationAsync(packetData);
byte stage = await dataTypes.ReadNextByteAsync(packetData);
handler.OnBlockBreakAnimation(playerId, blockLocation, stage);
}
break;
case PacketTypesIn.EntityAnimation:
if (handler.GetEntityHandlingEnabled())
{
int playerId2 = await dataTypes.ReadNextVarIntAsync(packetData);
byte animation = await dataTypes.ReadNextByteAsync(packetData);
handler.OnEntityAnimation(playerId2, animation);
}
break;
default:
return false; //Ignored packet
}
2020-06-29 21:52:17 +02:00
return true; //Packet processed
}
catch (Exception innerException)
{
if (innerException is ThreadAbortException || innerException is SocketException || innerException.InnerException is SocketException)
throw; //Thread abort or Connection lost rather than invalid data
throw new InvalidDataException(
string.Format(Translations.exception_packet_process,
packetPalette.GetIncommingTypeById(packetID),
2020-06-29 21:59:04 +02:00
packetID,
2022-09-02 09:29:24 +08:00
protocolVersion,
2020-06-29 21:59:04 +02:00
login_phase,
innerException.GetType()),
innerException);
2020-06-29 21:52:17 +02:00
}
}
/// <summary>
/// Disconnect from the server, cancel network reading.
/// </summary>
public void Dispose()
{
try
{
socketWrapper.Disconnect();
2020-06-29 21:52:17 +02:00
}
catch { }
}
/// <summary>
/// Send a packet to the server. Packet ID, compression, and encryption will be handled automatically.
/// </summary>
/// <param name="packet">packet type</param>
/// <param name="packetData">packet Data</param>
private async Task SendPacket(PacketTypesOut packet, IEnumerable<byte> packetData)
{
await SendPacket(packetPalette.GetOutgoingIdByType(packet), packetData);
}
/// <summary>
/// Send a packet to the server. Packet ID, compression, and encryption will be handled automatically.
/// </summary>
/// <param name="packet">packet type</param>
/// <param name="packetData">packet Data</param>
private async Task SendPacket(PacketTypesOut packet, PacketStream packetData)
2020-06-29 21:52:17 +02:00
{
await SendPacket(packetPalette.GetOutgoingIdByType(packet), await packetData.ReadFullPacket());
2020-06-29 21:52:17 +02:00
}
/// <summary>
/// Send a packet to the server. Compression and encryption will be handled automatically.
/// </summary>
/// <param name="packetID">packet ID</param>
/// <param name="packetData">packet Data</param>
private async Task SendPacket(int packetID, IEnumerable<byte> packetData)
2020-06-29 21:52:17 +02:00
{
if (handler.GetNetworkPacketCaptureEnabled())
{
List<byte> clone = packetData.ToList();
handler.OnNetworkPacket(packetID, clone, login_phase, false);
}
// log.Info("[C -> S] Sending packet " + packetID + " > " + dataTypes.ByteArrayToString(packetData.ToArray()));
2020-06-29 21:52:17 +02:00
//The inner packet
byte[] the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(packetID), packetData.ToArray());
if (socketWrapper.CompressionThreshold > 0) //Compression enabled?
2020-06-29 21:52:17 +02:00
{
if (the_packet.Length >= socketWrapper.CompressionThreshold) //Packet long enough for compressing?
{
//var compressed = new MemoryStream();
//var zLibStream = new ZLibStream(compressed, CompressionMode.Compress, false);
//zLibStream.Write(the_packet);
//byte[] compressed_packet = compressed.ToArray();
//zLibStream.Dispose();
byte[] compressed_packet;
using (MemoryStream memstream = new())
{
using (ZLibStream stream = new(memstream, CompressionMode.Compress))
{
stream.Write(the_packet);
}
compressed_packet = memstream.ToArray();
}
2020-06-29 21:52:17 +02:00
the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), compressed_packet);
}
else
{
2020-06-29 21:52:17 +02:00
byte[] uncompressed_length = dataTypes.GetVarInt(0); //Not compressed (short packet)
the_packet = dataTypes.ConcatBytes(uncompressed_length, the_packet);
}
2020-06-29 21:52:17 +02:00
}
//log.Debug("[C -> S] Sending packet " + packetID + " > " + dataTypes.ByteArrayToString(dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), the_packet)));
await socketWrapper.SendAsync(dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), the_packet));
2020-06-29 21:52:17 +02:00
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Do the Minecraft login.
/// </summary>
/// <returns>True if login successful</returns>
public async Task<bool> Login(HttpClient httpClient, PlayerKeyPair? playerKeyPair, SessionToken session)
2020-06-29 21:52:17 +02:00
{
2022-09-02 09:29:24 +08:00
byte[] protocol_version = dataTypes.GetVarInt(protocolVersion);
2020-06-29 21:52:17 +02:00
string server_address = pForge.GetServerAddress(handler.GetServerHost());
byte[] server_port = dataTypes.GetUShort((ushort)handler.GetServerPort());
2020-06-29 21:52:17 +02:00
byte[] next_state = dataTypes.GetVarInt(2);
byte[] handshake_packet = dataTypes.ConcatBytes(protocol_version, dataTypes.GetString(server_address), server_port, next_state);
await SendPacket(0x00, handshake_packet);
2020-06-29 21:52:17 +02:00
List<byte> fullLoginPacket = new();
fullLoginPacket.AddRange(dataTypes.GetString(handler.GetUsername())); // Username
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
{
if (playerKeyPair == null)
fullLoginPacket.AddRange(dataTypes.GetBool(false)); // Has Sig Data
else
{
fullLoginPacket.AddRange(dataTypes.GetBool(true)); // Has Sig Data
fullLoginPacket.AddRange(dataTypes.GetLong(playerKeyPair.GetExpirationMilliseconds())); // Expiration time
fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.Key)); // Public key received from Microsoft API
2022-08-28 18:39:59 +08:00
if (protocolVersion >= MC_1_19_2_Version)
2022-08-24 12:37:22 +08:00
fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.SignatureV2!)); // Public key signature received from Microsoft API
else
2022-09-04 17:34:12 +08:00
fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.Signature!)); // Public key signature received from Microsoft API
2022-08-24 12:37:22 +08:00
}
}
2022-08-28 18:39:59 +08:00
if (protocolVersion >= MC_1_19_2_Version)
2022-08-24 12:37:22 +08:00
{
2022-09-02 22:38:59 +08:00
Guid uuid = handler.GetUserUuid();
if (uuid == Guid.Empty)
2022-08-24 12:37:22 +08:00
fullLoginPacket.AddRange(dataTypes.GetBool(false)); // Has UUID
else
{
fullLoginPacket.AddRange(dataTypes.GetBool(true)); // Has UUID
2022-09-02 22:38:59 +08:00
fullLoginPacket.AddRange(dataTypes.GetUUID(uuid)); // UUID
}
}
await SendPacket(0x00, fullLoginPacket);
2020-06-29 21:52:17 +02:00
while (true)
{
(int packetID, PacketStream packetData) = await socketWrapper.GetNextPacket(handleCompress: protocolVersion >= MC_1_8_Version);
2020-06-29 21:52:17 +02:00
if (packetID == 0x00) //Login rejected
{
handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData)));
2020-06-29 21:52:17 +02:00
return false;
}
2020-06-29 21:52:17 +02:00
else if (packetID == 0x01) //Encryption request
{
isOnlineMode = true;
string serverID = await dataTypes.ReadNextStringAsync(packetData);
byte[] serverPublicKey = await dataTypes.ReadNextByteArrayAsync(packetData);
byte[] token = await dataTypes.ReadNextByteArrayAsync(packetData);
return await StartEncryption(httpClient, handler.GetUserUuidStr(), handler.GetSessionID(), token, serverID, serverPublicKey, playerKeyPair, session);
}
2020-06-29 21:52:17 +02:00
else if (packetID == 0x02) //Login successful
{
2022-11-30 16:08:55 +08:00
log.Info("§8" + Translations.mcc_server_offline);
2020-06-29 21:52:17 +02:00
login_phase = false;
if (!await pForge.CompleteForgeHandshake(socketWrapper))
2020-06-29 21:52:17 +02:00
{
2022-11-30 16:08:55 +08:00
log.Error("§8" + Translations.error_forge);
2020-06-29 21:52:17 +02:00
return false;
}
// StartUpdating();
2020-06-29 21:52:17 +02:00
return true; //No need to check session or start encryption
}
else
{
await HandlePacket(packetID, packetData);
}
2020-06-29 21:52:17 +02:00
}
}
/// <summary>
/// Start network encryption. Automatically called by Login() if the server requests encryption.
/// </summary>
/// <returns>True if encryption was successful</returns>
private async Task<bool> StartEncryption(HttpClient httpClient, string uuid, string sessionID, byte[] token, string serverIDhash, byte[] serverPublicKey, PlayerKeyPair? playerKeyPair, SessionToken session)
2020-06-29 21:52:17 +02:00
{
RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverPublicKey)!;
byte[] secretKey = CryptoHandler.ClientAESPrivateKey ?? CryptoHandler.GenerateAESPrivateKey();
2020-06-29 21:52:17 +02:00
2022-11-30 16:08:55 +08:00
log.Debug("§8" + Translations.debug_crypto);
2020-06-29 21:52:17 +02:00
if (serverIDhash != "-")
{
log.Info(Translations.mcc_session);
bool needCheckSession = true;
string serverHash = CryptoHandler.GetServerHash(serverIDhash, serverPublicKey, secretKey);
if (session.SessionPreCheckTask != null && session.ServerInfoHash != null && serverHash == session.ServerInfoHash)
{
try
{
bool preCheckResult = await session.SessionPreCheckTask;
if (preCheckResult) // PreCheck Successed
needCheckSession = false;
}
catch (HttpRequestException) { }
session.SessionPreCheckTask = null;
}
2020-06-29 21:52:17 +02:00
if (needCheckSession)
{
var sessionCheck = await ProtocolHandler.SessionCheckAsync(httpClient, uuid, sessionID, serverHash);
if (sessionCheck)
{
SessionCache.StoreServerInfo($"{InternalConfig.ServerIP}:{InternalConfig.ServerPort}", serverIDhash, serverPublicKey);
}
else
{
handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, Translations.mcc_session_fail);
return false;
}
}
}
2020-06-29 21:52:17 +02:00
// Encryption Response packet
List<byte> encryptionResponse = new();
encryptionResponse.AddRange(dataTypes.GetArray(RSAService.Encrypt(secretKey, false))); // Shared Secret
if (protocolVersion >= MC_1_19_Version)
{
if (playerKeyPair == null)
{
encryptionResponse.AddRange(dataTypes.GetBool(true)); // Has Verify Token
encryptionResponse.AddRange(dataTypes.GetArray(RSAService.Encrypt(token, false))); // Verify Token
}
else
{
byte[] salt = GenerateSalt();
byte[] messageSignature = playerKeyPair.PrivateKey.SignData(dataTypes.ConcatBytes(token, salt));
encryptionResponse.AddRange(dataTypes.GetBool(false)); // Has Verify Token
encryptionResponse.AddRange(salt); // Salt
encryptionResponse.AddRange(dataTypes.GetArray(messageSignature)); // Message Signature
}
}
else
{
encryptionResponse.AddRange(dataTypes.GetArray(RSAService.Encrypt(token, false))); // Verify Token
}
await SendPacket(0x01, encryptionResponse);
2020-06-29 21:52:17 +02:00
// Start client-side encryption
2022-09-02 09:29:24 +08:00
socketWrapper.SwitchToEncrypted(secretKey); // pre switch
2020-06-29 21:52:17 +02:00
// Process the next packet
int loopPrevention = ushort.MaxValue;
2020-06-29 21:52:17 +02:00
while (true)
{
(int packetID, PacketStream packetData) = await socketWrapper.GetNextPacket(handleCompress: protocolVersion >= MC_1_8_Version);
if (packetID < 0 || loopPrevention-- < 0) // Failed to read packet or too many iterations (issue #1150)
{
2022-11-30 16:08:55 +08:00
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "§8" + Translations.error_invalid_encrypt);
return false;
}
else if (packetID == 0x00) //Login rejected
{
handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(await dataTypes.ReadNextStringAsync(packetData)));
2020-06-29 21:52:17 +02:00
return false;
}
2020-06-29 21:52:17 +02:00
else if (packetID == 0x02) //Login successful
{
2022-09-04 20:44:38 +08:00
Guid uuidReceived;
if (protocolVersion >= MC_1_16_Version)
uuidReceived = await dataTypes.ReadNextUUIDAsync(packetData);
2022-09-04 20:44:38 +08:00
else
uuidReceived = Guid.Parse(await dataTypes.ReadNextStringAsync(packetData));
string userName = await dataTypes.ReadNextStringAsync(packetData);
Tuple<string, string, string>[]? playerProperty = null;
if (protocolVersion >= MC_1_19_Version)
{
int count = await dataTypes.ReadNextVarIntAsync(packetData); // Number Of Properties
playerProperty = new Tuple<string, string, string>[count];
for (int i = 0; i < count; ++i)
{
string name = await dataTypes.ReadNextStringAsync(packetData);
string value = await dataTypes.ReadNextStringAsync(packetData);
bool isSigned = await dataTypes.ReadNextBoolAsync(packetData);
string signature = isSigned ? await dataTypes.ReadNextStringAsync(packetData) : string.Empty;
playerProperty[i] = new Tuple<string, string, string>(name, value, signature);
}
}
handler.OnLoginSuccess(uuidReceived, userName, playerProperty);
2020-06-29 21:52:17 +02:00
login_phase = false;
if (!await pForge.CompleteForgeHandshake(socketWrapper))
{
2022-11-30 16:08:55 +08:00
log.Error("§8" + Translations.error_forge_encrypt);
2020-06-29 21:52:17 +02:00
return false;
}
// StartUpdating();
2020-06-29 21:52:17 +02:00
return true;
}
else
{
await HandlePacket(packetID, packetData);
}
2020-06-29 21:52:17 +02:00
}
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Disconnect from the server
/// </summary>
public void Disconnect()
{
2020-06-29 21:52:17 +02:00
socketWrapper.Disconnect();
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Autocomplete text while typing username or command
/// </summary>
/// <param name="BehindCursor">Text behind cursor</param>
/// <returns>Completed text</returns>
async Task<int> IAutoComplete.AutoComplete(string BehindCursor)
2020-06-29 21:52:17 +02:00
{
if (string.IsNullOrEmpty(BehindCursor))
return -1;
2020-06-29 21:52:17 +02:00
byte[] transaction_id = dataTypes.GetVarInt(autocomplete_transaction_id);
byte[] assume_command = new byte[] { 0x00 };
byte[] has_position = new byte[] { 0x00 };
byte[] tabcomplete_packet = Array.Empty<byte>();
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
2020-06-29 21:52:17 +02:00
{
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_13_Version)
2020-06-29 21:52:17 +02:00
{
tabcomplete_packet = dataTypes.ConcatBytes(tabcomplete_packet, transaction_id);
tabcomplete_packet = dataTypes.ConcatBytes(tabcomplete_packet, dataTypes.GetString(BehindCursor.Replace(' ', (char)0x00)));
2020-06-29 21:52:17 +02:00
}
else
{
tabcomplete_packet = dataTypes.ConcatBytes(tabcomplete_packet, dataTypes.GetString(BehindCursor));
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_9_Version)
2020-06-29 21:52:17 +02:00
tabcomplete_packet = dataTypes.ConcatBytes(tabcomplete_packet, assume_command);
2020-06-29 21:52:17 +02:00
tabcomplete_packet = dataTypes.ConcatBytes(tabcomplete_packet, has_position);
}
}
else
{
tabcomplete_packet = dataTypes.ConcatBytes(dataTypes.GetString(BehindCursor));
}
ConsoleIO.AutoCompleteDone = false;
await SendPacket(PacketTypesOut.TabComplete, tabcomplete_packet);
return autocomplete_transaction_id;
}
internal record PingResult
{
#pragma warning disable IDE1006 // Naming Styles
public bool previewsChat { init; get; }
public bool enforcesSecureChat { init; get; }
public Dictionary<string, string>? description { init; get; }
public PlayerInfo? players { init; get; }
public VersionInfo? version { init; get; }
public string? favicon { init; get; }
public FrogeInfoFML1? modinfo { init; get; }
public FrogeInfoFML2? forgeData { init; get; }
public record PlayerInfo
{
public int max { init; get; }
public int online { init; get; }
}
public record VersionInfo
{
public string? name { init; get; }
public int protocol { init; get; }
}
public record FrogeInfoFML1
{
public string? type { init; get; }
public ForgeInfo.ForgeMod[]? modList { init; get; }
}
public record FrogeInfoFML2
{
public FrogeChannelInfo[]? channels { init; get; }
public ForgeInfo.ForgeMod[]? mods { init; get; }
public string? fmlNetworkVersion { init; get; }
public record FrogeChannelInfo
{
public string? res { init; get; }
public string? version { init; get; }
public bool required { init; get; }
}
}
#pragma warning restore IDE1006 // Naming Styles
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Ping a Minecraft server to get information about the server
/// </summary>
/// <returns>True if ping was successful</returns>
public static async Task<Tuple<bool, int, ForgeInfo?>> DoPing(string host, int port, CancellationToken cancelToken)
{
TcpClient tcpClient = ProxyHandler.NewTcpClient(host, port, ProxyHandler.ClientType.Ingame);
tcpClient.ReceiveBufferSize = 1024 * 1024;
tcpClient.ReceiveTimeout = Config.Main.Advanced.TcpTimeout * 1000;
SocketWrapper socketWrapper = new(tcpClient);
DataTypes dataTypes = new(MC_1_8_Version);
2020-06-29 21:52:17 +02:00
byte[] packet_id = dataTypes.GetVarInt(0);
byte[] protocol_version = dataTypes.GetVarInt(-1);
byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port);
byte[] next_state = dataTypes.GetVarInt(1);
byte[] packet = dataTypes.ConcatBytes(packet_id, protocol_version, dataTypes.GetString(host), server_port, next_state);
byte[] tosend = dataTypes.ConcatBytes(dataTypes.GetVarInt(packet.Length), packet);
await socketWrapper.SendAsync(tosend, cancelToken);
2020-06-29 21:52:17 +02:00
byte[] status_request = dataTypes.GetVarInt(0);
byte[] request_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(status_request.Length), status_request);
await socketWrapper.SendAsync(request_packet, cancelToken);
(int packetID, PacketStream packetData) = await socketWrapper.GetNextPacket(false, cancelToken);
if (packetID == 0x00)
2020-06-29 21:52:17 +02:00
{
string result = await dataTypes.ReadNextStringAsync(packetData); //Get the Json data
if (Config.Logging.DebugMessages)
{
// May contain formatting codes, cannot use WriteLineFormatted
Console.ForegroundColor = ConsoleColor.DarkGray;
ConsoleIO.WriteLine(result);
Console.ForegroundColor = ConsoleColor.Gray;
}
if (!string.IsNullOrEmpty(result))
{
try
2020-06-29 21:52:17 +02:00
{
PingResult? jsonData = JsonSerializer.Deserialize<PingResult>(result);
if (jsonData != null && jsonData.version != null)
2020-06-29 21:52:17 +02:00
{
// Retrieve display name of the Minecraft version
string version = jsonData.version.name ?? string.Empty;
// Retrieve protocol version number for handling this server
int protocolVersion = jsonData.version.protocol;
2020-06-29 21:52:17 +02:00
// Check for forge on the server.
ForgeInfo? forgeInfo = null;
2020-06-29 21:52:17 +02:00
Protocol18Forge.ServerInfoCheckForge(jsonData, ref forgeInfo);
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.mcc_server_protocol,
version, protocolVersion + (forgeInfo == null ? string.Empty : Translations.mcc_with_forge)));
return new(true, protocolVersion, forgeInfo);
2020-06-29 21:52:17 +02:00
}
}
catch (JsonException) { }
catch (ArgumentNullException) { }
2020-06-29 21:52:17 +02:00
}
}
return new(false, 0, null);
}
Add SendPlaceBlock, PlayerDigging, OnExplosion, OnGamemodeUpdate, OnSetExperience (#1027) * Update ChatBot.cs + PlaceBlock * Update AutoAttack.cs + HitAnimation * Update PacketIncomingType.cs + Explosion, * Update McTcpClient.cs + OnExplosion * Update ChatBot.cs + OnExplosion * Update IMinecraftComHandler.cs + OnExplosion * Update Protocol18PacketTypes.cs + PacketIncomingType.Explosion * Update ChatBot.cs + Fix * Update AutoAttack.cs + Fix * Update ChatBot.cs + Fix * Update Protocol18PacketTypes.cs + Old versions * Update Protocol18PacketTypes.cs + 1.7 - 1.8 Explosion ID * Update Protocol18PacketTypes.cs + Fix * Update McTcpClient.cs + int ExplosionRecordCount * Update ChatBot.cs + recordcount * Update IMinecraftComHandler.cs + ExplosionRecordCount * Update Protocol18.cs * Update CSharpRunner.cs + using MinecraftClient.Inventory; * add OnGamemodeUpdate + OnGamemodeUpdate * + OnGamemodeUpdate(playername, uuid, gamemode) + OnGamemodeUpdate * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McTcpClient.cs Fix * Update McTcpClient.cs * Update Protocol18.cs + Location explodelocation * Update McTcpClient.cs + Location explode * Update ChatBot.cs + Fix * Update ChatBot.cs Remove excess + * Update Plays animation * Improve documentation * ItemType fix * OnExplosion(Location explod); * Update PacketIncomingType.cs add SetExperience, * + Old versions * Update IMinecraftComHandler.cs * Update McTcpClient.cs * Update Protocol18.cs * add GetLevel & GetTotalExperience * Fix * add GetLevel & GetTotalExperience * OnSetExpience * Update ChatBot.cs Fix * Update McTcpClient.cs + bot.OnSetExperience * Update Protocol18.cs + Fix * Update McTcpClient.cs + PlayerDigging * Update PacketOutgoingType.cs + PlayerDigging * Update Protocol18PacketTypes.cs + case PacketOutgoingType.PlayerDigging * Update Protocol18.cs + SendPlayerDigging * Update IMinecraftCom.cs + SendPlayerDigging * Update McTcpClient.cs + PlayerDigging * Update Protocol16.cs + SendPlayerDigging * Update ChatBot.cs + PlayerDigging * Update ChatBot.cs + Fix * Update McTcpClient.cs + Fix * Update ChatBot.cs Add WindowAction * ChatBot.cs Fixes * Further ChatBot.cs fixes * Further ChatBot.cs fixes * Protocol Handler fixes * Protocol Handler fixes * IMinecraftCom fixes * documentation fixes Co-authored-by: ORelio <oreliogitantispam.l0gin@spamgourmet.com>
2020-05-29 23:18:34 +05:00
2020-06-29 21:52:17 +02:00
/// <summary>
/// Get max length for chat messages
/// </summary>
/// <returns>Max length, in characters</returns>
public int GetMaxChatMessageLength()
{
2022-09-02 09:29:24 +08:00
return protocolVersion > MC_1_10_Version
2020-06-29 21:59:04 +02:00
? 256
: 100;
2020-06-29 21:52:17 +02:00
}
/// <summary>
/// Get the current protocol version.
/// </summary>
/// <remarks>
/// Version-specific operations should be handled inside the Protocol handled whenever possible.
/// </remarks>
/// <returns>Minecraft Protocol version number</returns>
public int GetProtocolVersion()
{
2022-09-02 09:29:24 +08:00
return protocolVersion;
}
2022-08-27 02:10:44 +08:00
/// <summary>
/// Send MessageAcknowledgment packet
/// </summary>
/// <param name="acknowledgment">Message acknowledgment</param>
/// <returns>True if properly sent</returns>
public async Task<bool> SendMessageAcknowledgment(LastSeenMessageList.Acknowledgment acknowledgment)
2022-08-27 02:10:44 +08:00
{
try
{
2022-10-17 10:03:01 +08:00
byte[] fields = dataTypes.GetAcknowledgment(acknowledgment, isOnlineMode && Config.Signature.LoginWithSecureProfile);
2022-08-27 02:10:44 +08:00
await SendPacket(PacketTypesOut.MessageAcknowledgment, fields);
2022-08-27 02:10:44 +08:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2022-08-27 02:10:44 +08:00
catch (ObjectDisposedException) { return false; }
}
public LastSeenMessageList.Acknowledgment ConsumeAcknowledgment()
2022-08-27 02:10:44 +08:00
{
pendingAcknowledgments = 0;
return new LastSeenMessageList.Acknowledgment(lastSeenMessagesCollector.GetLastSeenMessages(), lastReceivedMessage);
2022-08-27 02:10:44 +08:00
}
public async Task Acknowledge(ChatMessage message)
2022-08-27 02:10:44 +08:00
{
LastSeenMessageList.Entry? entry = message.ToLastSeenMessageEntry();
2022-08-27 02:10:44 +08:00
if (entry != null)
{
lastSeenMessagesCollector.Add(entry);
lastReceivedMessage = null;
if (pendingAcknowledgments++ > 64)
await SendMessageAcknowledgment(ConsumeAcknowledgment());
2022-08-27 02:10:44 +08:00
}
}
/// <summary>
2022-09-02 22:38:59 +08:00
/// Send a chat command to the server - 1.19 and above
/// </summary>
/// <param name="command">Command</param>
/// <param name="playerKeyPair">PlayerKeyPair</param>
/// <returns>True if properly sent</returns>
public async Task<bool> SendChatCommand(string command, PlayerKeyPair? playerKeyPair)
{
if (string.IsNullOrEmpty(command))
return true;
command = GetCharFilterRegex1().Replace(command, " ");
command = GetCharFilterRegex2().Replace(command, string.Empty);
log.Debug("chat command = " + command);
try
{
2022-09-02 21:38:43 +08:00
LastSeenMessageList.Acknowledgment? acknowledgment =
protocolVersion >= MC_1_19_2_Version ? ConsumeAcknowledgment() : null;
2022-08-27 02:10:44 +08:00
List<byte> fields = new();
// Command: String
fields.AddRange(dataTypes.GetString(command));
// Timestamp: Instant(Long)
DateTimeOffset timeNow = DateTimeOffset.UtcNow;
fields.AddRange(dataTypes.GetLong(timeNow.ToUnixTimeMilliseconds()));
2022-10-14 20:34:45 +08:00
List<Tuple<string, string>>? needSigned = null; // List< Argument Name, Argument Value >
2022-10-17 10:03:01 +08:00
if (playerKeyPair != null && isOnlineMode && protocolVersion >= MC_1_19_Version
&& Config.Signature.LoginWithSecureProfile && Config.Signature.SignMessageInCommand)
2022-10-14 20:34:45 +08:00
needSigned = DeclareCommands.CollectSignArguments(command);
2022-09-02 22:38:59 +08:00
if (needSigned == null || needSigned!.Count == 0)
{
fields.AddRange(dataTypes.GetLong(0)); // Salt: Long
fields.AddRange(dataTypes.GetVarInt(0)); // Signature Length: VarInt
}
else
{
2022-08-27 02:10:44 +08:00
Guid uuid = handler.GetUserUuid();
byte[] salt = GenerateSalt();
fields.AddRange(salt); // Salt: Long
fields.AddRange(dataTypes.GetVarInt(needSigned.Count)); // Signature Length: VarInt
2022-10-14 20:34:45 +08:00
foreach ((string argName, string message) in needSigned)
{
2022-10-14 20:34:45 +08:00
fields.AddRange(dataTypes.GetString(argName)); // Argument name: String
byte[] sign = protocolVersion >= MC_1_19_2_Version ?
2022-10-14 20:34:45 +08:00
playerKeyPair!.PrivateKey.SignMessage(message, uuid, timeNow, ref salt, acknowledgment!.lastSeen) :
playerKeyPair!.PrivateKey.SignMessage(message, uuid, timeNow, ref salt);
fields.AddRange(dataTypes.GetVarInt(sign.Length)); // Signature length: VarInt
fields.AddRange(sign); // Signature: Byte Array
}
}
// Signed Preview: Boolean
fields.AddRange(dataTypes.GetBool(false));
2022-08-28 18:39:59 +08:00
if (protocolVersion >= MC_1_19_2_Version)
2022-08-27 02:10:44 +08:00
{
// Message Acknowledgment
2022-10-17 10:03:01 +08:00
fields.AddRange(dataTypes.GetAcknowledgment(acknowledgment!, isOnlineMode && Config.Signature.LoginWithSecureProfile));
2022-08-27 02:10:44 +08:00
}
await SendPacket(PacketTypesOut.ChatCommand, fields);
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Send a chat message to the server
/// </summary>
/// <param name="message">Message</param>
/// <param name="playerKeyPair">PlayerKeyPair</param>
2020-06-29 21:52:17 +02:00
/// <returns>True if properly sent</returns>
public async Task<bool> SendChatMessage(string message, PlayerKeyPair? playerKeyPair)
2020-06-29 21:52:17 +02:00
{
if (string.IsNullOrEmpty(message))
2020-06-29 21:52:17 +02:00
return true;
// Process Chat Command - 1.19 and above
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version && message.StartsWith('/'))
return await SendChatCommand(message[1..], playerKeyPair);
2020-06-29 21:52:17 +02:00
try
{
List<byte> fields = new();
// Message: String (up to 256 chars)
fields.AddRange(dataTypes.GetString(message));
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
{
2022-09-02 22:38:59 +08:00
LastSeenMessageList.Acknowledgment? acknowledgment =
protocolVersion >= MC_1_19_2_Version ? ConsumeAcknowledgment() : null;
2022-09-02 22:38:59 +08:00
// Timestamp: Instant(Long)
DateTimeOffset timeNow = DateTimeOffset.UtcNow;
fields.AddRange(dataTypes.GetLong(timeNow.ToUnixTimeMilliseconds()));
2022-10-17 10:03:01 +08:00
if (!isOnlineMode || playerKeyPair == null || !Config.Signature.LoginWithSecureProfile || !Config.Signature.SignChat)
{
fields.AddRange(dataTypes.GetLong(0)); // Salt: Long
fields.AddRange(dataTypes.GetVarInt(0)); // Signature Length: VarInt
}
else
{
// Salt: Long
byte[] salt = GenerateSalt();
fields.AddRange(salt);
// Signature Length & Signature: (VarInt) and Byte Array
2022-08-27 02:10:44 +08:00
Guid uuid = handler.GetUserUuid();
byte[] sign = protocolVersion >= MC_1_19_2_Version ?
2022-08-27 02:10:44 +08:00
playerKeyPair.PrivateKey.SignMessage(message, uuid, timeNow, ref salt, acknowledgment!.lastSeen) :
playerKeyPair.PrivateKey.SignMessage(message, uuid, timeNow, ref salt);
fields.AddRange(dataTypes.GetVarInt(sign.Length));
fields.AddRange(sign);
}
// Signed Preview: Boolean
fields.AddRange(dataTypes.GetBool(false));
2022-08-24 12:37:22 +08:00
2022-08-28 18:39:59 +08:00
if (protocolVersion >= MC_1_19_2_Version)
2022-08-27 02:10:44 +08:00
{
// Message Acknowledgment
2022-10-17 10:03:01 +08:00
fields.AddRange(dataTypes.GetAcknowledgment(acknowledgment!, isOnlineMode && Config.Signature.LoginWithSecureProfile));
2022-08-27 02:10:44 +08:00
}
}
await SendPacket(PacketTypesOut.ChatMessage, fields);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SendEntityAction(int PlayerEntityID, int ActionID)
{
2020-06-29 21:52:17 +02:00
try
{
List<byte> fields = new();
2020-06-29 21:52:17 +02:00
fields.AddRange(dataTypes.GetVarInt(PlayerEntityID));
fields.AddRange(dataTypes.GetVarInt(ActionID));
fields.AddRange(dataTypes.GetVarInt(0));
await SendPacket(PacketTypesOut.EntityAction, fields);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Send a respawn packet to the server
/// </summary>
/// <returns>True if properly sent</returns>
public async Task<bool> SendRespawnPacket()
2020-06-29 21:52:17 +02:00
{
try
{
await SendPacket(PacketTypesOut.ClientStatus, new byte[] { 0 });
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Tell the server what client is being used to connect to the server
/// </summary>
/// <param name="brandInfo">Client string describing the client</param>
/// <returns>True if brand info was successfully sent</returns>
public async Task<bool> SendBrandInfo(string brandInfo)
2020-06-29 21:52:17 +02:00
{
if (string.IsNullOrEmpty(brandInfo))
2020-06-29 21:52:17 +02:00
return false;
// Plugin channels were significantly changed between Minecraft 1.12 and 1.13
// https://wiki.vg/index.php?title=Pre-release_protocol&oldid=14132#Plugin_Channels
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_13_Version)
2020-06-29 21:52:17 +02:00
{
return await SendPluginChannelPacket("minecraft:brand", dataTypes.GetString(brandInfo));
2020-06-29 21:52:17 +02:00
}
else
{
return await SendPluginChannelPacket("MC|Brand", dataTypes.GetString(brandInfo));
2020-06-29 21:52:17 +02:00
}
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Inform the server of the client's Minecraft settings
/// </summary>
/// <param name="language">Client language eg en_US</param>
/// <param name="viewDistance">View distance, in chunks</param>
/// <param name="difficulty">Game difficulty (client-side...)</param>
/// <param name="chatMode">Chat mode (allows muting yourself)</param>
/// <param name="chatColors">Show chat colors</param>
/// <param name="skinParts">Show skin layers</param>
/// <param name="mainHand">1.9+ main hand</param>
/// <returns>True if client settings were successfully sent</returns>
public async Task<bool> SendClientSettings(string language, byte viewDistance, byte difficulty, byte chatMode, bool chatColors, byte skinParts, byte mainHand)
2020-06-29 21:52:17 +02:00
{
try
{
List<byte> fields = new();
2020-06-29 21:52:17 +02:00
fields.AddRange(dataTypes.GetString(language));
fields.Add(viewDistance);
2022-08-27 02:10:44 +08:00
2022-08-28 18:39:59 +08:00
if (protocolVersion >= MC_1_9_Version)
2022-08-27 02:10:44 +08:00
fields.AddRange(dataTypes.GetVarInt(chatMode));
else
fields.AddRange(new byte[] { chatMode });
2020-06-29 21:52:17 +02:00
fields.Add(chatColors ? (byte)1 : (byte)0);
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
2020-06-29 21:52:17 +02:00
{
fields.Add(difficulty);
fields.Add((byte)(skinParts & 0x1)); //show cape
}
else fields.Add(skinParts);
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_9_Version)
2020-06-29 21:52:17 +02:00
fields.AddRange(dataTypes.GetVarInt(mainHand));
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_17_Version)
{
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_18_1_Version)
fields.Add(0); // 1.18 and above - Enable text filtering. (Always false)
else
fields.Add(1); // 1.17 and 1.17.1 - Disable text filtering. (Always true)
}
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_18_1_Version)
2021-12-02 21:07:57 +08:00
fields.Add(1); // 1.18 and above - Allow server listings
await SendPacket(PacketTypesOut.ClientSettings, fields);
2020-06-29 21:52:17 +02:00
}
catch (SocketException) { }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
return false;
}
2022-10-16 18:42:11 +08:00
2020-06-29 21:52:17 +02:00
/// <summary>
/// Send a location update to the server
/// </summary>
/// <param name="location">The new location of the player</param>
/// <param name="onGround">True if the player is on the ground</param>
/// <param name="yaw">Optional new yaw for updating player look</param>
/// <param name="pitch">Optional new pitch for updating player look</param>
/// <returns>True if the location update was successfully sent</returns>
public async Task<bool> SendLocationUpdate(Location location, bool onGround, float? yaw, float? pitch)
2022-10-16 18:42:11 +08:00
{
return await SendLocationUpdate(location, onGround, yaw, pitch, true);
2022-10-16 18:42:11 +08:00
}
public async Task<bool> SendLocationUpdate(Location location, bool onGround, float? yaw = null, float? pitch = null, bool forceUpdate = false)
2020-06-29 21:52:17 +02:00
{
if (handler.GetTerrainEnabled())
{
byte[] yawpitch = Array.Empty<byte>();
PacketTypesOut packetType = PacketTypesOut.PlayerPosition;
2022-11-01 16:37:50 +08:00
if (Config.Main.Advanced.TemporaryFixBadpacket)
{
if (yaw.HasValue && pitch.HasValue && (forceUpdate || yaw.Value != LastYaw || pitch.Value != LastPitch))
{
yawpitch = dataTypes.ConcatBytes(dataTypes.GetFloat(yaw.Value), dataTypes.GetFloat(pitch.Value));
packetType = PacketTypesOut.PlayerPositionAndRotation;
LastYaw = yaw.Value; LastPitch = pitch.Value;
}
}
else
2020-06-29 21:52:17 +02:00
{
2022-11-01 16:37:50 +08:00
if (yaw.HasValue && pitch.HasValue)
{
yawpitch = dataTypes.ConcatBytes(dataTypes.GetFloat(yaw.Value), dataTypes.GetFloat(pitch.Value));
packetType = PacketTypesOut.PlayerPositionAndRotation;
2022-10-16 18:42:11 +08:00
2022-11-01 16:37:50 +08:00
LastYaw = yaw.Value; LastPitch = pitch.Value;
}
2020-06-29 21:52:17 +02:00
}
2020-06-29 21:52:17 +02:00
try
{
await SendPacket(packetType, dataTypes.ConcatBytes(
2020-06-29 22:01:29 +02:00
dataTypes.GetDouble(location.X),
dataTypes.GetDouble(location.Y),
2022-09-02 09:29:24 +08:00
protocolVersion < MC_1_8_Version
2020-06-29 22:01:29 +02:00
? dataTypes.GetDouble(location.Y + 1.62)
: Array.Empty<byte>(),
2020-06-29 22:01:29 +02:00
dataTypes.GetDouble(location.Z),
yawpitch,
new byte[] { onGround ? (byte)1 : (byte)0 }));
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
else return false;
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Send a plugin channel packet (0x17) to the server, compression and encryption will be handled automatically
/// </summary>
/// <param name="channel">Channel to send packet on</param>
/// <param name="data">packet Data</param>
public async Task<bool> SendPluginChannelPacket(string channel, byte[] data)
2020-06-29 21:52:17 +02:00
{
try
{
// In 1.7, length needs to be included.
// In 1.8, it must not be.
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
2020-06-29 21:52:17 +02:00
{
byte[] length = BitConverter.GetBytes((short)data.Length);
Array.Reverse(length);
Add SendPlaceBlock, PlayerDigging, OnExplosion, OnGamemodeUpdate, OnSetExperience (#1027) * Update ChatBot.cs + PlaceBlock * Update AutoAttack.cs + HitAnimation * Update PacketIncomingType.cs + Explosion, * Update McTcpClient.cs + OnExplosion * Update ChatBot.cs + OnExplosion * Update IMinecraftComHandler.cs + OnExplosion * Update Protocol18PacketTypes.cs + PacketIncomingType.Explosion * Update ChatBot.cs + Fix * Update AutoAttack.cs + Fix * Update ChatBot.cs + Fix * Update Protocol18PacketTypes.cs + Old versions * Update Protocol18PacketTypes.cs + 1.7 - 1.8 Explosion ID * Update Protocol18PacketTypes.cs + Fix * Update McTcpClient.cs + int ExplosionRecordCount * Update ChatBot.cs + recordcount * Update IMinecraftComHandler.cs + ExplosionRecordCount * Update Protocol18.cs * Update CSharpRunner.cs + using MinecraftClient.Inventory; * add OnGamemodeUpdate + OnGamemodeUpdate * + OnGamemodeUpdate(playername, uuid, gamemode) + OnGamemodeUpdate * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McTcpClient.cs Fix * Update McTcpClient.cs * Update Protocol18.cs + Location explodelocation * Update McTcpClient.cs + Location explode * Update ChatBot.cs + Fix * Update ChatBot.cs Remove excess + * Update Plays animation * Improve documentation * ItemType fix * OnExplosion(Location explod); * Update PacketIncomingType.cs add SetExperience, * + Old versions * Update IMinecraftComHandler.cs * Update McTcpClient.cs * Update Protocol18.cs * add GetLevel & GetTotalExperience * Fix * add GetLevel & GetTotalExperience * OnSetExpience * Update ChatBot.cs Fix * Update McTcpClient.cs + bot.OnSetExperience * Update Protocol18.cs + Fix * Update McTcpClient.cs + PlayerDigging * Update PacketOutgoingType.cs + PlayerDigging * Update Protocol18PacketTypes.cs + case PacketOutgoingType.PlayerDigging * Update Protocol18.cs + SendPlayerDigging * Update IMinecraftCom.cs + SendPlayerDigging * Update McTcpClient.cs + PlayerDigging * Update Protocol16.cs + SendPlayerDigging * Update ChatBot.cs + PlayerDigging * Update ChatBot.cs + Fix * Update McTcpClient.cs + Fix * Update ChatBot.cs Add WindowAction * ChatBot.cs Fixes * Further ChatBot.cs fixes * Further ChatBot.cs fixes * Protocol Handler fixes * Protocol Handler fixes * IMinecraftCom fixes * documentation fixes Co-authored-by: ORelio <oreliogitantispam.l0gin@spamgourmet.com>
2020-05-29 23:18:34 +05:00
await SendPacket(PacketTypesOut.PluginMessage, dataTypes.ConcatBytes(dataTypes.GetString(channel), length, data));
2020-06-29 21:52:17 +02:00
}
else
{
await SendPacket(PacketTypesOut.PluginMessage, dataTypes.ConcatBytes(dataTypes.GetString(channel), data));
2020-06-29 21:52:17 +02:00
}
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
/// <summary>
/// Send a Login Plugin Response packet (0x02)
/// </summary>
/// <param name="messageId">Login Plugin Request message Id </param>
/// <param name="understood">TRUE if the request was understood</param>
/// <param name="data">Response to the request</param>
/// <returns>TRUE if successfully sent</returns>
public async Task<bool> SendLoginPluginResponse(int messageId, bool understood, byte[] data)
{
try
{
await SendPacket(0x02, dataTypes.ConcatBytes(dataTypes.GetVarInt(messageId), dataTypes.GetBool(understood), data));
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
/// <summary>
/// Send an Interact Entity Packet to server
/// </summary>
/// <param name="EntityID"></param>
/// <param name="type"></param>
/// <returns></returns>
public async Task<bool> SendInteractEntity(int EntityID, int type)
{
2020-06-29 21:52:17 +02:00
try
{
List<byte> fields = new();
2020-06-29 21:52:17 +02:00
fields.AddRange(dataTypes.GetVarInt(EntityID));
fields.AddRange(dataTypes.GetVarInt(type));
2020-07-29 21:35:00 +02:00
// Is player Sneaking (Only 1.16 and above)
// Currently hardcoded to false
// TODO: Update to reflect the real player state
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
fields.AddRange(dataTypes.GetBool(false));
await SendPacket(PacketTypesOut.InteractEntity, fields);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
// TODO: Interact at block location (e.g. chest minecart)
public async Task<bool> SendInteractEntity(int EntityID, int type, float X, float Y, float Z, int hand)
Add SendPlaceBlock, PlayerDigging, OnExplosion, OnGamemodeUpdate, OnSetExperience (#1027) * Update ChatBot.cs + PlaceBlock * Update AutoAttack.cs + HitAnimation * Update PacketIncomingType.cs + Explosion, * Update McTcpClient.cs + OnExplosion * Update ChatBot.cs + OnExplosion * Update IMinecraftComHandler.cs + OnExplosion * Update Protocol18PacketTypes.cs + PacketIncomingType.Explosion * Update ChatBot.cs + Fix * Update AutoAttack.cs + Fix * Update ChatBot.cs + Fix * Update Protocol18PacketTypes.cs + Old versions * Update Protocol18PacketTypes.cs + 1.7 - 1.8 Explosion ID * Update Protocol18PacketTypes.cs + Fix * Update McTcpClient.cs + int ExplosionRecordCount * Update ChatBot.cs + recordcount * Update IMinecraftComHandler.cs + ExplosionRecordCount * Update Protocol18.cs * Update CSharpRunner.cs + using MinecraftClient.Inventory; * add OnGamemodeUpdate + OnGamemodeUpdate * + OnGamemodeUpdate(playername, uuid, gamemode) + OnGamemodeUpdate * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McTcpClient.cs Fix * Update McTcpClient.cs * Update Protocol18.cs + Location explodelocation * Update McTcpClient.cs + Location explode * Update ChatBot.cs + Fix * Update ChatBot.cs Remove excess + * Update Plays animation * Improve documentation * ItemType fix * OnExplosion(Location explod); * Update PacketIncomingType.cs add SetExperience, * + Old versions * Update IMinecraftComHandler.cs * Update McTcpClient.cs * Update Protocol18.cs * add GetLevel & GetTotalExperience * Fix * add GetLevel & GetTotalExperience * OnSetExpience * Update ChatBot.cs Fix * Update McTcpClient.cs + bot.OnSetExperience * Update Protocol18.cs + Fix * Update McTcpClient.cs + PlayerDigging * Update PacketOutgoingType.cs + PlayerDigging * Update Protocol18PacketTypes.cs + case PacketOutgoingType.PlayerDigging * Update Protocol18.cs + SendPlayerDigging * Update IMinecraftCom.cs + SendPlayerDigging * Update McTcpClient.cs + PlayerDigging * Update Protocol16.cs + SendPlayerDigging * Update ChatBot.cs + PlayerDigging * Update ChatBot.cs + Fix * Update McTcpClient.cs + Fix * Update ChatBot.cs Add WindowAction * ChatBot.cs Fixes * Further ChatBot.cs fixes * Further ChatBot.cs fixes * Protocol Handler fixes * Protocol Handler fixes * IMinecraftCom fixes * documentation fixes Co-authored-by: ORelio <oreliogitantispam.l0gin@spamgourmet.com>
2020-05-29 23:18:34 +05:00
{
2020-06-29 21:52:17 +02:00
try
{
List<byte> fields = new();
2020-06-29 21:52:17 +02:00
fields.AddRange(dataTypes.GetVarInt(EntityID));
fields.AddRange(dataTypes.GetVarInt(type));
fields.AddRange(dataTypes.GetFloat(X));
fields.AddRange(dataTypes.GetFloat(Y));
fields.AddRange(dataTypes.GetFloat(Z));
fields.AddRange(dataTypes.GetVarInt(hand));
// Is player Sneaking (Only 1.16 and above)
// Currently hardcoded to false
// TODO: Update to reflect the real player state
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
fields.AddRange(dataTypes.GetBool(false));
await SendPacket(PacketTypesOut.InteractEntity, fields);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
Add SendPlaceBlock, PlayerDigging, OnExplosion, OnGamemodeUpdate, OnSetExperience (#1027) * Update ChatBot.cs + PlaceBlock * Update AutoAttack.cs + HitAnimation * Update PacketIncomingType.cs + Explosion, * Update McTcpClient.cs + OnExplosion * Update ChatBot.cs + OnExplosion * Update IMinecraftComHandler.cs + OnExplosion * Update Protocol18PacketTypes.cs + PacketIncomingType.Explosion * Update ChatBot.cs + Fix * Update AutoAttack.cs + Fix * Update ChatBot.cs + Fix * Update Protocol18PacketTypes.cs + Old versions * Update Protocol18PacketTypes.cs + 1.7 - 1.8 Explosion ID * Update Protocol18PacketTypes.cs + Fix * Update McTcpClient.cs + int ExplosionRecordCount * Update ChatBot.cs + recordcount * Update IMinecraftComHandler.cs + ExplosionRecordCount * Update Protocol18.cs * Update CSharpRunner.cs + using MinecraftClient.Inventory; * add OnGamemodeUpdate + OnGamemodeUpdate * + OnGamemodeUpdate(playername, uuid, gamemode) + OnGamemodeUpdate * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McTcpClient.cs Fix * Update McTcpClient.cs * Update Protocol18.cs + Location explodelocation * Update McTcpClient.cs + Location explode * Update ChatBot.cs + Fix * Update ChatBot.cs Remove excess + * Update Plays animation * Improve documentation * ItemType fix * OnExplosion(Location explod); * Update PacketIncomingType.cs add SetExperience, * + Old versions * Update IMinecraftComHandler.cs * Update McTcpClient.cs * Update Protocol18.cs * add GetLevel & GetTotalExperience * Fix * add GetLevel & GetTotalExperience * OnSetExpience * Update ChatBot.cs Fix * Update McTcpClient.cs + bot.OnSetExperience * Update Protocol18.cs + Fix * Update McTcpClient.cs + PlayerDigging * Update PacketOutgoingType.cs + PlayerDigging * Update Protocol18PacketTypes.cs + case PacketOutgoingType.PlayerDigging * Update Protocol18.cs + SendPlayerDigging * Update IMinecraftCom.cs + SendPlayerDigging * Update McTcpClient.cs + PlayerDigging * Update Protocol16.cs + SendPlayerDigging * Update ChatBot.cs + PlayerDigging * Update ChatBot.cs + Fix * Update McTcpClient.cs + Fix * Update ChatBot.cs Add WindowAction * ChatBot.cs Fixes * Further ChatBot.cs fixes * Further ChatBot.cs fixes * Protocol Handler fixes * Protocol Handler fixes * IMinecraftCom fixes * documentation fixes Co-authored-by: ORelio <oreliogitantispam.l0gin@spamgourmet.com>
2020-05-29 23:18:34 +05:00
}
public async Task<bool> SendInteractEntity(int EntityID, int type, int hand)
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
{
try
{
List<byte> fields = new();
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
fields.AddRange(dataTypes.GetVarInt(EntityID));
fields.AddRange(dataTypes.GetVarInt(type));
fields.AddRange(dataTypes.GetVarInt(hand));
// Is player Sneaking (Only 1.16 and above)
// Currently hardcoded to false
// TODO: Update to reflect the real player state
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_16_Version)
fields.AddRange(dataTypes.GetBool(false));
await SendPacket(PacketTypesOut.InteractEntity, fields);
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SendInteractEntity(int EntityID, int type, float X, float Y, float Z)
{
return await Task.FromResult(false);
}
public async Task<bool> SendUseItem(int hand, int sequenceId)
{
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_9_Version)
2020-06-29 21:52:17 +02:00
return false; // Packet does not exist prior to MC 1.9
2020-06-29 21:59:04 +02:00
// According to https://wiki.vg/index.php?title=Protocol&oldid=5486#Player_Block_Placement
// MC 1.7 does this using Player Block Placement with special values
// TODO once Player Block Placement is implemented for older versions
2020-06-29 21:52:17 +02:00
try
{
List<byte> packet = new();
2020-06-29 21:52:17 +02:00
packet.AddRange(dataTypes.GetVarInt(hand));
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
packet.AddRange(dataTypes.GetVarInt(sequenceId));
await SendPacket(PacketTypesOut.UseItem, packet);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
public async Task<bool> SendPlayerDigging(int status, Location location, Direction face, int sequenceId)
{
2020-06-29 21:52:17 +02:00
try
{
List<byte> packet = new();
2020-06-29 21:52:17 +02:00
packet.AddRange(dataTypes.GetVarInt(status));
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(dataTypes.GetVarInt(dataTypes.GetBlockFace(face)));
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
packet.AddRange(dataTypes.GetVarInt(sequenceId));
await SendPacket(PacketTypesOut.PlayerDigging, packet);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SendPlayerBlockPlacement(int hand, Location location, Direction face, int sequenceId)
{
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_14_Version)
2020-06-29 21:52:17 +02:00
return false; // NOT IMPLEMENTED for older MC versions
try
{
List<byte> packet = new();
2020-06-29 21:52:17 +02:00
packet.AddRange(dataTypes.GetVarInt(hand));
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(dataTypes.GetVarInt(dataTypes.GetBlockFace(face)));
packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorX
packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorY
packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorZ
packet.Add(0); // insideBlock = false;
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_19_Version)
packet.AddRange(dataTypes.GetVarInt(sequenceId));
await SendPacket(PacketTypesOut.PlayerBlockPlacement, packet);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SendHeldItemChange(short slot)
{
2020-06-29 21:52:17 +02:00
try
{
List<byte> packet = new();
2020-06-29 21:52:17 +02:00
packet.AddRange(dataTypes.GetShort(slot));
await SendPacket(PacketTypesOut.HeldItemChange, packet);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SendWindowAction(int windowId, int slotId, WindowActionType action, Item? item, List<Tuple<short, Item?>> changedSlots, int stateId)
2020-06-29 21:52:17 +02:00
{
try
{
short actionNumber;
lock (window_actions)
{
if (!window_actions.ContainsKey(windowId))
window_actions[windowId] = 0;
actionNumber = (short)(window_actions[windowId] + 1);
window_actions[windowId] = actionNumber;
}
2020-06-29 21:52:17 +02:00
byte button = 0;
byte mode = 0;
2020-06-29 21:52:17 +02:00
switch (action)
{
case WindowActionType.LeftClick: button = 0; break;
case WindowActionType.RightClick: button = 1; break;
case WindowActionType.MiddleClick: button = 2; mode = 3; break;
case WindowActionType.ShiftClick: button = 0; mode = 1; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.DropItem: button = 0; mode = 4; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.DropItemStack: button = 1; mode = 4; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.StartDragLeft: button = 0; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.StartDragRight: button = 4; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.StartDragMiddle: button = 8; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.EndDragLeft: button = 2; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.EndDragRight: button = 6; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.EndDragMiddle: button = 10; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.AddDragLeft: button = 1; mode = 5; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.AddDragRight: button = 5; mode = 5; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.AddDragMiddle: button = 9; mode = 5; item = new Item(ItemType.Null, 0, null); break;
2020-06-29 21:52:17 +02:00
}
2020-05-26 14:02:09 +05:00
List<byte> packet = new()
{
(byte)windowId // Window ID
};
// 1.18+
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_18_1_Version)
{
2022-07-25 01:13:41 +08:00
packet.AddRange(dataTypes.GetVarInt(stateId)); // State ID
packet.AddRange(dataTypes.GetShort((short)slotId)); // Slot ID
}
// 1.17.1
2022-09-02 09:29:24 +08:00
else if (protocolVersion == MC_1_17_1_Version)
{
2022-07-25 01:13:41 +08:00
packet.AddRange(dataTypes.GetShort((short)slotId)); // Slot ID
packet.AddRange(dataTypes.GetVarInt(stateId)); // State ID
}
// Older
else
{
2022-07-25 01:13:41 +08:00
packet.AddRange(dataTypes.GetShort((short)slotId)); // Slot ID
}
2022-07-25 01:13:41 +08:00
packet.Add(button); // Button
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_17_Version)
2022-07-25 01:13:41 +08:00
packet.AddRange(dataTypes.GetShort(actionNumber));
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_9_Version)
2022-07-25 01:13:41 +08:00
packet.AddRange(dataTypes.GetVarInt(mode)); // Mode
2020-06-29 21:52:17 +02:00
else packet.Add(mode);
// 1.17+ Array of changed slots
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_17_Version)
{
2022-07-25 01:13:41 +08:00
packet.AddRange(dataTypes.GetVarInt(changedSlots.Count)); // Length of the array
foreach (var slot in changedSlots)
{
packet.AddRange(dataTypes.GetShort(slot.Item1)); // slot ID
packet.AddRange(dataTypes.GetItemSlot(slot.Item2, itemPalette)); // slot Data
}
}
packet.AddRange(dataTypes.GetItemSlot(item, itemPalette)); // Carried item (Clicked item)
await SendPacket(PacketTypesOut.ClickWindow, packet);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SendCreativeInventoryAction(int slot, ItemType itemType, int count, Dictionary<string, object>? nbt)
{
2020-06-29 21:52:17 +02:00
try
{
List<byte> packet = new();
2020-06-29 21:52:17 +02:00
packet.AddRange(dataTypes.GetShort((short)slot));
packet.AddRange(dataTypes.GetItemSlot(new Item(itemType, count, nbt), itemPalette));
await SendPacket(PacketTypesOut.CreativeInventoryAction, packet);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
2022-10-12 19:51:01 +02:00
}
public async Task<bool> ClickContainerButton(int windowId, int buttonId)
2022-10-12 19:51:01 +02:00
{
try
{
List<byte> packet = new()
{
(byte)windowId,
(byte)buttonId
};
await SendPacket(PacketTypesOut.ClickWindowButton, packet);
2022-10-12 19:51:01 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2022-10-12 19:51:01 +02:00
catch (ObjectDisposedException) { return false; }
}
2020-06-29 21:52:17 +02:00
public async Task<bool> SendAnimation(int animation, int playerid)
{
2020-06-29 21:52:17 +02:00
try
{
if (animation == 0 || animation == 1)
{
List<byte> packet = new();
2020-06-29 21:52:17 +02:00
2022-09-02 09:29:24 +08:00
if (protocolVersion < MC_1_8_Version)
2020-06-29 21:52:17 +02:00
{
packet.AddRange(dataTypes.GetInt(playerid));
packet.Add(1); // Swing arm
2020-06-29 21:52:17 +02:00
}
2022-09-02 09:29:24 +08:00
else if (protocolVersion < MC_1_9_Version)
2020-06-29 21:52:17 +02:00
{
// No fields in 1.8.X
}
else // MC 1.9+
{
packet.AddRange(dataTypes.GetVarInt(animation));
}
await SendPacket(PacketTypesOut.Animation, packet);
2020-06-29 21:52:17 +02:00
return true;
}
else
{
return false;
}
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SendCloseWindow(int windowId)
Implement OnMapData, OnTitle, UpdateSign, OnEntityEquipment, Useblock (#1071) * + Fix null PlayerInventory + Fix null PlayerInventory * Update Protocol18.cs * Update McTcpClient.cs + Fix https://github.com/ORelio/Minecraft-Console-Client/issues/1022 * Update Protocol18.cs + MapData * Update PacketIncomingType.cs + MapData * Update Protocol18PacketTypes.cs * Update IMinecraftComHandler.cs + OnMapData * Update McTcpClient.cs + OnMapData * Update ChatBot.cs + OnMapData * Update Protocol18.cs * Update Protocol18PacketTypes.cs + Fix * Update PacketIncomingType.cs + Title * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnTitle * Update McTcpClient.cs * Update ChatBot.cs + OnTitle * Update Protocol18.cs Fix * Update IMinecraftComHandler.cs * Update McTcpClient.cs * add ClearInventories() * add ClearInventories() * Update McTcpClient.cs + OnTitle * Preparing to Add BlockAction * Update PacketOutgoingType.cs * Update PacketOutgoingType.cs * Update Protocol18.cs + SendUpdateSign * Update Protocol16.cs + SendUpdateSign * Update IMinecraftCom.cs + SendUpdateSign * Update McTcpClient.cs + UpdateSign * Update ChatBot.cs + UpdateSign * Update McTcpClient.cs Update PlaceBlock * Update ChatBot.cs * Update McTcpClient.cs * add SendCreativeInventoryAction nbt add SendCreativeInventoryAction nbt * Update Protocol18.cs * Update Protocol16.cs * Update McTcpClient.cs * Update ChatBot.cs * Update Inventory.cs * Update Protocol18PacketTypes.cs * Update PacketIncomingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18PacketTypes.cs Fix * Update Protocol18PacketTypes.cs Fix * Update IMinecraftComHandler.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McTcpClient.cs + OnEntityEquipment * Update Protocol18.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update Protocol18.cs * Update McTcpClient.cs * Update ChatBot.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update Protocol18.cs * Create Useblock.cs * Update MinecraftClient.csproj * Update McTcpClient.cs
2020-06-20 17:57:07 +05:00
{
2020-06-29 21:52:17 +02:00
try
{
lock (window_actions)
{
if (window_actions.ContainsKey(windowId))
window_actions[windowId] = 0;
}
await SendPacket(PacketTypesOut.CloseWindow, new[] { (byte)windowId });
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
Implement OnMapData, OnTitle, UpdateSign, OnEntityEquipment, Useblock (#1071) * + Fix null PlayerInventory + Fix null PlayerInventory * Update Protocol18.cs * Update McTcpClient.cs + Fix https://github.com/ORelio/Minecraft-Console-Client/issues/1022 * Update Protocol18.cs + MapData * Update PacketIncomingType.cs + MapData * Update Protocol18PacketTypes.cs * Update IMinecraftComHandler.cs + OnMapData * Update McTcpClient.cs + OnMapData * Update ChatBot.cs + OnMapData * Update Protocol18.cs * Update Protocol18PacketTypes.cs + Fix * Update PacketIncomingType.cs + Title * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnTitle * Update McTcpClient.cs * Update ChatBot.cs + OnTitle * Update Protocol18.cs Fix * Update IMinecraftComHandler.cs * Update McTcpClient.cs * add ClearInventories() * add ClearInventories() * Update McTcpClient.cs + OnTitle * Preparing to Add BlockAction * Update PacketOutgoingType.cs * Update PacketOutgoingType.cs * Update Protocol18.cs + SendUpdateSign * Update Protocol16.cs + SendUpdateSign * Update IMinecraftCom.cs + SendUpdateSign * Update McTcpClient.cs + UpdateSign * Update ChatBot.cs + UpdateSign * Update McTcpClient.cs Update PlaceBlock * Update ChatBot.cs * Update McTcpClient.cs * add SendCreativeInventoryAction nbt add SendCreativeInventoryAction nbt * Update Protocol18.cs * Update Protocol16.cs * Update McTcpClient.cs * Update ChatBot.cs * Update Inventory.cs * Update Protocol18PacketTypes.cs * Update PacketIncomingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18PacketTypes.cs Fix * Update Protocol18PacketTypes.cs Fix * Update IMinecraftComHandler.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McTcpClient.cs + OnEntityEquipment * Update Protocol18.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update Protocol18.cs * Update McTcpClient.cs * Update ChatBot.cs * Update ChatBot.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update McTcpClient.cs * Update Protocol18.cs * Create Useblock.cs * Update MinecraftClient.csproj * Update McTcpClient.cs
2020-06-20 17:57:07 +05:00
}
public async Task<bool> SendUpdateSign(Location sign, string line1, string line2, string line3, string line4)
2020-06-29 21:52:17 +02:00
{
try
{
if (line1.Length > 23)
line1 = line1[..23];
2020-06-29 21:52:17 +02:00
if (line2.Length > 23)
line2 = line1[..23];
2020-06-29 21:52:17 +02:00
if (line3.Length > 23)
line3 = line1[..23];
2020-06-29 21:52:17 +02:00
if (line4.Length > 23)
line4 = line1[..23];
2020-06-29 21:52:17 +02:00
List<byte> packet = new();
2020-06-29 21:52:17 +02:00
packet.AddRange(dataTypes.GetLocation(sign));
packet.AddRange(dataTypes.GetString(line1));
packet.AddRange(dataTypes.GetString(line2));
packet.AddRange(dataTypes.GetString(line3));
packet.AddRange(dataTypes.GetString(line4));
await SendPacket(PacketTypesOut.UpdateSign, packet);
2020-06-29 21:52:17 +02:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-06-29 21:52:17 +02:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> UpdateCommandBlock(Location location, string command, CommandBlockMode mode, CommandBlockFlags flags)
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
{
2022-09-02 09:29:24 +08:00
if (protocolVersion <= MC_1_13_Version)
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
{
try
{
List<byte> packet = new();
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(dataTypes.GetString(command));
packet.AddRange(dataTypes.GetVarInt((int)mode));
packet.Add((byte)flags);
await SendPacket(PacketTypesOut.UpdateSign, packet);
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
catch (ObjectDisposedException) { return false; }
}
else { return false; }
ChatBot API: OnScoreboardObjective, OnEntityEffect, OnUpdateScore, EntityInteract, Hand list (#1097) * Create Effect.cs * Rename Effect.cs to Effects.cs * Update MinecraftClient.csproj * Update Effects.cs * Update Effects.cs * add EntityEffect * Update McClient.cs * Update Protocol18.cs + EntityEffect * Update IMinecraftComHandler.cs * Update Protocol18PacketTypes.cs + EntityEffect * Update ChatBot.cs + OnEntityEquipment * Update PacketIncomingType.cs + ScoreboardObjective * Update Protocol18PacketTypes.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs + OnScoreboardObjective * Update McClient.cs + OnScoreboardObjective * Update ChatBot.cs + OnScoreboardObjective event * Update Protocol18.cs: fix scoreboard * Update McClient.cs * Update ChatBot.cs * Update PacketIncomingType.cs * Update ChatBot.cs + OnUpdateScore * Update McClient.cs + OnUpdateScore * Update IMinecraftComHandler.cs + OnUpdateScore * Update Protocol18.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs + fix micro lags * Update Protocol18.cs * Update Protocol18.cs * Update Protocol18.cs * Update Protocol16.cs * Update Protocol18.cs * Update McClient.cs * Update IMinecraftCom.cs * Update McClient.cs * Update McClient.cs * Update McClient.cs * Update ChatBot.cs + GetEntities() * Create Hand.cs * Update MinecraftClient.csproj * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs * Update ChatBot.cs: fix * Update AutoAttack.cs: Fix * Update McClient.cs: compile fix * Update ChatBot.cs * Update AutoAttack.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update IMinecraftComHandler.cs * Update ChatBot.cs * Update McClient.cs * Update McClient.cs: remove check distance * Update EntityActionType.cs: more actions * Create CommandBlockMode.cs * Create CommandBlockFlags.cs * Update IMinecraftCom.cs * Update McClient.cs * Update ChatBot.cs * Update Protocol18.cs * Update Protocol16.cs * Update PacketOutgoingType.cs * Update Protocol18PacketTypes.cs * Update Protocol18.cs
2020-07-04 13:45:51 +05:00
}
2020-07-31 16:34:16 +08:00
public async Task<bool> SendWindowConfirmation(byte windowID, short actionID, bool accepted)
2020-07-31 16:34:16 +08:00
{
try
{
List<byte> packet = new()
{
windowID
};
2020-07-31 16:34:16 +08:00
packet.AddRange(dataTypes.GetShort(actionID));
packet.Add(accepted ? (byte)1 : (byte)0);
await SendPacket(PacketTypesOut.WindowConfirmation, packet);
2020-07-31 16:34:16 +08:00
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
2020-07-31 16:34:16 +08:00
catch (ObjectDisposedException) { return false; }
}
public async Task<bool> SelectTrade(int selectedSlot)
{
// MC 1.13 or greater
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_13_Version)
{
try
{
List<byte> packet = new();
packet.AddRange(dataTypes.GetVarInt(selectedSlot));
await SendPacket(PacketTypesOut.SelectTrade, packet);
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
else { return false; }
}
public async Task<bool> SendSpectate(Guid UUID)
{
// MC 1.8 or greater
2022-09-02 09:29:24 +08:00
if (protocolVersion >= MC_1_8_Version)
{
try
{
List<byte> packet = new();
packet.AddRange(dataTypes.GetUUID(UUID));
await SendPacket(PacketTypesOut.Spectate, packet);
return true;
}
catch (SocketException) { return false; }
catch (IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
else { return false; }
}
private byte[] GenerateSalt()
{
byte[] salt = new byte[8];
randomGen.GetNonZeroBytes(salt);
return salt;
}
[GeneratedRegex("\\s+")]
private static partial Regex GetCharFilterRegex1();
[GeneratedRegex("\\s$")]
private static partial Regex GetCharFilterRegex2();
}
}