[solved] formspec with textlist and players

User avatar
bosapara
Member
 
Posts: 493
Joined: Fri Apr 07, 2017 08:49
GitHub: bosapara

[solved] formspec with textlist and players

by bosapara » Sat May 25, 2019 11:26

How to force the button "kick" work correctly with basic formspec? Sfinv form with "kick" working good.

Problems with sellected "name" and "receive_fields" function.

Image

Code with well working sfinv and basic formspec
Code: Select all
   minetest.register_tool("form:tool", {
   description = "Tool",
   inventory_image = "default_flint.png",
   range = 0,
   on_use = function(itemstack, user, pointed_thing)

      local player = user:get_player_name()
      local players = {}
      local formspec = {
            "size[7.4,7.1] bgcolor[#080808BB;true]box[0.075,0;7.05,0.65;#111111] label[3.5,0.1;TEST] background[5,5;1,1;gui_formbg.png;true]button[0.1,5.8;1.5,1.5;kick;kick] textlist[0.1,1;7.03,4.5;playerlist;"
        }      
        local is_first = true
        for _ , player in pairs(minetest.get_connected_players()) do
            local player_name = player:get_player_name()
            players[#players + 1] = player_name
            if not is_first then
                formspec[#formspec + 1] = ","
            end
            formspec[#formspec + 1] = minetest.formspec_escape(player_name)
            is_first = false
        end
        formspec[#formspec + 1] = "]"
      minetest.show_formspec(user:get_player_name(), "tool_form2",table.concat(formspec, ""), false)

   end,

})


minetest.register_on_player_receive_fields(function(player, formname, fields)
   if formname=="tool_form2" then

      if fields.kick then
      -- ...
      end   

   end
end)


--Well working sfinv form


sfinv.register_page("test:gui", {
    title = "Some Form",
   is_in_nav = function(self, player, context)
    return minetest.check_player_privs(player, { interact = true})
   end,
   get = function(self, player, context)
        local players = {}
        context.limit_players = players
      local formspec = {
            "box[0.075,0;7.8,0.65;#111111] label[3.5,0.1;TEST] textlist[0.1,1;7.8,1;playerlist;"
        }      
        local is_first = true
        for _ , player in pairs(minetest.get_connected_players()) do
            local player_name = player:get_player_name()
            players[#players + 1] = player_name
            if not is_first then
                formspec[#formspec + 1] = ","
            end
            formspec[#formspec + 1] = minetest.formspec_escape(player_name)
            is_first = false
        end
        formspec[#formspec + 1] = "]"
      
        -- Add buttons
        --formspec[#formspec + 1] = "field[0.4,2.7;8,1;reason;Reason;]"
        formspec[#formspec + 1] = "button[0.1,3.3;2,1;kick;kick]"

        return sfinv.make_formspec(player, context,
                table.concat(formspec, ""), false)
    end,
   
   
   
   on_player_receive_fields = function(self, player, context, fields)

    if fields.playerlist then
        local event = minetest.explode_textlist_event(fields.playerlist)
        if event.type == "CHG" then
            context.limit_selected_idx = event.index
        end
    elseif fields.kick then
        local player_name = context.limit_players[context.limit_selected_idx]
        local privs = minetest.get_player_privs(player:get_player_name()).kick      
      if player_name then
            minetest.kick_player(player_name, "kicked")
        end

   end
end,
})
Last edited by bosapara on Fri Jul 26, 2019 13:26, edited 1 time in total.
 

User avatar
Miniontoby
Member
 
Posts: 172
Joined: Fri Mar 01, 2019 19:25
Location: The Netherlands
In-game: Miniontoby

Re: [question] formspec with textlist and players

by Miniontoby » Thu Jun 27, 2019 15:04

I have a code (from the minetest modding book of Rubenwardy) and that code should create a kick and ban page, but it doesn't work because the code give this error
Code: Select all
2019-06-27 17:00:05: WARNING[Main]: Undeclared global variable "sfinv" accessed at $minetest\bin\..\mods\myadmin\init.lua:6
2019-06-27 17:00:05: ERROR[Main]: ModError: Failed to load and run script from $minetest\bin\..\mods\myadmin\init.lua:
2019-06-27 17:00:05: ERROR[Main]: $minetest\bin\..\mods\myadmin\init.lua:6: attempt to index global 'sfinv' (a nil value)
2019-06-27 17:00:05: ERROR[Main]: stack traceback:
2019-06-27 17:00:05: ERROR[Main]:    $minetest\bin\..\mods\myadmin\init.lua:6: in main chunk


and my code is this:
Code: Select all
-- myadmin mod
local sfinv_m = minetest.get_modpath('sfinv')
if sfinv_m then
   sfinv.register_page("myadmin:myadmin", {
      title = "Tab",
      get = function(self, player, context)
         local players = {}
         context.myadmin_players = players
   
         -- Using an array to build a formspec is considerably faster
         local formspec = {
            "textlist[0.1,0.1;7.8,3;playerlist;"
         }

         -- Add all players to the text list, and to the players list
         local is_first = true
         for _ , player in pairs(minetest.get_connected_players()) do
            local player_name = player:get_player_name()
            players[#players + 1] = player_name
            if not is_first then
               formspec[#formspec + 1] = ","
            end
            formspec[#formspec + 1] =
                  minetest.formspec_escape(player_name)
            is_first = false
         end
         formspec[#formspec + 1] = "]"

         -- Add buttons
         formspec[#formspec + 1] = "button[0.1,3.3;2,1;kick;Kick]"
         formspec[#formspec + 1] = "button[2.1,3.3;2,1;ban;Kick + Ban]"

         -- Wrap the formspec in sfinv's layout
         -- (ie: adds the tabs and background)
         return sfinv.make_formspec(player, context,
               table.concat(formspec, ""), false)
      end,
      on_player_receive_fields = function(self, player, context, fields)
         -- text list event,  check event type and set index if selection changed
         if fields.playerlist then
            local event = minetest.explode_textlist_event(fields.playerlist)
            if event.type == "CHG" then
               context.myadmin_selected_idx = event.index
            end

         -- Kick button was pressed
         elseif fields.kick then
            local player_name =
               context.myadmin_players[context.myadmin_selected_idx]
            if player_name then
               minetest.chat_send_player(player:get_player_name(),
                  "Kicked " .. player_name)
               minetest.kick_player(player_name)
            end
   
         -- Ban button was pressed
         elseif fields.ban then
            local player_name =
               context.myadmin_players[context.myadmin_selected_idx]
            if player_name then
               minetest.chat_send_player(player:get_player_name(),
                     "Banned " .. player_name)
               minetest.ban_player(player_name)
               minetest.kick_player(player_name, "Banned")
            end
         end
      end,
      is_in_nav = function(self, player, context)
         local privs = minetest.get_player_privs(player:get_player_name())
         local function on_grant_revoke(grantee, granter, priv)
            if priv ~= "kick" and priv ~= "ban" then
               return
            end

            local player = minetest.get_player_by_name(grantee)
            if not player then
               return
            end

            local context = sfinv.get_or_create_context(player)
            if context.page ~= "myadmin:myadmin" then
               return
            end

            sfinv.set_player_inventory_formspec(player, context)
         end

         minetest.register_on_priv_grant(on_grant_revoke)
         minetest.register_on_priv_revoke(on_grant_revoke)
         return privs.kick or privs.ban
      end,
      on_enter = function(self, player, context)
      
      end,
      on_leave = function(self, player, context)
         
      end,
   })
end
My English isn't very good because I'm Dutch

Check my mod "Doorbell"
 

User avatar
bosapara
Member
 
Posts: 493
Joined: Fri Apr 07, 2017 08:49
GitHub: bosapara

Re: [question] formspec with textlist and players

by bosapara » Thu Jun 27, 2019 16:04

Miniontoby,

Thanks you for reply. But i've asked about not sfinv form, it works good in my first message. I have problems only with default formspec and sellected textlist.
 

User avatar
Miniontoby
Member
 
Posts: 172
Joined: Fri Mar 01, 2019 19:25
Location: The Netherlands
In-game: Miniontoby
 


Return to Modding Discussion



Who is online

Users browsing this forum: Extex and 0 guests