ExchangeClone/exchangeclone/deconstructor.lua

206 lines
7.4 KiB
Lua
Raw Normal View History

2023-11-19 01:48:35 +01:00
local S = minetest.get_translator()
local formspec =
"size["..(exchangeclone.mcl and 9 or 8)..",9]"..
"label[2,1;"..S("Input").."]"..
"list[context;src;2,2;1,1;]"..
2024-02-12 19:02:52 +01:00
"label[5,1;"..S("Star").."]"..
2023-11-19 01:48:35 +01:00
"list[context;fuel;5,2;1,1;]"..
exchangeclone.inventory_formspec(0,5)..
"listring[current_player;main]"..
"listring[context;src]"..
"listring[current_player;main]"..
"listring[context;fuel]"..
"listring[current_player;main]"..
"listring[context;dst]"
if exchangeclone.mcl then
formspec = formspec..
mcl_formspec.get_itemslot_bg(2,2,1,1)..
mcl_formspec.get_itemslot_bg(5,2,1,1)
2021-08-25 18:01:36 +02:00
end
2023-10-07 17:26:21 +02:00
minetest.register_alias("exchangeclone:element_deconstructor", "exchangeclone:deconstructor")
2023-11-19 01:48:35 +01:00
local function deconstructor_action(pos, elapsed)
2024-02-12 19:02:52 +01:00
local limit
local using_star = true
2023-10-07 17:26:21 +02:00
local player
2021-08-25 18:01:36 +02:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-11-19 01:48:35 +01:00
2024-02-12 19:02:52 +01:00
if minetest.get_item_group(inv:get_stack("fuel", 1):get_name(), "klein_star") < 1 then
2023-10-31 00:54:59 +01:00
limit = exchangeclone.limit
2024-02-12 19:02:52 +01:00
using_star = false
2023-10-07 17:26:21 +02:00
player = minetest.get_player_by_name(meta:get_string("exchangeclone_placer"))
if not (player and player ~= "") then return end
2024-02-12 19:02:52 +01:00
else
limit = exchangeclone.get_star_max(inv:get_stack("fuel", 1))
2023-10-07 17:26:21 +02:00
end
2023-11-19 01:48:35 +01:00
local stack = inv:get_stack("src", 1)
2024-02-14 16:50:53 +01:00
local single_item = stack:peek_item(1)
local individual_emc_value = exchangeclone.get_item_emc(single_item)
if not (individual_emc_value and individual_emc_value > 0) then return end
2023-11-19 01:48:35 +01:00
local current_emc
2024-02-12 19:02:52 +01:00
if using_star then
current_emc = exchangeclone.get_star_emc(inv, "fuel", 1)
2023-10-07 17:26:21 +02:00
else
current_emc = exchangeclone.get_player_emc(player)
2023-10-07 17:26:21 +02:00
end
local max_count = math.floor((limit - current_emc)/individual_emc_value)
local add_count = math.min(max_count, stack:get_count())
local emc = individual_emc_value * add_count
2023-11-19 01:48:35 +01:00
2024-02-12 19:02:52 +01:00
if using_star then
2024-02-14 16:50:53 +01:00
exchangeclone.add_star_emc(inv, "fuel", 1, emc)
2023-10-07 17:26:21 +02:00
else
2024-02-14 16:50:53 +01:00
exchangeclone.add_player_emc(player, emc)
2023-10-07 17:26:21 +02:00
end
stack:set_count(stack:get_count() - add_count)
if stack:get_count() == 0 then stack = ItemStack("") end
inv:set_stack("src", 1, stack)
2023-11-19 01:48:35 +01:00
local timer = minetest.get_node_timer(pos)
if inv:get_stack("src", 1):is_empty() then
timer:stop()
else
if not timer:is_started() then
timer:start(1) -- keep trying to deconstruct if there are items in the src stack
end
end
2021-08-25 18:01:36 +02:00
end
local function on_construct(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("src", 1)
inv:set_size("fuel", 1)
2023-11-19 01:48:35 +01:00
meta:set_string("formspec", formspec)
meta:set_string("infotext", S("Deconstructor"))
deconstructor_action(pos, 0)
2021-08-25 18:01:36 +02:00
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
2023-11-20 18:17:17 +01:00
if player and player.get_player_name and minetest.is_protected(pos, player:get_player_name()) then
2021-08-25 18:40:33 +02:00
return 0
end
if listname == "fuel" then
2024-02-12 19:02:52 +01:00
if minetest.get_item_group(stack:get_name(), "klein_star") > 0 then
2021-08-25 18:01:36 +02:00
return stack:get_count()
else
return 0
end
elseif listname == "src" then
2021-08-25 18:01:36 +02:00
return stack:get_count()
else
return 0
2021-08-25 18:01:36 +02:00
end
end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
2021-08-25 18:40:33 +02:00
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)
2021-08-25 18:01:36 +02:00
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
2021-08-25 18:40:33 +02:00
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
2021-08-25 18:01:36 +02:00
end
minetest.register_node("exchangeclone:deconstructor", {
2023-11-19 01:48:35 +01:00
description = S("Deconstructor"),
2021-12-30 18:51:45 +01:00
tiles = {
2024-02-13 01:25:25 +01:00
"exchangeclone_deconstructor_up.png",
"exchangeclone_deconstructor_down.png",
"exchangeclone_deconstructor_right.png",
"exchangeclone_deconstructor_right.png",
"exchangeclone_deconstructor_right.png",
"exchangeclone_deconstructor_right.png",
2021-12-30 18:51:45 +01:00
},
2023-12-25 16:57:42 +01:00
groups = {cracky = 2, container = exchangeclone.mcl2 and 2 or 4, pickaxey = 2, tubedevice = 1, tubedevice_receiver = 1},
2023-05-26 18:55:55 +02:00
_mcl_hardness = 3,
_mcl_blast_resistance = 6,
sounds = exchangeclone.sound_mod.node_sound_metal_defaults(),
2021-08-25 18:40:33 +02:00
is_ground_content = false,
2023-12-25 16:57:42 +01:00
can_dig = exchangeclone.can_dig,
after_dig_node = exchangeclone.drop_after_dig({"src", "fuel"}),
2023-10-07 17:26:21 +02:00
after_place_node = function(pos, player, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
meta:set_string("exchangeclone_placer", player:get_player_name())
2023-11-20 18:17:17 +01:00
if exchangeclone.pipeworks then
pipeworks.after_place(pos, player, itemstack, pointed_thing)
end
2023-10-07 17:26:21 +02:00
end,
2021-08-25 18:40:33 +02:00
on_construct = on_construct,
on_metadata_inventory_move = deconstructor_action,
on_metadata_inventory_put = deconstructor_action,
2023-10-07 17:26:21 +02:00
on_metadata_inventory_take = function(pos, listname, index, stack, player)
deconstructor_action(pos)
end,
2023-12-25 16:57:42 +01:00
on_blast = exchangeclone.on_blast({"src", "fuel"}),
2021-08-25 18:01:36 +02:00
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-12-25 16:57:42 +01:00
on_timer = deconstructor_action,
2024-02-12 19:02:52 +01:00
_mcl_hoppers_on_try_push = exchangeclone.mcl2_hoppers_on_try_push(nil, function(stack) return minetest.get_item_group(stack:get_name(), "klein_star") > 0 end),
_mcl_hoppers_on_after_push = function(pos)
minetest.get_node_timer(pos):start(1.0)
end,
2023-12-25 16:57:42 +01:00
_on_hopper_in = exchangeclone.mcla_on_hopper_in(
nil,
2024-02-12 19:02:52 +01:00
function(stack) return minetest.get_item_group(stack:get_name(), "klein_star") > 0 end
2023-12-25 16:57:42 +01:00
),
2021-08-25 18:01:36 +02:00
})
2021-08-26 16:10:40 +02:00
2023-11-20 18:17:17 +01:00
if exchangeclone.pipeworks then
local function get_list(direction)
return (direction.y == 0 and "src") or "fuel"
end
minetest.override_item("exchangeclone:deconstructor", {
tube = {
2023-12-25 16:57:42 +01:00
input_inventory = "src",
2023-11-20 18:17:17 +01:00
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()
local result = inv:add_item(get_list(direction), stack)
if result then
deconstructor_action(pos)
end
return result
end,
can_insert = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if get_list(direction) == "fuel" then
2024-02-12 19:02:52 +01:00
if minetest.get_item_group(stack:get_name(), "klein_star") > 0 then
2023-11-20 18:17:17 +01:00
return inv:room_for_item("fuel", stack)
end
else
return inv:room_for_item("src", stack)
end
end,
},
on_rotate = pipeworks.on_rotate,
})
end
local recipe_ingredient = "default:furnace"
2023-09-29 01:23:49 +02:00
if exchangeclone.mcl then
recipe_ingredient = "mcl_furnaces:furnace"
end
2021-08-26 16:10:40 +02:00
minetest.register_craft({
output = "exchangeclone:deconstructor",
2021-08-26 16:10:40 +02:00
recipe = {
2024-02-12 19:02:52 +01:00
{"exchangeclone:klein_star_drei"},
{recipe_ingredient},
2024-02-12 19:02:52 +01:00
{"exchangeclone:klein_star_drei"}
2021-08-26 16:10:40 +02:00
}
})