From e7bc9aafca91543882ecbc3620ecb2888ff1d1cd Mon Sep 17 00:00:00 2001 From: luk3yx Date: Wed, 26 Aug 2020 21:55:30 +1200 Subject: [PATCH] Fix sscsm.get_player_control() on MT 5.4.0-dev. This further bloats sscsm_init.lua to ensure the LMB/RMB fields are equal to dig/place. --- README.md | 2 ++ sscsm_init.lua | 37 +++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bd68e09..3dd4d09 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,8 @@ SSCSMs can access most functions on [client_lua_api.txt](https://github.com/mine - `sscsm.get_player_control()`: Alternative for `minetest.localplayer:get_control()` that works with Minetest 5.2.0 and below. + - The `LMB` and `RMB` fields are deprecated and will probably be removed in + the future, use `dig` and `place` instead. - `sscsm.every(interval, func, ...)`: Calls `func` every `interval` seconds with any extra parameters specified. Use `minetest.register_globalstep` instead if `interval` is `0`. diff --git a/sscsm_init.lua b/sscsm_init.lua index cf3246b..35093e2 100644 --- a/sscsm_init.lua +++ b/sscsm_init.lua @@ -133,22 +133,39 @@ end -- A proper get_player_control didn't exist before Minetest 5.3.0. if minetest.localplayer.get_control then - function sscsm.get_player_control() - return minetest.localplayer:get_control() + -- Preserve API compatibility + if minetest.localplayer:get_control().LMB == nil then + -- MT 5.4+ + function sscsm.get_player_control() + local c = minetest.localplayer:get_control() + c.LMB, c.RMB = c.dig, c.place + return c + end + else + -- MT 5.3 + function sscsm.get_player_control() + local c = minetest.localplayer:get_control() + c.dig, c.place = c.LMB, c.RMB + return c + end end else + -- MT 5.0 to 5.2 + local floor = math.floor function sscsm.get_player_control() local n = minetest.localplayer:get_key_pressed() return { up = n % 2 == 1, - down = math.floor(n / 2) % 2 == 1, - left = math.floor(n / 4) % 2 == 1, - right = math.floor(n / 8) % 2 == 1, - jump = math.floor(n / 16) % 2 == 1, - aux1 = math.floor(n / 32) % 2 == 1, - sneak = math.floor(n / 64) % 2 == 1, - LMB = math.floor(n / 128) % 2 == 1, - RMB = math.floor(n / 256) % 2 == 1, + down = floor(n / 2) % 2 == 1, + left = floor(n / 4) % 2 == 1, + right = floor(n / 8) % 2 == 1, + jump = floor(n / 16) % 2 == 1, + aux1 = floor(n / 32) % 2 == 1, + sneak = floor(n / 64) % 2 == 1, + LMB = floor(n / 128) % 2 == 1, + RMB = floor(n / 256) % 2 == 1, + dig = floor(n / 128) % 2 == 1, + place = floor(n / 256) % 2 == 1, } end