I Tried to put this Sarah Wesker's [TFS 1.5] ❤ TradeTimes System ❤ into my TFS 1.4
the item shows the trade times it still have left or if there is no longer trades left on the item
but when there is no trades left, it was supposed to not let the player trade it or throw in the ground, but this part doesn't work.
there is no console errors as well so I have no idea what is missing.
Can Anyone help to find is missing to make this system work on TFS 1.4? or can point on how should I adapt it?
Here's the code:
the item shows the trade times it still have left or if there is no longer trades left on the item
but when there is no trades left, it was supposed to not let the player trade it or throw in the ground, but this part doesn't work.
there is no console errors as well so I have no idea what is missing.
Can Anyone help to find is missing to make this system work on TFS 1.4? or can point on how should I adapt it?
Here's the code:
Lua:
local tradeStoneId = 8302
local maxTradeTimes = 3
local tradeKey = "TradeTimes"
local effect = CONST_ME_FIREWORK_BLUE
-- Warning:
-- This prints a warning to the console if the execution order is not correct.
-- If the warning is triggered, it means that there is a possibility that an item with TradeTimes will fall to the ground.
-- If you are sure that the order is correct, you can disable this to make your script faster.
local checkDropLootOrder = true
-- If an item with TradeTimes is found on the corpse, should we remove it?
local removeDropedItem = true
-- If an item with TradeTimes is found on the corpse, should we reduce the times?
-- If removeDropedItem is false and this variable is true, then the item will be removed if it has no more times.
local reduceDropedItem = false
local function isSentToPlayer(player, toCylinder)
if not toCylinder then
return RETURNVALUE_NOTPOSSIBLE
end
if toCylinder:isTile() then
return RETURNVALUE_NOTPOSSIBLE
end
if toCylinder == player or toCylinder:getTopParent() == player then
return RETURNVALUE_NOERROR
end
return RETURNVALUE_NOTPOSSIBLE
end
local ec = EventCallback
function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
local toTile = Tile(toPosition)
if toTile and toTile:hasFlag(TILESTATE_TRASHHOLDER) then
return RETURNVALUE_NOERROR
elseif item:getCustomAttribute(tradeKey) then
return isSentToPlayer(player, toCylinder)
elseif item:getType():isContainer() then
for _, item in pairs(item:getItems(true)) do
if item:getCustomAttribute(tradeKey) then
return isSentToPlayer(player, toCylinder)
end
end
end
return RETURNVALUE_NOERROR
end
ec:register(-1)
function ec.onTradeRequest(player, target, item)
local itemTT = item:getCustomAttribute(tradeKey)
if itemTT and itemTT < 1 then
player:sendCancelMessage("You can't trade this item anymore.")
return false
end
if item:getType():isContainer() then
for _, it in pairs(item:getItems(true)) do
local itTT = it:getCustomAttribute(tradeKey)
if itTT and itTT < 1 then
player:sendCancelMessage("You can't trade this item anymore.")
return false
end
end
end
return true
end
ec:register(-1)
function ec.onTradeCompleted(player, target, item, targetItem, isSuccess)
if not isSuccess then
return
end
local itemTT = item:getCustomAttribute(tradeKey)
if itemTT then
item:setCustomAttribute(tradeKey, itemTT - 1)
end
if item:getType():isContainer() then
for _, it in pairs(item:getItems(true)) do
local itTT = it:getCustomAttribute(tradeKey)
if itTT then
it:setCustomAttribute(tradeKey, itTT - 1)
end
end
end
local targetItemTT = targetItem:getCustomAttribute(tradeKey)
if targetItemTT then
targetItem:setCustomAttribute(tradeKey, targetItemTT - 1)
end
if targetItem:getType():isContainer() then
for _, it in pairs(targetItem:getItems(true)) do
local itTT = it:getCustomAttribute(tradeKey)
if itTT then
it:setCustomAttribute(tradeKey, itTT - 1)
end
end
end
end
ec:register(-1)
local function onLook(item, description)
local tradeTimes = item:getCustomAttribute(tradeKey)
if not tradeTimes then
return description
end
if tradeTimes > 0 then
return string.format("%s\nIt can be traded %d more times.", description, tradeTimes)
end
return string.format("%s\nIt can no longer be traded.", description)
end
function ec.onLook(player, thing, position, distance, description)
if not thing:isItem() then
return description
end
return onLook(thing, description)
end
ec:register(1)
function ec.onLookInTrade(player, partner, item, distance, description)
return onLook(item, description)
end
ec:register(1)
local action = Action()
function action.onUse(player, item, fromPos, target, toPos, isHotkey)
if not target or not target:isItem() then
player:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
return true
end
local tradeTimes = target:getCustomAttribute(tradeKey)
if not tradeTimes then
player:sendCancelMessage("This item not support trade times.")
return true
end
if tradeTimes >= maxTradeTimes then
player:sendCancelMessage("This item already has the maximum trade times.")
return true
end
tradeTimes = tradeTimes + 1
target:setCustomAttribute(tradeKey, tradeTimes)
item:remove(1)
player:getPosition():sendMagicEffect(effect)
return true
end
action:id(tradeStoneId)
action:register()
local death = CreatureEvent("TradeTimesDeath")
function death.onDeath(player, corpse, lastHitKiller, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
if checkDropLootOrder then
local foundDropLoot = false
for _, eventName in pairs(player:getEvents(CREATURE_EVENT_DEATH)) do
if eventName == "DropLoot" then
foundDropLoot = true
elseif eventName == "TradeTimesDeath" and not foundDropLoot then
print("[TradeTimesDeath - Warning] must be after DropLoot.")
end
end
end
for _, item in pairs(corpse:getItems(true)) do
local tradeTimes = item:getCustomAttribute(tradeKey)
if tradeTimes then
if removeDropedItem then
item:remove()
elseif reduceDropedItem then
if tradeTimes > 1 then
item:setCustomAttribute(tradeKey, tradeTimes - 1)
else
item:remove()
end
end
end
end
return true
end
death:register()
local login = CreatureEvent("TradeTimesLogin")
function login.onLogin(player)
player:registerEvent("TradeTimesDeath")
return true
end
login:register()