Camas/mods/locks/init.lua

186 lines
4.9 KiB
Lua
Raw Normal View History

2015-09-29 22:32:12 +02:00
--
-- Locks mod
-- By Kaadmy, for Pixture
--
2015-10-01 22:36:22 +02:00
locks = {}
2015-10-01 22:35:20 +02:00
local picked_time = tonumber(minetest.setting_get("locks_picked_time")) or 15 -- unlocked for 15 seconds
local all_unlocked = minetest.setting_getbool("locks_all_unlocked")
2015-10-01 22:07:26 +02:00
function locks.is_owner(meta, player)
local name = player:get_player_name()
local owner = meta:get_string("lock_owner")
return name == owner
2015-10-01 22:07:26 +02:00
end
2015-09-30 18:49:31 +02:00
2015-10-01 22:07:26 +02:00
function locks.is_locked(meta, player)
2015-09-30 18:49:31 +02:00
if all_unlocked then
return false
end
2015-10-01 22:07:26 +02:00
if locks.is_owner(meta, player) then
2015-09-30 18:49:31 +02:00
return false
end
2015-09-30 18:49:31 +02:00
2015-10-01 22:07:26 +02:00
local t = minetest.get_gametime()
2015-09-30 18:49:31 +02:00
2015-10-01 22:07:26 +02:00
local lp = meta:get_float("last_lock_pick")
2015-10-01 22:35:20 +02:00
if lp == -1 or lp == nil then
lp = -1
2015-10-01 22:07:26 +02:00
end
2015-10-01 22:35:20 +02:00
if lp > t then
2015-10-01 22:07:26 +02:00
return false
else
meta:set_float("last_lock_pick", -1)
end
return true
2015-09-30 18:49:31 +02:00
end
minetest.register_tool(
2015-09-29 22:32:12 +02:00
"locks:pick",
{
description = "Lock Pick",
inventory_image = "locks_pick.png",
wield_image = "locks_pick.png",
2015-09-30 18:49:31 +02:00
stack_max = 1,
on_use = function(itemstack, player, pointed_thing)
if math.random(1, 5) <= 1 then
local pos = pointed_thing.under
local meta = minetest.get_meta(pos)
2015-10-01 22:35:20 +02:00
meta:set_float("last_lock_pick", minetest.get_gametime() + picked_time)
2015-09-30 18:49:31 +02:00
local own = meta:get_string("lock_owner")
if own then
minetest.chat_send_player(
own,
player:get_player_name() .. " has broken into your locked chest!"
)
end
end
itemstack:add_wear(8200) -- about 8 uses
return itemstack
2015-09-29 22:32:12 +02:00
end,
})
minetest.register_craftitem(
"locks:lock",
{
description = "Lock",
inventory_image = "locks_lock.png",
wield_image = "locks_lock.png",
})
minetest.register_craft(
{
output = "locks:pick",
recipe = {
{"", "", "default:ingot_steel"},
{"", "default:stick", ""},
{"default:stick", "", ""},
},
})
minetest.register_craft(
{
output = "locks:lock",
recipe = {
{"", "default:ingot_steel", ""},
{"default:ingot_steel", "", "default:ingot_steel"},
{"group:planks", "group:planks", "group:planks"},
},
})
minetest.register_node(
"locks:chest",
{
description = "Locked Chest",
tiles ={"default_chest_top.png", "default_chest_top.png", "default_chest_sides.png",
"default_chest_sides.png", "default_chest_sides.png", "locks_chest_front.png"},
paramtype2 = "facedir",
2015-09-30 18:49:31 +02:00
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
2015-09-30 18:49:31 +02:00
on_construct = function(pos)
2015-10-01 22:07:26 +02:00
local meta = minetest.get_meta(pos)
meta:set_float("last_lock_pick", -1)
local inv = meta:get_inventory()
inv:set_size("main", 8 * 4)
2015-09-30 18:49:31 +02:00
end,
after_place_node = function(pos, player)
2015-09-30 18:49:31 +02:00
local name = player:get_player_name()
2015-09-30 18:49:31 +02:00
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Locked Chest (Owned by " .. name .. ")")
meta:set_string("lock_owner", name)
end,
2015-10-01 22:07:26 +02:00
on_rightclick = function(pos, node, player)
2015-09-30 18:49:31 +02:00
local meta = minetest.get_meta(pos)
2015-10-01 22:35:20 +02:00
2015-09-30 18:49:31 +02:00
if not locks.is_locked(meta, player) then
2015-10-01 22:35:20 +02:00
local np = pos.x .. "," .. pos.y .. "," .. pos.z
local form = default.ui.get_page("core_2part")
form = form .. "list[nodemeta:" .. np .. ";main;0.25,0.25;8,4;]"
form = form .. "listring[nodemeta:" .. np .. ";main]"
form = form .. default.ui.get_itemslot_bg(0.25, 0.25, 8, 4)
form = form .. "list[current_player;main;0.25,4.75;8,4;]"
form = form .. "listring[current_player;main]"
form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
2015-09-30 18:49:31 +02:00
minetest.show_formspec(
player:get_player_name(),
"default_chest",
2015-10-01 22:35:20 +02:00
form
2015-09-30 18:49:31 +02:00
)
end
end,
allow_metadata_inventory_move = function(pos, from_l, from_i, to_l, to_i, cnt, player)
local meta = minetest.get_meta(pos)
if locks.is_locked(meta, player) then
return 0
end
2015-10-01 21:15:24 +02:00
return cnt
2015-09-30 18:49:31 +02:00
end,
allow_metadata_inventory_put = function(pos, listname, index, itemstack, player)
local meta = minetest.get_meta(pos)
if locks.is_locked(meta, player) then
return 0
end
2015-10-01 22:07:26 +02:00
return itemstack:get_count()
2015-09-30 18:49:31 +02:00
end,
allow_metadata_inventory_take = function(pos, listname, index, itemstack, player)
local meta = minetest.get_meta(pos)
if locks.is_locked(meta, player) then
return 0
end
2015-10-01 21:15:24 +02:00
return itemstack:get_count()
2015-09-30 18:49:31 +02:00
end,
can_dig = function(pos, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:is_empty("main") and locks.is_owner(meta, player)
end,
2015-09-30 18:49:31 +02:00
on_blast = function() end,
})
minetest.register_craft(
{
output = "locks:chest",
type = "shapeless",
recipe = {"default:chest", "locks:lock"},
})
2015-10-01 21:17:08 +02:00
default.log("mod:locks", "loaded")