...
 
Commits (4)
  • Dirk's avatar
    clean up init information for later reimplemtation · 2e0c7674
    Dirk authored
    2e0c7674
  • Dirk's avatar
    use function to get nodes instead of table · a97fedd0
    Dirk authored
    a97fedd0
  • Dirk's avatar
    implement node filtering · 3095475a
    Dirk authored
    the API function `get_nodes()` returns a list of all supported and
    pre-filtered nodes. In addition to that the API table `nodes` has this
    nodes already pre-loaded for use in other mods.
    
    The function takes a parameter for allowing ad-hoc whitelisting of nodes
    from the pre-filtered nodes list. The syntax is identical to the
    filterstrings implemented with the nodes whitelist and blacklist
    functionality and is described there. The optional parameter has to be
    a table of filterstrings. The built-in furniture uses the API table.
    3095475a
  • Dirk's avatar
    API documentation update · a0ddc479
    Dirk authored
    a0ddc479
......@@ -6,12 +6,13 @@ xFurniture provides API-like functions that allow and assist mods that add custo
Create all the necessary mod files (`depends.txt` and `init.lua` are enough for testing purposes). You need to depend on `xfurniture` to get access to the necessary functions. In your `init.lua` first you should localize the global function that will be used.
In addition to that you might want to get the nodes table. This table is different from Minetest’s `registered_nodes` as it is already filtered as defined by the user. The table can be iterated over later to register the custom furniture for all supported nodes.
In addition to that you might want to get the nodes table. This table is different from Minetest’s `registered_nodes` as it is already filtered as defined by the user. The table can be iterated over later to register the custom furniture for all supported nodes. There is also a function `get_nodes()` available that can be used to filter the pre-filtered nodes through a whitelist.
```Lua
local register = xfurniture.register
local apply_indent = xfurniture.apply_indent
local nodes = xfurniture.nodes
local get_nodes = xfurniture.get_nodes
```
`apply_indent` is a helper function that allows you to apply the user-configured indent to the nodebox table. The indentation value is read from the configuration value `xfurniture_window_stuff_indent`. As seen from the name this is intended to be used with window decorations (things that are placed against walls).
......@@ -65,7 +66,7 @@ This is only useful for things that are placed against walls. Do not rely on ind
# Register multiple objects
If you want to register multiple objects at once for all of the supported mods you can simply iterate over the previously mentioned `nodes` table.
If you want to register multiple objects at once for all of the supported mods you can simply iterate over the previously mentioned `nodes` table or use the `get_nodes()` function.
```Lua
for node in pairs(nodes) do
......@@ -88,3 +89,40 @@ end
As you can see this is pretty much the same function call. Except this time it is in an iteration loop and uses `node` in the recipe and material value. This registers `mymodname:all_nodes_table_default_wood` where `default_wood` is a placeholder for all node names of all the nodes that are in the `nodes` table.
Please note that this could register several hundred nodes depending on the user’s configuration and might crash Minetest by reaching the [max node limit](https://github.com/minetest/minetest/issues/6101).
## Using the `get_nodes()` function
*When not providing a table containing filterstrings using `nodes` is faster than using `get_nodes()` and provides the exact same result.*
With `get_nodes()` all already pre-filtered nodes are filtered through a whitelist represented by a table of filterstrings. A filterstring is a comma delimited string containing a filter and a value. For example `group:foobar` is a filterstring that filters for the node being in group `foobar`. The following filters are available.
* `group` – Matches the group a node is in
* `startswith` – Matches the beginning of node name
* `endswith` – Matches the end of a node name
* `contains` – Matches if a node name contains the value
* `origin` – Matches the node’s `mod_origin` value
* `drawtype` – Matches the node’s drawtype value
* `a node ID` – Matches the exact node ID
The following example registers a table made from all nodes that contain “wood” at some place in the name (and overwrites the recipe from the previous example).
```Lua
for node in pairs(get_nodes({'contains:wood'})) do
register('all_nodes_table', {
name = 'My Cool Table For Wood Nodes',
material = node,
nodebox = {
{-0.0625, -0.5, -0.0625, 0.0625, 0.4375, 0.0625}, -- pole
{-0.375, 0.375, -0.375, 0.375, 0.5, 0.375} -- top
},
recipe = {
{node, node, node},
{'', node, '' },
{'', node, '' }
}
})
end
```
Filter are not applied in combination but individually. So for example using `{ 'group:foo', 'endwith:bar' }` will not match all nodes that are in group “foo” and end with “bar” but will match all nodes in group “foo” and all nodes that end with “bar”.
......@@ -48,3 +48,20 @@ for node in pairs(nodes) do
}
})
end
-- Using get_nodes() to ad-hoc filter the nodes
for node in pairs(get_nodes({'contains:wood'})) do
register('wooden_nodes_table', {
name = 'My Cool Table For Wood Nodes',
material = node,
nodebox = {
{-0.0625, -0.5, -0.0625, 0.0625, 0.4375, 0.0625}, -- pole
{-0.375, 0.375, -0.375, 0.375, 0.5, 0.375} -- top
},
recipe = { -- Overwrites “Register multiple objects”
{node, node, node},
{'', node, '' },
{'', node, '' }
}
})
end
......@@ -58,6 +58,7 @@ dofile(modpath..'system'..DIR_DELIM..'init_information.lua')
-- Clean up and transfer functions to the xFurniture API
_xtend.clean({
'get_nodes',
'nodes',
'register',
'apply_indent'
......
......@@ -19,8 +19,20 @@ end
-- string. If the filter matches `true` will be returned, otherwise `false`
-- will be returned.
--
-- @param filterstring The filterstring as described in the documentation of
-- the `get_nodes` function
-- A filter string is a comma delimited string of two values. The first part is
-- the filter and the second part is the value.
--
-- The following filters are available.
--
-- group Matches the group a node is in
-- startswith Matches the beginning of node name
-- endswith Matches the end of a node name
-- contains Matches if a node name contains the value
-- origin Matches the node’s mod_origin value
-- drawtype Matches the node’s drawtype value
-- a node ID Matches the exact node ID
--
-- @param filterstring A filter string as descibed
-- @param definition A node definition
-- @return boolean The result
local filter_match = function(filterstring, definition)
......@@ -70,15 +82,21 @@ end
-- This function gets all nodes that match against the user-definable list of
-- filter strings and that are not filtered by the built-in blacklist.
--
-- @return table The table of nodes and their definition. The node ID are the
-- keys and the definitions are the values. If no nodes are
-- matched an empty table is returned.
local get_nodes = function ()
-- @param ad_hoc_filters A table of filterstrings see filter_match. The filters
-- are applied after all other white- and blacklists are
-- applied to the supported nodes and are a whitelist.
-- @return table The table of nodes and their definition. The node ID
-- are the keys and the definitions are the values. If
-- no nodes are matched an empty table is returned.
_xtend.mods.xfurniture.get_nodes = function (ad_hoc_filters)
local target_nodes = {}
local target_nodes_ad_hoc = {}
local filters = _xtend.get_option('node_whitelist')
local registered_nodes = minetest.registered_nodes
if filters == '' then filters = 'contains:' end
-- Filter all supported nodes through the white- and blacklists and return
-- them if no ad-hoc filters are set.
for filterstring in filters:gmatch('(%S+)') do
for id,definition in pairs(registered_nodes) do
if is_in_depends(id)
......@@ -88,8 +106,18 @@ local get_nodes = function ()
end
end
end
if not ad_hoc_filters then return target_nodes end
return target_nodes
-- Filter all target nodes through the ad-hoc filters and return the
-- ad-hoc filtered nodes table.
for _,filterstring in pairs(ad_hoc_filters) do
for id,definition in pairs(target_nodes) do
if filter_match(filterstring, definition) then
target_nodes_ad_hoc[id] = definition
end
end
end
return target_nodes_ad_hoc
end
_xtend.mods.xfurniture.nodes = get_nodes()
_xtend.mods.xfurniture.nodes = _xtend.mods.xfurniture.get_nodes()
local bases = 0
local nodes = _xtend.mods.xfurniture.nodes
for x,y in pairs(nodes) do bases = bases + 1 end
_xtend.log(_xtend.translate('Created objects from @1 base nodes', bases))
_xtend.log(_xtend.translate('Mod fully loaded'))
local ca_amount = _xtend.mods.xfurniture.crafting_addition.amount
local ca_id = _xtend.mods.xfurniture.crafting_addition.id
local crafting_yield = _xtend.get_option('crafting_yield')
local nodes = _xtend.mods.xfurniture.nodes
local nodes = _xtend.mods.xfurniture.get_nodes()
local relevant_nodes = {}
local relevant_nodes_counter = 0
local to_replace = _xtend.mods.xfurniture.replace
......