Skip to content

Commit

Permalink
Better technic support
Browse files Browse the repository at this point in the history
The hitech weapons are only compatible with the stable version of
Technic Plus. This change adds support for Technic and Technic Plus
Beta.

It also updates the code to use the new metadata functions instead
of the deprecated get_metadata and set_metadata.
  • Loading branch information
robertxgray committed Dec 29, 2024
1 parent aa965a2 commit 5917733
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions bweapons_api/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -406,28 +406,58 @@ function bweapons.register_weapon(def)
end

if def.has_durability then
local meta = minetest.deserialize(itemstack:get_metadata())
if
def.requires_technic and not meta or
def.requires_technic and meta.charge < technic_charge_per_use or
not def.requires_technic and def.custom_charge and (65535 - itemstack:get_wear()) < (65535 / uses)
then
local reload = false

if def.requires_technic then
-- Technic Plus Beta
if technic.use_RE_charge ~= nil then
if not technic.use_RE_charge(itemstack, technic_charge_per_use) then
reload = true
end
else
-- Technic Plus
local meta = itemstack:get_meta()
local old_metadata = minetest.deserialize(meta:get_string(""))
local charge = old_metadata.charge
-- Technic
if not charge then
charge = meta:get_int("technic:charge")
end

if charge < technic_charge_per_use then
reload = true
else
if not technic.creative_mode then
charge = charge - technic_charge_per_use
technic.set_RE_wear(itemstack, charge, technic_charge)
if old_metadata.charge then
old_metadata.charge = charge
meta:set_string("", minetest.serialize(old_metadata))
else
meta:set_int("technic:charge", charge)
end
end
end
end

-- Not Technic
else
if def.custom_charge and (65535 - itemstack:get_wear()) < (65535 / uses) then
reload = true
else
local wear = itemstack:get_wear()
wear = wear + (65535/uses)
if def.custom_charge and wear > 65535 then wear = 65535 end
itemstack:set_wear(wear)
end
end

if reload then
if def.reload_sound then
minetest.sound_play(def.reload_sound, {object=user, gain=reload_sound_gain, max_hear_distance=2*64})
end
return
end

if def.requires_technic then
meta.charge = meta.charge - technic_charge_per_use
technic.set_RE_wear(itemstack, meta.charge, technic_charge)
itemstack:set_metadata(minetest.serialize(meta))
else
local wear = itemstack:get_wear()
wear = wear + (65535/uses)
if def.custom_charge and wear > 65535 then wear = 65535 end
itemstack:set_wear(wear)
end
end

if def.ammo_type then
Expand Down

0 comments on commit 5917733

Please sign in to comment.