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

Action that uses a lib

luigilc

Lua Learner
Joined
Mar 24, 2010
Messages
863
Reaction score
36
Location
A music box
Hi, I have this lib that I got in another forum somewhere but I have a problem, as it's a lib I need to create the actions for it, cause it's a slot system lib. So, can anyone create an action as example or something like that?
I want an action that you click the item and then use it on the equipment and it does what is in the lib.

here is the lib:
Lua:
SlotCondition = {}
 
Slots = {
    config = {
        maxSlots = 2,
        msg = {
            type = MESSAGE_STATUS_DEFAULT,
            cantOpenSlot = "You cant open a slot on this item, or this isn't a valid sloter.",
            maxStat = "You can't add more bonus on this item with this sloter.",
            upgrade = "You upgrated the %s slot from %s to %s.",
            failed = "You failed upgrading the slot.",
            errorMsg = "Put the item on the table, and then the sloter, and use the hammer on it."
        },
 
        conditions = {
                    hp = {CONDITION_PARAM_STAT_MAXHEALTHPERCENT, 50},
                    mp = {CONDITION_PARAM_STAT_MAXMANAPERCENT, 51},
                    ml = {CONDITION_PARAM_STAT_MAGICLEVELPERCENT, 52},
                    club = {CONDITION_PARAM_SKILL_CLUBPERCENT, 53},
                    sword = {CONDITION_PARAM_SKILL_SWORDPERCENT, 54},
                    axe = {CONDITION_PARAM_SKILL_AXEPERCENT, 55},
                    shield = {CONDITION_PARAM_SKILL_SHIELDPERCENT, 56},
                    dist = {CONDITION_PARAM_SKILL_DISTANCEPERCENT, 57}
        },
 
        chance = function(level, status, max)
            return 1-(1-((level/5) - (status/max)))
        end
    },
 
    sloters = {
        [8303] = {"hp", {1, 15, 6}},
        [8302] = {"mp", {1, 8, 5}},
        [8301] = {"ml", {1, 4, 5}},
        [8310] = {"club", {1, 6, 6}},
        [8299] = {"sword", {1, 6, 6}},
        [8298] = {"axe", {1, 6, 6}},
        [8309] = {"shield", {1, 4, 5}},
        [8297] = {"dist", {1, 5, 5}}
    },
 
    appendItemName = function(self, txt)
        return self:setItemName(self:getItemName() .. " " .. txt)
    end,
 
    playerMessage = function(self, cid, msg, ...)
        return doPlayerSendTextMessage(cid, self.config.msg.type, self.config.msg[msg]:format(...))
    end,
 
    isHandEquipment = function(self)
        local weptype = self:getItemWeaponType()
        return (weptype > 0) and (weptype < 7)
    end,
 
    isEquipment = function(self)
        return self:isHandEquipment() or (self.item.info.armor ~= 0)
    end,
 
 
    loadConditions = function(self)
        for cond, id in pairs(self.config.conditions) do
            SlotCondition[cond] = {}
 
            for i = 1, 100 do
                local slotCondition = createConditionObject(CONDITION_ATTRIBUTES)
                setConditionParam(slotCondition, CONDITION_PARAM_TICKS, -1)
                setConditionParam(slotCondition, id[1], 100+i)
                setConditionParam(slotCondition, CONDITION_PARAM_BUFF, true)
                setConditionParam(slotCondition, CONDITION_PARAM_SUBID, id[2])
 
                SlotCondition[cond][i] = slotCondition
            end
        end
    end,
 
    setItemName = function(self, name)
        return doItemSetAttribute(self.item.uid, "name", name)
    end
}
 
Slots:loadConditions()
 
function Slots:loadItem(item)
    local newObj = setmetatable({item = {uid = item.uid, info = getItemInfo(item.itemid)}}, {__index = function(self, index)
        if(_G[index]) then
            return setmetatable({callback = _G[index]}, {__call = function(self, ...)
                return self.callback(item.uid, ...)
            end})
        else
            return Slots[index]
        end
    end})
 
    if(not newObj:isEquipment()) then return false end
 
    newObj:getItemSlotCount()
    return newObj
end
 
 
function Slots:getItemSlotCount()
    local e, n = self:getItemName():gsub("%[.-%]", "")
    self.item.slotCount = n
    return true
end
 
function Slots:getItemStatusBonus(status)
    local bonus = self:getItemName():match("%[".. status .."%+(.-)%%%]")
 
    return tonumber(bonus) or 0
end
 
function Slots:getItemBonus()
    local result = {}
    self:getItemName():gsub("%[(.-)%+.-%%%]", function(s) table.insert(result, {s, self:getItemStatusBonus(s)}) return "" end)
 
    return result
end
 
