From 65db88d24e65972b34da824743ba7dc6d307def0 Mon Sep 17 00:00:00 2001
From: kilbith <jeanpatrick.guerrero@gmail.com>
Date: Mon, 18 Apr 2016 18:20:04 +0200
Subject: [PATCH] Mainmenu: Unify favorite servers with main serverlist

---
 builtin/mainmenu/common.lua                  |  26 ++--
 builtin/mainmenu/tab_multiplayer.lua         | 154 +++++++++----------
 textures/base/pack/server_flags_favorite.png | Bin 0 -> 916 bytes
 3 files changed, 85 insertions(+), 95 deletions(-)
 create mode 100644 textures/base/pack/server_flags_favorite.png

diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua
index 62ab2b578..203b1382b 100644
--- a/builtin/mainmenu/common.lua
+++ b/builtin/mainmenu/common.lua
@@ -85,7 +85,7 @@ function order_favorite_list(list)
 end
 
 --------------------------------------------------------------------------------
-function render_favorite(spec,render_details)
+function render_favorite(spec, is_favorite)
 	local text = ""
 
 	if spec.name ~= nil then
@@ -105,13 +105,15 @@ function render_favorite(spec,render_details)
 		end
 	end
 
-	if not render_details then
-		return text
-	end
-
 	local details = ""
 	local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
 
+	if is_favorite then
+		details = "1,"
+	else
+		details = "0,"
+	end
+
 	if spec.clients ~= nil and spec.clients_max ~= nil then
 		local clients_color = ''
 		local clients_percent = 100 * spec.clients / spec.clients_max
@@ -253,15 +255,13 @@ function asyncOnlineFavourites()
 		end,
 		nil,
 		function(result)
-			if core.setting_getbool("public_serverlist") then
-				local favs = order_favorite_list(result)
-				if favs[1] then
-					menudata.public_known = favs
-					menudata.favorites = menudata.public_known
-					menudata.favorites_is_public = true
-				end
-				core.event_handler("Refresh")
+			local favs = order_favorite_list(result)
+			if favs[1] then
+				menudata.public_known = favs
+				menudata.favorites = menudata.public_known
+				menudata.favorites_is_public = true
 			end
+			core.event_handler("Refresh")
 		end
 	)
 end
diff --git a/builtin/mainmenu/tab_multiplayer.lua b/builtin/mainmenu/tab_multiplayer.lua
index f3ba122fc..b5323fa99 100644
--- a/builtin/mainmenu/tab_multiplayer.lua
+++ b/builtin/mainmenu/tab_multiplayer.lua
@@ -20,65 +20,64 @@ local function get_formspec(tabview, name, tabdata)
 	-- Update the cached supported proto info,
 	-- it may have changed after a change by the settings menu.
 	common_update_cached_supp_proto()
+	local fav_selected = menudata.favorites[tabdata.fav_selected]
 
-	local render_details = core.is_yes(core.setting_getbool("public_serverlist"))
-	
 	local retval =
-		"label[7.75,-0.15;" .. fgettext("Address / Port :") .. "]" ..
-		"label[7.75,1.05;" .. fgettext("Name / Password :") .. "]" ..
-		"field[8,0.75;3.4,0.5;te_address;;" ..
-		core.formspec_escape(core.setting_get("address")) .. "]" ..
-		"field[11.25,0.75;1.3,0.5;te_port;;" ..
-		core.formspec_escape(core.setting_get("remote_port")) .. "]" ..
-		"checkbox[0,4.85;cb_public_serverlist;" .. fgettext("Public Serverlist") .. ";" ..
-		dump(core.setting_getbool("public_serverlist")) .. "]"
-
-	if not core.setting_getbool("public_serverlist") then
-		retval = retval ..
-		"button[8,4.9;2,0.5;btn_delete_favorite;" .. fgettext("Delete") .. "]"
-	end
-
-	retval = retval ..
-		"button[10,4.9;2,0.5;btn_mp_connect;" .. fgettext("Connect") .. "]" ..
+		"label[7.75,-0.15;" .. fgettext("Address / Port") .. "]" ..
+		"label[7.75,1.05;" .. fgettext("Name / Password") .. "]" ..
+		"field[8,0.75;3.3,0.5;te_address;;" ..
+			core.formspec_escape(core.setting_get("address")) .. "]" ..
+		"field[11.15,0.75;1.4,0.5;te_port;;" ..
+			core.formspec_escape(core.setting_get("remote_port")) .. "]" ..
+		"button[10.1,4.9;2,0.5;btn_mp_connect;" .. fgettext("Connect") .. "]" ..
 		"field[8,1.95;2.95,0.5;te_name;;" ..
