From ebe1db179d70596ff16df86b79ea416a5e2b777b Mon Sep 17 00:00:00 2001 From: tenplus1 Date: Sun, 25 Aug 2024 10:46:50 +0100 Subject: [PATCH] hoes can deal damage to mobs and players --- README.md | 2 +- hoes.lua | 136 +++++++++++++++++++++++++++++++----------------------- init.lua | 3 +- 3 files changed, 82 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index ecb0678..ec4143e 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,7 @@ on an older map are enabled and growing properly. ### Changelog: -- 1.49 - Added {eatable=1} groups to food items with the value giving HP when eaten, improved mineclone support, separated foods from crop files. +- 1.49 - Added {eatable=1} groups to food items with the value giving HP when eaten, improved mineclone support, separated foods from crop files., hoes can deal damage. - 1.48 - added 'farming_use_utensils' setting to enable/disable utensils in recipes, added mayonnaise (thx felfa), added gingerbread man, Added MineClone2 compatibility - 1.47 - Now blueberries can make blue dye, tweak soil types to work better with older 0.4.x clients and add spanish translation (thx mckaygerhard), add trellis setting to registered_crops and fix pea and soy crop names (thx nixnoxus), add strawberries if ethereal mod not active, added asparagus; spinach; eggplant (thx Atlante for new textures), Sugar Cube - 1.46 - Added min/max default light settings, added lettuce and blackberries with food items (thanks OgelGames), added soya, vanilla and sunflowers (thanks Felfa), added tofu, added salt crystals (thanks gorlock) diff --git a/hoes.lua b/hoes.lua index ecefbca..3d255e7 100644 --- a/hoes.lua +++ b/hoes.lua @@ -28,6 +28,7 @@ farming.register_hoe = function(name, def) inventory_image = def.inventory_image, groups = def.groups, sound = {breaks = "default_tool_breaks"}, + damage_groups = def.damage_groups, on_use = function(itemstack, user, pointed_thing) return farming.hoe_on_use(itemstack, user, pointed_thing, def.max_uses) @@ -58,71 +59,87 @@ end function farming.hoe_on_use(itemstack, user, pointed_thing, uses) - local pt = pointed_thing + local pt = pointed_thing or {} + local is_used = false -- am I going to hoe the top of a dirt node? - if not pt or pt.type ~= "node" or pt.above.y ~= pt.under.y + 1 then - return + if pt.type == "node" and pt.above.y == pt.under.y + 1 then + + local under = minetest.get_node(pt.under) + local upos = pointed_thing.under + + if minetest.is_protected(upos, user:get_player_name()) then + minetest.record_protection_violation(upos, user:get_player_name()) + return + end + + local p = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z} + local above = minetest.get_node(p) + + -- return if any of the nodes is not registered + if not minetest.registered_nodes[under.name] + or not minetest.registered_nodes[above.name] then return end + + -- check if the node above the pointed thing is air + if above.name ~= "air" then return end + + -- check if pointing at dirt + if minetest.get_item_group(under.name, "soil") ~= 1 then return end + + -- check if (wet) soil defined + local ndef = minetest.registered_nodes[under.name] + + if ndef.soil == nil or ndef.soil.wet == nil or ndef.soil.dry == nil then + return + end + + if minetest.is_protected(pt.under, user:get_player_name()) then + minetest.record_protection_violation(pt.under, user:get_player_name()) + return + end + + -- turn the node into soil, wear out item and play sound + minetest.set_node(pt.under, {name = ndef.soil.dry}) ; is_used = true + + minetest.sound_play("default_dig_crumbly", {pos = pt.under, gain = 0.5}, true) end - local under = minetest.get_node(pt.under) - local upos = pointed_thing.under - - if minetest.is_protected(upos, user:get_player_name()) then - minetest.record_protection_violation(upos, user:get_player_name()) - return - end - - local p = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z} - local above = minetest.get_node(p) - - -- return if any of the nodes is not registered - if not minetest.registered_nodes[under.name] - or not minetest.registered_nodes[above.name] then return end - - -- check if the node above the pointed thing is air - if above.name ~= "air" then return end - - -- check if pointing at dirt - if minetest.get_item_group(under.name, "soil") ~= 1 then return end - - -- check if (wet) soil defined - local ndef = minetest.registered_nodes[under.name] - - if ndef.soil == nil or ndef.soil.wet == nil or ndef.soil.dry == nil then - return - end - - if minetest.is_protected(pt.under, user:get_player_name()) then - minetest.record_protection_violation(pt.under, user:get_player_name()) - return - end - - -- turn the node into soil, wear out item and play sound - minetest.set_node(pt.under, {name = ndef.soil.dry}) - - minetest.sound_play("default_dig_crumbly", {pos = pt.under, gain = 0.5}, true) - local wdef = itemstack:get_definition() local wear = 65535 / (uses - 1) - if farming.is_creative(user:get_player_name()) then + -- using hoe as weapon + if pt.type == "object" then - if mod_tr then - wear = 1 - else - wear = 0 + local ent = pt.ref and pt.ref:get_luaentity() + local dir = user:get_look_dir() + + if (ent and ent.name ~= "__builtin:item" + and ent.name ~= "__builtin:falling_node") or pt.ref:is_player() then + + pt.ref:punch(user, nil, {full_punch_interval = 1.0, + damage_groups = wdef.damage_groups}, dir) + + is_used = true end end - if mod_tr then - itemstack = toolranks.new_afteruse(itemstack, user, under, {wear = wear}) - else - itemstack:add_wear(wear) - end + -- only when used on soil top or external entity + if is_used then - if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then - minetest.sound_play(wdef.sound.breaks, {pos = pt.above, gain = 0.5}, true) + -- cretive doesnt wear tools but toolranks registers uses with wear so set to 1 + if farming.is_creative(user:get_player_name()) then + if mod_tr then wear = 1 else wear = 0 end + end + + if mod_tr then + itemstack = toolranks.new_afteruse(itemstack, user, under, {wear = wear}) + else + itemstack:add_wear(wear) + end + + if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then + minetest.sound_play(wdef.sound.breaks, {pos = pt.above, gain = 0.5}, true) + end end return itemstack @@ -134,7 +151,8 @@ farming.register_hoe(":farming:hoe_wood", { description = S("Wooden Hoe"), inventory_image = "farming_tool_woodhoe.png", max_uses = 30, - material = "group:wood" + material = "group:wood", + damage_groups = {fleshy = 2} }) minetest.register_craft({ @@ -147,7 +165,8 @@ farming.register_hoe(":farming:hoe_stone", { description = S("Stone Hoe"), inventory_image = "farming_tool_stonehoe.png", max_uses = 90, - material = "group:stone" + material = "group:stone", + damage_groups = {fleshy = 2} }) farming.register_hoe(":farming:hoe_steel", { @@ -162,7 +181,8 @@ farming.register_hoe(":farming:hoe_bronze", { inventory_image = "farming_tool_bronzehoe.png", max_uses = 250, groups = {not_in_creative_inventory = 1}, - material = "default:bronze_ingot" + material = "default:bronze_ingot", + damage_groups = {fleshy = 3} }) farming.register_hoe(":farming:hoe_mese", { @@ -170,13 +190,15 @@ farming.register_hoe(":farming:hoe_mese", { inventory_image = "farming_tool_mesehoe.png", max_uses = 350, groups = {not_in_creative_inventory = 1}, + damage_groups = {fleshy = 4} }) farming.register_hoe(":farming:hoe_diamond", { description = S("Diamond Hoe"), inventory_image = "farming_tool_diamondhoe.png", max_uses = 500, - groups = {not_in_creative_inventory = 1} + groups = {not_in_creative_inventory = 1}, + damage_groups = {fleshy = 4} }) -- Toolranks support diff --git a/init.lua b/init.lua index d8832e5..7211c40 100644 --- a/init.lua +++ b/init.lua @@ -5,13 +5,14 @@ ]] -- Translation support + local S = minetest.get_translator("farming") -- global farming = { mod = "redo", - version = "20240812", + version = "20240825", path = minetest.get_modpath("farming"), select = {type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5}}, select_final = {type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -2.5/16, 0.5}},