Page 1 of 1

Lua Bits n Pieces...

PostPosted: Sat Jul 12, 2014 17:30
by TenPlus1
I thought this would be an ideal place to store snippets of code that do interesting things for other people to check out and maybe learn new ideas...

Here's the code for a Node Inspector tool, punch any node to find the item code for it...

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
--= Node Inspector (click on node to give details)

local huds={}

minetest.register_tool("mymod:node_inspector", {
   description = "Node Inspector",
   inventory_image = "default_tool_steelaxe.png",
   liquids_pointable = true,

   on_use = function(itemstack, user, pointed_thing)

      local pll = user:get_player_name()
      local pos = pointed_thing.under

      if pointed_thing.type ~= "node" then
         desc = "..."
      else
         local node = minetest.get_node(pos)
         desc = node.name
      end

      -- Clear HUD element
      user:hud_remove(huds[pll])

      -- Display new HUD text
      local off = {x=0, y=-80}
      huds[pll] = user:hud_add({
         hud_elem_type = "text",
         position = {x=0.5, y=1},
         offset = off,
         alignment = {x=0, y=0},
         number = 0xFFFFFF ,
         text = desc,
      })
   end,
})

Re: LUA Bits n Pieces...

PostPosted: Sat Jul 12, 2014 17:34
by Sockbat
Thanks TenPlus1, that's quite useful.

Re: LUA Bits n Pieces...

PostPosted: Sat Jul 12, 2014 18:50
by kaeza
Déja vu. Wasn't there a similar topic already?

Re: LUA Bits n Pieces...

PostPosted: Sat Jul 12, 2014 19:08
by LionsDen
Here is a little trash can I made because dropping stuff doesn't work with the item_drop mod. You just place it like a chest and put anything in it that you want to remove and then just dig up the node and it deletes everything that was in it.

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 trash_can_formspec =
   "size[8,9]"..
   "list[current_name;main;0,0;8,4;]"..
   "list[current_player;main;0,5;8,4;]"

--   Trash Can Node.
minetest.register_node("trashit:trash_can", {
     description = "Trash Can",
     tiles = {"whatever png file you like goes here"},
     groups = {oddly_breakable_by_hand=3},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string("formspec",trash_can_formspec)
      meta:set_string("infotext", "Trash Can")
      local inv = meta:get_inventory()
      inv:set_size("main", 8*4)
   end,
   can_dig = function(pos,player)
      return true
   end,
   on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
      minetest.log("action", player:get_player_name()..
            " moves stuff in trash can at "..minetest.pos_to_string(pos))
   end,
     on_metadata_inventory_put = function(pos, listname, index, stack, player)
      minetest.log("action", player:get_player_name()..
            " moves stuff to trash can at "..minetest.pos_to_string(pos))
   end,
     on_metadata_inventory_take = function(pos, listname, index, stack, player)
      minetest.log("action", player:get_player_name()..
            " takes stuff from trash can at "..minetest.pos_to_string(pos))
   end,
})

--   Crafts
--   Trash Can
minetest.register_craft({
     output = "trashit:trash_can",
     recipe = {
          {'default:stick', '', 'default:stick'},
          {'default:stick', '', 'default:stick'},
          {'', 'default:stick', ''},
     }
})

Re: LUA Bits n Pieces...

PostPosted: Sun Jul 13, 2014 05:43
by ExeterDad
I love topics like this. Almost as much as I like browsing the sed one liners.

Re: Lua Bits n Pieces...

PostPosted: Tue May 05, 2015 04:11
by sofar
I really need to see param/param2 and metadata often, so here's a version that outputs all that.

TBH I'd rather see a formspec here instead. Maybe a next post? :)


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
--= Node Inspector (click on node to give details)

local huds={}

minetest.register_tool("inspector:node_inspector", {
   description = "Node Inspector",
   inventory_image = "default_tool_steelaxe.png",
   liquids_pointable = true,

   on_use = function(itemstack, user, pointed_thing)

      local pll = user:get_player_name()
      local pos = pointed_thing.under

      if pointed_thing.type ~= "node" then
         desc = "..."
      else
         local node = minetest.get_node(pos)
         local meta = minetest.get_meta(pos)
         local tbl = minetest.serialize(meta:to_table())
         desc = dump(node) .. ", meta = { " .. tbl .. " }"
      end

      -- Clear HUD element
      user:hud_remove(huds[pll])

      -- Display new HUD text
      local off = {x=0, y=-80}
      huds[pll] = user:hud_add({
         hud_elem_type = "text",
         position = {x=0.5, y=1},
         offset = off,
         alignment = {x=0, y=0},
         number = 0xFFFFFF ,
         text = desc,
      })
   end,
})

Re: Lua Bits n Pieces...

PostPosted: Thu Jan 07, 2016 22:25
by sofar
I've spent a lot of time tweaking the inspector code to show me all sorts of data. It now pops up a dialog with a text box, so it's far more readable, and it can cope with node metadata, formspecs etc. a lot better.

It's not entirely ideal, but if you're looking for an in-game tool to inspect nodes, it's pretty darn useful.

Code is on github, so get it here:

Image

Re: Lua Bits n Pieces...

PostPosted: Sun Mar 13, 2016 06:07
by sofar
I've attempted to show code-wise why ABM's are so bad for a lot of things. But here's the code proof:

https://gist.github.com/sofar/f82b93d2efc0c04f518f

