Very nice, added to the scriptuse a recursive container search:
Lua:function searchContainer(container, items) items = items or {} for i = container:getSize()-1, 0, -1 do local item = container:getItem(i) if item:getType():isContainer() then searchContainer(item, items) else items[#items+1] = item end end return items end
function Player:onTradeAccept(target, item, targetItem)
file = io.open('data/logs/trade.log',"a")
file:write(""..os.date("%c")..": "..self:getName().." traded:")
if item:isContainer() then
local pitems = searchContainer(item)
for i = 1, #pitems do
file:write(string.format(' %s (%s)(%s),'...
I trust your judgement but doesn't Lua index from 1 not from 0? Is that why he is getting the error?use a recursive container search:
Lua:function searchContainer(container, items) items = items or {} for i = container:getSize()-1, 0, -1 do local item = container:getItem(i) if item:getType():isContainer() then searchContainer(item, items) else items[#items+1] = item end end return items end
Lua:local items = searchContainer(item)
you are correct, Lua does, but not C++I trust your judgement but doesn't Lua index from 1 not from 0? Is that why he is getting the error?
EDIT: Try it again now, I think it is my fault
function Player:onTradeAccept(target, item, targetItem)
file = io.open('data/logs/trade.log',"a")
file:write(""..os.date("%c")..": "..self:getName().." traded:")
if item:isContainer() then
local pitems = searchContainer(item)
for i = 1, #pitems do
file:write(string.format(' %s (%s)(%s),', pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount()))
end
else
file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount()))
end
file:write(" with "..target:getName().." for:")
if targetItem:isContainer() then
local titems = searchContainer(targetItem)
for i = 1, #titems do
file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount()))
end
else
file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount()))
end
file:write('\n-------------------------\n\n')
file:close()
return true
end
Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false).
-------------------------
Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false).
-------------------------
Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
just use item:getCount() or check > 0 instead of > 1I made some changes:
Lua:function Player:onTradeAccept(target, item, targetItem) file = io.open('data/logs/trade.log',"a") file:write(""..os.date("%c")..": "..self:getName().." traded:") if item:isContainer() then local pitems = searchContainer(item) local count = item.getCount() local countshow = count > 1 for i = 1, #pitems do file:write(string.format(' %s (%s)(%s),', pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount())) end else file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount())) end file:write(" with "..target:getName().." for:") if targetItem:isContainer() then local titems = searchContainer(targetItem) for i = 1, #titems do file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount())) end else file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount())) end file:write('\n-------------------------\n\n') file:close() return true end
Code:Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false). ------------------------- Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false). -------------------------
But someone can help me with this (false) if count is 1, want to looks like:
Code:Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
I made some changes:
Lua:function Player:onTradeAccept(target, item, targetItem) file = io.open('data/logs/trade.log',"a") file:write(""..os.date("%c")..": "..self:getName().." traded:") if item:isContainer() then local pitems = searchContainer(item) for i = 1, #pitems do file:write(string.format(' %s (%s)(%s),', pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount())) end else file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount())) end file:write(" with "..target:getName().." for:") if targetItem:isContainer() then local titems = searchContainer(targetItem) for i = 1, #titems do file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount())) end else file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount())) end file:write('\n-------------------------\n\n') file:close() return true end
Code:Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false). ------------------------- Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false). -------------------------
But someone can help me with this (false) if count is 1, want to looks like:
Code:Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
just use item:getCount() or check > 0 instead of > 1
I made some changes:
Lua:function Player:onTradeAccept(target, item, targetItem) file = io.open('data/logs/trade.log',"a") file:write(""..os.date("%c")..": "..self:getName().." traded:") if item:isContainer() then local pitems = searchContainer(item) for i = 1, #pitems do file:write(string.format(' %s (%s)(%s),', pitems[i]:getName(), pitems[i]:getId(), pitems[i]:getCount() > 1 and pitems[i]:getCount())) end else file:write(string.format(' %s (%s)(%s),', item:getName(), item:getId(), item:getCount() > 1 and item:getCount())) end file:write(" with "..target:getName().." for:") if targetItem:isContainer() then local titems = searchContainer(targetItem) for i = 1, #titems do file:write(string.format(' %s (%s)(%s),', titems[i]:getName(), titems[i]:getId(), titems[i]:getCount() > 1 and titems[i]:getCount())) end else file:write(string.format(' %s (%s)(%s).', targetItem:getName(), targetItem:getId(), targetItem:getCount() > 1 and targetItem:getCount())) end file:write('\n-------------------------\n\n') file:close() return true end
Code:Thu Apr 20 12:12:18 2017: Knight traded: soul orb (5944)(6), with Druid for: giant sword (2393)(false). ------------------------- Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893)(false). -------------------------
But someone can help me with this (false) if count is 1, want to looks like:
Code:Thu Apr 20 12:12:48 2017: Knight traded: platinum coin (2152)(78), with Druid for: lightning boots (7893).
just use item:getCount() or check > 0 instead of > 1
Awesome guys thanks for the insight, added to the 'best answer' post for everyone else's benefit
if item:isContainer() then