/item handles its parameters smarter now, the quotes for the first parameter are now optional, even if it contains whitespaces. Also, /item returns better error messages now.
This commit is contained in:
parent
899fd6f299
commit
33700e2730
1 changed files with 68 additions and 40 deletions
|
|
@ -3492,59 +3492,87 @@ namespace TShockAPI
|
||||||
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /item <item name/id> [item amount] [prefix id/name]");
|
args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /item <item name/id> [item amount] [prefix id/name]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (args.Parameters[0].Length == 0)
|
|
||||||
{
|
int amountParamIndex = -1;
|
||||||
args.Player.SendErrorMessage("Missing an item name/id.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int itemAmount = 0;
|
int itemAmount = 0;
|
||||||
int prefix = 0;
|
for (int i = 1; i < args.Parameters.Count; i++)
|
||||||
if (args.Parameters.Count == 2)
|
{
|
||||||
int.TryParse(args.Parameters[1], out itemAmount);
|
if (int.TryParse(args.Parameters[i], out itemAmount))
|
||||||
else if (args.Parameters.Count == 3)
|
{
|
||||||
{
|
amountParamIndex = i;
|
||||||
int.TryParse(args.Parameters[1], out itemAmount);
|
break;
|
||||||
var found = TShock.Utils.GetPrefixByIdOrName(args.Parameters[2]);
|
}
|
||||||
if (found.Count == 1)
|
|
||||||
prefix = found[0];
|
|
||||||
}
|
}
|
||||||
var items = TShock.Utils.GetItemByIdOrName(args.Parameters[0]);
|
|
||||||
if (items.Count == 0)
|
string itemNameOrId;
|
||||||
|
if (amountParamIndex == -1)
|
||||||
|
itemNameOrId = string.Join(" ", args.Parameters);
|
||||||
|
else
|
||||||
|
itemNameOrId = string.Join(" ", args.Parameters.Take(amountParamIndex));
|
||||||
|
|
||||||
|
Item item;
|
||||||
|
List<Item> matchedItems = TShock.Utils.GetItemByIdOrName(itemNameOrId);
|
||||||
|
if (matchedItems.Count == 0)
|
||||||
{
|
{
|
||||||
args.Player.SendErrorMessage("Invalid item type!");
|
args.Player.SendErrorMessage("Invalid item type!");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else if (items.Count > 1)
|
else if (matchedItems.Count > 1)
|
||||||
{
|
{
|
||||||
args.Player.SendErrorMessage(string.Format("More than one ({0}) item matched!", items.Count));
|
args.Player.SendErrorMessage("More than one item matched:");
|
||||||
}
|
args.Player.SendErrorMessage(string.Join(", ", matchedItems.Select(i => i.name)));
|
||||||
else
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
item = matchedItems[0];
|
||||||
|
}
|
||||||
|
if (item.type < 1 && item.type >= Main.maxItemTypes)
|
||||||
|
{
|
||||||
|
args.Player.SendErrorMessage("The item type {0} is invalid.", itemNameOrId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int prefixId = 0;
|
||||||
|
if (amountParamIndex != -1 && args.Parameters.Count > amountParamIndex + 1)
|
||||||
|
{
|
||||||
|
string prefixidOrName = args.Parameters[amountParamIndex + 1];
|
||||||
|
List<int> matchedPrefixIds = TShock.Utils.GetPrefixByIdOrName(prefixidOrName);
|
||||||
|
if (matchedPrefixIds.Count > 1)
|
||||||
|
{
|
||||||
|
args.Player.SendErrorMessage("More than one ({0}) prefixes matched \"{1}\".", matchedPrefixIds.Count, prefixidOrName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (matchedPrefixIds.Count == 0)
|
||||||
|
{
|
||||||
|
args.Player.SendErrorMessage("No prefix matched \"{0}\".", prefixidOrName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
prefixId = matchedPrefixIds[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.Player.InventorySlotAvailable || item.name.Contains("Coin"))
|
||||||
{
|
{
|
||||||
var item = items[0];
|
if (itemAmount == 0 || itemAmount > item.maxStack)
|
||||||
if (item.type >= 1 && item.type < Main.maxItemTypes)
|
itemAmount = item.maxStack;
|
||||||
|
|
||||||
|
if (args.Player.GiveItemCheck(item.type, item.name, item.width, item.height, itemAmount, prefixId))
|
||||||
{
|
{
|
||||||
if (args.Player.InventorySlotAvailable || item.name.Contains("Coin"))
|
item.prefix = (byte)prefixId;
|
||||||
{
|
args.Player.SendSuccessMessage("Gave {0} {1}(s).", itemAmount, item.AffixName());
|
||||||
if (itemAmount == 0 || itemAmount > item.maxStack)
|
|
||||||
itemAmount = item.maxStack;
|
|
||||||
if (args.Player.GiveItemCheck(item.type, item.name, item.width, item.height, itemAmount, prefix))
|
|
||||||
{
|
|
||||||
args.Player.SendSuccessMessage(string.Format("Gave {0} {1}(s).", itemAmount, item.name));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
args.Player.SendErrorMessage("The item is banned and the config prevents you from spawning banned items.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
args.Player.SendErrorMessage("You don't have free slots!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
args.Player.SendErrorMessage("Invalid item type!");
|
args.Player.SendErrorMessage("The item is banned and the config prevents you from spawning banned items.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
args.Player.SendErrorMessage("Your inventory seems full.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Give(CommandArgs args)
|
private static void Give(CommandArgs args)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue