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

Add get_charge, set_charge and use_charge aliases #392

Open
S-S-X opened this issue Jan 5, 2025 · 1 comment · May be fixed by #396
Open

Add get_charge, set_charge and use_charge aliases #392

S-S-X opened this issue Jan 5, 2025 · 1 comment · May be fixed by #396
Labels
Compatibility Aims to improve compatibility with something.
Milestone

Comments

@S-S-X
Copy link
Member

S-S-X commented Jan 5, 2025

We've had technic.get_RE_charge, technic.set_RE_charge and technic.use_RE_charge available for some time (since 2022), those are also used in various mods already.

Around 2024 minetest-mods/technic added technic.get_charge and technic.set_charge which have compatible interface with Technic Plus counterparts.

For consistency would be good to also add technic.use_charge, preferably even attempt to bring last one also to minetest-mods/technic. Function itself is fairly simple, basically just a shortcut wrapper around get/set functions.
Should then probably also prefer technic.get_charge, technic.set_charge and technic.use_charge by replacing their .*_RE.* counterparts in documentation.

This would give significantly better API compatibility for power tools.

@S-S-X S-S-X added the Compatibility Aims to improve compatibility with something. label Jan 5, 2025
@S-S-X S-S-X added this to the 2.0.0 milestone Jan 5, 2025
@S-S-X
Copy link
Member Author

S-S-X commented Jan 25, 2025

Examples what I think should be possible afterwards:

SwissalpS/replacer (ATOW @ master)
https://github.com/SwissalpS/replacer/blob/5d5dfc0f2127bb8d257bd4e518acc2aeafba26df/replacer/replacer.lua#L115-L138

- if technic.plus then
- 	replacer.get_charge = technic.get_RE_charge
- 	replacer.set_charge = technic.set_RE_charge
- else
- 	-- technic still stores data serialized, so this is the nearest we get to current standard
- 	function replacer.get_charge(itemstack)
- 		local meta = deserialize(itemstack:get_meta():get_string(''))
- 		if (not meta) or (not meta.charge) then
- 			return 0
- 		end
- 		return meta.charge
- 	end
-
- 	function replacer.set_charge(itemstack, charge, maximum)
- 		technic.set_RE_wear(itemstack, charge, maximum)
- 		local meta = itemstack:get_meta()
- 		local data = deserialize(meta:get_string(''))
- 		if (not data) or (not data.charge) then
- 			data = { charge = 0 }
- 		end
- 		data.charge = charge
- 		meta:set_string('', serialize(data))
- 	end
- end
+ replacer.get_charge = technic.get_charge
+ replacer.set_charge = technic.set_charge

robertxgray/bweapons_modpack (ATOW PR on mt-historical fixing technic compatibility)
https://github.com/robertxgray/bweapons_modpack/blob/8bfce6db0eb6ea1ee7a767a601306b5423fe889b/bweapons_api/api.lua#L412-L444
Following diff ignores charge migration but so did minetest-mods Technic.
Technic Plus Beta does not need migration at all as there's magic ingredients.

  -- Technic Plus
  if technic.use_charge ~= nil then
  	if not technic.use_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
+ 	local charge = technic.get_charge(itemstack)
- 	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
+ 	elseif 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
+ 		technic.set_charge(itemstack, charge - technic_charge_per_use)
	end
end

@S-S-X S-S-X linked a pull request Jan 26, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compatibility Aims to improve compatibility with something.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant