From fd047c4c99ed9ebd6327d3f390dea1c232c08da5 Mon Sep 17 00:00:00 2001 From: Beha Date: Fri, 19 Oct 2018 14:01:11 -0400 Subject: [PATCH] Add Sfinv support. (#48) * Add Sfinv support --- README.md | 2 +- depends.txt | 1 + smartfs.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 63681dd..1cf7b3b 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Now that you have located your element you can modify it. button1:setPos(4,0) ## Inventory Support -Smartfs supports adding a button to Inventory+ or Unified Inventory which will open one of your own custom forms. Use the smartfs.add\_to\_inventory(form, icon, title) function where form is the smartfs form linked to by the button, icon is the button image (only for unified inventory), and title is the button text (only for inventory+). +Smartfs supports adding a button to Sfinv, Inventory+, or Unified Inventory which will open one of your own custom forms. Use the smartfs.add\_to\_inventory(form, icon, title) function where form is the smartfs form linked to by the button, icon is the button image (only for unified inventory), and title is the button text (for inventory+ and sfinv). smartfs.add_to_inventory(form, icon, title) diff --git a/depends.txt b/depends.txt index 4893cdc..d8b1e6c 100644 --- a/depends.txt +++ b/depends.txt @@ -1,2 +1,3 @@ unified_inventory? inventory_plus? +sfinv? diff --git a/smartfs.lua b/smartfs.lua index 53184ca..840d806 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -79,6 +79,8 @@ function smartfs.inventory_mod() return "unified_inventory" elseif minetest.global_exists("inventory_plus") then return "inventory_plus" + elseif minetest.global_exists("sfinv") then + return "sfinv" else return nil end @@ -175,6 +177,56 @@ smartfs._ldef.inventory_plus = { end } +-- Sfinv plugin +smartfs._ldef.sfinv = { + add_to_inventory = function(form, icon, title) + sfinv.register_page(form.name, { + title = title, + get = function(self, player, context) + local name = player:get_player_name() + local state + if smartfs.inv[name] then + state = smartfs.inv[name] + else + local statelocation = smartfs._ldef.sfinv._make_state_location_(name) + state = smartfs._makeState_(form, nil, statelocation, name) + smartfs.inv[name] = state + if form.form_setup_callback(state) ~= false then + smartfs.inv[name] = state + else + return "" + end + end + local fs = state:_buildFormspec_(false) + return sfinv.make_formspec(player, context, fs, true) + end, + on_player_receive_fields = function(self, player, _, fields) + local name = player:get_player_name() + if smartfs.inv[name] then + smartfs.inv[name]:_sfs_on_receive_fields_(name, fields) + end + end, + on_leave = function(self, player) + local name = player:get_player_name() + if smartfs.inv[name] then + smartfs.inv[name].players:disconnect(name) + smartfs.inv[name] = nil + end + end, + }) + end, + _make_state_location_ = function(player) + return { + type = "inventory", + inventory_handles_fields = true, + player = player, + _show_ = function(state) + sfinv.set_player_inventory_formspec(minetest.get_player_by_name(state.location.player)) + end, + } + end +} + -- Show to player smartfs._ldef.player = { _make_state_location_ = function(player) @@ -347,7 +399,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) smartfs.opened[name] = nil end end - elseif smartfs.inv[name] and smartfs.inv[name].location.type == "inventory" then + elseif smartfs.inv[name] and smartfs.inv[name].location.type == "inventory" and not smartfs.inv[name].location.inventory_handles_fields then local state = smartfs.inv[name] state:_sfs_on_receive_fields_(name, fields) end