Skip to content

Commit

Permalink
Merge pull request #6 from cartman-2000/master
Browse files Browse the repository at this point in the history
Fix for an InvalidCastException and an invalid item/vehicle id NRE error in the shop command.
  • Loading branch information
Zamirathe committed Jul 30, 2015
2 parents bc046a9 + ab6fb3f commit 6aad3ed
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 44 deletions.
113 changes: 82 additions & 31 deletions CommandShop.cs
Expand Up @@ -5,7 +5,6 @@
using Rocket.Core; using Rocket.Core;
using Rocket.Core.Logging; using Rocket.Core.Logging;
using Rocket.Core.Permissions; using Rocket.Core.Permissions;
using Rocket.Unturned;
using Rocket.Unturned.Chat; using Rocket.Unturned.Chat;
using Rocket.Unturned.Commands; using Rocket.Unturned.Commands;
using Rocket.Unturned.Player; using Rocket.Unturned.Player;
Expand Down Expand Up @@ -56,13 +55,11 @@ public List<string> Permissions
public void Execute(IRocketPlayer caller, string[] msg) public void Execute(IRocketPlayer caller, string[] msg)
{ {
bool console = (caller is ConsolePlayer); bool console = (caller is ConsolePlayer);
UnturnedPlayer playerid = (UnturnedPlayer)caller;
string[] permnames = { "shop.*", "shop.add", "shop.rem", "shop.chng", "shop.buy" }; string[] permnames = { "shop.*", "shop.add", "shop.rem", "shop.chng", "shop.buy" };
bool[] perms = { false, false, false, false, false }; bool[] perms = { false, false, false, false, false };
bool anyuse = false; bool anyuse = false;
string message; string message;
List<string> permlist = (console) ? new List<string>() : R.Permissions.GetPermissions(playerid); foreach (string s in caller.GetPermissions())
foreach (string s in permlist)
{ {
switch (s) switch (s)
{ {
Expand Down Expand Up @@ -96,38 +93,41 @@ public void Execute(IRocketPlayer caller, string[] msg)
break; break;
} }
} }
if (console || playerid.IsAdmin) if (!console)
{ {
perms[0] = true; if (((UnturnedPlayer)caller).IsAdmin)
perms[1] = true; {
perms[2] = true; perms[0] = true;
perms[3] = true; perms[1] = true;
perms[4] = true; perms[2] = true;
anyuse = true; perms[3] = true;
perms[4] = true;
anyuse = true;
}
} }
if (!anyuse) if (!anyuse)
{ {
// Assume this is a player // 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; return;
} }
if (msg.Length == 0) if (msg.Length == 0)
{ {
message = ZaupShop.Instance.Translate("shop_command_usage", new object[] {}); message = ZaupShop.Instance.Translate("shop_command_usage", new object[] {});
// We are going to print how to use // We are going to print how to use
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
if (msg.Length < 2) if (msg.Length < 2)
{ {
message = ZaupShop.Instance.Translate("no_itemid_given", new object[] {}); message = ZaupShop.Instance.Translate("no_itemid_given", new object[] {});
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
if (msg.Length == 2 && msg[0] != "rem") if (msg.Length == 2 && msg[0] != "rem")
{ {
message = ZaupShop.Instance.Translate("no_cost_given", new object[] { }); message = ZaupShop.Instance.Translate("no_cost_given", new object[] { });
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
else if (msg.Length >= 2) else if (msg.Length >= 2)
Expand All @@ -136,22 +136,22 @@ public void Execute(IRocketPlayer caller, string[] msg)
if (type.Length > 1 && type[0] != "v") if (type.Length > 1 && type[0] != "v")
{ {
message = ZaupShop.Instance.Translate("v_not_provided", new object[] { }); message = ZaupShop.Instance.Translate("v_not_provided", new object[] { });
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
ushort id; ushort id;
if (type.Length > 1) if (type.Length > 1)
{ {
if (!ushort.TryParse(type[1], out id)) { if (!ushort.TryParse(type[1], out id)) {
message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { }); message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
} else { } else {
if (!ushort.TryParse(type[0], out id)) if (!ushort.TryParse(type[0], out id))
{ {
message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { }); message = ZaupShop.Instance.Translate("invalid_id_given", new object[] { });
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
} }
Expand All @@ -165,7 +165,7 @@ public void Execute(IRocketPlayer caller, string[] msg)
if (!perms[3] && !perms[0]) if (!perms[3] && !perms[0])
{ {
message = ZaupShop.Instance.Translate("no_permission_shop_chng", new object[] { }); message = ZaupShop.Instance.Translate("no_permission_shop_chng", new object[] { });
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
change = true; change = true;
Expand All @@ -177,14 +177,20 @@ public void Execute(IRocketPlayer caller, string[] msg)
if (!perms[1] && !perms[0]) if (!perms[1] && !perms[0])
{ {
message = ZaupShop.Instance.Translate("no_permission_shop_add", new object[] { }); message = ZaupShop.Instance.Translate("no_permission_shop_add", new object[] { });
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
} }
string ac = (pass) ? ZaupShop.Instance.Translate("changed", new object[] { }) : ZaupShop.Instance.Translate("added", new object[] { }); string ac = (pass) ? ZaupShop.Instance.Translate("changed", new object[] { }) : ZaupShop.Instance.Translate("added", new object[] { });
switch (type[0]) switch (type[0])
{ {
case "v": 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); VehicleAsset va = (VehicleAsset)Assets.find(EAssetType.VEHICLE, id);
message = ZaupShop.Instance.Translate("changed_or_added_to_shop", new object[] { message = ZaupShop.Instance.Translate("changed_or_added_to_shop", new object[] {
ac, ac,
Expand All @@ -196,9 +202,15 @@ public void Execute(IRocketPlayer caller, string[] msg)
{ {
message = ZaupShop.Instance.Translate("error_adding_or_changing", new object[] { va.Name }); message = ZaupShop.Instance.Translate("error_adding_or_changing", new object[] { va.Name });
} }
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
break; break;
default: 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); ItemAsset ia = (ItemAsset)Assets.find(EAssetType.ITEM, id);
message = ZaupShop.Instance.Translate("changed_or_added_to_shop", new object[] { message = ZaupShop.Instance.Translate("changed_or_added_to_shop", new object[] {
ac, ac,
Expand All @@ -210,46 +222,64 @@ public void Execute(IRocketPlayer caller, string[] msg)
{ {
message = ZaupShop.Instance.Translate("error_adding_or_changing", new object[] { ia.Name }); message = ZaupShop.Instance.Translate("error_adding_or_changing", new object[] { ia.Name });
} }
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
break; break;
} }
break; break;
case "rem": case "rem":
if (!perms[2] && !perms[0]) if (!perms[2] && !perms[0])
{ {
message = ZaupShop.Instance.Translate("no_permission_shop_rem", new object[] { }); message = ZaupShop.Instance.Translate("no_permission_shop_rem", new object[] { });
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; return;
} }
switch (type[0]) switch (type[0])
{ {
case "v": 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); VehicleAsset va = (VehicleAsset)Assets.find(EAssetType.VEHICLE, id);
message = ZaupShop.Instance.Translate("removed_from_shop", new object[] { va.Name }); message = ZaupShop.Instance.Translate("removed_from_shop", new object[] { va.Name });
success = ZaupShop.Instance.ShopDB.DeleteVehicle((int)id); success = ZaupShop.Instance.ShopDB.DeleteVehicle((int)id);
if (!success) if (!success)
{ {
message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { va.Name }); message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { va.Name });
} }
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
break; break;
default: 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); ItemAsset ia = (ItemAsset)Assets.find(EAssetType.ITEM, id);
message = ZaupShop.Instance.Translate("removed_from_shop", new object[] { ia.Name }); message = ZaupShop.Instance.Translate("removed_from_shop", new object[] { ia.Name });
success = ZaupShop.Instance.ShopDB.DeleteItem((int)id); success = ZaupShop.Instance.ShopDB.DeleteItem((int)id);
if (!success) if (!success)
{ {
message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { ia.Name }); 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;
} }
break; break;
case "buy": case "buy":
if (!perms[4] && !perms[0]) if (!perms[4] && !perms[0])
{ {
message = ZaupShop.Instance.Translate("no_permission_shop_buy", new object[] { }); 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; return;
} }
ItemAsset iab = (ItemAsset)Assets.find(EAssetType.ITEM, id); ItemAsset iab = (ItemAsset)Assets.find(EAssetType.ITEM, id);
Expand All @@ -264,26 +294,47 @@ public void Execute(IRocketPlayer caller, string[] msg)
{ {
message = ZaupShop.Instance.Translate("not_in_shop_to_buyback", new object[] { iab.Name }); message = ZaupShop.Instance.Translate("not_in_shop_to_buyback", new object[] { iab.Name });
} }
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
break; break;
default: default:
// We shouldn't get this, but if we do send an error. // We shouldn't get this, but if we do send an error.
message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { });; message = ZaupShop.Instance.Translate("not_in_shop_to_remove", new object[] { });;
this.sendMessage(playerid, message, console); this.sendMessage(caller, message, console);
return; 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) if (console)
{ {
Logger.Log(message); Logger.Log(message);
} }
else else
{ {
UnturnedChat.Say(playerid, message); UnturnedChat.Say(caller, message);
} }
} }
} }
} }
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")] [assembly: AssemblyVersion("1.3.0.1")]
[assembly: AssemblyFileVersion("1.3.0.0")] [assembly: AssemblyFileVersion("1.3.0.1")]
34 changes: 23 additions & 11 deletions ZaupShop.cs
Expand Up @@ -122,13 +122,17 @@ public override TranslationList DefaultTranslations
"no_cost_given", "no_cost_given",
"A cost is required." "A cost is required."
}, },
{
"invalid_amt",
"You have entered in an invalid amount."
},
{ {
"v_not_provided", "v_not_provided",
"You must specify v for vehicle or just an item id. Ex. /shop rem/101" "You must specify v for vehicle or just an item id. Ex. /shop rem/101"
}, },
{ {
"invalid_id_given", "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", "no_permission_shop_chng",
Expand Down Expand Up @@ -210,7 +214,12 @@ public void Buy(UnturnedPlayer playerid, string[] components0)
byte amttobuy = 1; byte amttobuy = 1;
if (components0.Length > 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], '.'); string[] components = Parser.getComponentsFromSerial(components0[0], '.');
if (components.Length == 2 && components[0] != "v") if (components.Length == 2 && components[0] != "v")
Expand Down Expand Up @@ -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[] { message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[1]});
components[1]});
UnturnedChat.Say(playerid, message); UnturnedChat.Say(playerid, message);
return; return;
} }
Expand Down Expand Up @@ -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]}); message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message); UnturnedChat.Say(playerid, message);
return; return;

} }
else if (name == null && id != 0) else if (name == null && id != 0)
{ {
Expand Down Expand Up @@ -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]}); message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[1]});
UnturnedChat.Say(playerid, message); UnturnedChat.Say(playerid, message);
Expand Down Expand Up @@ -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]}); message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message); UnturnedChat.Say(playerid, message);
Expand Down Expand Up @@ -448,7 +455,12 @@ public void Sell(UnturnedPlayer playerid, string[] components)
byte amttosell = 1; byte amttosell = 1;
if (components.Length > 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; byte amt = amttosell;
ushort id; ushort id;
Expand All @@ -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]}); message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message); UnturnedChat.Say(playerid, message);
Expand Down
Binary file modified lib/Assembly-CSharp.dll
Binary file not shown.
Binary file modified lib/Rocket.API.dll
Binary file not shown.
Binary file modified lib/Rocket.Core.dll
Binary file not shown.
Binary file modified lib/Rocket.Unturned.dll
Binary file not shown.

0 comments on commit 6aad3ed

Please sign in to comment.