Lua Bits n Pieces...

User avatar
TenPlus1
Member
 
Posts: 2628
Joined: Mon Jul 29, 2013 13:38
GitHub: tenplus1

Lua Bits n Pieces...

by TenPlus1 » Sat Jul 12, 2014 17:30

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...

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,
})
Last edited by TenPlus1 on Sat Jul 12, 2014 17:35, edited 1 time in total.
 

Sockbat
New member
 
Posts: 1
Joined: Sat Jul 12, 2014 17:30
In-game: Sockbat

Re: LUA Bits n Pieces...

by Sockbat » Sat Jul 12, 2014 17:34

Thanks TenPlus1, that's quite useful.
 

User avatar
kaeza
Moderator
 
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
Location: Montevideo, Uruguay
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: LUA Bits n Pieces...

by kaeza » Sat Jul 12, 2014 18:50

Déja vu. Wasn't there a similar topic already?
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal
 

User avatar
LionsDen
Member
 
Posts: 530
Joined: Thu Jun 06, 2013 03:19

Re: LUA Bits n Pieces...

by LionsDen » Sat Jul 12, 2014 19:08

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.

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', ''},
     }
})
 

User avatar
ExeterDad
Member
 
Posts: 1717
Joined: Sun Jun 01, 2014 20:00
Location: New Hampshire U.S.A
In-game: ExeterDad

Re: LUA Bits n Pieces...

by ExeterDad » Sun Jul 13, 2014 05:43

I love topics like this. Almost as much as I like browsing the sed one liners.
 

sofar
Developer
 
Posts: 2075
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Lua Bits n Pieces...

by sofar » Tue May 05, 2015 04:11

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? :)


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,
})
 

sofar
Developer
 
Posts: 2075
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Lua Bits n Pieces...

by sofar » Thu Jan 07, 2016 22:25

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
 

sofar
Developer
 
Posts: 2075
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Lua Bits n Pieces...

by sofar » Sun Mar 13, 2016 06:07

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.
 

User avatar
TenPlus1
Member
 
Posts: 2628
Joined: Mon Jul 29, 2013 13:38
GitHub: tenplus1

New /limit command and priv for servers.

by TenPlus1 » Mon Mar 14, 2016 19:07

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.

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,
})
 

red-001
Member
 
Posts: 205
Joined: Tue Jan 26, 2016 20:15
GitHub: red-001
IRC: red-001

Re: Lua Bits n Pieces...

by red-001 » Fri Sep 30, 2016 11:12

Chat commands to set the look yaw and pitch, useful for taking screenshots,etc
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,
})
 

red-001
Member
 
Posts: 205
Joined: Tue Jan 26, 2016 20:15
GitHub: red-001
IRC: red-001

Re: Lua Bits n Pieces...

by red-001 » Fri Sep 30, 2016 11:18

A few commands that could be useful for server owners
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,
})
   
 

User avatar
bigfoot
Member
 
Posts: 203
Joined: Fri Sep 02, 2016 23:31
Location: Somewhere between polar bears and penguins.
GitHub: bigfoot547
IRC: bigfoot547 or bigfoot
In-game: bigfoot547 or bigfoot

Re: Lua Bits n Pieces...

by bigfoot » Sat Oct 01, 2016 18:41

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.
God's not dead, He's surely alive!
My Stuff | My User Page | Check out my website!
<bigfoot547> I major in existing. | <RedPanda246> Not every day is good, but there is good in every day.
Stay elusive ⓐⓝⓓ ⓣⓐⓛⓚ ⓘⓝ ⓒⓘⓡⓒⓛⓔⓢ!
 

red-001
Member
 
Posts: 205
Joined: Tue Jan 26, 2016 20:15
GitHub: red-001
IRC: red-001
 

User avatar
bigfoot
Member
 
Posts: 203
Joined: Fri Sep 02, 2016 23:31
Location: Somewhere between polar bears and penguins.
GitHub: bigfoot547
IRC: bigfoot547 or bigfoot
In-game: bigfoot547 or bigfoot

Re: Lua Bits n Pieces...

by bigfoot » Mon Oct 03, 2016 17:32

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!
God's not dead, He's surely alive!
My Stuff | My User Page | Check out my website!
<bigfoot547> I major in existing. | <RedPanda246> Not every day is good, but there is good in every day.
Stay elusive ⓐⓝⓓ ⓣⓐⓛⓚ ⓘⓝ ⓒⓘⓡⓒⓛⓔⓢ!
 

User avatar
TenPlus1
Member
 
Posts: 2628
Joined: Mon Jul 29, 2013 13:38
GitHub: tenplus1

Re: Lua Bits n Pieces...

by TenPlus1 » Sat Nov 09, 2019 11:55

Some modders have been wondering how to add item drops to a block without overriding it's existing ones, so I wrote this little function to help:

Code: Select all
local function add_drop(nodename, drop_table)

   -- does node exist?
   if not minetest.registered_nodes[nodename] then return end

   -- get existing drops
   local drops = minetest.registered_nodes[nodename].drop

   -- if single item string then apend to end of new drop_table
   if type(drops) == "string" then print ("== string")

      table.insert(drop_table.items, {items = {drops}})
   elseif type(drops) == "table" then print ("== table")

      -- apend existing drops to new drop_table
      for _, item in ipairs(drops.items) do
         table.insert(drop_table.items, item)
      end

      -- use new max_items or default to current
      drop_table.max_items = drop_table.max_items or drops.max_items
   end

   -- override node with new drops
   minetest.override_item(nodename, {drop = drop_table})
end

minetest.register_node("test:block", {
   description = "Test Block",
   tiles = {"default_wood.png"},
   groups = {crumbly = 3},
--   drop = "default:dirt",
   drop = {
      max_items = 1,
      items = {
         {items = {"default:gravel"}, rarity = 2},
         {items = {"default:dirt"}, rarity = 1},
      }
   }
})

add_drop("test:block", {
   max_items = 1,
   items = {
      {items = {"default:sand"}, rarity = 2},
   }
})
 

ThorfinnS
Member
 
Posts: 188
Joined: Mon Feb 25, 2019 22:05
GitHub: ThorfinnS

Re: Lua Bits n Pieces...

by ThorfinnS » Mon Nov 11, 2019 18:34

Oh, thanks! Of course that's how to do it. Don't know why it didn't occur to me; I use that same method elsewhere.

More to the point, I use something similar. If I understood Lua developer docs correctly, table.insert is due to be deprecated at some time. As I understand it, the recommended way to add elements is
Code: Select all
drop_table.items[#drop_table.items+1]=whatever
 

User avatar
TenPlus1
Member
 
Posts: 2628
Joined: Mon Jul 29, 2013 13:38
GitHub: tenplus1

Re: Lua Bits n Pieces...

by TenPlus1 » Mon Nov 11, 2019 19:16

table.insert was changed so that it uses the same method :P
 

User avatar
Linuxdirk
Member
 
Posts: 2200
Joined: Wed Sep 17, 2014 11:21
Location: Germany
In-game: Linuxdirk

Re: Lua Bits n Pieces...

by Linuxdirk » Tue Nov 12, 2019 06:41

ThorfinnS wrote:table.insert is due to be deprecated at some time.

Since Minetest uses a very old version of Lua I'm pretty sure there is nothing to worry about anytime soon.
 


Return to WIP Mods



Who is online

Users browsing this forum: Andrey01 and 3 guests