Skip to content

fix(battery): Fix battery holder accepting technic charges #115

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 5 additions & 26 deletions nodes/node_battery_holder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,11 @@ local def = {
-- Allow all items with energy storage to be placed in the inventory
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
if listname == "batteries" then
if not minetest.global_exists("technic") then
return 0
end

local node_name = stack:get_name()

-- Allow all items with energy storage from technic mod
if technic.power_tools[node_name] ~= nil then
local meta = stack:get_metadata()
local md = minetest.deserialize(meta)
-- And specifically if they hold any charge
-- Disregard empty batteries, the player should know better
if md and md.charge > 0 then
if digtron.check_protected_and_record(pos, player) then
return 0
end
return stack:get_count()
else
if minetest.global_exists("technic") and technic.get_charge(stack) > 0 then
if digtron.check_protected_and_record(pos, player) then
return 0
end
else
return 0
return stack:get_count()
Copy link
Member

@SmallJoker SmallJoker Aug 21, 2025

Choose a reason for hiding this comment

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

Do you really intend to allow any player to use this slot for arbitrary storage if technic is not installed?
EDIT: The GitHub web diff view is misleading. Please ignore this comment.

end
end
return 0
Expand Down Expand Up @@ -116,12 +99,8 @@ local def = {
return inv:add_item("batteries", stack)
end,
can_insert = function(pos, _, stack)
local meta = stack:get_metadata()
local md = minetest.deserialize(meta)
-- And specifically if they hold any charge
-- Disregard empty batteries, the player should know better
if md and md.charge > 0 then
meta = minetest.get_meta(pos)
if minetest.global_exists("technic") and technic.get_charge(stack) > 0 then
Copy link
Member

Choose a reason for hiding this comment

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

Please keep the previous comment about -- Disregard empty batteries, the player should know better to justify the charge > 0 check.

local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:room_for_item("batteries", stack)
end
Expand Down
17 changes: 7 additions & 10 deletions util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ digtron.tap_batteries = function(battery_positions, target, test)
-- setting Moved to digtron.config.power_ratio

for _, location in pairs(battery_positions) do
if current_burned > target then
if current_burned >= target then
break
end
node_inventory_table.pos = location.pos
Expand All @@ -262,23 +262,20 @@ digtron.tap_batteries = function(battery_positions, target, test)
end

for _, itemstack in pairs(invlist) do
local meta = minetest.deserialize(itemstack:get_metadata())
if (meta ~= nil) then
local power_available = math.floor(meta.charge / digtron.config.power_ratio)
if minetest.global_exists("technic") then
local power_available = math.floor(technic.get_charge(itemstack) / digtron.config.power_ratio)
if power_available ~= 0 then
local actual_burned = power_available -- we just take all we have from the battery, since they aren't stackable
-- we take only what's necessary to reach target power
local actual_burned = math.min(power_available, (target - current_burned))
-- don't bother recording the items if we're just testing, nothing is actually being removed.
if test ~= true then
-- since we are taking everything, the wear and charge can both be set to 0
itemstack:set_wear(0)
meta.charge = 0
itemstack:set_metadata(minetest.serialize(meta))
technic.set_charge(itemstack, (power_available - actual_burned) * digtron.config.power_ratio)
end
current_burned = current_burned + actual_burned
end
end

if current_burned > target then
if current_burned >= target then
break
end
end
Expand Down