From 3487e5c32b98e4f823dee3c9a45e0c4b04886844 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 26 Jun 2020 21:15:13 +0100
Subject: [PATCH] //subdivide: Tidy up output

---
 .../commands/subdivide.lua                    | 31 +++++++++++++------
 worldeditadditions_commands/init.lua          |  3 ++
 worldeditadditions_commands/multi.lua         |  4 +--
 .../player_notify_suppress.lua                | 20 ++++++++++++
 4 files changed, 47 insertions(+), 11 deletions(-)
 create mode 100644 worldeditadditions_commands/player_notify_suppress.lua

diff --git a/worldeditadditions_commands/commands/subdivide.lua b/worldeditadditions_commands/commands/subdivide.lua
index fef44a8..12ab0d4 100644
--- a/worldeditadditions_commands/commands/subdivide.lua
+++ b/worldeditadditions_commands/commands/subdivide.lua
@@ -62,20 +62,29 @@ worldedit.register_command("subdivide", {
 		
 		local pos1, pos2 = worldedit.sort_pos(worldedit.pos1[name], worldedit.pos2[name])
 		local volume = worldedit.volume(pos1, pos2)
+		
 		local cmd = worldedit.registered_commands[cmd_name]
 		if not minetest.check_player_privs(name, cmd.privs) then
 			return false, "Error: Your privileges are unsufficient to run '"..cmd_name.."'."
 		end
 		
+		local i = 1
+		local chunks_total = math.ceil((pos2.x - pos1.x) / (chunk_size.x - 1))
+			* math.ceil((pos2.y - pos1.y) / (chunk_size.y - 1))
+			* math.ceil((pos2.z - pos1.z) / (chunk_size.z - 1))
+		
+		worldedit.player_notify(name,
+			"[ subdivide | "..cmd_name.." "..args.." ] Starting - "
+			-- ..wea.vector.tostring(pos1).." - "..wea.vector.tostring(pos2)
+			.." chunk size: "..wea.vector.tostring(chunk_size)
+			..", "..chunks_total.." chunks total"
+			.." ("..volume.." nodes)"
+		)
+		
 		chunk_size.x = chunk_size.x - 1 -- WorldEdit regions are inclusive
 		chunk_size.y = chunk_size.y - 1 -- WorldEdit regions are inclusive
 		chunk_size.z = chunk_size.z - 1 -- WorldEdit regions are inclusive
 		
-		worldedit.player_notify(name, wea.vector.tostring(pos1).." - "..wea.vector.tostring(pos2).." chunk size: "..wea.vector.tostring(chunk_size))
-		local i = 1
-		local chunks_total = math.ceil((pos2.x - pos1.x) / chunk_size.x)
-			* math.ceil((pos2.y - pos1.y) / chunk_size.y)
-			* math.ceil((pos2.z - pos1.z) / chunk_size.z)
 		
 		local time_last_msg = worldeditadditions.get_ms_time()
 		local time_chunks = {}
@@ -94,25 +103,28 @@ worldedit.register_command("subdivide", {
 					if c_pos1.z < pos1.z then c_pos1.z = pos1.z end
 					
 					local time_this = worldeditadditions.get_ms_time()
+					worldedit.player_notify_suppress(name)
 					worldedit.pos1[name] = c_pos1
 					worldedit.pos2[name] = c_pos2
 					cmd.func(name, args)
 					if will_trigger_saferegion(name, cmd_name, args) then
 						minetest.chatcommands["/y"].func()
 					end
+					worldedit.player_notify_unsuppress(name)
 					time_this = worldeditadditions.get_ms_time() - time_this
 					table.insert(time_chunks, time_this)
 					
 					local time_average = wea.average(time_chunks)
 					local eta = (chunks_total - i) * time_average
+					print("eta", eta, "time_average", time_average, "chunks_left", chunks_total - i)
 					
-					if worldeditadditions.get_ms_time() - time_last_msg > 5 * 1000 then
+					-- Send updates every 2 seconds, and after the first 3 chunks are done
+					if worldeditadditions.get_ms_time() - time_last_msg > 2 * 1000 or i == 3 then
 						worldedit.player_notify(name,
 							"[ //subdivide "..cmd_name.." "..args.." ] "
 							..i.." / "..chunks_total.." (~"
 							..string.format("%.2f", (i / chunks_total) * 100).."%) complete | "
-							.."this chunk: "..wea.human_time(time_this)
-							.."("..worldedit.volume(c_pos1, c_pos2).." nodes)"
+							.."last chunk: "..wea.human_time(time_this)
 							..", average: "..wea.human_time(time_average)
 							..", ETA: ~"..wea.human_time(eta)
 						)
@@ -123,12 +135,13 @@ worldedit.register_command("subdivide", {
 				end
 			end
 		end
+		i = i - 1
 		worldedit.pos1[name] = pos1
 		worldedit.pos2[name] = pos2
 		time_total = worldeditadditions.get_ms_time() - time_total
 		
 		
 		minetest.log("action", name.." used //subdivide at "..wea.vector.tostring(pos1).." - "..wea.vector.tostring(pos2)..", with "..i.." chunks and "..worldedit.volume(pos1, pos2).." total nodes in "..time_total.."s")
-		return true, "/subdivide complete"
+		return true, "[ subdivide | "..cmd_name.." "..args.." ] "..i.." chunks processed in "..wea.human_time(time_total)
 	end
 })
diff --git a/worldeditadditions_commands/init.lua b/worldeditadditions_commands/init.lua
index b075c1a..cbeb822 100644
--- a/worldeditadditions_commands/init.lua
+++ b/worldeditadditions_commands/init.lua
@@ -10,6 +10,9 @@ local we_c = worldeditadditions_commands
 
 we_c.modpath = minetest.get_modpath("worldeditadditions_commands")
 
+dofile(we_c.modpath.."/player_notify_suppress.lua")
+
+
 dofile(we_c.modpath.."/multi.lua")
 
 -- We no longer need our own implementation of safe_region thanks to @sfan5's
diff --git a/worldeditadditions_commands/multi.lua b/worldeditadditions_commands/multi.lua
index 59aa1b7..aafdc03 100644
--- a/worldeditadditions_commands/multi.lua
+++ b/worldeditadditions_commands/multi.lua
@@ -61,11 +61,11 @@ minetest.register_chatcommand("/multi", {
 			minetest.log("action", name.." runs "..command)
 			cmd.func(name, args)
 			
-			times[#times + 1] = (worldeditadditions.get_ms_time() - start_time) * 1000
+			times[#times + 1] = (worldeditadditions.get_ms_time() - start_time)
 			i = i + 1
 		end
 		
-		local total_time = (worldeditadditions.get_ms_time() - master_start_time) * 1000
+		local total_time = (worldeditadditions.get_ms_time() - master_start_time)
 		local done_message = {}
 		table.insert(done_message,
 			string.format("Executed %d commands in %s (",
diff --git a/worldeditadditions_commands/player_notify_suppress.lua b/worldeditadditions_commands/player_notify_suppress.lua
new file mode 100644
index 0000000..0544eb8
--- /dev/null
+++ b/worldeditadditions_commands/player_notify_suppress.lua
@@ -0,0 +1,20 @@
+-- Overrides worldedit.player_notify() to add the ability to
+-- suppress messages (used in //subdivide)
+
+local player_notify_suppressed = {}
+
+local orig_player_notify = worldedit.player_notify
+function worldedit.player_notify(name, message)
+	if not player_notify_suppressed[name] then
+		orig_player_notify(name, message)
+	end
+end
+
+--- Disables sending worldedit messages to the player with the given name.
+function worldedit.player_notify_suppress(name)
+	player_notify_suppressed[name] = true
+end
+--- Enables sending worldedit messages to the player with the given name.
+function worldedit.player_notify_unsuppress(name)
+	player_notify_suppressed[name] = nil
+end
