2013-07-27 16:24:03 +01:00
function HandleGiveCommand ( Split , Player )
2013-08-07 18:36:38 +01:00
-- Make sure there are a correct number of arguments.
if # Split ~= 3 and # Split ~= 4 and # Split ~= 5 then
2013-08-11 16:00:53 +01:00
SendMessage ( Player , "Usage: /give <player> <item> [amount] [meta]" )
2013-08-07 18:36:38 +01:00
return true
2013-07-27 16:24:03 +01:00
end
2013-08-07 18:36:38 +01:00
-- Get the item from the arguments and check it's valid.
local Item = cItem ()
if # Split == 5 then
2013-08-11 16:00:53 +01:00
local FoundItem = StringToItem ( Split [ 3 ] .. ":" .. Split [ 5 ], Item )
2013-08-07 18:36:38 +01:00
else
2013-08-11 16:00:53 +01:00
local FoundItem = StringToItem ( Split [ 3 ], Item )
end
if not IsValidItem ( Item.m_ItemType ) then -- StringToItem does not check if item is valid
2013-07-27 16:24:03 +01:00
FoundItem = false
end
2013-08-07 18:36:38 +01:00
if not FoundItem then
2013-08-11 16:00:53 +01:00
SendMessageFailure ( Player , "Invalid item id or name!" )
2013-07-27 16:24:03 +01:00
return true
end
2013-08-07 18:36:38 +01:00
-- Work out how many items the user wants.
local ItemAmount = 1
if # Split > 3 then
2013-08-11 16:00:53 +01:00
ItemAmount = tonumber ( Split [ 4 ] )
2013-08-07 18:36:38 +01:00
if ItemAmount == nil or ItemAmount < 1 or ItemAmount > 512 then
2013-08-11 16:00:53 +01:00
SendMessageFailure ( Player , "Invalid amount!" )
2013-08-07 18:36:38 +01:00
return true
2013-07-27 16:24:03 +01:00
end
end
2013-08-07 18:36:38 +01:00
Item.m_ItemCount = ItemAmount
2013-07-27 16:24:03 +01:00
2013-08-07 18:36:38 +01:00
-- Get the playername from the split.
local playerName = Split [ 2 ]
2013-08-11 16:00:53 +01:00
local function giveItems ( newPlayer )
local ItemsGiven = newPlayer : GetInventory (): AddItem ( Item )
2013-08-07 18:36:38 +01:00
if ItemsGiven == ItemAmount then
2013-08-11 16:00:53 +01:00
SendMessageSuccess ( newPlayer , "You were given " .. Item.m_ItemCount .. " of " .. Item.m_ItemType .. "." )
2013-08-07 18:36:38 +01:00
if not newPlayer == Player then
2013-08-11 16:00:53 +01:00
SendMessageSuccess ( Player , "Items given!" )
2013-08-07 18:36:38 +01:00
end
2013-08-11 16:00:53 +01:00
LOG ( "Gave " .. newPlayer : GetName () .. " " .. Item.m_ItemCount .. " times " .. Item.m_ItemType .. ":" .. Item.m_ItemDamage )
2013-08-07 18:36:38 +01:00
else
2013-08-11 16:00:53 +01:00
SendMessageFailure ( Player , "Not enough space in inventory, only gave " .. ItemsGiven )
2013-08-07 18:36:38 +01:00
LOG ( "Player " .. Player : GetName () .. " asked for " .. Item.m_ItemCount .. " times " .. Item.m_ItemType .. ":" .. Item.m_ItemDamage .. ", but only could fit " .. ItemsGiven )
end
return true
end
-- Finally give the items to the player.
2013-08-11 16:00:53 +01:00
itemStatus = cRoot : Get (): FindAndDoWithPlayer ( playerName , giveItems )
2013-08-07 18:36:38 +01:00
-- Check to make sure that giving items was successful.
if not itemStatus then
2013-08-11 16:00:53 +01:00
SendMessageFailure ( Player , "There was no player that matched your query." )
2013-07-27 16:24:03 +01:00
end
2013-08-07 18:36:38 +01:00
return true
end