-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
253 lines (226 loc) · 7.42 KB
/
Player.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
*
* _ __ _ _ ____ ____
*\ \ / / _ _ __(_) | _____ / ___/ ___|
* \ V / | | | '__| | |/ / _ \ _____| | \___ \
* | || |_| | | | | < (_) |_____| |___ ___) |
* |_| \__,_|_| |_|_|\_\___/ \____|____/
*
* Yuriko-CS, brand a new Minecraft: Pocket Edition
* server software in C#
* Copyright 2016 ItalianDevs4PM.
*
* See LICENSE.md for the license applied to this file/project
*
*
* @author ItalianDevs4PM
* @link http://github.com/ItalianDevs4PM
*
*
*/
using System;
using System.Collections.Generic;
using System.Net;
namespace YurikoCS {
class Player /*: CommandSender*/ {
private Dictionary<string, Permission> PlayerPermissions = new Dictionary<string, Permission>();
private Dictionary<int, Packet> PacketQueue = new Dictionary<int, Packet>();
private IPEndPoint endpoint;
private string username = "";
private int packetcount = 0;
private bool firstrecv = false;
private int firstpacket;
private int lastpacket;
public Player(IPEndPoint endpoint){
this.endpoint = endpoint;
Server.GetInstance().OnlinePlayers.Add(this);
}
public bool HasPermission(string node){
return PlayerPermissions.ContainsKey(node.ToLower());
}
public bool AddPermission(Permission permission){
if(HasPermission(permission.GetNode())){
return false;
}
PlayerPermissions.Add(permission.GetNode(), permission);
return true;
}
public bool OverridePermission(Permission permission){
if(HasPermission(permission.GetNode())){
PlayerPermissions[permission.GetNode()] = permission;
return true;
}
return false;
}
public bool RemovePermission(Permission permission){
if(HasPermission(permission.GetNode())){
PlayerPermissions.Remove(permission.GetNode());
return true;
}
return false;
}
public void RecalculatePermissions(){
Dictionary<string, Permission> globalpermissions = Permission.getGlobalPermissions();
foreach(Permission permission in globalpermissions.Values){
if(permission.GetPermissionLevel() == PermissionLevel.All){ //Add permission to All players
AddPermission(permission);
}else if(permission.GetPermissionLevel() == PermissionLevel.OP){ //Add permission only to OPs
if(IsOP()){
AddPermission(permission);
}
}else if(permission.GetPermissionLevel() == PermissionLevel.NotOP){ //Add permission only to players who are not OPs
if(!IsOP()){
AddPermission(permission);
}
}
}
}
public IPEndPoint GetIPEndPoint(){
return endpoint;
}
public string GetIPAddress(){
return endpoint.Address.ToString();
}
public short GetPort(){
return (short) endpoint.Port;
}
public string GetUsername(){
return username;
}
public Dictionary<int, Packet> GetPacketQueue(){
return PacketQueue;
}
/**
* [Unsafe] Sends a direct data packet to the client without checking if it has been received
*
*/
public void SendDirectDataPacket(Packet packet){
packet.SetPacketCount(new Triad(packetcount));
byte[] content = packet.Encode();
PacketHandler.server.Send(content, content.Length, GetIPEndPoint());
}
/**
* Sends a data packet to the client and resends it until it has been received from the client
*
*/
public void SendQueuedDataPacket(Packet packet){
if(PacketQueue.ContainsKey(packetcount)){
packet.SetPacketCount(new Triad(packetcount));
PacketQueue[packetcount] = packet;
packetcount++;
}else{
packet.SetPacketCount(new Triad(packetcount));
PacketQueue.Add(packetcount, packet);
packetcount++;
}
}
public void HandleDataPackets(byte[] data, IAsyncResult res){
Logger.GetLogger().Critical(packetcount.ToString());
if(data[0] >= 0x80 && data[0] <= 0x8F){ //Encapsulated Packet
EncapsulationHelper helper = EncapsulationHelper.Decode(data);
//Process Packet
byte packetID = helper.GetPacketID();
Logger.GetLogger().Debug(packetID.ToString("X2"));
if(packetID == PacketID.MCPE_PING_PACKET){
PingPacket ping = new PingPacket();
ping.data = data;
PongPacket pong = new PongPacket();
pong.Identifier = ping.Identifier;
pong.PacketCount = new Triad(packetcount);
SendQueuedDataPacket(pong);
}else{
//Send ACK to Client
// if(helper.GetPacketCount().ToInt32() == test){
// ACK ack = new ACK(new Triad(helper.GetPacketCount().ToInt32()));
// server.Send(ack.GetContent(), ack.GetContent().Length, sender);
// }else{
// ACK ack = new ACK(new Triad(test), new Triad(helper.GetPacketCount().ToInt32()));
// server.Send(ack.GetContent(), ack.GetContent().Length, sender);
// }
// test = helper.GetPacketCount().ToInt32();
// }
if(!firstrecv){
firstrecv = true;
firstpacket = helper.GetPacketCount().ToInt32();
}
if(res.IsCompleted){
firstrecv = false;
lastpacket = helper.GetPacketCount().ToInt32();
ACK ack = new ACK();
ack.FirstPacket = new Triad(firstpacket);
if(firstpacket == lastpacket){
ack.AdditionalPacket = true;
}else{
ack.AdditionalPacket = false;
ack.LastPacket = new Triad(lastpacket);
}
SendDirectDataPacket(ack);
}
if(packetID == PacketID.MCPE_CLIENT_CONNECT_PACKET){
ClientConnectPacket ccp = new ClientConnectPacket();
ccp.data = data;
//ACK ack = new ACK(ccp, new Triad(0));
//server.Send(ack.GetContent(), ack.GetContent().Length, sender);
ccp.Decode();
ServerHandshakePacket shp = new ServerHandshakePacket();
shp.ServerPort = (short) Server.GetInstance().GetPort();
shp.Session = ccp.Session;
SendQueuedDataPacket(shp);
Logger.GetLogger().Info("[§b" + GetIPAddress() + ":" + GetPort() + "§f] connected to the server");
}else if(packetID == PacketID.MCPE_LOGIN_PACKET){
LoginPacket lpacket = new LoginPacket();
lpacket.data = data;
lpacket.Decode();
//CALL PRELOGIN EVENT HERE
LoginStatusPacket lstatus = new LoginStatusPacket();
lstatus.Status = 1; //To-Do
lstatus.PacketCount = new Triad(packetcount);
SendQueuedDataPacket(lstatus);
//CALL LOGIN EVENT HERE
StartGamePacket startgame = new StartGamePacket(); //To-Do
startgame.Seed = 100;
startgame.Generator = 100;
startgame.Gamemode = 1;
startgame.SpawnX = 0;
startgame.SpawnY = 128;
startgame.SpawnZ = 0;
startgame.X = 0;
startgame.Y = 0;
startgame.Z = 0;
SendQueuedDataPacket(startgame);
Logger.GetLogger().Info("§e" + lpacket.Username + " joined the game");
SetTimePacket settime = new SetTimePacket(); //To-Do
settime.Ticks = 1000;
SendQueuedDataPacket(settime);
//CALL JOIN EVENT HERE
}
}
}else if(data[0] == PacketID.ACK){ //ACK
ACK ack = new ACK();
ack.data = data;
ack.Decode();
if(ack.AdditionalPacket){
int firstpacket = ack.FirstPacket.ToInt32();
if(PacketQueue.ContainsKey(firstpacket)){
PacketQueue.Remove(firstpacket);
}
}else{
int firstpacket = ack.FirstPacket.ToInt32();
int lastpacket = ack.LastPacket.ToInt32();
int orderedfirst = Math.Min(firstpacket, lastpacket);
int orderedlast = Math.Max(firstpacket, lastpacket);
for(int i = orderedfirst; i <= orderedlast; i++){
if(PacketQueue.ContainsKey(i)){
PacketQueue.Remove(i);
}
}
}
}else if(data[0] == PacketID.NACK){ //NACK
}
}
public bool IsOP(){
//To-Do
return false;
}
}
}