-		core.formspec_escape(core.setting_get("name")) .. "]" ..
+			core.formspec_escape(core.setting_get("name")) .. "]" ..
 		"pwdfield[10.78,1.95;1.77,0.5;te_pwd;]" ..
-		"box[7.73,2.35;4.3,2.28;#999999]" ..
-		"textarea[8.1,2.4;4.26,2.6;;"
-		
-	if tabdata.fav_selected ~= nil and
-		menudata.favorites[tabdata.fav_selected] ~= nil and
-		menudata.favorites[tabdata.fav_selected].description ~= nil then
-		retval = retval ..
-			core.formspec_escape(menudata.favorites[tabdata.fav_selected].description,true)
-	end
+		"box[7.73,2.35;4.3,2.28;#999999]"
 
-	retval = retval ..
-		";]"
+	if tabdata.fav_selected and fav_selected then
+		if gamedata.fav then
+			retval = retval .. "button[7.85,4.9;2.3,0.5;btn_delete_favorite;" ..
+				fgettext("Del. Favorite") .. "]"
+		end
+		if fav_selected.description then
+			retval = retval .. "textarea[8.1,2.4;4.26,2.6;;" ..
+				core.formspec_escape((gamedata.serverdescription or ""), true) .. ";]"
+		end
+	end
 
 	--favourites
-	if render_details then
-		retval = retval .. "tablecolumns[" ..
-			"color,span=3;" ..
-			"text,align=right;" ..                -- clients
-			"text,align=center,padding=0.25;" ..  -- "/"
-			"text,align=right,padding=0.25;" ..   -- clients_max
-			image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
-			image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
-			image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
-			"color,span=1;" ..
-			"text,padding=1]"                               -- name
-	else
-		retval = retval .. "tablecolumns[text]"
-	end
-	retval = retval ..
-		"table[-0.15,-0.1;7.75,5;favourites;"
+	retval = retval .. "tablecolumns[" ..
+		image_column(fgettext("Favorite"), "favorite") .. ";" ..
+		"color,span=3;" ..
+		"text,align=right;" ..                -- clients
+		"text,align=center,padding=0.25;" ..  -- "/"
+		"text,align=right,padding=0.25;" ..   -- clients_max
+		image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
+		image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
+		image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
+		"color,span=1;" ..
+		"text,padding=1]" ..
+		"table[-0.15,-0.1;7.75,5.5;favourites;"
 
 	if #menudata.favorites > 0 then
-		retval = retval .. render_favorite(menudata.favorites[1],render_details)
-
-		for i=2,#menudata.favorites,1 do
-			retval = retval .. "," .. render_favorite(menudata.favorites[i],render_details)
+		local favs = core.get_favorites("local")
+		if #favs > 0 then
+			for i = 1, #favs do
+			for j = 1, #menudata.favorites do
+				if menudata.favorites[j].address == favs[i].address and
+						menudata.favorites[j].port == favs[i].port then
+					table.insert(menudata.favorites, i, table.remove(menudata.favorites, j))
+				end
+			end
+				if favs[i].address ~= menudata.favorites[i].address then
+					table.insert(menudata.favorites, i, favs[i])
+				end
+			end
+		end
+		retval = retval .. render_favorite(menudata.favorites[1], (#favs > 0))
+		for i = 2, #menudata.favorites do
+			retval = retval .. "," .. render_favorite(menudata.favorites[i], (i <= #favs))
 		end
 	end
 
@@ -100,9 +99,10 @@ local function main_button_handler(tabview, fields, name, tabdata)
 
 	if fields["favourites"] ~= nil then
 		local event = core.explode_table_event(fields["favourites"])
+		local fav = menudata.favorites[event.row]
+
 		if event.type == "DCL" then
 			if event.row <= #menudata.favorites then
-				local fav = menudata.favorites[event.row]
 				if menudata.favorites_is_public and
 						not is_server_protocol_compat_or_error(
 							fav.proto_min, fav.proto_max) then
@@ -131,8 +131,18 @@ local function main_button_handler(tabview, fields, name, tabdata)
 
 		if event.type == "CHG" then
 			if event.row <= #menudata.favorites then
-				local address = menudata.favorites[event.row].address
-				local port    = menudata.favorites[event.row].port
+				gamedata.fav = false
+				local favs = core.get_favorites("local")
+				local address = fav.address
+				local port    = fav.port
+				gamedata.serverdescription = fav.description
+
+				for i = 1, #favs do
+					if fav.address == favs[i].address and
+							fav.port == favs[i].port then
+						gamedata.fav = true
+					end
+				end
 
 				if address ~= nil and
 					port ~= nil then
@@ -181,29 +191,16 @@ local function main_button_handler(tabview, fields, name, tabdata)
 		return true
 	end
 
-	if fields["cb_public_serverlist"] ~= nil then
-		core.setting_set("public_serverlist", fields["cb_public_serverlist"])
-
-		if core.setting_getbool("public_serverlist") then
-			asyncOnlineFavourites()
-		else
-			menudata.favorites = core.get_favorites("local")
-			menudata.favorites_is_public = false
-		end
-		tabdata.fav_selected = nil
-		return true
-	end
-
-	if fields["btn_delete_favorite"] ~= nil then
+	if fields.btn_delete_favorite then
 		local current_favourite = core.get_table_index("favourites")
-		if current_favourite == nil then return end
+		if not current_favourite then return end
+
 		core.delete_favorite(current_favourite)
-		menudata.favorites = core.get_favorites("local")
+		asyncOnlineFavourites()
 		tabdata.fav_selected = nil
 
-		core.setting_set("address","")
-		core.setting_set("remote_port","30000")
-
+		core.setting_set("address", "")
+		core.setting_set("remote_port", "30000")
 		return true
 	end
 
@@ -247,16 +244,9 @@ local function main_button_handler(tabview, fields, name, tabdata)
 	return false
 end
 
-local function on_change(type,old_tab,new_tab)
-	if type == "LEAVE" then
-		return
-	end
-	if core.setting_getbool("public_serverlist") then
-		asyncOnlineFavourites()
-	else
-		menudata.favorites = core.get_favorites("local")
-		menudata.favorites_is_public = false
-	end
+local function on_change(type, old_tab, new_tab)
+	if type == "LEAVE" then return end
+	asyncOnlineFavourites()
 end
 
 --------------------------------------------------------------------------------
diff --git a/textures/base/pack/server_flags_favorite.png b/textures/base/pack/server_flags_favorite.png
new file mode 100644
index 0000000000000000000000000000000000000000..6a3fc5efe7ce3a9ffa0f2e0a800afdace65285c4
GIT binary patch
literal 916
zcmV;F18e+=P)<h;3K|Lk000e1NJLTq000mG000mO1^@s6AM^iV00004XF*Lt006JZ
zHwB960000PbVXQnQ*UN;cVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBU#CP_p=RCwBA
z{MX6A!0?BG;s0+2hTlJd^nV68U}s|3*m!_pMdLAs<uI`<RR)GKb)Z_H`qE1b3;+Sd
z1e0K71Y#ilA7t2Huz?l~YO;P7>Ky5;j10^mHOxQ-`a%p0KmH*s0SF)_1|+})G695v
z1~9NPXE6ANGPrrGS(%G4*n;HEL>L&{r5G5#{Xysf2p|*#z?wm>_`#seq^=ymAb5{~
z(MFjyN|!l>4JenV0W|wRiVg;V0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