Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hitech weapons #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions bweapons_api/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -406,28 +406,61 @@ 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 = nil
if old_metadata then
charge = old_metadata.charge
end
-- 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 and 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
Comment on lines +418 to +443
Copy link

@S-S-X S-S-X Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be enough for both migration and usage with original Technic, Technic Plus stable and Technic Plus Beta if we get mt-mods/technic#391 through:

Suggested change
-- Technic Plus
local meta = itemstack:get_meta()
local old_metadata = minetest.deserialize(meta:get_string(""))
local charge = nil
if old_metadata then
charge = old_metadata.charge
end
-- 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 and 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
-- Technic, old metadata migration
local meta = itemstack:get_meta()
local old_metadata = core.deserialize(meta:get_string(""))
if old_metadata and old_metadata.charge then
technic.set_charge(itemstack, old_metadata.charge)
meta:set_string("", "")
end
-- Technic, check and use charge
local charge = technic.get_charge(itemstack)
if charge < technic_charge_per_use then
reload = true
elseif not technic.creative_mode then
technic.set_charge(itemstack, charge - technic_charge_per_use)
end

That's already +15 -26 LoC

Considering simplicity of changes and compatibility gains on Technic Plus side for me this seems significant improvement.

Copy link

@S-S-X S-S-X Jan 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also any Technic fork that implement technic.get_charge and technic.set_charge no matter how charge is actually stored.

Though if any of the forks with these are still using old meta then migration would do unnecessary metadata reset each time.

Also I'm not sure if full old metadata reset is safe with these items, if something else round here uses old metadata then that something else would be lost too. However I'd argue full cleanup of old metadata would be very good in case it isn't needed.

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