From d88fa0c438973b1463d7ffdbe4000afd7fa238c7 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 18 Feb 2020 22:05:08 +0100 Subject: [PATCH] Can equip/swap armor with rightclick --- mods/ITEMS/mcl_armor/init.lua | 97 +++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/mods/ITEMS/mcl_armor/init.lua b/mods/ITEMS/mcl_armor/init.lua index 9d4646d9..05b12db2 100644 --- a/mods/ITEMS/mcl_armor/init.lua +++ b/mods/ITEMS/mcl_armor/init.lua @@ -8,6 +8,62 @@ dofile(minetest.get_modpath(minetest.get_current_modname()).."/alias.lua") local longdesc = S("This is a piece of equippable armor which reduces the amount of damage you receive.") local usage = S("To equip it, put it on the corresponding armor slot in your inventory menu.") +local function on_armor_use(itemstack, user, pointed_thing) + if not user or user:is_player() == false then + return itemstack + end + + -- Call on_rightclick if the pointed node defines it + if pointed_thing.type == "node" then + local node = minetest.get_node(pointed_thing.under) + if user and not user:get_player_control().sneak then + if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then + return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack + end + end + end + + local name, player_inv, armor_inv = armor:get_valid_player(user, "[on_armor_use]") + if not name then + return itemstack + end + + local def = itemstack:get_definition() + local slot + if def.groups and def.groups.armor_head then + slot = 2 + elseif def.groups and def.groups.armor_torso then + slot = 3 + elseif def.groups and def.groups.armor_legs then + slot = 4 + elseif def.groups and def.groups.armor_feet then + slot = 5 + end + + if slot then + local itemstack_single = ItemStack(itemstack) + itemstack_single:set_count(1) + local itemstack_slot = armor_inv:get_stack("armor", slot) + if itemstack_slot:is_empty() then + armor_inv:set_stack("armor", slot, itemstack_single) + player_inv:set_stack("armor", slot, itemstack_single) + armor:set_player_armor(user) + armor:update_inventory(user) + armor:play_equip_sound(user, itemstack_single) + itemstack:take_item() + elseif itemstack:get_count() <= 1 then + armor_inv:set_stack("armor", slot, itemstack_single) + player_inv:set_stack("armor", slot, itemstack_single) + armor:set_player_armor(user) + armor:update_inventory(user) + armor:play_equip_sound(user, itemstack_single) + itemstack = ItemStack(itemstack_slot) + end + end + + return itemstack +end + minetest.register_tool("mcl_armor:helmet_leather", { description = S("Leather Cap"), _doc_items_longdesc = longdesc, @@ -19,6 +75,8 @@ minetest.register_tool("mcl_armor:helmet_leather", { _mcl_armor_equip = "mcl_armor_equip_leather", _mcl_armor_unequip = "mcl_armor_unequip_leather", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:helmet_iron", { @@ -32,6 +90,9 @@ minetest.register_tool("mcl_armor:helmet_iron", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:helmet_gold", { @@ -45,6 +106,8 @@ minetest.register_tool("mcl_armor:helmet_gold", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:helmet_diamond",{ @@ -58,6 +121,8 @@ minetest.register_tool("mcl_armor:helmet_diamond",{ _mcl_armor_equip = "mcl_armor_equip_diamond", _mcl_armor_unequip = "mcl_armor_unequip_diamond", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:helmet_chain", { @@ -71,6 +136,8 @@ minetest.register_tool("mcl_armor:helmet_chain", { _mcl_armor_equip = "mcl_armor_equip_chainmail", _mcl_armor_unequip = "mcl_armor_unequip_chainmail", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) -- Regisiter Torso Armor @@ -86,6 +153,8 @@ minetest.register_tool("mcl_armor:chestplate_leather", { _mcl_armor_equip = "mcl_armor_equip_leather", _mcl_armor_unequip = "mcl_armor_unequip_leather", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:chestplate_iron", { @@ -99,6 +168,8 @@ minetest.register_tool("mcl_armor:chestplate_iron", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:chestplate_gold", { @@ -112,6 +183,8 @@ minetest.register_tool("mcl_armor:chestplate_gold", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:chestplate_diamond",{ @@ -125,6 +198,8 @@ minetest.register_tool("mcl_armor:chestplate_diamond",{ _mcl_armor_equip = "mcl_armor_equip_diamond", _mcl_armor_unequip = "mcl_armor_unequip_diamond", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:chestplate_chain", { @@ -138,6 +213,8 @@ minetest.register_tool("mcl_armor:chestplate_chain", { _mcl_armor_equip = "mcl_armor_equip_chainmail", _mcl_armor_unequip = "mcl_armor_unequip_chainmail", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) -- Regisiter Leg Armor @@ -153,6 +230,8 @@ minetest.register_tool("mcl_armor:leggings_leather", { _mcl_armor_equip = "mcl_armor_equip_leather", _mcl_armor_unequip = "mcl_armor_unequip_leather", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:leggings_iron", { @@ -166,6 +245,8 @@ minetest.register_tool("mcl_armor:leggings_iron", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:leggings_gold", { @@ -179,6 +260,8 @@ minetest.register_tool("mcl_armor:leggings_gold", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:leggings_diamond",{ @@ -192,6 +275,8 @@ minetest.register_tool("mcl_armor:leggings_diamond",{ _mcl_armor_equip = "mcl_armor_equip_diamond", _mcl_armor_unequip = "mcl_armor_unequip_diamond", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:leggings_chain", { @@ -205,6 +290,8 @@ minetest.register_tool("mcl_armor:leggings_chain", { _mcl_armor_equip = "mcl_armor_equip_chainmail", _mcl_armor_unequip = "mcl_armor_unequip_chainmail", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) -- Regisiter Boots @@ -219,6 +306,8 @@ minetest.register_tool("mcl_armor:boots_leather", { _mcl_armor_equip = "mcl_armor_equip_leather", _mcl_armor_unequip = "mcl_armor_unequip_leather", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:boots_iron", { @@ -232,6 +321,8 @@ minetest.register_tool("mcl_armor:boots_iron", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:boots_gold", { @@ -245,6 +336,8 @@ minetest.register_tool("mcl_armor:boots_gold", { _mcl_armor_equip = "mcl_armor_equip_iron", _mcl_armor_unequip = "mcl_armor_unequip_iron", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:boots_diamond",{ @@ -258,6 +351,8 @@ minetest.register_tool("mcl_armor:boots_diamond",{ _mcl_armor_equip = "mcl_armor_equip_diamond", _mcl_armor_unequip = "mcl_armor_unequip_diamond", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) minetest.register_tool("mcl_armor:boots_chain", { @@ -271,6 +366,8 @@ minetest.register_tool("mcl_armor:boots_chain", { _mcl_armor_equip = "mcl_armor_equip_chainmail", _mcl_armor_unequip = "mcl_armor_unequip_chainmail", }, + on_place = on_armor_use, + on_secondary_use = on_armor_use, }) -- Register Craft Recipies