From 20405958da944c97c0c54ac586edadf654f271da Mon Sep 17 00:00:00 2001 From: stujones11 Date: Sun, 24 Mar 2019 15:06:10 +0000 Subject: [PATCH] More minor corrections --- shooter/README.md | 5 +++++ shooter/api.lua | 22 +++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/shooter/README.md b/shooter/README.md index 6de7c35..5f0caf6 100644 --- a/shooter/README.md +++ b/shooter/README.md @@ -155,6 +155,11 @@ Used by `shooter.register_weapon` description = "My Awesome Gun (Unloaded)", inventory_image = "my_awesome_gun_unloaded.png", }, + on_use = function(itemstack, user, pointed_thing) + -- Called before the built-in tool `one_use` handler + -- Should return a valid `ItemStack` or `nil` to cancel the shot + return itemstack + end, on_hit = function(pointed_thing, spec, dir) -- May be used for arbitary shot effects like knock-back, etc. -- Return `true` to override built-in damage effects diff --git a/shooter/api.lua b/shooter/api.lua index 8ad7ce8..f99cb94 100644 --- a/shooter/api.lua +++ b/shooter/api.lua @@ -59,7 +59,6 @@ local config = shooter.config local server_step = minetest.settings:get("dedicated_server_step") shooter.register_weapon = function(name, def) - shooter.registered_weapons[name] = def -- Fix definition table def.sounds = def.sounds or {} def.sounds.reload = def.sounds.reload or "shooter_reload" @@ -73,6 +72,7 @@ shooter.register_weapon = function(name, def) description = def.description.." (unloaded)", inventory_image = def.inventory_image, } + shooter.registered_weapons[name] = table.copy(def) -- Register loaded item tool minetest.register_tool(name.."_loaded", { description = def.description, @@ -81,11 +81,13 @@ shooter.register_weapon = function(name, def) if type(def.on_use) == "function" then itemstack = def.on_use(itemstack, user, pointed_thing) end - local spec = table.copy(def.spec) - if shooter.fire_weapon(user, itemstack, spec) then - itemstack:add_wear(def.spec.wear) - if itemstack:get_count() == 0 then - itemstack:replace(def.unloaded_item.name) + if itemstack then + local spec = table.copy(def.spec) + if shooter.fire_weapon(user, itemstack, spec) then + itemstack:add_wear(def.spec.wear) + if itemstack:get_count() == 0 then + itemstack:replace(def.unloaded_item.name) + end end end return itemstack @@ -175,6 +177,9 @@ shooter.is_valid_object = function(object) end shooter.punch_node = function(pos, spec) + if config.enable_protection and minetest.is_protected(pos, spec.user) then + return + end local node = minetest.get_node(pos) if not node then return @@ -183,11 +188,6 @@ shooter.punch_node = function(pos, spec) if not item then return end - if config.enable_protection then - if minetest.is_protected(pos, spec.user) then - return - end - end if item.groups then for k, v in pairs(spec.groups) do local level = item.groups[k] or 0