[question] formspec with textlist and players

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

[question] 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,
})
 

Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 1 guest