From 164cb539d3e2df17f60db72faf9c0c4c0cc30112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikita=20Wi=C5=9Bniewski?= <rudzik8@protonmail.com> Date: Sat, 4 Jan 2025 13:42:47 +0700 Subject: [PATCH] Pickmob functionality --- mods/PLAYER/mcl_meshhand/init.lua | 5 +++++ mods/PLAYER/vl_pickblock/init.lua | 33 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/mods/PLAYER/mcl_meshhand/init.lua b/mods/PLAYER/mcl_meshhand/init.lua index ef9119c1b..3baeacb4a 100644 --- a/mods/PLAYER/mcl_meshhand/init.lua +++ b/mods/PLAYER/mcl_meshhand/init.lua @@ -113,4 +113,9 @@ minetest.override_item("", { return vl_pickblock.pickblock(itemstack, placer, pointed_thing) end end, + on_secondary_use = function(itemstack, placer, pointed_thing) + if minetest.is_creative_enabled(placer:get_player_name()) then + return vl_pickblock.pickmob(itemstack, placer, pointed_thing) + end + end, }) diff --git a/mods/PLAYER/vl_pickblock/init.lua b/mods/PLAYER/vl_pickblock/init.lua index a96170919..0e8f83049 100644 --- a/mods/PLAYER/vl_pickblock/init.lua +++ b/mods/PLAYER/vl_pickblock/init.lua @@ -1,5 +1,8 @@ vl_pickblock = {} +-- The main Pickblock handler function. +-- To be called in hand's `on_place` +-- (assumes that pointed_thing.type == "node") function vl_pickblock.pickblock(itemstack, placer, pointed_thing) local pos = pointed_thing.under local node = minetest.get_node_or_nil(pointed_thing.under) @@ -37,3 +40,33 @@ function vl_pickblock.pickblock(itemstack, placer, pointed_thing) return rnode end + +-- Pickblock handler for mobs. +-- To be called in hand's `on_secondary_use` +-- (assumes that pointed_thing.type ~= "node") +function vl_pickblock.pickmob(itemstack, clicker, pointed_thing) + if pointed_thing.type ~= "object" + -- only pick mobs when crouching + or (not clicker:get_player_control().sneak) then + return + end + + local le = pointed_thing.ref:get_luaentity() + if not (le and le.is_mob) then return end + + local def = minetest.registered_craftitems[le.name] + if not def then return end + + -- check if the picked mob egg is already on the hotbar + -- if so, remove it! + local inv = clicker:get_inventory() + for i = 1, clicker:hud_get_hotbar_itemcount() do + local stack = inv:get_stack("main", i) + if stack:get_name() == le.name then + inv:set_stack("main", i, ItemStack()) + break -- only remove one + end + end + + return le.name +end