2017-05-16 19:47:12 +02:00
|
|
|
|
2015-09-29 22:32:12 +02:00
|
|
|
--
|
|
|
|
-- Locks mod
|
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
2019-08-28 17:31:41 +02:00
|
|
|
local S = minetest.get_translator("locks")
|
|
|
|
|
2015-10-01 22:36:22 +02:00
|
|
|
locks = {}
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Settings
|
|
|
|
|
2017-06-27 21:13:30 +02:00
|
|
|
local picked_time = tonumber(minetest.settings:get("locks_picked_time")) or 15 -- unlocked for 15 seconds
|
|
|
|
local all_unlocked = minetest.settings:get_bool("locks_all_unlocked")
|
2015-09-30 17:55:54 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- API functions
|
|
|
|
|
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")
|
|
|
|
|
2015-10-02 18:20:11 +02:00
|
|
|
return name == owner
|
2015-10-01 22:07:26 +02:00
|
|
|
end
|
2015-09-30 18:49:31 +02:00
|
|
|
|
2019-09-21 16:33:59 +02:00
|
|
|
function locks.has_owner(meta)
|
|
|
|
return meta:get_string("lock_owner") ~= ""
|
|
|
|
end
|
|
|
|
|
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
|
2015-09-30 17:55:54 +02:00
|
|
|
end
|
2015-09-30 18:49:31 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
local t = minetest.get_gametime()
|
2015-09-30 18:49:31 +02:00
|
|
|
|
2019-09-22 05:20:26 +02:00
|
|
|
local cracked = meta:get_int("lock_cracked")
|
2015-10-01 22:35:20 +02:00
|
|
|
|
2019-09-22 05:20:26 +02:00
|
|
|
if cracked == 1 then
|
2015-10-01 22:07:26 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
2015-09-30 18:49:31 +02:00
|
|
|
end
|
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Items and nodes
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_tool(
|
2015-09-29 22:32:12 +02:00
|
|
|
"locks:pick",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Lock Pick"),
|
2017-05-11 23:03:08 +02:00
|
|
|
|
2015-09-29 22:32:12 +02:00
|
|
|
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)
|
2019-08-30 02:53:58 +02:00
|
|
|
if pointed_thing.type ~= "node" then
|
|
|
|
return itemstack
|
|
|
|
end
|
2019-08-30 03:00:16 +02:00
|
|
|
local pos = pointed_thing.under
|
2019-09-25 21:54:22 +02:00
|
|
|
if minetest.is_protected(pos, player:get_player_name()) and
|
|
|
|
not minetest.check_player_privs(player, "protection_bypass") then
|
|
|
|
minetest.record_protection_violation(pos, player:get_player_name())
|
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
|
2019-08-30 03:00:16 +02:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
if minetest.get_item_group(node.name, "locked") == 0 then
|
|
|
|
return itemstack
|
|
|
|
end
|
2019-09-22 05:26:12 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local cracked = meta:get_int("lock_cracked") == 1
|
|
|
|
if cracked then
|
|
|
|
-- Is already open
|
|
|
|
return itemstack
|
|
|
|
end
|
2019-09-21 19:18:39 +02:00
|
|
|
-- Attempt to pick lock
|
2017-05-11 23:03:08 +02:00
|
|
|
if math.random(1, 5) <= 1 then
|
2019-09-21 19:18:39 +02:00
|
|
|
-- Success!
|
2019-09-22 05:20:26 +02:00
|
|
|
meta:set_int("lock_cracked", 1)
|
|
|
|
local timer = minetest.get_node_timer(pos)
|
|
|
|
-- Unlock node for a limited time
|
|
|
|
timer:start(picked_time)
|
|
|
|
|
|
|
|
local owner = meta:get_string("lock_owner")
|
|
|
|
if owner == "" then
|
|
|
|
meta:set_string("infotext", S("Locked Chest (cracked open)"))
|
|
|
|
else
|
|
|
|
meta:set_string("infotext", S("Locked Chest (cracked open) (Owned by @1)", owner))
|
|
|
|
end
|
2017-05-11 23:03:08 +02:00
|
|
|
|
2019-09-21 19:06:05 +02:00
|
|
|
-- TODO: Add graphical effect to show success
|
2019-08-30 02:52:30 +02:00
|
|
|
|
|
|
|
local burglar = player:get_player_name()
|
|
|
|
if owner then
|
|
|
|
if owner ~= burglar then
|
|
|
|
minetest.chat_send_player(
|
|
|
|
owner,
|
|
|
|
minetest.colorize("#f00",
|
|
|
|
S("@1 has broken into your locked chest!", burglar)))
|
|
|
|
minetest.chat_send_player(
|
|
|
|
burglar,
|
|
|
|
minetest.colorize("#0f0", S("You have broken the lock!")))
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(
|
|
|
|
burglar,
|
|
|
|
minetest.colorize("#0f0", S("You have broken into your own locked chest!")))
|
|
|
|
end
|
|
|
|
else
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.chat_send_player(
|
2019-08-30 02:52:30 +02:00
|
|
|
burglar,
|
|
|
|
minetest.colorize("#0f0", S("You have broken the lock!")))
|
2017-05-11 23:03:08 +02:00
|
|
|
end
|
2019-09-22 03:37:22 +02:00
|
|
|
achievements.trigger_achievement(player, "burglar")
|
2019-09-21 19:18:39 +02:00
|
|
|
minetest.sound_play({name="locks_unlock",gain=0.8},{pos=pos, max_hear_distance=16})
|
|
|
|
else
|
|
|
|
-- Failure!
|
|
|
|
minetest.sound_play({name="locks_pick",gain=0.5},{pos=pos, max_hear_distance=16})
|
2017-05-11 23:03:08 +02:00
|
|
|
end
|
|
|
|
|
2019-08-31 00:07:26 +02:00
|
|
|
if not minetest.settings:get_bool("creative_mode") then
|
|
|
|
itemstack:add_wear(8200) -- about 8 uses
|
|
|
|
end
|
2017-05-11 23:03:08 +02:00
|
|
|
return itemstack
|
|
|
|
end,
|
|
|
|
})
|
2015-09-30 17:55:54 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_craftitem(
|
2015-09-30 18:58:28 +02:00
|
|
|
"locks:lock",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Lock"),
|
2015-09-30 18:58:28 +02:00
|
|
|
|
|
|
|
inventory_image = "locks_lock.png",
|
|
|
|
wield_image = "locks_lock.png",
|
2017-05-11 23:03:08 +02:00
|
|
|
})
|
2015-09-30 18:58:28 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_node(
|
2015-09-30 17:55:54 +02:00
|
|
|
"locks:chest",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Locked Chest"),
|
2017-05-17 17:57:14 +02:00
|
|
|
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"
|
|
|
|
},
|
2015-09-30 17:55:54 +02:00
|
|
|
paramtype2 = "facedir",
|
2019-08-30 03:00:16 +02:00
|
|
|
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 2, locked = 1},
|
2015-09-30 17:55:54 +02:00
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
2015-09-30 18:49:31 +02:00
|
|
|
on_construct = function(pos)
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2019-09-22 05:20:26 +02:00
|
|
|
meta:set_int("lock_cracked", 0)
|
2019-09-21 16:33:59 +02:00
|
|
|
meta:set_string("infotext", S("Locked Chest"))
|
2017-05-11 23:03:08 +02:00
|
|
|
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("main", 8 * 4)
|
|
|
|
end,
|
2015-09-30 17:55:54 +02:00
|
|
|
after_place_node = function(pos, player)
|
2017-05-11 23:03:08 +02:00
|
|
|
local name = player:get_player_name()
|
2015-09-30 17:55:54 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2019-08-28 17:31:41 +02:00
|
|
|
meta:set_string("infotext", S("Locked Chest (Owned by @1)", name))
|
2017-05-11 23:03:08 +02:00
|
|
|
meta:set_string("lock_owner", name)
|
|
|
|
end,
|
2015-10-01 22:07:26 +02:00
|
|
|
on_rightclick = function(pos, node, player)
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2017-05-11 23:03:08 +02:00
|
|
|
|
|
|
|
if not locks.is_locked(meta, player) then
|
|
|
|
local np = pos.x .. "," .. pos.y .. "," .. pos.z
|
2017-05-13 20:50:15 +02:00
|
|
|
local form = default.ui.get_page("default:2part")
|
2017-05-11 23:03:08 +02:00
|
|
|
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)
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.show_formspec(
|
2017-05-11 23:03:08 +02:00
|
|
|
player:get_player_name(),
|
|
|
|
"default_chest",
|
|
|
|
form
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end,
|
2019-09-22 05:20:26 +02:00
|
|
|
on_timer = function(pos, elapsed)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:set_int("lock_cracked", 0)
|
|
|
|
local owner = meta:get_string("lock_owner")
|
|
|
|
if owner == "" then
|
|
|
|
meta:set_string("infotext", S("Locked Chest"))
|
|
|
|
else
|
|
|
|
meta:set_string("infotext", S("Locked Chest (Owned by @1)", owner))
|
|
|
|
end
|
|
|
|
end,
|
2015-09-30 18:49:31 +02:00
|
|
|
allow_metadata_inventory_move = function(pos, from_l, from_i, to_l, to_i, cnt, player)
|
2019-09-25 21:54:22 +02:00
|
|
|
if minetest.is_protected(pos, player:get_player_name()) and
|
|
|
|
not minetest.check_player_privs(player, "protection_bypass") then
|
|
|
|
minetest.record_protection_violation(pos, player:get_player_name())
|
|
|
|
return 0
|
|
|
|
end
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2017-05-11 23:03:08 +02:00
|
|
|
if locks.is_locked(meta, player) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return cnt
|
|
|
|
end,
|
2015-09-30 18:49:31 +02:00
|
|
|
allow_metadata_inventory_put = function(pos, listname, index, itemstack, player)
|
2019-09-25 21:54:22 +02:00
|
|
|
if minetest.is_protected(pos, player:get_player_name()) and
|
|
|
|
not minetest.check_player_privs(player, "protection_bypass") then
|
|
|
|
minetest.record_protection_violation(pos, player:get_player_name())
|
|
|
|
return 0
|
|
|
|
end
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2017-05-11 23:03:08 +02:00
|
|
|
if locks.is_locked(meta, player) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return itemstack:get_count()
|
|
|
|
end,
|
2015-09-30 18:49:31 +02:00
|
|
|
allow_metadata_inventory_take = function(pos, listname, index, itemstack, player)
|
2019-09-25 21:54:22 +02:00
|
|
|
if minetest.is_protected(pos, player:get_player_name()) and
|
|
|
|
not minetest.check_player_privs(player, "protection_bypass") then
|
|
|
|
minetest.record_protection_violation(pos, player:get_player_name())
|
|
|
|
return 0
|
|
|
|
end
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2017-05-11 23:03:08 +02:00
|
|
|
if locks.is_locked(meta, player) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return itemstack:get_count()
|
|
|
|
end,
|
2015-09-30 17:55:54 +02:00
|
|
|
can_dig = function(pos, player)
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2017-05-11 23:03:08 +02:00
|
|
|
local inv = meta:get_inventory()
|
2019-09-21 16:33:59 +02:00
|
|
|
return inv:is_empty("main") and (locks.is_owner(meta, player) or (not locks.has_owner(meta)))
|
2017-05-11 23:03:08 +02:00
|
|
|
end,
|
2015-10-19 00:24:47 +02:00
|
|
|
write_name = function(pos, text)
|
2017-05-12 04:29:55 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2017-05-11 23:03:08 +02:00
|
|
|
|
2017-05-17 17:57:14 +02:00
|
|
|
if text == "" then
|
2019-08-28 17:31:41 +02:00
|
|
|
meta:set_string("infotext", S("Locked Chest (Owned by @1)",
|
|
|
|
meta:get_string("lock_owner")))
|
2017-05-11 23:03:08 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
meta:set_string("infotext", S("@1 (Owned by @2)",
|
|
|
|
text, meta:get_string("lock_owner")))
|
2017-05-11 23:03:08 +02:00
|
|
|
end
|
|
|
|
end,
|
2015-09-30 18:49:31 +02:00
|
|
|
on_blast = function() end,
|
2017-05-11 23:03:08 +02:00
|
|
|
})
|
2015-09-30 18:58:28 +02:00
|
|
|
|
2017-05-16 19:47:12 +02:00
|
|
|
-- Crafting
|
|
|
|
|
|
|
|
crafting.register_craft(
|
|
|
|
{
|
|
|
|
output = "locks:pick",
|
|
|
|
items = {
|
2017-05-17 17:57:14 +02:00
|
|
|
"default:ingot_steel 2",
|
2017-05-16 19:47:12 +02:00
|
|
|
"default:stick 3",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
crafting.register_craft(
|
|
|
|
{
|
|
|
|
output = "locks:lock",
|
|
|
|
items = {
|
|
|
|
"default:ingot_steel 3",
|
|
|
|
"group:planks 2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
crafting.register_craft(
|
2015-09-30 18:58:28 +02:00
|
|
|
{
|
|
|
|
output = "locks:chest",
|
2017-05-16 19:47:12 +02:00
|
|
|
items = {
|
|
|
|
"default:chest",
|
|
|
|
"locks:lock",
|
|
|
|
},
|
2017-05-11 23:03:08 +02:00
|
|
|
})
|
2015-09-30 18:58:28 +02:00
|
|
|
|
2015-10-25 22:18:54 +01:00
|
|
|
-- Achievements
|
|
|
|
|
|
|
|
achievements.register_achievement(
|
|
|
|
"locksmith",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
title = S("Locksmith"),
|
2019-09-02 14:54:29 +02:00
|
|
|
description = S("Craft a lock."),
|
2015-10-25 22:18:54 +01:00
|
|
|
times = 1,
|
|
|
|
craftitem = "locks:lock",
|
2017-05-11 23:03:08 +02:00
|
|
|
})
|
2015-10-25 22:18:54 +01:00
|
|
|
|
|
|
|
achievements.register_achievement(
|
|
|
|
"burglar",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
title = S("Burglar"),
|
2019-09-22 03:37:22 +02:00
|
|
|
description = S("Break into a locked chest."),
|
2015-10-25 22:18:54 +01:00
|
|
|
times = 1,
|
2017-05-11 23:03:08 +02:00
|
|
|
})
|
2015-10-25 22:18:54 +01:00
|
|
|
|
2017-05-11 23:03:08 +02:00
|
|
|
default.log("mod:locks", "loaded")
|