From 7a32634ddc2df7063577273c58fe0dcfead46cd1 Mon Sep 17 00:00:00 2001 From: Zughy <4279489-marco_a@users.noreply.gitlab.com> Date: Mon, 27 Jul 2020 16:25:11 +0200 Subject: [PATCH] Callbacks implemented --- README.md | 11 +++++++++-- callbacks.lua | 11 +++++++++++ deserializer.lua | 1 - init.lua | 6 ++++-- locale/magic_compass.it.tr | 2 +- locale/template.txt | 2 +- player_manager.lua | 15 +++++++++++++++ 7 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 callbacks.lua diff --git a/README.md b/README.md index 867b64c..e427faf 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,11 @@ Teleport system for Minetest (yes, GUI aspect will be improved) ### How to use it -Look for the "Magic Compass" item in your inventory, put it in your hand and left click it to open the locations menu +Look for the "Magic Compass" item in your creative inventory, put it in your hand and left click it to open the locations menu ### Customisation + +##### Create new locations Every icon in the menu is a location. Locations must be declared in a .txt document inside the `locations` folder like so: ``` Red Forest -- the name you want to show in the menu when hovering the icon @@ -22,7 +24,12 @@ HIDE -- (optional) whether to hide the icon to players who don't have the The file name is important too, as it must start with a number followed by an underscore like `5_whatever name.txt`. The number indicates the position of the associated item in the grid (which scales according to the highest number declared), and empty spaces are generated automatically if the numbers of the items don't represent a full sequence. -Also, don't forget to edit `config.txt` to suit your needs! +##### Callbacks +* `magic_compass.register_on_use(function(player, ID, item_name, pos))`: use it to run more checks BEFORE using the item. If it returns nil or false, the action is cancelled. If true, it keeps going +* `magic_compass.register_on_after_use(function(player, ID, item_name, pos))`: use it to run additional code AFTER having been teleported + +##### Graphic aspect +Edit `config.txt` to suit your needs! ### Want to help? Feel free to: diff --git a/callbacks.lua b/callbacks.lua new file mode 100644 index 0000000..52ea03e --- /dev/null +++ b/callbacks.lua @@ -0,0 +1,11 @@ +-- I had no idea how to do it, so, uhm... this is how Minetest handles callbacks +local function make_registration() + local t = {} + local registerfunc = function(func) + t[#t+1] = func + end + return t, registerfunc +end + +magic_compass.registered_on_use, magic_compass.register_on_use = make_registration() +magic_compass.registered_on_after_use, magic_compass.register_on_after_use = make_registration() diff --git a/deserializer.lua b/deserializer.lua index 0a28e19..b12e406 100644 --- a/deserializer.lua +++ b/deserializer.lua @@ -1,4 +1,3 @@ -magic_compass = {} magic_compass.items = {} local S = minetest.get_translator("magic_compass") diff --git a/init.lua b/init.lua index 9192037..363f7bf 100644 --- a/init.lua +++ b/init.lua @@ -1,9 +1,11 @@ -local version = "1.2.0" +magic_compass = {} +local version = "1.3.0" +dofile(minetest.get_modpath("magic_compass") .. "/callbacks.lua") dofile(minetest.get_modpath("magic_compass") .. "/deserializer.lua") dofile(minetest.get_modpath("magic_compass") .. "/formspec.lua") dofile(minetest.get_modpath("magic_compass") .. "/items.lua") dofile(minetest.get_modpath("magic_compass") .. "/load_config.lua") dofile(minetest.get_modpath("magic_compass") .. "/player_manager.lua") -minetest.log("action", "[MAGIC_COMPASS] Mod initialised, running version " .. version) +minetest.log("action", "[MAGIC COMPASS] Mod initialised, running version " .. version) diff --git a/locale/magic_compass.it.tr b/locale/magic_compass.it.tr index 831cfc6..ee84de4 100644 --- a/locale/magic_compass.it.tr +++ b/locale/magic_compass.it.tr @@ -1,4 +1,4 @@ -# version 1.2.0 +# version 1.3.0 # author(s): Zughy # reviewer(s): # textdomain: magic_compass diff --git a/locale/template.txt b/locale/template.txt index a04ea6e..6feb262 100644 --- a/locale/template.txt +++ b/locale/template.txt @@ -1,4 +1,4 @@ -# version 1.2.0 +# version 1.3.0 # author(s): # reviewer(s): # textdomain: magic_compass diff --git a/player_manager.lua b/player_manager.lua index 2b66aa1..4fa0941 100644 --- a/player_manager.lua +++ b/player_manager.lua @@ -34,12 +34,27 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) }) return end + -- se non passa gli eventuali callback, annullo + for _, callback in ipairs(magic_compass.registered_on_use) do + if not callback(player, ID, item.desc, item.pos) then + minetest.sound_play("magiccompass_teleport_deny", { + to_player = p_name + }) + return + end + end + -- teletrasporto player:set_pos(minetest.string_to_pos(item.pos)) minetest.sound_play("magiccompass_teleport", { to_player = p_name }) + -- eventuali callback dopo l'uso + for _, callback in ipairs(magic_compass.registered_on_after_use) do + callback(player, ID, item.desc, item.pos) + end + -- eventuale cooldown if item.cooldown then