ExchangeClone/energy_collector.lua

210 lines
6.9 KiB
Lua
Raw Normal View History

local sound_mod = mcl_sounds or default
2023-05-13 21:13:22 +02:00
local 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
stored = stored + exchangeclone.collector_speed
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, 1)
2021-12-30 22:45:58 +01:00
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
2023-05-13 21:13:22 +02:00
minetest.log("blocked put")
2021-12-30 22:45:58 +01:00
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-05-13 21:13:22 +02:00
exchangeclone.get_inventory_drops(pos, "main", drops)
drops[#drops+1] = "exchangeclone:energy_collector"
2021-12-30 22:45:58 +01:00
minetest.remove_node(pos)
2023-05-13 21:13:22 +02:00
2021-12-30 22:45:58 +01:00
return drops
end
2023-05-13 21:13:22 +02:00
local function on_dig_node(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
end
minetest.register_node("exchangeclone:energy_collector", {
2021-12-30 22:45:58 +01:00
description = "Energy Collector",
tiles = {
2023-05-23 16:53:45 +02:00
"exchangeclone_energy_collector_up.png",
"exchangeclone_energy_collector_down.png",
"exchangeclone_energy_collector_right.png",
"exchangeclone_energy_collector_right.png",
"exchangeclone_energy_collector_right.png",
"exchangeclone_energy_collector_right.png"
2021-12-30 22:45:58 +01:00
},
2023-05-26 18:55:55 +02:00
groups = {cracky = 2, container = 2, pickaxey = 2},
_mcl_hardness = 3,
_mcl_blast_resistance = 6,
sounds = sound_mod.node_sound_metal_defaults(),
2021-12-30 22:45:58 +01:00
is_ground_content = false,
can_dig = can_dig,
2021-12-30 22:45:58 +01:00
on_timer = on_timer,
on_construct = on_construct,
2023-05-26 18:55:55 +02:00
after_dig_node = on_dig_node,
2021-12-30 22:45:58 +01:00
on_metadata_inventory_move = function(pos)
minetest.get_node_timer(pos):start(1)
2021-12-30 22:45:58 +01:00
end,
on_metadata_inventory_put = function(pos)
minetest.get_node_timer(pos):start(1)
2021-12-30 22:45:58 +01:00
end,
on_metadata_inventory_take = function(pos)
minetest.get_node_timer(pos):start(1)
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,
})
2023-05-13 21:13:22 +02:00
--[[if minetest.get_modpath("pipeworks") then
minetest.override_item("exchangeclone:energy_collector", {
tube = {
input_inventory = "main",
connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1},
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("main", stack)
end,
can_insert = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if stack:get_name() == "exchangeclone:exchange_orb" then
minetest.log(inv:room_for_item("main", stack))
return inv:room_for_item("main", stack)
else
minetest.log("failed")
end
end,
},
after_place_node = function(pos, placer)
pipeworks.after_place(pos)
end,
after_dig_node = pipeworks.after_dig,
on_rotate = pipeworks.on_rotate,
})
end]]
local recipe_item_1 = "default:steelblock"
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
}
})