Untitled

 avatar
unknown
plain_text
25 days ago
3.1 kB
14
Indexable
addIcon("checkPlayer", {
    item = 9018,  -- Możesz przypisać ID przedmiotu, jeśli jest wymagane
    text = "Check Players",  -- Tekst wyświetlany na ikonie
    pos = { x = 20, y = 100 },  -- Pozycja ikony na ekranie
}, function(widget, enabled)
  local found

  local function checkPlayers()
    for i, spec in ipairs(getSpectators()) do
      if spec:isPlayer() and spec:getText() == "" and spec:getPosition().z == posz() and spec ~= player then
          g_game.look(spec)
          found = now
      end
    end
    return true  -- Return a boolean value to prevent the error
  end

  -- When the icon is enabled, start checking players
  if enabled then
    schedule(500, function()
      checkPlayers()
      return true  -- Ensuring that the callback returns a boolean value
    end)
  end

  -- When player position changes, check if it's the same z level
  onPlayerPositionChange(function(x, y)
    if not enabled then return false end  -- Ensure we only run if the icon is enabled
    if x.z ~= y.z then
      schedule(20, function() checkPlayers() end)
    end
    return true  -- Ensuring the callback returns a boolean value
  end)

  -- When a creature appears, check if it's a player and it's on the same z level
  onCreatureAppear(function(creature)
    if not enabled then return false end  -- Ensure we only run if the icon is enabled
    if creature:isPlayer() and creature:getText() == "" and creature:getPosition().z == posz() and creature ~= player then
        g_game.look(creature)
        found = now
    end
    return true  -- Ensuring the callback returns a boolean value
  end)

  -- Regex pattern to match text containing player name, level, and guild
  local regex = [[You see ([^\(]*) \(Level ([0-9]*)\)((?:.)* of the ([\w ]*),|)]]
  
  -- Process incoming text messages that match the pattern
  onTextMessage(function(mode, text)
    if not enabled then return false end  -- Ensure we only run if the icon is enabled

    local re = regexMatch(text, regex)
    if #re ~= 0 then
        local name = re[1][2]
        local level = re[1][3]
        local guild = re[1][5] or ""

        -- Truncate guild name to 10 characters and append "..." if longer
        if guild:len() > 10 then
          guild = guild:sub(1, 10)
          guild = guild.."..."
        end

        local voc
        if text:lower():find("sorcerer") then
            voc = "MS"
        elseif text:lower():find("druid") then
            voc = "ED"
        elseif text:lower():find("knight") then
            voc = "EK"
        elseif text:lower():find("paladin") then
            voc = "RP"
        end

        local creature = getCreatureByName(name)
        if creature then
            creature:setText("\n"..level..voc.."\n"..guild)
        end

        -- Clear messages if the player has been found recently
        if found and now - found < 500 then
          modules.game_textmessage.clearMessages()
        end
    end
    return true  -- Ensuring the callback returns a boolean value
  end)
end)
Editor is loading...
Leave a Comment