ExchangeClone/energy_collector.lua

171 lines
5.6 KiB
Lua
Raw Normal View History

2021-12-30 22:45:58 +01:00
function get_energy_collector_formspec()
2023-03-26 00:03:03 +01:00
if not exchangeclone.mineclone then
local formspec = {
"size[8,9]",
"label[3,2;Orb]",
2023-03-26 16:43:44 +02:00
"list[context;main;4,2;1,1;]",
2023-03-26 00:03:03 +01:00
"list[current_player;main;0,5;8,4;]",
2023-03-27 01:14:32 +02:00
"listring[current_player;main]",
"listring[context;main]"
2023-03-26 00:03:03 +01:00
}
return table.concat(formspec, "")
else
local formspec = {
"size[9,10]",
2023-03-26 00:03:03 +01:00
"label[3,2;Orb]",
2023-03-26 16:43:44 +02:00
"list[context;main;4,2;1,1;]",
2023-03-26 00:03:03 +01:00
mcl_formspec.get_itemslot_bg(4,2,1,1),
"list[current_player;main;0,5;9,3;9]",
mcl_formspec.get_itemslot_bg(0,5,9,3),
"list[current_player;main;0,8.5;9,1;]",
mcl_formspec.get_itemslot_bg(0,8.5,9,1),
2023-03-26 00:03:03 +01:00
"listring[current_player;main]",
2023-03-26 16:43:44 +02:00
"listring[context;main]"
2023-03-26 00:03:03 +01:00
}
return table.concat(formspec, "")
2023-03-26 00:03:03 +01:00
end
2021-12-30 22:45:58 +01:00
end
local function can_dig(pos, player)
if exchangeclone.mineclone then return true end
2021-12-30 22:45:58 +01:00
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
2023-03-26 16:43:44 +02:00
return inv:is_empty("main")
2021-12-30 22:45:58 +01:00
end
local function on_timer(pos, elapsed)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
-- get block above
pos.y = pos.y + 1
2023-03-26 16:43:44 +02:00
if inv:is_empty("main") then
2021-12-30 22:45:58 +01:00
-- stop timer
minetest.get_node_timer(pos):stop()
return false
end
if minetest.get_natural_light(pos) == 15 then
2023-03-26 16:43:44 +02:00
local dest_orb = inv:get_stack("main", 1)
local stored = dest_orb:get_meta():get_float("stored_charge") or 0
2021-12-30 22:45:58 +01:00
stored = stored + 1
dest_orb:get_meta():set_float("stored_charge", stored)
2023-03-27 00:50:36 +02:00
dest_orb:get_meta():set_string("description", "Exchange Orb\nCurrent Charge: "..tostring(stored))
2023-03-26 16:43:44 +02:00
inv:set_stack("main", 1, dest_orb)
2021-12-30 22:45:58 +01:00
end
return true
end
local function on_construct(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-03-26 16:43:44 +02:00
inv:set_size("main", 1)
2021-12-30 22:45:58 +01:00
meta:set_string("formspec", get_energy_collector_formspec())
meta:set_string("infotext", "Energy Collector")
on_timer(pos, 0)
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-03-26 16:43:44 +02:00
if listname == "main" then
if stack:get_name() == "exchangeclone:exchange_orb" then
2021-12-30 22:45:58 +01:00
return stack:get_count()
else
return 0
end
end
end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end
local function on_blast(pos)
local drops = {}
2023-03-26 16:43:44 +02:00
default.get_inventory_drops(pos, "main", drops)
drops[#drops+1] = "exchangeclone:energy_collector"
2021-12-30 22:45:58 +01:00
minetest.remove_node(pos)
return drops
end
minetest.register_node("exchangeclone:energy_collector", {
2021-12-30 22:45:58 +01:00
description = "Energy Collector",
tiles = {
"ee_energy_collector_up.png",
"ee_energy_collector_down.png",
"ee_energy_collector_right.png",
"ee_energy_collector_right.png",
"ee_energy_collector_right.png",
"ee_energy_collector_right.png"
},
2023-03-26 16:43:44 +02:00
groups = {cracky = 2, container = 2},
2021-12-30 22:45:58 +01:00
is_ground_content = false,
can_dig = can_dig,
2023-03-27 00:19:19 +02:00
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if exchangeclone.mineclone then
local meta = minetest.get_meta(pos)
local meta2 = meta:to_table()
meta:from_table(oldmetadata)
local inv = meta:get_inventory()
for _, listname in ipairs({"main"}) do
local stack = inv:get_stack(listname, 1)
if not stack:is_empty() then
local p = {x=pos.x+math.random(0, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5}
minetest.add_item(p, stack)
end
end
meta:from_table(meta2)
end
2023-03-27 00:19:19 +02:00
end,
2021-12-30 22:45:58 +01:00
on_timer = on_timer,
on_construct = on_construct,
on_metadata_inventory_move = function(pos)
minetest.get_node_timer(pos):start(exchangeclone.collector_interval)
2021-12-30 22:45:58 +01:00
end,
on_metadata_inventory_put = function(pos)
minetest.get_node_timer(pos):start(exchangeclone.collector_interval)
2021-12-30 22:45:58 +01:00
end,
on_metadata_inventory_take = function(pos)
minetest.get_node_timer(pos):start(exchangeclone.collector_interval)
2021-12-30 22:45:58 +01:00
end,
on_blast = on_blast,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take,
})
local recipe_item_1 = "default:steel_block"
local recipe_item_2 = "default:obsidian_glass"
local recipe_item_3 = "default:chest"
if exchangeclone.mineclone then
recipe_item_1 = "mcl_core:ironblock"
recipe_item_2 = "mcl_core:glass"
recipe_item_3 = "mcl_chests:chest"
end
2021-12-30 22:45:58 +01:00
minetest.register_craft({
type = "shaped",
output = "exchangeclone:energy_collector",
2021-12-30 22:45:58 +01:00
recipe = {
{recipe_item_2, recipe_item_2, recipe_item_2},
{"exchangeclone:exchange_orb", recipe_item_3, "exchangeclone:exchange_orb"},
{recipe_item_1, recipe_item_1, recipe_item_1}
2021-12-30 22:45:58 +01:00
}
})