Permalink
Browse files

Fix: Trying to cast caller to UnturnedPlayer when ran from the consol…

…e will result in a Invalid Cast Exception.

I've also fixed the NRE error on the shop command if you enter in an invalid item/vehicle id.
  • Loading branch information...
1 parent bc046a9 commit 135f9fae4529d18a24746db16e2ab2aff17569f9 @cartman-2000 cartman-2000 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,12 +55,16 @@ public List<string> Permissions
public void Execute(IRocketPlayer caller, string[] msg)
{
bool console = (caller is ConsolePlayer);
- UnturnedPlayer playerid = (UnturnedPlayer)caller;
+ UnturnedPlayer playerid = null;
+ if (!console)
+ {
+ 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);
+ List<string> permlist = (console) ? new List<string>() : caller.GetPermissions();
foreach (string s in permlist)
{
switch (s)
@@ -108,26 +111,26 @@ public void Execute(IRocketPlayer caller, string[] msg)
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 +139,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 +168,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 +180,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 +205,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 +225,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 +297,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
@@ -128,7 +128,7 @@ public override TranslationList DefaultTranslations
},
{
"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",
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.

0 comments on commit 135f9fa

Please sign in to comment.