-
Notifications
You must be signed in to change notification settings - Fork 8
/
CommandSell.cs
234 lines (232 loc) · 9.32 KB
/
CommandSell.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 Rocket.API;
using Rocket.Core.Logging;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using SDG.Unturned;
using System;
using System.Collections.Generic;
using UnityEngine;
using fr34kyn01535.Uconomy;
namespace LIGHT
{
public class CommandSell : IRocketCommand
{
public AllowedCaller AllowedCaller
{
get
{
return AllowedCaller.Player;
}
}
public string Name
{
get
{
return "sell";
}
}
public string Help
{
get
{
return "Allows you to sell items to the shop from your inventory.";
}
}
public string Syntax
{
get
{
return "<name or id> [amount]";
}
}
public List<string> Aliases
{
get { return new List<string>(); }
}
public List<string> Permissions
{
get
{
return new List<string>() {"sell"};
}
}
public void Execute(IRocketPlayer caller, params string[] command)
{
if (!LIGHT.Instance.Configuration.Instance.EnableShop)
{
UnturnedChat.Say(caller, LIGHT.Instance.Translate("shop_disable"));
return;
}
UnturnedPlayer player = (UnturnedPlayer)caller;
if (command.Length == 0)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("sell_command_usage"));
return;
}
byte amttosell = 1;
byte amt = amttosell;
ushort id;
if (!LIGHT.Instance.Configuration.Instance.CanSellItems)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("sell_items_off"));
return;
}
string name = null;
ItemAsset vAsset = null;
string ItemName = "";
bool HaveAmt = false;
if (command.Length > 1)
{
HaveAmt = byte.TryParse(command[command.Length - 1], out amt);
if (!HaveAmt)
{
amt = 1;
if (command.Length == 2)
ItemName = command[0] + " " + command[1];
else if (command.Length == 1)
ItemName = command[0];
}
else
{
for (int i = 0; i < (command.Length - 1); i++)
{
if (i == (command.Length - 2))
ItemName += command[i];
else
ItemName += command[i] + " ";
}
}
}
else
ItemName = command[0].Trim();
amttosell = amt;
if (!ushort.TryParse(ItemName, out id))
{
Asset[] array = Assets.find(EAssetType.ITEM);
Asset[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
vAsset = (ItemAsset)array2[i];
if (vAsset != null && vAsset.Name != null && vAsset.Name.ToLower().Contains(ItemName.ToLower()))
{
id = vAsset.Id;
name = vAsset.Name;
break;
}
}
}
if (name == null && id == 0)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("could_not_find", ItemName));
return;
}
else if (name == null && id != 0)
{
try
{
vAsset = (ItemAsset)Assets.find(EAssetType.ITEM, id);
name = vAsset.Name;
}
catch
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("item_invalid"));
return;
}
}
if (player.Inventory.has(id) == null)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("not_have_item_sell", name));
return;
}
List<InventorySearch> list = player.Inventory.search(id, true, true);
if (list.Count == 0 || (vAsset.Amount == 1 && list.Count < amttosell))
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("not_enough_items_sell", amttosell.ToString(), name));
return;
}
if (vAsset.Amount > 1)
{
int ammomagamt = 0;
foreach (InventorySearch ins in list)
{
ammomagamt += ins.ItemJar.Item.Amount;
}
if (ammomagamt < amttosell)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("not_enough_ammo_sell", name));
return;
}
}
// We got this far, so let's buy back the items and give them money.
// Get cost per item. This will be whatever is set for most items, but changes for ammo and magazines.
decimal price = LIGHT.Instance.ShopDB.GetItemBuyPrice(id);
if (price <= 0.00m)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("no_sell_price_set", name));
return;
}
byte quality = 100;
decimal peritemprice = 0;
decimal addmoney = 0;
switch (vAsset.Amount)
{
case 1:
// These are single items, not ammo or magazines
while (amttosell > 0)
{
if (player.Player.Equipment.checkSelection(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY))
{
player.Player.Equipment.dequip();
}
if (LIGHT.Instance.Configuration.Instance.QualityCounts)
quality = list[0].ItemJar.Item.Durability;
peritemprice = decimal.Round(price * (quality / 100.0m), 2);
addmoney += peritemprice;
player.Inventory.removeItem(list[0].InventoryGroup, player.Inventory.getIndex(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY));
list.RemoveAt(0);
amttosell--;
}
break;
default:
// This is ammo or magazines
byte amttosell1 = amttosell;
while (amttosell > 0)
{
if (player.Player.Equipment.checkSelection(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY))
{
player.Player.Equipment.dequip();
}
if (list[0].ItemJar.Item.Amount >= amttosell)
{
byte left = (byte)(list[0].ItemJar.Item.Amount - amttosell);
list[0].ItemJar.Item.Amount = left;
player.Inventory.sendUpdateAmount(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY, left);
amttosell = 0;
if (left == 0)
{
player.Inventory.removeItem(list[0].InventoryGroup, player.Inventory.getIndex(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY));
list.RemoveAt(0);
}
}
else
{
amttosell -= list[0].ItemJar.Item.Amount;
player.Inventory.sendUpdateAmount(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY, 0);
player.Inventory.removeItem(list[0].InventoryGroup, player.Inventory.getIndex(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY));
list.RemoveAt(0);
}
}
peritemprice = decimal.Round(price * ((decimal)amttosell1 / (decimal)vAsset.Amount), 2);
addmoney += peritemprice;
break;
}
decimal balance = Uconomy.Instance.Database.IncreaseBalance(player.Id, addmoney);
if (OnShopSell != null)
OnShopSell(player, addmoney, amttosell, id);
player.Player.gameObject.SendMessage("ZaupShopOnSell", new object[] { player, addmoney, amt, id }, SendMessageOptions.DontRequireReceiver);
UnturnedChat.Say(player, LIGHT.Instance.Translate("sold_items",amt, name, addmoney, Uconomy.Instance.Configuration.Instance.MoneyName, balance, Uconomy.Instance.Configuration.Instance.MoneyName));
}
public delegate void PlayerShopSell(UnturnedPlayer player, decimal amt, byte items, ushort item);
public event PlayerShopSell OnShopSell;
}
}