Magic Mirror of Returning: What is wrong with my code?

User avatar
FreeGamers
Member
 
Posts: 334
Joined: Sat May 25, 2019 00:15
Location: United States
GitHub: is proprietary I use NotABug

Magic Mirror of Returning: What is wrong with my code?

by FreeGamers » Tue Dec 03, 2019 10:46

EDIT: SOLVED THIS MYSELF.

I'm working on an item that was originally design by Wuzzy and Davidthecreator. I'm a bit of a novice when it comes to programming and I would appreciate any pointers on tips on where I'm going wrong.

I'm trying to add mana compatibility to the tool/item.
Observed behavior:
if the mana mod is turned off, the tool becomes an unknown item.
if the mana mod is disabled, the tool is registered but cannot be used at all.

repo file: https://www.notabug.org/FreeGamers/Sara ... r/init.lua

Here is the code:


Code: Select all
-- FREEGAMERS CHANGES:
-- changed sound effect to "Warp SFX" by Breviceps - https://freesound.org/people/Breviceps/sounds/453391/
-- add no mana sound from bweapons_magic_modpack
-- add mana_mod check and fuctions, add a 2 second delay before teleporting.
-- Pull static spawn point from minetest.conf or settings.

-- Sets the fallback position for users with no bed spawn.
local placeholder_pos  = minetest.setting_get_pos("static_spawnpoint") or {x=0,y=0,z=0}

-- Check for the mana mod and if its found, set status.
        if minetest.get_modpath("mana") ~= nil then
                recall_mirror_mana = true
        else
                recall_mirror_mana = false
        end

-- If not mana mod, we use timer functions to track allowed use. Otherwise get mana requirements
        if recall_mirror_mana == false then

                -- The cooldown timer value.
                recall_mirror_cooldown = 60

                -- Attempt to get recall mirror cooldown overrides from minetest settings.
                if tonumber(minetest.settings:get("recall_mirror_cooldown")) ~= nil then
                        recall_mirror_cooldown = tonumber(minetest.settings:get("recall_mirror_cooldown"))
                end

                -- Timer functions
                rectimer = 0
                minetest.register_globalstep(function(dtime, player)
                        rectimer = rectimer + dtime;
                if rectimer >= 1.0 then
                        rectimer = 0
                        for _, player in pairs(minetest.get_connected_players()) do
                         meta = player:get_meta()
                        local recall_time = meta:get_int("rec_time")
                if recall_time < recall_mirror_cooldown then
                        meta:set_int("rec_time", recall_time + 1)
                        end end end end)
        return

-- If we have the mana mod,
        elseif recall_mirror_mana == true then

                -- Add Mana Support
                recall_mirror_cost_teleport = 200

                -- Check settings for mana cost overrides.
                if tonumber(minetest.settings:get("recall_mirror_cost_teleport")) ~= nil then
                        recall_mirror_cost_teleport = tonumber(minetest.settings:get("recall_mirror_cost_teleport"))
                end
        end

--minetest.register_craft({
--      output = "recall_mirror:recall_mirror",
--      recipe = {
--              {"default:gold_ingot", "default:diamond", "default:gold_ingot"},
--              {"default:mese", "default:glass", "default:mese"},
--              {"default:gold_ingot", "default:diamond", "default:gold_ingot"},
--      }
--})


-- REGISTER THE MIRROR TOOL
minetest.register_tool("recall_mirror:recall_mirror", {
        description = "" ..core.colorize("#35cdff","Recall Mirror\n") ..core.colorize("#FFFFFF", "Returns you to the last bed you've slept in...alive!\n")..core.colorize("#FFFFFF", "Has a cooldown."),
        range = 0,
        inventory_image = "recall_mirror.png",
        on_use = function(itemstack,user)

        -- User Meta (for timer)
-- NO MANA MOD FOUND, So we use timer functions.
        if recall_mirror_mana == false then
                -- Get timer from user meta.
                meta = user:get_meta()
                local recall_time = meta:get_int("rec_time")
                -- Check timer, Need to wait for timer cooldown to expire.
                if recall_time < recall_mirror_cooldown then
                        minetest.chat_send_player(user:get_player_name(), "" ..core.colorize("#ff0000","You need to wait "..recall_mirror_cooldown - recall_time.. " more seconds, before teleporting again!"))
                end
                -- Check timer, Cooldown timer has expired, user can teleport & do effects at departure location.
                if recall_time >= recall_mirror_cooldown then
                        local firstpos = user:getpos()
                        minetest.sound_play("recall", {pos = firstpos, gain = 0.8})
                        for i=1,50 do
                                minetest.add_particle({
                                        pos = firstpos,
                                        acceleration = 0,
                                        velocity = {x=math.random(-20, 20)/10, y=math.random(-20, 20)/10, z=math.random(-20, 20)/10},
                                        size = math.random(2,6),
                                        expirationtime = 3.0,
                                        collisiondetection = false,
                                        vertical = false,
                                        texture = "recall_particle.png",
                                        animation = {type="vertical_frames", aspect_w=8, aspect_h=8, length = 0.50,},
                                        glow = 30,
                                })
                        end
                        -- Reset rec_time.
                        meta:set_int("rec_time", 0)
                        local name = user:get_player_name()
                        local rec_pos = beds.spawn[name]
                        -- Move the player.
                        if rec_pos then
                                user:set_pos(rec_pos)
                        else
                                user:set_pos(placeholder_pos)
                        end
                                -- Teleportation effects on arrival location.
                                local nextpos = user:getpos()
                                minetest.sound_play("recall", {pos = nextpos, gain = 1.0})
                                for i=1,50 do
                                        minetest.add_particle({
                                                pos = nextpos,
                                                acceleration = 0,
                                                velocity = {x=math.random(-20, 20)/10, y=math.random(-20, 20)/10, z=math.random(-20, 20)/10},
                                                size = math.random(2,6),
                                                expirationtime = 3.0,
                                                collisiondetection = false,
                                                vertical = false,
                                                texture = "recall_particle.png",
                                                animation = {type="vertical_frames", aspect_w=8, aspect_h=8, length = 0.2,},
                                                glow = 30,
                                        })
                                end
                end -- end mana check allow true
                -- User does not have enough mana, play sound effect
                if allowed == false then
                                local firstpos = user:getpos()
                                minetest.sound_play("bweapons_magic_pack_reload", {pos = firstpos, gain = 0.25})
                                return
                end -- end mana check allow false
        return
end -- end register tool function
end,
})


Thank you.
 

User avatar
FreeGamers
Member
 
Posts: 334
Joined: Sat May 25, 2019 00:15
Location: United States
GitHub: is proprietary I use NotABug

Re: Magic Mirror of Returning: What is wrong with my code?

by FreeGamers » Tue Dec 03, 2019 13:48

I ended up dropping all functionality with timers and just depending on the mana mod so I could focus on my if statements and existing code. I figured if someone wanted to use timers only mode they could use the original "Recall Mirror" by davidthecreator anyways.

I think the major problem I had in this project came from not understanding how local variables and global variables worked.

Here is the final version:
https://www.notabug.org/FreeGamers/Sara ... gic_mirror
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 3 guests