Skip to content

Repository files navigation

mt-api

Type definitions for using the minetest API

using

Install dev dependency in your typescript project:

npm i @repcomm/mt-api --save-dev

And use:

import type {} from "@repcomm/mt-api";

The module declares the minetest global the same way you'd use it in lua
you can also utilize the types it provides in your own code by importing them:

import type { MtVec3 } from "@repcomm/mt-api";
let myVec: MtVec3 = { x: 0, y: 0, z: 0 };

implemented

  • minetest global namespace
minetest.register_on_joinplayer((player)=>{
let playername=player:get_player_name();minetest.chat_send_player(playername,"Welcome!")});

dev-dependencies

contributors (any contributions welcome, thank you!)

also see

contributing

Users of minetest's lua api will noticed a lack of ":" in typescript

Lua uses obj:method and obj.func to differentiate with obj is passed as self as the first argument

For instance:

localobj= {
method=function (self)
--"self" refers to obj, similar to "this" in typescriptendfunc=function ()
--no self variable hereend
};
obj.method() -- self will be nilobj:method() -- self will be objobj.func() -- self will be nilobj:func() -- self will still be nil because its not declared in function args

In typescript this is handled by providing a this definition:

interfaceMinetestGlobal{register_on_joinplayer(this: void,cb: MtPlayerJoinCallback): void;}declare global minetest: MinetestGlobal;

Because

this: void

TypeScript calls to minetest.register_on_joinplayer() will properly output: minetest.register_on_joinplayer() in lua

Without providing this: void, this would generate: minetest:register_on_joinplayer() as typescript-to-lua compiler assumes we want to provide a self reference as first argument

On the flip-side:

functionhandle_player_join (player) --player is ObjRefplayer:get_player_name() -- passes player as first arg to get_player_name codeendminetest.register_on_joinplayer ( handle_player_join )

In typescript definitions:

interfaceObjRef{//implicit this: ObjRefget_player_name(): string;//same asget_player_name(this: ObjRef): string;}

Which both properly output:

player:get_player_name()

About

TypeScript definitions for base minetest API, for use with typescript-to-lua

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages