Permalink
Browse files

Merge pull request #6 from cartman-2000/master

Fix for an InvalidCastException and an invalid item/vehicle id NRE error in the shop command.
  • Loading branch information...
2 parents bc046a9 + ab6fb3f commit 6aad3ed4cc0ee403c3b7ed7ffb5666d8a48f3176 @Zamirathe Zamirathe committed Jul 30, 2015
View
@@ -5,7 +5,6 @@
using Rocket.Core;
using Rocket.Core.Logging;
using Rocket.Core.Permissions;
-using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
@@ -56,13 +55,11 @@ public List<string> Permissions
public void Execute(IRocketPlayer caller, string[] msg)
{
bool console = (caller is ConsolePlayer);
- UnturnedPlayer playerid = (UnturnedPlayer)caller;
string[] permnames = { "shop.*", "shop.add", "shop.rem", "shop.chng", "shop.buy" };
bool[] perms = { false, false, false, false, false };
bool anyuse = false;
string message;
- List<string> permlist = (console) ? new List<string>() : R.Permissions.GetPermissions(playerid);
- foreach (string s in permlist)
+ foreach (string s in caller.GetPermissions())
{
switch (s)
{
@@ -96,38 +93,41 @@ public void Execute(IRocketPlayer caller, string[] msg)
break;
}
}
- if (console || playerid.IsAdmin)
+ if (!console)
{
- perms[0] = true;
- perms[1] = true;
- perms[2] = true;
- perms[3] = true;
- perms[4] = true;
- anyuse = true;
+ if (((UnturnedPlayer)caller).IsAdmin)
+ {
+ perms[0] = true;
+ perms[1] = true;
+ perms[2] = true;
+ perms[3] = true;
+ perms[4] = true;
+ anyuse = true;
+ }
}
if (!anyuse)
{
// Assume this is a player
- UnturnedChat.Say(playerid, "You don't have permission to use the /shop command.");
+ UnturnedChat.Say(caller, "You don't have permission to use the /shop command.");
return;
}
if (msg.Length == 0)
{
message = ZaupShop.Instance.Translate("shop_command_usage", new object[] {});
// We are going to print how to use
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
if (msg.Length < 2)
{
message = ZaupShop.Instance.Translate("no_itemid_given", new object[] {});
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
if (msg.Length == 2 && msg[0] != "rem")
{
message = ZaupShop.Instance.Translate("no_cost_given", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
else if (msg.Length >= 2)
@@ -136,22 +136,22 @@ public void Execute(IRocketPlayer caller, string[] msg)
if (type.Length > 1 && type[0] != "v")
{
message = ZaupShop.Instance.Translate("v_not_provided", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
ushort id;
if (type.Length > 1)
{
if (!ushort.TryParse(type[1], out id)) {
message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
} else {
if (!ushort.TryParse(type[0], out id))
{
message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
}
@@ -165,7 +165,7 @@ public void Execute(IRocketPlayer caller, string[] msg)
if (!perms[3] && !perms[0])
{
message = ZaupShop.Instance.Translate("no_permission_shop_chng", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
change = true;
@@ -177,14 +177,20 @@ public void Execute(IRocketPlayer caller, string[] msg)
if (!perms[1] && !perms[0])
{
message = ZaupShop.Instance.Translate("no_permission_shop_add", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
}
string ac = (pass) ? ZaupShop.Instance.Translate("changed", new object[] { }) : ZaupShop.Instance.Translate("added", new object[] { });
switch (type[0])
{
case "v":
+ if (!this.IsAsset(id, "v"))
+ {
+ message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
+ this.sendMessage(caller, message, console);
+ return;
+ }
VehicleAsset va = (VehicleAsset)Assets.find(EAssetType.VEHICLE, id);
message = ZaupShop.Instance.Translate("changed_or_added_to_shop", new object[] {
ac,
@@ -196,9 +202,15 @@ public void Execute(IRocketPlayer caller, string[] msg)
{
message = ZaupShop.Instance.Translate("error_adding_or_changing", new object[] { va.Name });
}
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
break;
default:
+ if (!this.IsAsset(id, "i"))
+ {
+ message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
+ this.sendMessage(caller, message, console);
+ return;
+ }
ItemAsset ia = (ItemAsset)Assets.find(EAssetType.ITEM, id);
message = ZaupShop.Instance.Translate("changed_or_added_to_shop", new object[] {
ac,
@@ -210,46 +222,64 @@ public void Execute(IRocketPlayer caller, string[] msg)
{
message = ZaupShop.Instance.Translate("error_adding_or_changing", new object[] { ia.Name });
}
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
break;
}
break;
case "rem":
if (!perms[2] && !perms[0])
{
message = ZaupShop.Instance.Translate("no_permission_shop_rem", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
switch (type[0])
{
case "v":
+ if (!this.IsAsset(id, "v"))
+ {
+ message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
+ this.sendMessage(caller, message, console);
+ return;
+ }
VehicleAsset va = (VehicleAsset)Assets.find(EAssetType.VEHICLE, id);
message = ZaupShop.Instance.Translate("removed_from_shop", new object[] { va.Name });
success = ZaupShop.Instance.ShopDB.DeleteVehicle((int)id);
if (!success)
{
message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { va.Name });
}
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
break;
default:
+ if (!this.IsAsset(id, "i"))
+ {
+ message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
+ this.sendMessage(caller, message, console);
+ return;
+ }
ItemAsset ia = (ItemAsset)Assets.find(EAssetType.ITEM, id);
message = ZaupShop.Instance.Translate("removed_from_shop", new object[] { ia.Name });
success = ZaupShop.Instance.ShopDB.DeleteItem((int)id);
if (!success)
{
message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { ia.Name });
}
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
break;
}
break;
case "buy":
if (!perms[4] && !perms[0])
{
message = ZaupShop.Instance.Translate("no_permission_shop_buy", new object[] { });
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
+ return;
+ }
+ if (!this.IsAsset(id, "i"))
+ {
+ message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
+ this.sendMessage(caller, message, console);
return;
}
ItemAsset iab = (ItemAsset)Assets.find(EAssetType.ITEM, id);
@@ -264,26 +294,47 @@ public void Execute(IRocketPlayer caller, string[] msg)
{
message = ZaupShop.Instance.Translate("not_in_shop_to_buyback", new object[] { iab.Name });
}
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
break;
default:
// We shouldn't get this, but if we do send an error.
message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { });;
- this.sendMessage(playerid, message, console);
+ this.sendMessage(caller, message, console);
return;
}
}
}
- private void sendMessage(UnturnedPlayer playerid, string message, bool console)
+ 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;
+ }
+ }
+ private void sendMessage(IRocketPlayer caller, string message, bool console)
{
if (console)
{
Logger.Log(message);
}
else
{
- UnturnedChat.Say(playerid, message);
+ UnturnedChat.Say(caller, message);
}
}
}
-}
+}
@@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.3.0.0")]
-[assembly: AssemblyFileVersion("1.3.0.0")]
+[assembly: AssemblyVersion("1.3.0.1")]
+[assembly: AssemblyFileVersion("1.3.0.1")]
View
@@ -123,12 +123,16 @@ public override TranslationList DefaultTranslations
"A cost is required."
},
{
+ "invalid_amt",
+ "You have entered in an invalid amount."
+ },
+ {
"v_not_provided",
"You must specify v for vehicle or just an item id. Ex. /shop rem/101"
},
{
"invalid_id_given",
- "You need to provide an item or vehicle id."
+ "You need to provide a valid item or vehicle id."
},
{
"no_permission_shop_chng",
@@ -210,7 +214,12 @@ public void Buy(UnturnedPlayer playerid, string[] components0)
byte amttobuy = 1;
if (components0.Length > 1)
{
- amttobuy = byte.Parse(components0[1]);
+ if (!byte.TryParse(components0[1], out amttobuy))
+ {
+ message = ZaupShop.Instance.Translate("invalid_amt", new object[] { });
+ UnturnedChat.Say(playerid, message);
+ return;
+ }
}
string[] components = Parser.getComponentsFromSerial(components0[0], '.');
if (components.Length == 2 && components[0] != "v")
@@ -246,10 +255,9 @@ public void Buy(UnturnedPlayer playerid, string[] components0)
}
}
}
- if (name == null && id == 0)
+ if (Assets.find(EAssetType.VEHICLE, id) == null)
{
- message = ZaupShop.Instance.Translate("could_not_find", new object[] {
- components[1]});
+ message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[1]});
UnturnedChat.Say(playerid, message);
return;
}
@@ -307,12 +315,11 @@ public void Buy(UnturnedPlayer playerid, string[] components0)
}
}
}
- if (name == null && id == 0)
+ if (Assets.find(EAssetType.ITEM, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message);
return;
-
}
else if (name == null && id != 0)
{
@@ -379,7 +386,7 @@ public void Cost(UnturnedPlayer playerid, string[] components)
}
}
}
- if (name == null && id == 0)
+ if (Assets.find(EAssetType.VEHICLE, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[1]});
UnturnedChat.Say(playerid, message);
@@ -414,7 +421,7 @@ public void Cost(UnturnedPlayer playerid, string[] components)
}
}
}
- if (name == null && id == 0)
+ if (Assets.find(EAssetType.ITEM, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message);
@@ -448,7 +455,12 @@ public void Sell(UnturnedPlayer playerid, string[] components)
byte amttosell = 1;
if (components.Length > 1)
{
- amttosell = byte.Parse(components[1]);
+ if (!byte.TryParse(components[1], out amttosell))
+ {
+ message = ZaupShop.Instance.Translate("invalid_amt", new object[] { });
+ UnturnedChat.Say(playerid, message);
+ return;
+ }
}
byte amt = amttosell;
ushort id;
@@ -475,7 +487,7 @@ public void Sell(UnturnedPlayer playerid, string[] components)
}
}
}
- if (name == null && id == 0)
+ if (Assets.find(EAssetType.ITEM, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message);
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.

0 comments on commit 6aad3ed

Please sign in to comment.