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

Expose get/set charge functions #645

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions technic/doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ Unsorted functions:
* If `technic.power_tools[itemstack:get_name()]` is `nil` (or `false`), this
function does nothing, else that value is the maximum charge.
* The itemstack metadata is changed to contain the charge.
* `technic.get_charge(itemstack)`
* Returns the charge and max charge of the given itemstack.
* If the itemstack is not an RE chargeable item, both return values will be zero.
* `technic.set_charge(itemstack, charge)`
* Modifies the charge of the given itemstack.

### Node-specific
* `technic.get_or_load_node(pos)`
Expand Down
12 changes: 6 additions & 6 deletions technic/machines/register/battery_box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ minetest.register_on_player_receive_fields(
end
)

local function default_get_charge(itemstack)
function technic.get_charge(itemstack)
-- check if is chargable
local tool_name = itemstack:get_name()
if not technic.power_tools[tool_name] then
Expand All @@ -419,7 +419,7 @@ local function default_get_charge(itemstack)
return item_meta:get_int("technic:charge"), technic.power_tools[tool_name]
end

local function default_set_charge(itemstack, charge)
function technic.set_charge(itemstack, charge)
local tool_name = itemstack:get_name()
if technic.power_tools[tool_name] then
technic.set_RE_wear(itemstack, charge, technic.power_tools[tool_name])
Expand All @@ -437,8 +437,8 @@ function technic.charge_tools(meta, batt_charge, charge_step)

-- get callbacks
local src_def = src_stack:get_definition()
local technic_get_charge = src_def.technic_get_charge or default_get_charge
local technic_set_charge = src_def.technic_set_charge or default_set_charge
local technic_get_charge = src_def.technic_get_charge or technic.get_charge
local technic_set_charge = src_def.technic_set_charge or technic.set_charge

-- get tool charge
local tool_charge, item_max_charge = technic_get_charge(src_stack)
Expand Down Expand Up @@ -471,8 +471,8 @@ function technic.discharge_tools(meta, batt_charge, charge_step, max_charge)

-- get callbacks
local src_def = src_stack:get_definition()
local technic_get_charge = src_def.technic_get_charge or default_get_charge
local technic_set_charge = src_def.technic_set_charge or default_set_charge
local technic_get_charge = src_def.technic_get_charge or technic.get_charge
local technic_set_charge = src_def.technic_set_charge or technic.set_charge

-- get tool charge
local tool_charge, item_max_charge = technic_get_charge(src_stack)
Expand Down