-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protocol.AsyncSending.cs
150 lines (125 loc) · 5.15 KB
/
Protocol.AsyncSending.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MineLib.Core;
using MineLib.Core.Data.Structs;
using ProtocolClassic.Enums;
using ProtocolClassic.Packets.Client;
namespace ProtocolClassic
{
public partial class Protocol
{
private Dictionary<Type, Func<ISendingAsyncArgs, Task>> SendingAsyncHandlers { get; set; }
public void RegisterSending(Type sendingAsyncType, Func<ISendingAsyncArgs, Task> func)
{
var any = sendingAsyncType.GetTypeInfo().ImplementedInterfaces.Any(p => p == typeof(ISendingAsync));
if (!any)
throw new InvalidOperationException("AsyncSending type must implement MineLib.Network.IAsyncSending");
SendingAsyncHandlers[sendingAsyncType] = func;
}
private void RegisterSupportedSendings()
{
RegisterSending(typeof(ConnectToServerAsync), ConnectToServerAsync);
RegisterSending(typeof(PlayerMovedAsync), PlayerMovedAsync);
RegisterSending(typeof(PlayerSetRemoveBlockAsync), PlayerSetRemoveBlockAsync);
RegisterSending(typeof(SendMessageAsync), SendMessageAsync);
}
public Task DoSendingAsync(Type sendingAsyncType, ISendingAsyncArgs args)
{
var any = sendingAsyncType.GetTypeInfo().ImplementedInterfaces.Any(p => p == typeof(ISendingAsync));
if (!any)
throw new InvalidOperationException("AsyncSending type must implement MineLib.Network.IAsyncSending");
return SendingAsyncHandlers[sendingAsyncType](args);
}
public void DoSending(Type sendingType, ISendingAsyncArgs args)
{
var any = sendingType.GetTypeInfo().ImplementedInterfaces.Any(p => p == typeof(ISendingAsync));
if (!any)
throw new InvalidOperationException("AsyncSending type must implement MineLib.Network.IAsyncSending");
SendingAsyncHandlers[sendingType](args).Wait();
}
private Task ConnectToServerAsync(ISendingAsyncArgs args)
{
var data = (ConnectToServerAsyncArgs) args;
State = ConnectionState.Joining;
return SendPacketAsync(new PlayerIdentificationPacket
{
ProtocolVersion = 0x07,
Username = _minecraft.ClientUsername,
VerificationKey = _minecraft.AccessToken,
UnUsed = 0x42
});
}
private Task PlayerMovedAsync(ISendingAsyncArgs args)
{
var data = (PlayerMovedAsyncArgs)args;
switch (data.Mode)
{
case PlaverMovedMode.OnGround:
{
var pdata = (PlaverMovedDataOnGround)data.Data;
return null;
}
case PlaverMovedMode.Vector3:
{
var pdata = (PlaverMovedDataVector3)data.Data;
return null;
}
case PlaverMovedMode.YawPitch:
{
var pdata = (PlaverMovedDataYawPitch)data.Data;
return null;
}
case PlaverMovedMode.All:
{
var pdata = (PlaverMovedDataAll)data.Data;
return SendPacketAsync(new PositionAndOrientationPacket
{
Position = pdata.Vector3,
Yaw = (byte) pdata.Yaw,
Pitch = (byte) pdata.Pitch,
PlayerID = 255
});
}
default:
throw new Exception("PacketError");
}
}
private Task PlayerSetRemoveBlockAsync(ISendingAsyncArgs args)
{
var data = (PlayerSetRemoveBlockAsyncArgs)args;
switch (data.Mode)
{
case PlayerSetRemoveBlockMode.Place:
{
var pdata = (PlayerSetRemoveBlockDataPlace) data.Data;
return null;
}
case PlayerSetRemoveBlockMode.Dig:
{
var pdata = (PlayerSetRemoveBlockDataDig) data.Data;
return null;
}
case PlayerSetRemoveBlockMode.Remove:
{
var pdata = (PlayerSetRemoveBlockDataRemove) data.Data;
return SendPacketAsync(new SetBlockPacket
{
Coordinates = pdata.Location,
BlockType = (byte) pdata.BlockID,
Mode = (SetBlockMode) pdata.Mode
});
}
default:
throw new Exception("PacketError");
}
}
private Task SendMessageAsync(ISendingAsyncArgs args)
{
var data = (SendMessageAsyncArgs) args;
return SendPacketAsync(new MessagePacket { Message = data.Message });
}
}
}