function onUse(player, item, fromPosition, target, toPosition, isHotkey)
local chest = Container(item.uid)
if not chest then
return true
end
local uniqueid = chest:getUniqueId()
if player:getStorageValue(uniqueid) == -2 then
player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
return true
end
local reward = nil
local start = player:getStorageValue(uniqueid) == -1 and 0 or player:getStorageValue(uniqueid)
for i = start, chest:getSize() do
reward = chest:getItem(i)
if not reward then
break
end
if reward:getWeight() > player:getFreeCapacity() then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')
player:setStorageValue(uniqueid, i)
break
else
local reward_container = Container(reward:getUniqueId())
if reward_container then
reward_container = reward_container:clone()
reward_container:moveTo(player)
else
player:addItem(reward:getId(), reward:getCount())
end
local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
if reward:getCount() > 1 then
reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
end
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')
player:setStorageValue(uniqueid, -2)
end
end
return true
end