forked from bdfzchen2015/ServerSideCharacter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerPlayer.cs
234 lines (186 loc) · 5.56 KB
/
ServerPlayer.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using ServerSideCharacter.GroupManage;
using ServerSideCharacter.Region;
using Terraria;
using Terraria.Chat;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace ServerSideCharacter
{
public class ServerPlayer
{
private static int _nextId;
public bool HasPassword { get; set; }
public bool IsLogin { get; set; }
public bool TpProtect { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public int Uuid { get; set; }
public Group PermissionGroup { get; set; }
public int LifeMax { get; set; }
public int StatLife { get; set; }
public int ManaMax { get; set; }
public int StatMana { get; set; }
public Item[] Inventory = new Item[59];
public Item[] Armor = new Item[20];
public Item[] Dye = new Item[10];
public Item[] MiscEquips = new Item[5];
public Item[] MiscDye = new Item[5];
public Chest Bank = new Chest(true);
public Chest Bank2 = new Chest(true);
public Chest Bank3 = new Chest(true);
public Player PrototypePlayer { get; set; }
public RegionInfo EnteredRegion { get; set; }
public List<RegionInfo> Ownedregion = new List<RegionInfo>();
private void SetupPlayer()
{
for (int i = 0; i < Inventory.Length; i++)
Inventory[i] = new Item();
for (int i = 0; i < Armor.Length; i++)
Armor[i] = new Item();
for (int i = 0; i < Dye.Length; i++)
Dye[i] = new Item();
for (int i = 0; i < MiscEquips.Length; i++)
MiscEquips[i] = new Item();
for (int i = 0; i < MiscDye.Length; i++)
MiscDye[i] = new Item();
for (int i = 0; i < Bank.item.Length; i++)
Bank.item[i] = new Item();
for (int i = 0; i < Bank2.item.Length; i++)
Bank2.item[i] = new Item();
for (int i = 0; i < Bank3.item.Length; i++)
Bank3.item[i] = new Item();
}
public ServerPlayer()
{
SetupPlayer();
}
public ServerPlayer(Player player)
{
SetupPlayer();
PrototypePlayer = player;
}
public void CopyFrom(Player player)
{
LifeMax = player.statLifeMax;
StatLife = player.statLife;
StatMana = player.statMana;
ManaMax = player.statManaMax;
player.inventory.CopyTo(Inventory, 0);
player.armor.CopyTo(Armor, 0);
player.dye.CopyTo(Dye, 0);
player.miscEquips.CopyTo(MiscEquips, 0);
player.miscDyes.CopyTo(MiscDye, 0);
Bank = (Chest)player.bank.Clone();
Bank2 = (Chest)player.bank2.Clone();
Bank3 = (Chest)player.bank3.Clone();
}
public void ApplyLockBuffs(int time = 180)
{
ServerSideCharacter.Instance.TryFind("Locked", out ModBuff buff);
PrototypePlayer.AddBuff(buff.Type, time * 2, false);
PrototypePlayer.AddBuff(BuffID.Frozen, time, false);
NetMessage.SendData(MessageID.AddPlayerBuff, PrototypePlayer.whoAmI, -1,
NetworkText.Empty, PrototypePlayer.whoAmI,
buff.Type, time * 2);
NetMessage.SendData(MessageID.AddPlayerBuff, PrototypePlayer.whoAmI, -1,
NetworkText.Empty, PrototypePlayer.whoAmI,
BuffID.Frozen, time);
}
public void SendSuccessInfo(string msg)
{
ChatHelper.SendChatMessageToClient(NetworkText.FromLiteral(msg), new Color(255, 50, 255, 50), PrototypePlayer.whoAmI);
}
public void SendInfo(string msg)
{
ChatHelper.SendChatMessageToClient(NetworkText.FromLiteral(msg), new Color(255, 255, 255, 0), PrototypePlayer.whoAmI);
}
public void SendErrorInfo(string msg)
{
ChatHelper.SendChatMessageToClient(NetworkText.FromLiteral(msg), new Color(255, 20, 20, 0), PrototypePlayer.whoAmI);
}
// public static string GenHashCode(string name)
//{
// long hash = name.GetHashCode();
// hash += DateTime.Now.ToLongTimeString().GetHashCode() * 233;
// short res = (short)(hash % 65536);
// return Convert.ToString(res, 16);
//}
public static ServerPlayer CreateNewPlayer(Player p)
{
ServerPlayer player = new ServerPlayer(p);
int i = 0;
foreach (var item in ServerSideCharacter.Config.StartupItems)
{
player.Inventory[i++] = Utils.GetItemFromNet(item);
}
player.Name = p.name;
player.Uuid = GetNextId();
player.HasPassword = false;
player.PermissionGroup = ServerSideCharacter.GroupManager.Groups["default"];
player.IsLogin = false;
player.TpProtect = true;
player.Password = "";
player.LifeMax = 100;
player.StatLife = 100;
player.ManaMax = 20;
player.StatMana = 20;
return player;
}
public static void SendInfoToAll(string msg)
{
ChatHelper.BroadcastChatMessage(NetworkText.FromLiteral(msg), new Color(255, 255, 255));
}
public static ServerPlayer FindPlayer(int uuid)
{
foreach (var pair in ServerSideCharacter.XmlData.Data)
{
if (pair.Value.Uuid == uuid)
{
return pair.Value;
}
}
throw new Exception("Cannot find the player!");
}
/// <summary>
/// 如果玩家在任何领地内
/// </summary>
/// <param name="region"></param>
/// <returns></returns>
public bool InAnyRegion(out RegionInfo region)
{
foreach (var reg in ServerSideCharacter.RegionManager.GetList())
{
Rectangle worldArea = new Rectangle(reg.Area.X * 16, reg.Area.Y * 16,
reg.Area.Width * 16, reg.Area.Height * 16);
if (worldArea.Intersects(PrototypePlayer.Hitbox))
{
region = reg;
return true;
}
}
region = null;
return false;
}
public void SavePlayer()
{
CopyFrom(PrototypePlayer);
ServerSideCharacter.MainWriter.Write(this);
}
private static int GetNextId()
{
return _nextId++;
}
internal static void IncreaseUuid()
{
_nextId++;
}
internal static void ResetUuid()
{
_nextId = 0;
}
}
}