• 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!

Blocking Items from being thrown at a specific item/object/tile

Wusse

Member
Joined
Oct 3, 2023
Messages
48
Reaction score
12
Hey!

Using TFS 1.4.2 10.98

i came across this thread (Lua - Block item throw to specified tile (https://otland.net/threads/block-item-throw-to-specified-tile.277995/#post-2672662))
and im trying to use the second example Sarah mentions.

this is how my script looks like:

Lua:
local ids = {9999}

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local tile = Tile(toPosition)
    if tile then
        for _, i in pairs(tile:getItems()) do
            if i:getActionId() == ids then
                return false
            end
        end
    end
    return true
end

ec:register(-1)


this is my events.xml:


ive tried it but it doesnt seem to work for me and i dont really understand why, if someone could shed some light into this problem id appreciate it!

GIF showing the problem:

 
Lua:
local ids = {9999}

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local tile = Tile(toPosition)
    if tile then
        for _, i in ipairs(tile:getItems()) do
            if table.contains(ids, i:getActionId()) then
                return false
            end
        end
    end
    return true
end

ec:register(-1)

ill send the entire script with the other functions that work, its just the last one that isnt applying, really dont understand why.

script:


Lua:
local ids = {9999}

local ec = EventCallback

ec.onMoveItem = function(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder)

    if table.contains(ids, item:getActionId()) then
        return RETURNVALUE_NOTPOSSIBLE
    end

    return RETURNVALUE_NOERROR
end

ec:register(1)
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onTradeRequest = function(self, target, item)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onTradeAccept = function(self, target, item, targetItem)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onTradeCompleted = function(self, target, item, targetItem, isSuccess)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onRotateItem = function(self, item)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local tile = Tile(toPosition)
    if tile then
        for _, i in ipairs(tile:getItems()) do
            if table.contains(ids, i:getActionId()) then
                return false
            end
        end
    end
    return true
end

ec:register(-1)
 
put a print just inside the onMoveItem function, and make sure you see the print in the console to test its being registered.

If you don't see the print, it might be an issue with the ec:register().

Btw, if you have all of these EC's in one script, you need to combine the two onMoveItems (one at the very top, one at the bottom) or give them different register id's
 
Last edited:
put a print just inside the function, and make sure you see the print in the console.

If you don't see the print, it might be an issue with the ec:register().

Btw, if you have all of these EC's in one script, you have two onMoveItems (one at the very top, one at the bottom)
did multiple of tests with prints and non of them would show up, i also believe the issue lies in ec:register(-1), sadly i dont understand it at all, tested:

ec:register()
ec:register(1)
ec:register(-1)

non of them work.

i tried combining the functions and i made it work:


part of script:


Lua:
local ids = {9999}

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if table.contains(ids, item:getActionId()) then
        return RETURNVALUE_NOTPOSSIBLE
    end

    local tile = Tile(toPosition)
    if tile then
        for _, i in ipairs(tile:getItems()) do
            if table.contains(ids, i:getActionId()) then
                return RETURNVALUE_NOTPOSSIBLE
            end
        end
    end

    return RETURNVALUE_NOERROR
end

ec:register(1)

silly mistakes...
thank you for your help!
 
Lua:
local ids = {9999}

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local tile = Tile(toPosition)
    if tile then
        for _, i in ipairs(tile:getItems()) do
            if table.contains(ids, i:getActionId()) then
                return false
            end
        end
    end
    return true
end

ec:register(-1)

ill send the entire script with the other functions that work, its just the last one that isnt applying, really dont understand why.

script:


Lua:
local ids = {9999}

local ec = EventCallback

ec.onMoveItem = function(self, item, count, fromPosition, toPosition, fromCylinder, toCylinder)

    if table.contains(ids, item:getActionId()) then
        return RETURNVALUE_NOTPOSSIBLE
    end

    return RETURNVALUE_NOERROR
end

ec:register(1)
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onTradeRequest = function(self, target, item)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onTradeAccept = function(self, target, item, targetItem)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onTradeCompleted = function(self, target, item, targetItem, isSuccess)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

ec.onRotateItem = function(self, item)

    if table.contains(ids, item:getActionId()) then
        return false
    end

    return true
end

ec:register()
-----------------------------------------------------------------------------------------------------------------------
local ids = {9999}

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local tile = Tile(toPosition)
    if tile then
        for _, i in ipairs(tile:getItems()) do
            if table.contains(ids, i:getActionId()) then
                return false
            end
        end
    end
    return true
end

ec:register(-1)
Lua:
local ids = {9999}

local positionCheck = {
    north_west = Position(963, 932, 7),
    south_east = Position(981, 949, 7),
    enabled = true -- This table is used for testing purposes. It defines a rectangular area on the map where item movement restrictions apply. If you want to ignore these restrictions, just keep the setting as false. 
}

local function isPositionBlocked(position)
    return position.x >= positionCheck.north_west.x and position.x <= positionCheck.south_east.x and
           position.y >= positionCheck.north_west.y and position.y <= positionCheck.south_east.y and
           position.z == positionCheck.north_west.z
end

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if table.contains(ids, item:getActionId()) then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot move this item here.")
        return RETURNVALUE_NOTPOSSIBLE
    end

    if positionCheck.enabled and isPositionBlocked(toPosition) then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot throw items in this area.")
        return RETURNVALUE_NOTPOSSIBLE
    end

    local tile = Tile(toPosition)
    if tile then
        for _, i in ipairs(tile:getItems()) do
            if table.contains(ids, i:getActionId()) then
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot move this item here.")
                return RETURNVALUE_NOTPOSSIBLE
            end
        end
    end

    return RETURNVALUE_NOERROR
end

ec:register(1)
 
Last edited:
 
See the gif... working OK.
blopck.gif

Lua:
local BLOCKED_ACTIONID = 9999

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local tile = Tile(toPosition)
    if tile then
        for i = 0, tile:getThingCount() - 1 do
            local thing = tile:getThing(i)
            if thing and thing:isItem() then
                local actionId = thing:getActionId()
                if actionId == BLOCKED_ACTIONID then
                    return RETURNVALUE_NOTPOSSIBLE
                end
            end
        end
    end

    return RETURNVALUE_NOERROR
end

ec:register(-6666)
 
Last edited:
Is the script working for you then?

i managed to solve it earlier in the thread,

here:

did multiple of tests with prints and non of them would show up, i also believe the issue lies in ec:register(-1), sadly i dont understand it at all, tested:

ec:register()
ec:register(1)
ec:register(-1)

non of them work.

i tried combining the functions and i made it work:


part of script:


Lua:
local ids = {9999}

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if table.contains(ids, item:getActionId()) then
        return RETURNVALUE_NOTPOSSIBLE
    end

    local tile = Tile(toPosition)
    if tile then
        for _, i in ipairs(tile:getItems()) do
            if table.contains(ids, i:getActionId()) then
                return RETURNVALUE_NOTPOSSIBLE
            end
        end
    end

    return RETURNVALUE_NOERROR
end

ec:register(1)

silly mistakes...
thank you for your help!
 
Back
Top