Fix lodestone compass meta handling.

* The nature of a compass was being determined by looking at its meta.
  This caused lodestone compasses with unset meta to turn into regular
  compasses.  Fixed by using string matching on the itemname.
* Changed lodestone rightclick handler to explicitly set the correct
  name and frame of the compass used on it instead of waiting for
  globalstep to do this.
This commit is contained in:
kabou 2022-05-11 21:31:50 +02:00
parent 8ae605165b
commit 14c882f982
1 changed files with 13 additions and 7 deletions

View File

@ -35,6 +35,7 @@ local m_atan2 = math.atan2
local m_floor = math.floor local m_floor = math.floor
local m_rnd = math.random local m_rnd = math.random
local vec_new = vector.new local vec_new = vector.new
local string_find = string.find
local string_to_pos = minetest.string_to_pos local string_to_pos = minetest.string_to_pos
local get_connected_players = minetest.get_connected_players local get_connected_players = minetest.get_connected_players
local get_item_group = minetest.get_item_group local get_item_group = minetest.get_item_group
@ -90,8 +91,7 @@ end
-- itemstack: the compass including its optional lodestone metadata. -- itemstack: the compass including its optional lodestone metadata.
-- --
local function get_compass_frame(pos, dir, itemstack) local function get_compass_frame(pos, dir, itemstack)
local lpos_str = itemstack:get_meta():get_string("pointsto") if not string_find(itemstack:get_name(), "_lodestone") then -- normal compass
if lpos_str == "" then -- normal compass
-- Compasses only work in the overworld -- Compasses only work in the overworld
if compass_works(pos) then if compass_works(pos) then
local spawn_pos = setting_get_pos("static_spawnpoint") local spawn_pos = setting_get_pos("static_spawnpoint")
@ -101,6 +101,7 @@ local function get_compass_frame(pos, dir, itemstack)
return random_frame return random_frame
end end
else -- lodestone compass else -- lodestone compass
local lpos_str = itemstack:get_meta():get_string("pointsto")
local lpos = string_to_pos(lpos_str) local lpos = string_to_pos(lpos_str)
if not lpos then if not lpos then
minetest.log("warning", "mcl_compass: invalid lodestone position!") minetest.log("warning", "mcl_compass: invalid lodestone position!")
@ -185,10 +186,11 @@ minetest.register_globalstep(function(dtime)
-- check if current compass image still matches true orientation -- check if current compass image still matches true orientation
compass_frame = get_compass_frame(pos, dir, stack) compass_frame = get_compass_frame(pos, dir, stack)
if compass_nr - 1 ~= compass_frame then if compass_nr - 1 ~= compass_frame then
if stack:get_meta():get_string("pointsto") == "" then
stack:set_name("mcl_compass:" .. compass_frame) if string_find(stack:get_name(), "_lodestone") then
else
stack:set_name("mcl_compass:" .. compass_frame .. "_lodestone") stack:set_name("mcl_compass:" .. compass_frame .. "_lodestone")
else
stack:set_name("mcl_compass:" .. compass_frame)
end end
inv:set_stack("main", j, stack) inv:set_stack("main", j, stack)
end end
@ -251,9 +253,13 @@ minetest.register_alias("mcl_compass:compass", "mcl_compass:" .. stereotype_fram
minetest.register_node("mcl_compass:lodestone",{ minetest.register_node("mcl_compass:lodestone",{
description=S("Lodestone"), description=S("Lodestone"),
on_rightclick = function(pos, node, player, itemstack) on_rightclick = function(pos, node, player, itemstack)
if itemstack.get_name(itemstack).match(itemstack.get_name(itemstack),"mcl_compass:") then local name = itemstack.get_name(itemstack)
if itemstack.get_name(itemstack) ~= "mcl_compass:lodestone" then if string_find(name,"mcl_compass:") then
if name ~= "mcl_compass:lodestone" then
itemstack:get_meta():set_string("pointsto", minetest.pos_to_string(pos)) itemstack:get_meta():set_string("pointsto", minetest.pos_to_string(pos))
local dir = player:get_look_horizontal()
local frame = get_compass_frame(pos, dir, itemstack)
itemstack:set_name("mcl_compass:" .. frame .. "_lodestone")
end end
end end
end, end,