• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ Help to make script less ugly

E

Evil Puncker

Guest
my intent to make this into official tfs, it is working, but I think it can be made better, so here I am to ask you guys for help once again into making it friendlier and in tfs standard!

Lua:
local machines = {
    [5469] = 5513,
    [5470] = 5514
}

local sugarOat = Action()

function sugarOat.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if target.itemid == 2694 then
        if toPosition.x ~= CONTAINER_POSITION then
            Game.createItem(13939, 1, toPosition)
        else
            player:addItem(13939, 1)
            toPosition = player:getPosition()
        end
        toPosition:sendMagicEffect(CONST_ME_MAGIC_BLUE)
        item:remove(1)
        target:remove(1)
        return true
    end
    
    local targetId = target.itemid
    if not targetId then
        return true
    end

    local machine = machines[targetId]
    if machine then
        target:transform(machine)
        target:decay()
        item:remove(1)
        toPosition:sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end
    return true
end

sugarOat:id(5467)
sugarOat:register()
 
Solution
Lua:
    if target.itemid == 2694 then
    end
     -- ...
    local targetId = target.itemid
    if not targetId then
        return true
    end
Um... Good one.

Lua:
function sugarOat.onUse(player, item, fromPosition, target, toPosition, isHotkey)
  local targetId = target.itemid

  if targetId == 2694 then
    if toPosition.x ~= CONTAINER_POSITION then
      Game.createItem(13939, 1, toPosition)
    else
      player:addItem(13939, 1)
    end
    player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
    item:remove(1)
    target:remove(1)
  else
    local machine = machines[targetId]
    if machine then
      target:transform(machine)
      target:decay()
      item:remove(1)...
Lua:
    if target.itemid == 2694 then
    end
     -- ...
    local targetId = target.itemid
    if not targetId then
        return true
    end
Um... Good one.

Lua:
function sugarOat.onUse(player, item, fromPosition, target, toPosition, isHotkey)
  local targetId = target.itemid

  if targetId == 2694 then
    if toPosition.x ~= CONTAINER_POSITION then
      Game.createItem(13939, 1, toPosition)
    else
      player:addItem(13939, 1)
    end
    player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
    item:remove(1)
    target:remove(1)
  else
    local machine = machines[targetId]
    if machine then
      target:transform(machine)
      target:decay()
      item:remove(1)
      player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
    end
  end

  return true
end
 
Solution
or.....

Code:
local machines = {
    [2694] = {addItem = 13939},
    [5469] = {transform = 5513},
    [5470] = {transform = 5514}
}

local sugarOat = Action()

function sugarOat.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local machine = machines[target.itemid]
    if not machine then return false end
  
    if machine.transform then
        target:transform(machine.transform)
        target:decay()
        item:remove(1)
        fromPosition:sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end
  
    if machine.addItem then
        if toPosition.x ~= CONTAINER_POSITION then
            Game.createItem(13939, 1, toPosition)
        else
            player:addItem(13939, 1)
        end
        fromPosition:sendMagicEffect(CONST_ME_MAGIC_BLUE)
        item:remove(1)
        target:remove(1)
    end
    return true
end

sugarOat:id(5467)
sugarOat:register()

:p
 
Back
Top