function Slots:openNewSlot(cid, sloter)
    local sloterInfo = self.sloters[sloter.itemid]
 
    if(not sloterInfo) then
        self:playerMessage(cid, "errorMsg")
        return false
    end
 
    local statusBonus = self:getItemStatusBonus(sloterInfo[1])
 
    if((statusBonus == 0) and self.item.slotCount == self.config.maxSlots) then
        self:playerMessage(cid, "cantOpenSlot")
        return false
    end
 
    if(self:isEquipment()) then
        if(statusBonus >= sloterInfo[2][2]) then
            self:playerMessage(cid, "maxStat")
            return false
        end
 
        doRemoveItem(sloter.uid, 1)
 
        if(math.random(0, 100) < (self.config.chance(sloterInfo[2][3], statusBonus, sloterInfo[2][2]) * 100)) then
            if(statusBonus > 0) then
                self:setItemName(self:getItemName():gsub("%[".. sloterInfo[1] .."%+(.-)%%%]", ("[%s+%s%%]"):format(sloterInfo[1], statusBonus + sloterInfo[2][1] .. "%")))
            else
                self:appendItemName(("[%s+%s%%]"):format(sloterInfo[1], statusBonus + sloterInfo[2][1]))
            end
 
            self:playerMessage(cid, "upgrade", sloterInfo[1], statusBonus .. "%", statusBonus + sloterInfo[2][1] .. "%")
 
            return true
        end
 
        self:playerMessage(cid, "failed")
 
        return false
    else
        self:playerMessage(cid, "cantOpenSlot")
        return false
    end
end
 
Set = {
    config = {
        checkTime = 2000
    },
 
    isOnline = function(self)
        local players = getOnlinePlayers()
 
        for i, v in ipairs(players) do
            if(getCreatureByName(v) == self.cid) then
                return true
            end
        end
 
        return false
    end
}
 
function Set:loadSetFromPlayer(cid, n)
    local newSet = setmetatable({cid = cid}, {__index = self})
 
    return n and newSet:loadBonus() or newSet:removeConditions()
end
 
function Set:removeConditions(subid)
    if(subid) then
        doRemoveCondition(self.cid, CONDITION_ATTRIBUTES, subid)
 
        return true
    end
 
    for cond, id in pairs(Slots.config.conditions) do
        doRemoveCondition(self.cid, CONDITION_ATTRIBUTES, id[2])
    end
end
 
function Set:loadBonus()
    if(not self:isOnline()) then return false end
 
    for subid = 50, #Slots.config.conditions do
        doRemoveCondition(self.cid, CONDITION_ATTRIBUTES, subid)
    end
 
    local bonus = {}
 
    for slot = 1, 9 do
        local slotItem = getPlayerSlotItem(self.cid, slot)
 
        if(slotItem.itemid ~= 0) then
            local item = Slots:loadItem(slotItem)
 
            if(item) then
                for i, v in ipairs(item:getItemBonus()) do
                    if((slot == 5) or (slot == 6)) then
                        if(item:isHandEquipment()) then
                            bonus[v[1]] = (bonus[v[1]] or 0) + v[2]
                        end
                    else
                        bonus[v[1]] = (bonus[v[1]] or 0) + v[2]
                    end
                end
            end
        end
    end
 
    for cond, id in pairs(Slots.config.conditions) do
        if(bonus[cond]) then
            local v = (cond == "hp") and getCreatureHealth(self.cid) or (cond == "mp") and getCreatureMana(self.cid)
 
            doAddCondition(self.cid, SlotCondition[cond][bonus[cond] > 100 and 100 or bonus[cond]])
            doCreatureAddHealth(self.cid, (cond == "hp") and (v - getCreatureHealth(self.cid)) or 0)
            doCreatureAddMana(self.cid, (cond == "mp") and (v - getCreatureMana(self.cid)) or 0)
        else
            doRemoveCondition(self.cid, CONDITION_ATTRIBUTES, id[2])
        end
 
    end
 
    addEvent(self.loadBonus, self.config.checkTime, self)
end

I appreciate any tips/help, Thank you
 
No, libs are bundles of functions which are called by another script, if these are global functions(functions that can be called by creaturescript, action, moveevent, etc.) then it goes under "lib\" folder, otherwise it goes in their respective "lib" folders according to what uses it.
 
Here's an example of script with and without libs/classes (Script from My Server)
It's the same script/function!

Normal Script (without libs/classes):
Code:
function onUse(cid, item, frompos, item2, topos)
if item.uid == 1023 then
  queststatus = getPlayerStorageValue(cid,1023)
  if queststatus == -1 and getPlayerAccess(cid) == 0 then
   doPlayerSendTextMessage(cid,22,"You have found a rapier.")
   item_uid = doPlayerAddItem(cid,2384,1)
   setPlayerStorageValue(cid,1023,1)

  else
   doPlayerSendTextMessage(cid,22,"The chest is empty.")
  end
else
  return 0
end
return 1
end

Script with lib/classes:
Code:
function onUse(cid, item, frompos, item2, topos)

Player = Actor.create(cid)
Item = Item.create(item.uid)

if Item:GetItem().uid == 1023 then
	Player:SetQuest("chest", 1023, 2384, 1)
end	

	return TRUE
end
 
see? I'm so bad at lua I can't even differ things LOL
still looking for someone to help me out on creating an action that uses the functions on the lib I posted
 
Back
Top