Browse Source

Added the content

master
Yves Quemener 1 month ago
parent
commit
ce22cad0e5
5 changed files with 164 additions and 2 deletions
  1. 13
    2
      README.md
  2. 147
    0
      init.lua
  3. 4
    0
      mod.conf
  4. BIN
      sc.png
  5. BIN
      textures/quest_chest_lock.png

+ 13
- 2
README.md View File

@@ -1,2 +1,13 @@
# quest_chest
Mod for Minetest: Chests whose content can only be taken from and that offers the same content to each player once.
Chest whose content can only be taken from and that offers the same content to each player once. I wrote this because I made treasure hunting game. I wanted players to receive each a reward for finding the chest, not have a first-arrived-takes-all.

Players are only able to take from it, not put things in, a bit similar to the way bones work.

To fill it, open it with a user who has the 'protection_bypass' privilege. This user will access the default inventory of the chest.

The way this works under the hood is that each chest has an inventory that is only shown to admins with 'protection_bypass'. All others are shown a an inventory page that is actually attached to their player and that is initialized with the content of the default inventory.

Inspired by Megaf's more_chest wifi chest:
https://github.com/minetest-mods/more_chests

I'd love to remove the default dependency but that would require that issue to be fixed first
https://github.com/minetest/minetest/issues/7068

+ 147
- 0
init.lua View File

@@ -0,0 +1,147 @@
local quest_chests_storage = minetest.get_mod_storage()



minetest.register_node("quest_chest:chest", {
description = "Quest Chest",
tiles = {
"default_chest_top.png",
"default_chest_top.png",
"default_chest_side.png",
"default_chest_side.png",
"default_chest_side.png",
"quest_chest_lock.png"
},
paramtype2 = "facedir",
groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,},
legacy_facedir_simple = true,
sounds = default.node_sound_wood_defaults(),
can_dig = function(pos,player)
return minetest.check_player_privs(player:get_player_name(), "protection_bypass")
end,
on_destruct = function(pos)
local meta = minetest.get_meta(pos)
local chestid = meta:get_int("chestid")
print("Destroying chest_"..chestid)
local ids = minetest.deserialize(quest_chests_storage:get_string("ids"))
for index, value in pairs(ids) do
if value == chestid then
table.remove(ids, index)
break
end
end
quest_chests_storage:set_string("ids", minetest.serialize(ids))
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local chestid = quest_chests_storage:get_int("next_id")
meta:set_string("chestid", chestid)
local ids = minetest.deserialize(quest_chests_storage:get_string("ids"))
table.insert(ids, chestid)
quest_chests_storage:set_string("ids", minetest.serialize(ids))
quest_chests_storage:set_int("next_id", chestid+1)
meta:set_string("formspec",
"size[8,9]"..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"list[current_player;quest_chest:chest"..chestid..";0,0.3;8,4;]"..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]" ..
"listring[current_player;quest_chest:chest"..chestid.."]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,4.85))
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
meta:set_string("formspec_admin",
"size[8,9]"..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]" ..
"listring[nodemeta:" .. spos .. ";main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,4.85))
local inv = meta:get_inventory()
inv:set_size("main", 8*2)
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local playername = player:get_player_name()

if minetest.check_player_privs(playername, "protection_bypass") then
return stack:get_count()
else
return 0
end
end,

on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
local playername = clicker:get_player_name()

if minetest.check_player_privs(playername, "protection_bypass") then
minetest.show_formspec(clicker:get_player_name(),
node.name, meta:get_string("formspec_admin"))
else
local inv = clicker:get_inventory()
local chestname = "quest_chest:chest"..meta:get_int("chestid")
if inv:get_size(chestname)==0 then
inv:set_size(chestname, 8)
inv:set_list(chestname, meta:get_inventory():get_list("main"))
end
minetest.show_formspec(clicker:get_player_name(),
node.name, meta:get_string("formspec"))
end

end,

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 quest chest 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 quest chest 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 quest chest at "..minetest.pos_to_string(pos))
end,
})

minetest.register_on_joinplayer(function(player)
--[[ local ids = minetest.deserialize(quest_chests_storage:get_string("ids"))
local inv = player:get_inventory()
print(dump(inv:get_size("quest_chest:chest333")))
for i,chestid in ipairs(ids) do
inv:set_size("quest_chest:chest"..chestid, 8)
end ]]--

minetest.register_on_player_inventory_action(
function(player, action, inventory, inventory_info)
local inv = player:get_inventory()
if inventory_info.from_list and inventory_info.from_index then
local stack = inv:get_stack(
inventory_info.from_list,
inventory_info.from_index
)
end
end)
minetest.register_allow_player_inventory_action(
function(player, action, inventory, inventory_info)
local inv = player:get_inventory()
if inventory_info.from_list and inventory_info.from_index then
local stack = inv:get_stack(
inventory_info.from_list,
inventory_info.from_index
)
if string.sub(inventory_info.to_list,1,11)=="quest_chest" then
return 0
else
return stack:get_count()
end
end
return
end)
end)

+ 4
- 0
mod.conf View File

@@ -0,0 +1,4 @@
name = quest_chest
description = A chest that gives each player the same reward. Can only be taken from.
title = Quest chest
depends = default

BIN
sc.png View File


BIN
textures/quest_chest_lock.png View File


Loading…
Cancel
Save