forked from mtcontrib/replacer
removed deprecated functions get_/set_metadata and renamed metadata to pattern
This commit is contained in:
parent
9ef8940bc4
commit
76fcea22e0
@ -3,13 +3,13 @@
|
|||||||
replacer.max_hist_size = 20
|
replacer.max_hist_size = 20
|
||||||
|
|
||||||
|
|
||||||
-- turn stored metadata string (<node_name> <param1> <param2>) into something readable by human beeings
|
-- turn stored pattern string (<node_name> <param1> <param2>) into something readable by human beeings
|
||||||
replacer.human_readable_metadata = function(metadata)
|
replacer.human_readable_pattern = function(pattern)
|
||||||
if(not(metadata)) then
|
if(not(pattern)) then
|
||||||
return "(nothing)"
|
return "(nothing)"
|
||||||
end
|
end
|
||||||
-- data is stored in the form "<nodename> <param1> <param2>"
|
-- data is stored in the form "<nodename> <param1> <param2>"
|
||||||
local parts = string.split(metadata, " ")
|
local parts = string.split(pattern, " ")
|
||||||
if(not(parts) or #parts < 3) then
|
if(not(parts) or #parts < 3) then
|
||||||
return "(corrupted data)"
|
return "(corrupted data)"
|
||||||
end
|
end
|
||||||
@ -44,35 +44,37 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- set the replacer to a new pattern
|
-- set the replacer to a new pattern
|
||||||
replacer.set_to = function(player_name, metadata, player, itemstack)
|
replacer.set_to = function(player_name, pattern, player, itemstack)
|
||||||
if(not(player_name) or not(player) or not(itemstack)) then
|
if(not(player_name) or not(player) or not(itemstack)) then
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
-- fallback if nothing is given
|
-- fallback if nothing is given
|
||||||
if(not(metadata)) then
|
if(not(pattern)) then
|
||||||
metadata = "default:dirt 0 0"
|
pattern = "default:dirt 0 0"
|
||||||
end
|
end
|
||||||
itemstack:set_metadata( metadata )
|
|
||||||
|
|
||||||
local set_to = replacer.human_readable_metadata(metadata)
|
local set_to = replacer.human_readable_pattern(pattern)
|
||||||
-- change the description of the tool so that it's easier to see which replacer (if you
|
-- change the description of the tool so that it's easier to see which replacer (if you
|
||||||
-- have more than one in your inv) is set to which node
|
-- have more than one in your inv) is set to which node
|
||||||
local meta = itemstack:get_meta()
|
local meta = itemstack:get_meta()
|
||||||
|
-- actually store the new pattern
|
||||||
|
meta:set_string("pattern", pattern )
|
||||||
|
|
||||||
meta:set_string("description", "Node replacement tool set to:\n"..set_to..
|
meta:set_string("description", "Node replacement tool set to:\n"..set_to..
|
||||||
"\n["..tostring(metadata).."]")
|
"\n["..tostring(pattern).."]")
|
||||||
|
|
||||||
minetest.chat_send_player(player_name, "Node replacement tool set to: "..set_to..
|
minetest.chat_send_player(player_name, "Node replacement tool set to: "..set_to..
|
||||||
"["..tostring(metadata).."].")
|
"["..tostring(pattern).."].")
|
||||||
|
|
||||||
replacer.add_to_hist(player_name, metadata)
|
replacer.add_to_hist(player_name, pattern)
|
||||||
return itemstack -- nothing consumed but data changed
|
return itemstack -- nothing consumed but data changed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- keep a history of stored patterns for each player (not for each replacer);
|
-- keep a history of stored patterns for each player (not for each replacer);
|
||||||
-- this history is not saved over server shutdown
|
-- this history is not saved over server shutdown
|
||||||
replacer.add_to_hist = function(player_name, metadata )
|
replacer.add_to_hist = function(player_name, pattern)
|
||||||
if(not(player_name) or not(metadata) or metadata == "") then
|
if(not(player_name) or not(pattern) or pattern == "") then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if(not(replacer.history)) then
|
if(not(replacer.history)) then
|
||||||
@ -81,7 +83,7 @@ replacer.add_to_hist = function(player_name, metadata )
|
|||||||
if(not(replacer.history[ player_name ])) then
|
if(not(replacer.history[ player_name ])) then
|
||||||
replacer.history[ player_name ] = {}
|
replacer.history[ player_name ] = {}
|
||||||
end
|
end
|
||||||
local index = table.indexof( replacer.history[ player_name ], metadata )
|
local index = table.indexof(replacer.history[ player_name ], pattern)
|
||||||
-- only add new entries; do not store duplicates
|
-- only add new entries; do not store duplicates
|
||||||
if(index and index > -1) then
|
if(index and index > -1) then
|
||||||
return
|
return
|
||||||
@ -90,12 +92,12 @@ replacer.add_to_hist = function(player_name, metadata )
|
|||||||
if(#replacer.history[ player_name ] >= replacer.max_hist_size) then
|
if(#replacer.history[ player_name ] >= replacer.max_hist_size) then
|
||||||
table.remove(replacer.history[ player_name ], 1)
|
table.remove(replacer.history[ player_name ], 1)
|
||||||
end
|
end
|
||||||
table.insert(replacer.history[ player_name ], metadata)
|
table.insert(replacer.history[ player_name ], pattern)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- show a formspec with a history of stored patterns to select from
|
-- show a formspec with a history of stored patterns to select from
|
||||||
replacer.get_formspec = function(player_name, current_metadata, player)
|
replacer.get_formspec = function(player_name, current_pattern, player)
|
||||||
-- is the player in creative mode?
|
-- is the player in creative mode?
|
||||||
local in_creative_mode = (minetest.settings:get_bool("creative_mode")
|
local in_creative_mode = (minetest.settings:get_bool("creative_mode")
|
||||||
or minetest.check_player_privs(player_name, {creative=true}))
|
or minetest.check_player_privs(player_name, {creative=true}))
|
||||||
@ -121,11 +123,11 @@ replacer.get_formspec = function(player_name, current_metadata, player)
|
|||||||
";text,align=left,tooltip=Stored pattern:]"..
|
";text,align=left,tooltip=Stored pattern:]"..
|
||||||
"table[0.2,1.0;13,6;replacer_history;"
|
"table[0.2,1.0;13,6;replacer_history;"
|
||||||
-- make sure all variables exist and the current entry is stored
|
-- make sure all variables exist and the current entry is stored
|
||||||
replacer.add_to_hist(player_name, current_metadata)
|
replacer.add_to_hist(player_name, current_pattern)
|
||||||
local hist_entries = {}
|
local hist_entries = {}
|
||||||
local selected = 1
|
local selected = 1
|
||||||
for i, v in ipairs(replacer.history[ player_name ]) do
|
for i, v in ipairs(replacer.history[ player_name ]) do
|
||||||
if(v == current_metadata) then
|
if(v == current_pattern) then
|
||||||
selected = i
|
selected = i
|
||||||
end
|
end
|
||||||
local amount_left = "#00FF00,infinite supply:"
|
local amount_left = "#00FF00,infinite supply:"
|
||||||
@ -145,7 +147,7 @@ replacer.get_formspec = function(player_name, current_metadata, player)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
hist_entries[ i ] = tostring(amount_left)..","..
|
hist_entries[ i ] = tostring(amount_left)..","..
|
||||||
minetest.formspec_escape(replacer.human_readable_metadata(v).." ["..v.."]")
|
minetest.formspec_escape(replacer.human_readable_pattern(v).." ["..v.."]")
|
||||||
end
|
end
|
||||||
return formspec..table.concat(hist_entries, ",")..";"..tostring(selected).."]"
|
return formspec..table.concat(hist_entries, ",")..";"..tostring(selected).."]"
|
||||||
end
|
end
|
||||||
|
27
init.lua
27
init.lua
@ -23,6 +23,7 @@
|
|||||||
-- Changelog:
|
-- Changelog:
|
||||||
-- 02.09.2021 * Added a history of stored patterns (not saved over server restart)
|
-- 02.09.2021 * Added a history of stored patterns (not saved over server restart)
|
||||||
-- * Added a menu to select a history entry. It is accessable via AUX1 + left click.
|
-- * Added a menu to select a history entry. It is accessable via AUX1 + left click.
|
||||||
|
-- * Removed deprecated functions get/set_metadata(..) and renamed metadata to pattern
|
||||||
-- 29.09.2021 * AUX1 key works now same as SNEAK key for storing new pattern (=easier when flying)
|
-- 29.09.2021 * AUX1 key works now same as SNEAK key for storing new pattern (=easier when flying)
|
||||||
-- * The description of the tool now shows which pattern is stored
|
-- * The description of the tool now shows which pattern is stored
|
||||||
-- * The description of the stored pattern is more human readable
|
-- * The description of the stored pattern is more human readable
|
||||||
@ -89,7 +90,6 @@ minetest.register_tool( "replacer:replacer",
|
|||||||
},
|
},
|
||||||
--]]
|
--]]
|
||||||
node_placement_prediction = nil,
|
node_placement_prediction = nil,
|
||||||
metadata = "default:dirt", -- default replacement: common dirt
|
|
||||||
|
|
||||||
on_place = function(itemstack, placer, pointed_thing)
|
on_place = function(itemstack, placer, pointed_thing)
|
||||||
|
|
||||||
@ -116,12 +116,12 @@ minetest.register_tool( "replacer:replacer",
|
|||||||
local node = minetest.get_node_or_nil( pos );
|
local node = minetest.get_node_or_nil( pos );
|
||||||
|
|
||||||
--minetest.chat_send_player( name, " Target node: "..minetest.serialize( node ).." at pos "..minetest.serialize( pos )..".");
|
--minetest.chat_send_player( name, " Target node: "..minetest.serialize( node ).." at pos "..minetest.serialize( pos )..".");
|
||||||
local metadata = "default:dirt 0 0";
|
local pattern = "default:dirt 0 0";
|
||||||
if( node ~= nil and node.name ) then
|
if( node ~= nil and node.name ) then
|
||||||
metadata = node.name..' '..node.param1..' '..node.param2;
|
pattern = node.name..' '..node.param1..' '..node.param2;
|
||||||
end
|
end
|
||||||
|
|
||||||
return replacer.set_to(name, metadata, placer, itemstack) -- nothing consumed but data changed
|
return replacer.set_to(name, pattern, placer, itemstack) -- nothing consumed but data changed
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
|
||||||
@ -159,21 +159,22 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local item = itemstack:to_table();
|
local meta = itemstack:get_meta()
|
||||||
|
local pattern = meta:get_string("pattern")
|
||||||
|
|
||||||
-- make sure it is defined
|
-- make sure it is defined
|
||||||
if( not( item[ "metadata"] ) or item["metadata"]=="" ) then
|
if(not(pattern) or pattern == "") then
|
||||||
item["metadata"] = "default:dirt 0 0";
|
pattern = "default:dirt 0 0";
|
||||||
end
|
end
|
||||||
|
|
||||||
local keys=user:get_player_control();
|
local keys=user:get_player_control();
|
||||||
if( keys["aux1"]) then
|
if( keys["aux1"]) then
|
||||||
minetest.show_formspec(name, "replacer:menu", replacer.get_formspec(name, item["metadata"], user))
|
minetest.show_formspec(name, "replacer:menu", replacer.get_formspec(name, pattern, user))
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- regain information about nodename, param1 and param2
|
-- regain information about nodename, param1 and param2
|
||||||
local daten = item[ "metadata"]:split( " " );
|
local daten = pattern:split( " " );
|
||||||
-- the old format stored only the node name
|
-- the old format stored only the node name
|
||||||
if( #daten < 3 ) then
|
if( #daten < 3 ) then
|
||||||
daten[2] = 0;
|
daten[2] = 0;
|
||||||
@ -218,7 +219,7 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
|
|||||||
-- fortunately, dirt and dirt_with_grass does not make use of rotation
|
-- fortunately, dirt and dirt_with_grass does not make use of rotation
|
||||||
if( daten[1] == "default:dirt_with_grass" ) then
|
if( daten[1] == "default:dirt_with_grass" ) then
|
||||||
daten[1] = "default:dirt";
|
daten[1] = "default:dirt";
|
||||||
item["metadata"] = "default:dirt 0 0";
|
pattern = "default:dirt 0 0";
|
||||||
end
|
end
|
||||||
|
|
||||||
-- does the player carry at least one of the desired nodes with him?
|
-- does the player carry at least one of the desired nodes with him?
|
||||||
@ -244,7 +245,7 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
|
|||||||
-- thus can be replaced
|
-- thus can be replaced
|
||||||
local node_def = minetest.registered_nodes[node.name]
|
local node_def = minetest.registered_nodes[node.name]
|
||||||
if(not(node_def) or not(node_def.buildable_to)) then
|
if(not(node_def) or not(node_def.buildable_to)) then
|
||||||
minetest.chat_send_player( name, "Replacing '"..( node.name or "air" ).."' with '"..( item[ "metadata"] or "?" ).."' failed. Unable to remove old node.");
|
minetest.chat_send_player( name, "Replacing '"..( node.name or "air" ).."' with '"..( pattern or "?" ).."' failed. Unable to remove old node.");
|
||||||
return nil;
|
return nil;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -257,9 +258,9 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
|
|||||||
--user:get_inventory():add_item( "main", node.name.." 1");
|
--user:get_inventory():add_item( "main", node.name.." 1");
|
||||||
end
|
end
|
||||||
|
|
||||||
--minetest.chat_send_player( name, "Replacing node '"..( node.name or "air" ).."' with '"..( item[ "metadata"] or "?" ).."'.");
|
--minetest.chat_send_player( name, "Replacing node '"..( node.name or "air" ).."' with '"..( pattern or "?" ).."'.");
|
||||||
|
|
||||||
--minetest.place_node( pos, { name = item[ "metadata" ] } );
|
--minetest.place_node( pos, { name = pattern } );
|
||||||
minetest.add_node( pos, { name = daten[1], param1 = daten[2], param2 = daten[3] } );
|
minetest.add_node( pos, { name = daten[1], param1 = daten[2], param2 = daten[3] } );
|
||||||
return nil; -- no item shall be removed from inventory
|
return nil; -- no item shall be removed from inventory
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user