]> git.lizzy.rs Git - minetest.git/blobdiff - builtin/filterlist.lua
Add pointed_thing to minetest.register_on_placenode
[minetest.git] / builtin / filterlist.lua
index c9135d207cc4179d2ce3693f7d32c24df809c77d..379a5cea90a0c38134b6652c77d6ee4d8d65bb09 100644 (file)
 
 --------------------------------------------------------------------------------
 -- Generic implementation of a filter/sortable list                           --
+-- Usage:                                                                     --
+-- Filterlist needs to be initialized on creation. To achieve this you need to --
+-- pass following functions:                                                  --
+-- raw_fct() (mandatory):                                                     --
+--     function returning a table containing the elements to be filtered      --
+-- compare_fct(element1,element2) (mandatory):                                --
+--     function returning true/false if element1 is same element as element2  --
+-- uid_match_fct(element1,uid) (optional)                                     --
+--     function telling if uid is attached to element1                        --
+-- filter_fct(element,filtercriteria) (optional)                              --
+--     function returning true/false if filtercriteria met to element         --
+-- fetch_param (optional)                                                     --
+--     parameter passed to raw_fct to aquire correct raw data                 --
+--                                                                            --
 --------------------------------------------------------------------------------
 filterlist = {}
 
@@ -157,7 +171,7 @@ function filterlist.process(this)
        this.m_processed_list = {}
        
        for k,v in pairs(this.m_raw_list) do
-               if this.m_filtercriteria == nil or 
+               if this.m_filtercriteria == nil or
                        this.m_filter_fct(v,this.m_filtercriteria) then
                        table.insert(this.m_processed_list,v)
                end
@@ -167,7 +181,7 @@ function filterlist.process(this)
                return
        end
        
-       if this.m_sort_list[this.m_sortmode] ~= nil and 
+       if this.m_sort_list[this.m_sortmode] ~= nil and
                type(this.m_sort_list[this.m_sortmode]) == "function" then
                
                this.m_sort_list[this.m_sortmode](this)
@@ -237,20 +251,51 @@ function compare_worlds(world1,world2)
 end
 
 --------------------------------------------------------------------------------
-function sort_worlds_alphabetic(this) 
+function sort_worlds_alphabetic(this)
 
-       table.sort(this.m_processed_list, function(a, b) 
-                       local n1 = a.name 
-                       local n2 = b.name 
-                       local count = math.min(#n1, #n2) 
+       table.sort(this.m_processed_list, function(a, b)
+               --fixes issue #857 (crash due to sorting nil in worldlist)
+               if a == nil or b == nil then
+                       if a == nil and b ~= nil then return false end
+                       if b == nil and a ~= nil then return true end
+                       return false
+               end
+               if a.name:lower() == b.name:lower() then
+                       return a.name < b.name
+               end
+               return a.name:lower() < b.name:lower()
+       end)
+end
+
+--------------------------------------------------------------------------------
+function sort_mod_list(this)
+
+       table.sort(this.m_processed_list, function(a, b)
+               -- Show game mods at bottom
+               if a.typ ~= b.typ then
+                       return b.typ == "game_mod"
+               end
+               -- If in same or no modpack, sort by name
+               if a.modpack == b.modpack then
+                       if a.name:lower() == b.name:lower() then
+                               return a.name < b.name
+                       end
+                       return a.name:lower() < b.name:lower()
+               -- Else compare name to modpack name
+               else
+                       -- Always show modpack pseudo-mod on top of modpack mod list
+                       if a.name == b.modpack then
+                               return true
+                       elseif b.name == a.modpack then
+                               return false
+                       end
                        
-                       for i=1,count do 
-                               if n1:sub(i, i):lower() < n2:sub(i, i):lower() then 
-                                       return true 
-                               elseif n1:sub(i, i):lower() > n2:sub(i, i):lower() then 
-                                       return false 
-                               end 
-                       end 
-                       return (#n1 <= #n2) 
-               end) 
+                       local name_a = a.modpack or a.name
+                       local name_b = b.modpack or b.name
+                       if name_a:lower() == name_b:lower() then
+                               return  name_a < name_b
+                       end
+                       return name_a:lower() < name_b:lower()
+               end
+       end)
 end