Making Plugin API

Discussion in 'Rust Development' started by PaiN, May 25, 2015.

  1. Hello again,

    This will be my first API btw
    So the plugin that i will upload in some days needs an API from my other plugin "AFK System"
    I've checked some plugins like Chat Mute and used his code for a function but i want to make the same API but for if afk= true or false..

    For now i got only
    Code:
    function PLUGIN:IsAfk(player)
        local targetSteamID = rust.UserIDFromPlayer(player)
        if not afkData[targetSteamID] then
            return false
        end
        if afkData[targetSteamID] then
            afkData[targetSteamID] = false
            datafile.SaveDataTable(afkData)
            return false
        end
        if afkData[targetSteamID] then
            afkData[targetSteamID] = true
            datafile.SaveDataTable(afkData)
            return true
        end
    endfunction PLUGIN:afkData(steamID)
        return afkData[steamID] ~= false
    endfunction PLUGIN:APIAfk(steamID)
        if afkData[steamID] then return false end
        afkData[steamID] = {}
        afkData[steamID].steamID = steamID
        table.insert(muteData, muteData[steamID])
        datafile.SaveDataTable(afkData)
        return true
    end
    
    What i've created for the plugin that needs the API
    and error for this
    Code:
                local isAfk = AFKSystem:Call("IsAfk", targetPlayer)
                if isAfk then
                rust.SendChatMessage(player, self.Config.Messages.ChatPrefix, "This Player is AFK!")
                else
                rust.SendChatMessage(player, self.Config.Messages.ChatPrefix, "This Player is NOT AFK!")
                end
    
    Code:
    [Oxide] 9:31 PM [Error] Failed to call hook 'cmdATest' on plugin 'PaiN Test'
    File: p-test.lua Line: 80 attempt to index upvalue 'AFKSystem' (a nil value):
      at NLua.Lua.ThrowExceptionFromError (Int32 oldTop) [0x00000] in <filename unknown>:0
      at NLua.Lua.CallFunction (System.Object function, System.Object[] args, System.Type[] returnTypes) [0x00000] in <filename unknown>:0
      at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
      at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
     
  2. You need to get a reference to your AFKSystem plugin..
     
  3. Reference.. ? The first code on my post is in my Afk System that's the reference isn't it

    EDIT: I've probably fixed the error give me a sec i will test it
    [DOUBLEPOST=1432579273][/DOUBLEPOST]Okay i fixed the error now I have the Data file with my steam id inside and its true so the my plugin should say "Player is AFK" but it still says "is NOT AFK"

    and i'm 99% that something is wrong with the API code that i wrote on my first post
     
    Last edited by a moderator: May 25, 2015
  4. You shouldn't be making a plugin API if you don't understand how it will be called from other plugins.

    Plugin A has methods that are exposed for plugin B to call

    Plugin B will need to get a reference to plugin A to be able to call those exposed methods.

    Your error is exactly what it means, you need to get a reference to your afksystem plugin before calling methods on it.

    http://oxidemod.org/threads/how-to-use-plugin-api.7883/#post-80777
     
  5. I've got the reference its okay now 0 errors...

    NOW i probably have something wrong in my API ... I have the Data file with my steam id inside and its true so the my plugin should say "Player is AFK" but it still says "is NOT AFK" I cant find anything wrong with my code ...
     
  6. Post your code please
     
  7. First post nothing changed just added reference to the plugin that needs the api from afksystem :))
     
  8. You logic is wrong here:

    Code:
    if afkData[targetSteamID] then
            afkData[targetSteamID] = false
            datafile.SaveDataTable(afkData)
            return false
        end
        if afkData[targetSteamID] then
            afkData[targetSteamID] = true
            datafile.SaveDataTable(afkData)
            return true
        end
     
  9. I was sure that something is wrong here but dunno how should it go i'm based on ChatMute Plugin but it has expiration and other things cannot find a good example...

    I mean the ChatMute is a good example but not for what i need right now :)
     
  10. Write pseudocode for what you want to do and implement it, you might also want to make a flowchart before implementing you method. This is all basic stuff you should do when learning to program.