mirror of
https://github.com/OgelGames/powerbanks.git
synced 2024-07-15 04:19:28 +02:00
code tidy
This commit is contained in:
parent
cc630c9539
commit
25222f0fd1
50
init.lua
50
init.lua
@ -23,9 +23,7 @@ local function is_chargeable(stack)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function register_powerbank(data)
|
local base_formspec =
|
||||||
|
|
||||||
local formspec =
|
|
||||||
"size[8,6]" ..
|
"size[8,6]" ..
|
||||||
"list[current_name;main;0,0.25;4,1;]" ..
|
"list[current_name;main;0,0.25;4,1;]" ..
|
||||||
"list[current_player;main;0,2;8,4;]" ..
|
"list[current_player;main;0,2;8,4;]" ..
|
||||||
@ -33,28 +31,27 @@ local function register_powerbank(data)
|
|||||||
"listring[current_player;main]" ..
|
"listring[current_player;main]" ..
|
||||||
"image[5.4,0.2;3,1;powerbanks_battery_bg.png]"
|
"image[5.4,0.2;3,1;powerbanks_battery_bg.png]"
|
||||||
|
|
||||||
local function update_formspec(pos, charge)
|
local function update_formspec(pos, charge, data)
|
||||||
local fraction = charge / data.max_charge
|
local fraction = charge / data.max_charge
|
||||||
|
|
||||||
local red = math.min(510 - (510 * fraction), 255)
|
local red = math.min(510 - (510 * fraction), 255)
|
||||||
local green = math.min(510 * fraction, 255)
|
local green = math.min(510 * fraction, 255)
|
||||||
local color = "#"..string.format("%02X", red)..string.format("%02X", green).."00FF"
|
local color = "#"..string.format("%02X", red)..string.format("%02X", green).."00FF"
|
||||||
|
|
||||||
local new_formspec = formspec..
|
local new_formspec = base_formspec.."box[5.45,0.25;"..(fraction * 2.12)..",0.8;"..color.."]"
|
||||||
"box[5.45,0.25;"..(fraction * 2.12)..",0.8;"..color.."]"
|
|
||||||
|
|
||||||
minetest.get_meta(pos):set_string("formspec", new_formspec)
|
minetest.get_meta(pos):set_string("formspec", new_formspec)
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function update_infotext(pos, is_charging)
|
local function update_infotext(pos, is_charging, data)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local current_charge = technic.EU_string(meta:get_int("charge"))
|
local current_charge = technic.EU_string(meta:get_int("charge"))
|
||||||
local max_charge = technic.EU_string(data.max_charge)
|
local max_charge = technic.EU_string(data.max_charge)
|
||||||
|
|
||||||
local status = "Idle"
|
local status = "Idle"
|
||||||
if is_charging then
|
if is_charging then
|
||||||
status = "Charging"
|
status = "Charging"
|
||||||
end
|
end
|
||||||
|
|
||||||
local infotext = "Powerbank Mk"..data.mark..": "..current_charge.." / "..max_charge.." "..status
|
local infotext = "Powerbank Mk"..data.mark..": "..current_charge.." / "..max_charge.." "..status
|
||||||
meta:set_string("infotext", infotext)
|
meta:set_string("infotext", infotext)
|
||||||
end
|
end
|
||||||
@ -78,7 +75,7 @@ local function register_powerbank(data)
|
|||||||
return item, powerbank_charge, (item_charge == item_max_charge)
|
return item, powerbank_charge, (item_charge == item_max_charge)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function do_charging(pos, charge_step)
|
local function do_charging(pos, charge_step, data)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local current_charge = meta:get_int("charge")
|
local current_charge = meta:get_int("charge")
|
||||||
local inv = meta:get_inventory()
|
local inv = meta:get_inventory()
|
||||||
@ -98,35 +95,32 @@ local function register_powerbank(data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
meta:set_int("charge", current_charge)
|
meta:set_int("charge", current_charge)
|
||||||
update_infotext(pos, still_charging)
|
update_infotext(pos, still_charging, data)
|
||||||
update_formspec(pos, current_charge)
|
update_formspec(pos, current_charge, data)
|
||||||
|
|
||||||
return still_charging and (current_charge > 0)
|
return still_charging and (current_charge > 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_itemstack(metadata, is_node)
|
local function create_itemstack(metadata, is_node, data)
|
||||||
if not metadata.charge then
|
if not metadata.charge then
|
||||||
metadata.charge = 0
|
metadata.charge = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
local extension = ""
|
local extension = ""
|
||||||
if is_node then
|
if is_node then
|
||||||
extension = "_node"
|
extension = "_node"
|
||||||
end
|
end
|
||||||
|
|
||||||
local itemstack = ItemStack({
|
local itemstack = ItemStack({
|
||||||
name = "powerbanks:powerbank_mk"..data.mark..extension,
|
name = "powerbanks:powerbank_mk"..data.mark..extension,
|
||||||
count = 1,
|
count = 1,
|
||||||
metadata = minetest.serialize({charge = metadata.charge})
|
metadata = minetest.serialize({charge = metadata.charge})
|
||||||
})
|
})
|
||||||
|
|
||||||
if not is_node then
|
if not is_node then
|
||||||
technic.set_RE_wear(itemstack, metadata.charge, data.max_charge)
|
technic.set_RE_wear(itemstack, metadata.charge, data.max_charge)
|
||||||
end
|
end
|
||||||
|
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function register_powerbank(data)
|
||||||
minetest.register_node("powerbanks:powerbank_mk"..data.mark.."_node", {
|
minetest.register_node("powerbanks:powerbank_mk"..data.mark.."_node", {
|
||||||
description = "Powerbank Mk"..data.mark.." Node",
|
description = "Powerbank Mk"..data.mark.." Node",
|
||||||
tiles = {
|
tiles = {
|
||||||
@ -140,7 +134,6 @@ local function register_powerbank(data)
|
|||||||
groups = {oddly_breakable_by_hand = 1, not_in_creative_inventory = 1},
|
groups = {oddly_breakable_by_hand = 1, not_in_creative_inventory = 1},
|
||||||
is_ground_content = false,
|
is_ground_content = false,
|
||||||
drop = {},
|
drop = {},
|
||||||
|
|
||||||
can_dig = function(pos, digger)
|
can_dig = function(pos, digger)
|
||||||
if not digger then return end
|
if not digger then return end
|
||||||
|
|
||||||
@ -160,7 +153,6 @@ local function register_powerbank(data)
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
|
|
||||||
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
||||||
if is_owner(pos, player) then
|
if is_owner(pos, player) then
|
||||||
return count
|
return count
|
||||||
@ -179,40 +171,34 @@ local function register_powerbank(data)
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end,
|
end,
|
||||||
|
|
||||||
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
||||||
local node_meta = minetest.get_meta(pos)
|
local node_meta = minetest.get_meta(pos)
|
||||||
local itemstack_meta = minetest.deserialize(itemstack:get_metadata()) or {}
|
local itemstack_meta = minetest.deserialize(itemstack:get_metadata()) or {}
|
||||||
|
|
||||||
if not itemstack_meta.charge then
|
if not itemstack_meta.charge then
|
||||||
itemstack_meta.charge = 0
|
itemstack_meta.charge = 0 -- set default charge (in case node was obtained with /give)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- set node metadata
|
|
||||||
node_meta:get_inventory():set_size("main", data.charging_slots)
|
node_meta:get_inventory():set_size("main", data.charging_slots)
|
||||||
node_meta:set_string("formspec", formspec)
|
node_meta:set_string("formspec", formspec)
|
||||||
node_meta:set_string("owner", placer:get_player_name())
|
node_meta:set_string("owner", placer:get_player_name())
|
||||||
node_meta:set_int("charge", itemstack_meta.charge)
|
node_meta:set_int("charge", itemstack_meta.charge)
|
||||||
|
|
||||||
update_infotext(pos, false)
|
update_infotext(pos, false, data)
|
||||||
update_formspec(pos, itemstack_meta.charge)
|
update_formspec(pos, itemstack_meta.charge, data)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
on_metadata_inventory_put = function(pos, listname, index, stack, player)
|
on_metadata_inventory_put = function(pos, listname, index, stack, player)
|
||||||
local timer = minetest.get_node_timer(pos)
|
local timer = minetest.get_node_timer(pos)
|
||||||
if not timer:is_started() then
|
if not timer:is_started() then
|
||||||
timer:start(charge_time) -- start charging item immediately
|
timer:start(charge_time) -- start charging item immediately
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
on_timer = function(pos, elapsed)
|
on_timer = function(pos, elapsed)
|
||||||
local steps = math.floor((elapsed / charge_time) + 0.5)
|
local steps = math.floor((elapsed / charge_time) + 0.5)
|
||||||
return do_charging(pos, steps * data.charge_step)
|
return do_charging(pos, steps * data.charge_step, data)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
after_dig_node = function(pos, node, metadata, player)
|
after_dig_node = function(pos, node, metadata, player)
|
||||||
-- create item to give player
|
-- create item to give player
|
||||||
local item = create_itemstack({charge = metadata.fields.charge}, false)
|
local item = create_itemstack({charge = metadata.fields.charge}, false, data)
|
||||||
|
|
||||||
-- give the item, or drop if inventory is full
|
-- give the item, or drop if inventory is full
|
||||||
local player_inv = player:get_inventory()
|
local player_inv = player:get_inventory()
|
||||||
@ -234,11 +220,10 @@ local function register_powerbank(data)
|
|||||||
stack_max = 1,
|
stack_max = 1,
|
||||||
wear_represents = "technic_RE_charge",
|
wear_represents = "technic_RE_charge",
|
||||||
on_refill = technic.refill_RE_charge,
|
on_refill = technic.refill_RE_charge,
|
||||||
|
|
||||||
on_place = function(itemstack, placer, pointed_thing)
|
on_place = function(itemstack, placer, pointed_thing)
|
||||||
-- create fake itemstack of node to place
|
-- create fake itemstack of node to place
|
||||||
local item_meta = minetest.deserialize(itemstack:get_metadata()) or {}
|
local item_meta = minetest.deserialize(itemstack:get_metadata()) or {}
|
||||||
local node_itemstack = create_itemstack(item_meta, true)
|
local node_itemstack = create_itemstack(item_meta, true, data)
|
||||||
|
|
||||||
-- place node like player
|
-- place node like player
|
||||||
local _, placed = minetest.item_place(node_itemstack, placer, pointed_thing)
|
local _, placed = minetest.item_place(node_itemstack, placer, pointed_thing)
|
||||||
@ -247,7 +232,6 @@ local function register_powerbank(data)
|
|||||||
if placed then
|
if placed then
|
||||||
itemstack:clear()
|
itemstack:clear()
|
||||||
end
|
end
|
||||||
|
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user