Sethome for beds. Fix bug please

amadin
Member
 
Posts: 471
Joined: Tue Jun 16, 2015 16:23
GitHub: Amadin

Sethome for beds. Fix bug please

by amadin » Tue Dec 22, 2015 21:24

If player which don't slept in a bed yet type anything in public chat he see this message "You haven't slept in a bed yet." but he must see this message only if he type /home. Please fix this because author don't respond.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_on_chat_message(function(name, message, playername, player)
   local player_spawns = {}
   local file = io.open(minetest.get_worldpath().."/beds_player_spawns", "r")
   if file then
      player_spawns = minetest.deserialize(file:read("*all"))
      file:close()
   end
   local cmd = "/home"
   local player = minetest.get_player_by_name(name)
   if (player_spawns[name]) then
       if message:sub(0, #cmd) == cmd then
           if message == '/home' then
              minetest.chat_send_player(player:get_player_name(), "Teleporting to bed...")
              player:setpos(player_spawns[name])
              return true
           end
       end
   else
      minetest.chat_send_player(player:get_player_name(), "You haven't slept in a bed yet.")
      return true
   end
end)


And if you want add message for /sethome "You must slept in a bad for this". This mod work with colored bed mod instead default sethome mod on my server.
 

blert2112
Member
 
Posts: 244
Joined: Sat Apr 25, 2015 04:05
GitHub: blert2112

Re: Sethome for beds. Fix bug please

by blert2112 » Wed Dec 23, 2015 16:55

Pretty sure that you can't really intercept a chat command with register_on_chat_message().
I believe the following works like you want it to. Replace your 'sethome' mod with this...
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local homes_file = minetest.get_worldpath() .. "/beds_spawns"
local homepos = {}

local function loadhomes()
    local input = io.open(homes_file, "r")
    if input then
      repeat
            local x = input:read("*n")
            if x == nil then
               break
            end
            local y = input:read("*n")
            local z = input:read("*n")
            local name = input:read("*l")
            homepos[name:sub(2)] = {x = x, y = y, z = z}
        until input:read(0) == nil
        io.close(input)
    else
        homepos = {}
    end
end

minetest.register_privilege("home", "Can use /sethome and /home")

minetest.register_chatcommand("home", {
    description = "Teleport you to your home point",
    privs = {home=true},
    func = function(name)
        local player = minetest.get_player_by_name(name)
        if player == nil then
            -- just a check to prevent the server crashing
            return false
        end
      loadhomes()
        if homepos[name] then
            player:setpos(homepos[name])
            minetest.chat_send_player(name, "Teleported to home!")
        else
            minetest.chat_send_player(name, "You haven't slept in a bed yet.")
        end
    end,
})

minetest.register_chatcommand("sethome", {
    description = "Set your home point",
    func = function(name)
      minetest.chat_send_player(name, "You must sleep in a bed for this.")
    end,
})
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 6 guests

cron