From 608c8c42ac3154f3b9393a85773eb01361cdd101 Mon Sep 17 00:00:00 2001 From: SwissalpS Date: Fri, 21 Feb 2020 20:11:59 +0100 Subject: [PATCH] limit certain nodes to individual max Some nodes have cpu intensive side-effects when placed. To avoid lag, this commit adds mechanism to limit per node. --- init.lua | 4 ++++ replacer.lua | 29 ++++++++++++++++++++++++++++- replacer_blabla.lua | 3 +++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 079d623..6ac73af 100644 --- a/init.lua +++ b/init.lua @@ -52,6 +52,10 @@ local path = minetest.get_modpath("replacer") replacer = {} +-- limit by node, use replacer.register_limit(sName, iMax) +replacer.limit_list = {} + +-- don't allow these at all replacer.blacklist = {} -- playing with tnt and creative building are usually contradictory diff --git a/replacer.lua b/replacer.lua index 22570c5..2134d66 100644 --- a/replacer.lua +++ b/replacer.lua @@ -28,6 +28,33 @@ replacer.mode_colours[r.modes[2]] = "#54FFAC" replacer.mode_colours[r.modes[3]] = "#9F6200" replacer.mode_colours[r.modes[4]] = "#FF5457" +local is_int = function(value) + return type(value) == 'number' and math.floor(value) == value +end + +function replacer.register_limit(node_name, node_max) + -- ignore nil and negative numbers + if (nil == node_max) or (0 > node_max) then + return + end + -- ignore non-integers + if not is_int(node_max) then + return + end + -- add to blacklist if limit is zero + if 0 == node_max then + replacer.blacklist[node_name] = true + minetest.log("info", rb.blacklist_insert:format(node_name)) + return + end + -- log info if already limited + if nil ~= r.limit_list[node_name] then + minetest.log("info", rb.limit_override:format(node_name, r.limit_list[node_name])) + end + r.limit_list[node_name] = node_max + minetest.log("info", rb.limit_insert:format(node_name, node_max)) +end + function replacer.get_data(stack) local metaRef = stack:get_meta() local data = metaRef:get_string("replacer"):split(" ") or {} @@ -257,7 +284,7 @@ function replacer.replace(itemstack, user, pt, right_clicked) return end - local max_nodes = replacer.max_nodes + local max_nodes = r.limit_list[nnd.name] or r.max_nodes local charge if replacer.has_technic_mod and (not (creative_enabled or has_give)) then charge = r.get_charge(itemstack) diff --git a/replacer_blabla.lua b/replacer_blabla.lua index ad5e137..c715ec4 100644 --- a/replacer_blabla.lua +++ b/replacer_blabla.lua @@ -26,4 +26,7 @@ replacer.blabla.not_in_inventory = 'Item not in your inventory: "%s".' replacer.blabla.set_to = 'Node replacement tool set to: "%s".' replacer.blabla.description_basic = "Node replacement tool" replacer.blabla.description_technic = "Node replacement tool (technic)" +replacer.blabla.limit_override = 'Setting already set node-limit for "%s" was %d.' +replacer.blabla.limit_insert = 'Setting node-limit for "%s" to %d.' +replacer.blabla.blacklist_insert = 'Blacklisted "%s".'