In short, you create a huge slanted binomial curve that can take forever to finish the work that you intended to do with it.

code in the gist. use as you see fit.

New /limit command and priv for servers.

PostPosted: Mon Mar 14, 2016 19:07
by TenPlus1
This adds a new command that allows you to /limit <player> and remove player privelages safely by giving the 'limit' privelage to a basic admin player, it has a safety feature build in so the server admin cannot be limited.

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
-- Limit command
core.register_privilege("limit", "limits player interaction with world")

core.register_chatcommand("limit", {
   params = "<name>",
   description = "Limits player interaction with world",
   privs = {limit=true},

   func = function(name, param)

      local revoke_name = string.match(param, "([^ ]+)")
      if not revoke_name then
         return false, "Invalid parameters (see /help limit)"
      elseif not core.auth_table[revoke_name] then
         return false, "Player " .. revoke_name .. " does not exist."
      end

      -- server admin cannot be limited
      if revoke_name == "TenPlus1"
      or revoke_name == "Shinji-Ikari" then
      core.chat_send_player(name, "Cannot limit Admin's privs")
         return
      end

      core.set_player_privs(revoke_name, {})
      core.log("action", revoke_name..' privs revoked by '.. name)

      if revoke_name ~= name then
         core.chat_send_player(revoke_name, name
               .. " revoked your privs")
      end

      return true, "Privileges of " .. revoke_name .. ": "
         .. core.privs_to_string(
            core.get_player_privs(revoke_name), ' ')
   end,
})

Re: Lua Bits n Pieces...

PostPosted: Fri Sep 30, 2016 11:12
by red-001
Chat commands to set the look yaw and pitch, useful for taking screenshots,etc
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
-- Setyaw by red-001
minetest.register_chatcommand("setyaw", {
   params = "<yaw>",
   description = "Set player yaw",
   func = function(caller,yaw)
      if (yaw ~= '') and tonumber(yaw) then
         local player = minetest.get_player_by_name(caller)
         player:set_look_yaw(math.rad(yaw))
         return true, "Yaw set to "..yaw
      end
      return false, "invaild input"
   end,
})

-- Setpitch by red-001
minetest.register_chatcommand("setpitch", {
   params = "<pitch>",
   description = "Set player pitch",
   func = function(caller,pitch)
      if (pitch ~= '') and tonumber(pitch) then
         local player = minetest.get_player_by_name(caller)
         player:set_look_pitch(math.rad(pitch))
         return true, "Pitch set to "..pitch
      end
      return false, "invaild input"
   end,
})

Re: Lua Bits n Pieces...

PostPosted: Fri Sep 30, 2016 11:18
by red-001
A few commands that could be useful for server owners
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 jail_pos = {x=-29.5,y=28.5,z=29889}
local spawn_pos = {x=-20,y=6,z=-34}
core.register_privilege("jail","Jail and free players")

minetest.register_chatcommand("jail", {
   params = "<name>",
   description = "Jail a player",
   privs = {jail=true},
   func = function(caller,name)
      if name=="" then return end
      local privs = minetest.get_player_privs(name)
      privs["interact"] = nil
      privs["home"] = nil
      minetest.set_player_privs(name,privs)
      local player = minetest.get_player_by_name(name)
      if not player then return end
      player:setpos(jail_pos)
      minetest.chat_send_all(name.." sent to jail by:"..caller)
      minetest.log(name.." sent to jail by:"..caller)
   end,
})

minetest.register_chatcommand("free", {
   params = "<name>",
   description = "Free a player",
   privs = {jail=true},
   func = function(caller,name)
      if name=="" then return end
      local privs = minetest.get_player_privs(name)
      privs["interact"] = true
      privs["home"] = true
      minetest.set_player_privs(name,privs)
      local player = minetest.get_player_by_name(name)
      if not player then return end
      player:setpos(spawn_pos)
      minetest.chat_send_all(name.." freed from jail by:"..caller)
      minetest.log(name.." freed from jail by:"..caller)
   end,
})

minetest.register_chatcommand("tospawn", {
   params = "<name>",
   description = "Sent a player to spawn",
   privs = {jail=true},
   func = function(caller,name)
      local player = minetest.get_player_by_name(name)
      if not player then return end
      player:setpos(spawn_pos)
      minetest.chat_send_all(name.." sent back to spawn by:"..caller)
   end,
})

minetest.register_chatcommand("spawn", {
   description = "Teleport back to spawn",
   privs = {interact=true},
   func = function(caller,_)
      local player = minetest.get_player_by_name(caller)
      if not player then return end
      player:setpos(spawn_pos)
      minetest.chat_send_all(caller.." sprints back to the spawn point")
   end,
})
   

Re: Lua Bits n Pieces...

PostPosted: Sat Oct 01, 2016 18:41
by bigfoot547
red-001 wrote:A few commands that could be useful for server owners:
-snip-


Some of these commands would be great in my morecommands mod!
I like these ideas.

Re: Lua Bits n Pieces...

PostPosted: Sun Oct 02, 2016 00:00
by red-001
bigfoot547: you're welcome to use them

Re: Lua Bits n Pieces...

PostPosted: Mon Oct 03, 2016 17:32
by bigfoot547
red-001 wrote:bigfoot547: you're welcome to use them


Ok cool! Thanks!
I will give you credit for both of your posts.

P.S. You are awesome red-001!