Skip to content

open.mp compatibility  #1

@Mergevos

Description

@Mergevos
#include <open.mp>
#include <YALP>

main()
{
    return;
}

new Lua:l;

public OnGameModeInit()
{
    print("Test");
    new File:scriptfile = fopen("main.lua", io_read);
    l = lua_newstate();
    if(lua_load(l, "ScriptReader", _:scriptfile, flength(scriptfile) char) || !lua_bind(l))
    {
        lua_stackdump(l);
        lua_close(l);
    }
    return 1;
}

forward ScriptReader(Lua:L, buffer[], File:file, size);
public ScriptReader(Lua:L, buffer[], File:file, size)
{
    new length = flength(file);
    new read = fblockread(file, buffer, length * 8 / cellbits);
    buffer[read] =
        ((fgetchar(file, false) & 0xFF)) |
        ((fgetchar(file, false) & 0xFF) << 8) |
        ((fgetchar(file, false) & 0xFF) << 16);
    fclose(file);
    return -length;
}
do return "test" end

local interop = require "interop"

local public = interop.public
local SendClientMessage, SetPlayerHealth
import(interop.native) -- assigns SendClientMessage and SetPlayerHealth from interop.native.SendClientMessage and interop.native.SetPlayerHealth

local COLOR_WHITE = 0xFFFFFFFF

local function tofloat(num) -- when calling native functions with a float parameter, a float value must be ensured
    return 0.0 + num
end

function public.OnPlayerConnect(playerid) -- adding a function to the "public" table automatically registers the callback (based on the name)
    SendClientMessage(playerid, COLOR_WHITE, "Hello from Lua!") -- calling a native function requires almost no modifications
end

local commands = {}

function commands.hp(playerid, params)
    local hp = tonumber(params)
    if not hp then
        return SendClientMessage(playerid, COLOR_WHITE, "Usage: /hp [value]")
      end
      SetPlayerHealth(playerid, tofloat(hp))
      SendClientMessage(playerid, COLOR_WHITE, "Your health was set to "..hp.."!")
      return true
end

function public.OnPlayerCommandText(playerid, cmdtext)
      playerid = interop.asinteger(playerid) -- YALP cannot guess the type of the arguments, so they must be explicitly converted
      cmdtext = interop.asstring(cmdtext)
  
      local ret
      cmdtext:gsub("^/([^ ]+) ?(.*)$", function(cmd, params)
        local handler = commands[string.lower(cmd)]
        if handler then
              ret = handler(playerid, params)
        end
      end)
      return ret
end

print("Lua script initialized!")

[2023-10-06T02:45:44+0200] [Info] Loading plugin: YALP
[2023-10-06T02:45:44+0200] [Info] YALP v1.1 loaded
[2023-10-06T02:45:44+0200] [Info] Created by IllidanS4
[2023-10-06T02:45:44+0200] [Info] Test
[2023-10-06T02:45:44+0200] [Error] (sleep mode)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions