forked from Zamirathe/ZaupShop
-
Notifications
You must be signed in to change notification settings - Fork 19
/
CommandShop.cs
201 lines (188 loc) · 8.88 KB
/
CommandShop.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
using System;
using System.Runtime.Remoting.Messaging;
using Rocket.API.Commands;
using Rocket.API.Plugins;
using Rocket.Core.Commands;
using Rocket.Core.I18N;
using Rocket.Core.Plugins;
using SDG.Unturned;
namespace ZaupShop
{
public class CommandShop : ICommand
{
private readonly ZaupShop _parentPlugin;
public CommandShop(IPlugin plugin)
{
_parentPlugin = (ZaupShop)plugin;
}
public string Name => "shop";
public string Summary => "Allows admins to change, add, or remove items/vehicles from the shop.";
public string Description => null;
public string Permission => null;
public string Syntax => "<add | rem | chng | buy> [v.]<itemid> <cost>";
public IChildCommand[] ChildCommands => null;
public string[] Aliases => null;
public string Permissions => "shop";
public bool SupportsUser(Type user)
{
return true;
}
public void Execute(ICommandContext context)
{
var parameters = context.Parameters;
if (parameters.Length == 0)
{
throw new CommandWrongUsageException();
}
if (parameters.Length < 2)
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("no_itemid_given"));
}
if (parameters.Length == 2 && parameters[0] != "rem")
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("no_cost_given"));
}
if (parameters.Length >= 2)
{
string[] type = Parser.getComponentsFromSerial(parameters[1], '.');
if (type.Length > 1 && type[0] != "v")
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("v_not_provided"));
}
ushort id;
if (type.Length > 1)
{
if (!ushort.TryParse(type[1], out id))
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("invalid_id_given"));
}
}
else
{
if (!ushort.TryParse(type[0], out id))
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("invalid_id_given"));
}
}
// All basic checks complete. Let's get down to business.
bool success = false;
bool change = false;
bool pass = false;
switch (parameters[0])
{
case "chng":
change = true;
pass = true;
goto case "add";
case "add":
string ac = (pass) ? _parentPlugin.Translations.Get("changed") : _parentPlugin.Translations.Get("added");
switch (type[0])
{
case "v":
if (!IsAsset(id, "v"))
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("invalid_id_given"));
}
VehicleAsset va = (VehicleAsset)Assets.find(EAssetType.VEHICLE, id);
success = _parentPlugin.Database.AddVehicle(id, va.vehicleName, decimal.Parse(parameters[2]), change);
if (!success)
{
context.User.SendLocalizedMessage(_parentPlugin.Translations, "error_adding_or_changing", va.vehicleName);
return;
}
context.User.SendLocalizedMessage(_parentPlugin.Translations, "changed_or_added_to_shop", ac, va.vehicleName, parameters[2]);
break;
default:
if (!IsAsset(id, "i"))
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("invalid_id_given"));
}
ItemAsset ia = (ItemAsset)Assets.find(EAssetType.ITEM, id);
success = _parentPlugin.Database.AddItem(id, ia.itemName, decimal.Parse(parameters[2]), change);
if (!success)
{
context.User.SendLocalizedMessage(_parentPlugin.Translations, "error_adding_or_changing", ia.itemName);
return;
}
context.User.SendLocalizedMessage(_parentPlugin.Translations, "changed_or_added_to_shop", ac, ia.itemName, parameters[2]);
break;
}
break;
case "rem":
switch (type[0])
{
case "v":
if (!IsAsset(id, "i"))
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("invalid_id_given"));
}
VehicleAsset va = (VehicleAsset)Assets.find(EAssetType.VEHICLE, id);
success = _parentPlugin.Database.DeleteVehicle(id);
if (!success)
{
context.User.SendLocalizedMessage(_parentPlugin.Translations, "not_in_shop_to_remove", va.vehicleName);
return;
}
context.User.SendLocalizedMessage(_parentPlugin.Translations, "removed_from_shop", va.vehicleName);
break;
default:
if (!IsAsset(id, "i"))
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("invalid_id_given"));
}
ItemAsset ia = (ItemAsset)Assets.find(EAssetType.ITEM, id);
success = _parentPlugin.Database.DeleteItem(id);
if (!success)
{
context.User.SendLocalizedMessage(_parentPlugin.Translations, "not_in_shop_to_remove", ia.itemName);
return;
}
context.User.SendLocalizedMessage(_parentPlugin.Translations, "removed_from_shop", ia.itemName);
break;
}
break;
case "buy":
if (!IsAsset(id, "i"))
{
throw new CommandWrongUsageException(_parentPlugin.Translations.Get("invalid_id_given"));
}
ItemAsset iab = (ItemAsset)Assets.find(EAssetType.ITEM, id);
var buyb = parameters.Get<decimal>(2);
success = _parentPlugin.Database.SetBuyPrice(id, buyb);
if (!success)
{
context.User.SendLocalizedMessage(_parentPlugin.Translations, "not_in_shop_to_buyback", iab.itemName);
break;
}
context.User.SendLocalizedMessage(_parentPlugin.Translations, "set_buyback_price", iab.itemName, buyb.ToString());
break;
default:
// We shouldn't get this, but if we do send an error.
context.User.SendLocalizedMessage(_parentPlugin.Translations, "not_in_shop_to_remove");
return;
}
}
}
private bool IsAsset(ushort id, string type)
{
// Check for valid Item/Vehicle Id.
switch (type)
{
case "i":
if (Assets.find(EAssetType.ITEM, id) != null)
{
return true;
}
return false;
case "v":
if (Assets.find(EAssetType.VEHICLE, id) != null)
{
return true;
}
return false;
default:
return false;
}
}
}
}