-
-
Save GreenXenith/d0cedb7292ca92d57bbde6209ecf0afd to your computer and use it in GitHub Desktop.
stocking.lua (modname is christmas_decor)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local stocking = {} | |
local stuffer = {} | |
local stuffer.stuffers = {} | |
local stuffer.groups = {} | |
function stuffer.register_stuff(name, count) | |
if count == nil then count = 1 end | |
local stuff = { | |
name = name, | |
count = count, | |
metadata = "", | |
} | |
table.insert(stuffer.stuffers, stuff) | |
if stuffer_groups == nil then stuffer_groups = "default" end | |
end | |
function stuffer.select_stuffers(count, stuffer_groups) | |
local p_stuffers = {} | |
for i=1,#stuffer.stuffers do | |
table.insert(p_stuffers, stuffer.stuffers[i]) | |
end | |
local itemstacks = {} | |
for i=1,#stuffer.stuffers do | |
itemstacks[i] = stuffer.stuff_to_itemstack(stuffer.stuffers[i]) | |
end | |
return itemstacks | |
end | |
function stuffer.stuff_to_itemstack(stuff) | |
local itemstack = {} | |
itemstack.name = stuff.name | |
itemstack.count = stuffer.determine_count(stuff) | |
itemstack.metadata = stuff.metadata | |
return ItemStack(itemstack) | |
end | |
function stuffer.determine_count(stuff) | |
if(type(stuff.count)=="number") then | |
return stuff.count | |
end | |
end | |
stuffer.register_stuff("default:cobble",45) --test code | |
function stocking.get_stocking_formspec(pos) | |
local spos = pos.x .. "," .. pos.y .. "," ..pos.z | |
local formspec = | |
"size[9,9]".. | |
"background[-0.8,-0.4;10,10;stocking_bg.png]".. | |
"image_button_exit[7.75,1;1,1;exit_button.png;exit;]".. | |
"listcolors[#D4393C;#d45658]".. | |
"list[nodemeta:".. spos .. ";main;-0.2,2;8,2;]".. | |
"list[current_player;main;-0.2,5;8,4;]" .. | |
"listring[current_player;main]" | |
return formspec | |
end | |
stocking.can_dig_function = function(pos, player) | |
local meta = minetest.get_meta(pos); | |
local name = player and player:get_player_name() | |
local owner = meta:get_string("owner") | |
local inv = meta:get_inventory() | |
return name == owner and inv:is_empty("main") | |
end | |
minetest.register_node("christmas_decor:stocking", { | |
description = "Stocking", | |
drawtype = "mesh", | |
mesh = "stocking.obj", | |
tiles = {"velvet_fluff.png"}, | |
use_texture_alpha = true, | |
inventory_image = "inv_stocking.png", | |
walkable = false, | |
selection_box = { | |
type = "fixed", | |
fixed = {-0.4, -0.5, 0.5, 0.4, 0.5, 0.2}, | |
}, | |
collision_box = { | |
type = "fixed", | |
fixed = {-0.4, -0.5, 0.5, 0.4, 0.5, 0.2}, | |
}, | |
paramtype = "light", | |
sunlight_propagates = true, | |
paramtype2 = "facedir", | |
groups = {snappy = 3}, | |
sounds = default.node_sound_leaves_defaults(), | |
allow_metadata_inventory_put = function(pos, listname, index, stack, player) | |
if player then | |
minetest.chat_send_player(player:get_player_name(), | |
"Wait until Christmas Eve for Santa to fill your stocking!") | |
return 0 | |
end | |
end, | |
on_place = function(itemstack, placer, pointed_thing) | |
if minetest.is_yes(placer:get_attribute("has_placed_stocking")) then | |
minetest.chat_send_player(placer:get_player_name(), | |
"Santa won't fill more than one stocking!") | |
return itemstack | |
else | |
return minetest.item_place(itemstack, placer, pointed_thing) | |
end | |
end, | |
after_place_node = function(pos, placer, itemstack, pointed_thing) | |
local meta = minetest.get_meta(pos) | |
local owner = placer:get_player_name() | |
meta:set_string("owner", owner) | |
meta:set_string("infotext", owner.."'s Stocking") | |
local inv = meta:get_inventory() | |
inv:set_size("main", 8*2) | |
placer:set_attribute("has_placed_stocking", "true") | |
end, | |
on_rightclick = function(pos, node, clicker, itemstack) | |
local meta = minetest.get_meta(pos) | |
local stuffers = stuffer.select_stuffers() | |
if stocking.can_fill = true and meta:get_int("has_filled") == 0 then | |
for i=1, #stuffers do | |
inv:set_stack("main", i, stuffers[i]) | |
end | |
meta:set_int("has_filled", 1) | |
end | |
local player = clicker:get_player_name() | |
local owner = meta:get_string("owner") | |
if owner == player then | |
minetest.show_formspec( | |
clicker:get_player_name(), | |
"default:chest_locked", | |
stocking.get_stocking_formspec(pos)) | |
else | |
return itemstack | |
end | |
return itemstack | |
end, | |
can_dig = stocking.can_dig_function, | |
on_metadata_inventory_move = function(pos, from_list, from_index, | |
to_list, to_index, count, player) | |
minetest.log("action", player:get_player_name() .. | |
" moves stuff in stocking at " .. minetest.pos_to_string(pos)) | |
end, | |
on_metadata_inventory_put = function(pos, listname, index, stack, player) | |
minetest.log("action", player:get_player_name() .. | |
" moves stuff to stocking at " .. minetest.pos_to_string(pos)) | |
end, | |
on_metadata_inventory_take = function(pos, listname, index, stack, player) | |
minetest.log("action", player:get_player_name() .. | |
" takes stuff from stocking at " .. minetest.pos_to_string(pos)) | |
end, | |
on_dig = function(pos, node, digger) | |
if stocking.can_dig_function(pos, digger) then | |
digger:set_attribute("has_placed_stocking", "false") | |
minetest.remove_node(pos) | |
end | |
end | |
}) | |
local stocking.can_fill | |
function stocking.enable_can_fill() | |
stocking.can_fill = true | |
end | |
minetest.register_chatcommand("canfillstockings", { | |
privs = {protection_bypass = true}, | |
func = function(name, param) | |
stocking.enable_can_fill() | |
end | |
}) | |
--[[function stocking.cs(player) | |
if type(player) == "string" then | |
player = minetest.get_player_by_name(player) | |
player:set_attribute("has_placed_stocking", "false") | |
end | |
end | |
minetest.register_chatcommand("cs", { | |
privs = {shout = true}, | |
func = function(name, param) | |
stocking.cs(name) | |
end | |
})]] | |
--[[lowkey when you dont need the lbm >.< | |
minetest.register_lbm({ | |
label = "Fill stockings", | |
name = "christmas_decor:fill_stockings", | |
nodenames = {"christmas_decor:stocking"}, | |
run_at_every_load = false, | |
action = function(pos, node) | |
local meta = minetest.get_meta(pos) | |
local inv = meta:get_inventory() | |
local stuffers = stuffer.select_stuffers() | |
if meta:get_int("filled") == 0 then | |
for i=1, #stuffers do | |
inv:set_stack("main", i, stuffers[i]) | |
end | |
meta:set_int("filled", 1) | |
else | |
return | |
end | |
end, | |
})]] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment