From b25b229eb22c1b07038547b417f16070a87dff88 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 17 Apr 2021 19:40:37 -0500 Subject: [PATCH] Add Player.instance & Player:teleport() --- scripts/test.lua | 16 +++++++++--- src/lua/PlayerWrapper.cpp | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/scripts/test.lua b/scripts/test.lua index edfd67e..888d925 100644 --- a/scripts/test.lua +++ b/scripts/test.lua @@ -1,15 +1,23 @@ -print("Hello world!") +print("Hello wtf!") -World.onPlayerAdded:listen(function(plr) +function onJoin(plr) print(plr.type .. " " .. plr.name .. " joined from LUA!!") plr.onChat:listen(function(msg) - print(plr.name .. " said : " .. msg) + print(plr.name .. " said : \'" .. msg .. "\'") if msg == "kickme" then plr:kick() + elseif msg == "hi" then + print("hello " .. plr.name) end end) -end) +end + +World.onPlayerAdded:listen(onJoin) + +for i, plr in ipairs(World.players) do + onJoin(plr) +end wait(2) print("Hello world ~2 seconds later! running protcol version " .. World.version) \ No newline at end of file diff --git a/src/lua/PlayerWrapper.cpp b/src/lua/PlayerWrapper.cpp index 72e3f02..0d20116 100644 --- a/src/lua/PlayerWrapper.cpp +++ b/src/lua/PlayerWrapper.cpp @@ -62,6 +62,16 @@ static int plr_getName(lua_State *state) { return 1; } +static int plr_getInstance(lua_State *state) { + Player *plr = grabPlayer(state, 1); + + if (plr == NULL) + return 0; + + lua_pushnumber(state, plr->instanceID); + return 1; +} + static int plr_getChatted(lua_State *state) { Player *plr = grabPlayer(state, 1); @@ -79,13 +89,32 @@ static int plr_getChatted(lua_State *state) { static const luaL_Reg plr_getters[] = { {"name", plr_getName}, + {"instance", plr_getInstance}, {"onChat", plr_getChatted}, {0, 0} }; // =============================================== [[ SETTERS ]] =============================================== +static int plr_setInstance(lua_State *state) { + EntityRef *ref = grabEntityRef(state, 1); + int newInst = luaL_checkint(state, 2); + Player *plr; + CNSocket *sock; + + // sanity check + if (ref == NULL) + return 0; + + plr = (Player*)ref->getEntity(); + sock = ref->sock; + + PlayerManager::sendPlayerTo(sock, plr->x, plr->y, plr->z, newInst); + return 0; +} + static const luaL_Reg plr_setters[] = { + {"instance", plr_setInstance}, {0, 0} }; @@ -118,8 +147,30 @@ static int plr_kick(lua_State *state) { return 0; } +static int plr_teleport(lua_State *state) { + EntityRef *ref = grabEntityRef(state, 1); + Player *plr; + CNSocket *sock; + + // sanity check + if (ref == NULL) + return 0; + + plr = (Player*)ref->getEntity(); + sock = ref->sock; + + int X = luaL_checkint(state, 2); + int Y = luaL_checkint(state, 3); + int Z = luaL_checkint(state, 4); + int inst = luaL_optint(state, 5, plr->instanceID); + + PlayerManager::sendPlayerTo(sock, X, Y, Z, inst); + return 0; +} + static const luaL_Reg plr_methods[] = { {"kick", plr_kick}, + {"teleport", plr_teleport}, {0, 0} };