forked from Mirrorlandia_minetest/emeraldbank
117 lines
3.8 KiB
Lua
117 lines
3.8 KiB
Lua
|
|
||
|
|
||
|
-- Copyright (C) 2021 Sandro Del Toro De Ana
|
||
|
|
||
|
-- This file is part of Emeraldbank Minetest Mod.
|
||
|
|
||
|
-- Emeraldbank is free software: you can redistribute it and/or modify
|
||
|
-- it under the terms of the GNU General Public License as published by
|
||
|
-- the Free Software Foundation, either version 3 of the License, or
|
||
|
-- any later version.
|
||
|
|
||
|
-- Emeraldbank is distributed in the hope that it will be useful,
|
||
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
-- GNU General Public License for more details.
|
||
|
|
||
|
-- You should have received a copy of the GNU General Public License
|
||
|
-- along with Emeraldbank. If not, see <https://www.gnu.org/licenses/>.
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
local S = core.get_translator(core.get_current_modname())
|
||
|
|
||
|
local bankcraft = core.settings:get_bool("emeraldbank_craft")
|
||
|
|
||
|
emeraldbank = {}
|
||
|
|
||
|
|
||
|
function emeraldbank.keep(player, itemstack, num)
|
||
|
local player_meta = player:get_meta()
|
||
|
local bankemeralds = player_meta:get_int("emeraldbank:emerald")
|
||
|
local name = player:get_player_name()
|
||
|
itemstack:take_item(num)
|
||
|
player_meta:set_int("emeraldbank:emerald", bankemeralds+num)
|
||
|
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds+num) )
|
||
|
end
|
||
|
|
||
|
function emeraldbank.take(player, num)
|
||
|
local player_meta = player:get_meta()
|
||
|
local bankemeralds = player_meta:get_int("emeraldbank:emerald")
|
||
|
local name = player:get_player_name()
|
||
|
local pos = player:get_pos()
|
||
|
if bankemeralds-num >= 0 then
|
||
|
player_meta:set_int("emeraldbank:emerald", bankemeralds-num)
|
||
|
core.add_item(pos, "mcl_core:emerald")
|
||
|
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds-num) )
|
||
|
return true
|
||
|
end
|
||
|
core.chat_send_player(name, S("Not enough Emeralds in your account") )
|
||
|
return false
|
||
|
end
|
||
|
|
||
|
|
||
|
-- register bank node
|
||
|
core.register_node("emeraldbank:bank", {
|
||
|
description = S("Emerald Bank"),
|
||
|
_doc_items_longdesc = S("This block can keep your emeralds."),
|
||
|
is_ground_content = false,
|
||
|
tiles = {"default_steel_block.png", "default_steel_block.png", "default_steel_block.png^mcl_core_emerald.png"},
|
||
|
stack_max = 64,
|
||
|
groups = {handy=1, building_block=1, enderman_takable=1},
|
||
|
sounds = mcl_sounds.node_sound_metal_defaults(),
|
||
|
_mcl_blast_resistance = 5,
|
||
|
_mcl_hardness = 1,
|
||
|
|
||
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||
|
local wielditem = clicker:get_wielded_item()
|
||
|
local itemname = wielditem:get_name()
|
||
|
local name = clicker:get_player_name()
|
||
|
if itemname == "mcl_core:emerald" then
|
||
|
emeraldbank.keep(clicker, itemstack, 1)
|
||
|
else
|
||
|
core.chat_send_player(name, S("You need keep emeralds in your hand!") )
|
||
|
end
|
||
|
end,
|
||
|
|
||
|
on_punch = function(pos, node, puncher, pointed_thing)
|
||
|
emeraldbank.take(puncher, 1)
|
||
|
end,
|
||
|
})
|
||
|
|
||
|
if bankcraft then
|
||
|
core.register_craft({
|
||
|
output = "emeraldbank:bank",
|
||
|
recipe = {
|
||
|
{"mcl_core:emerald", "mcl_core:emerald", "mcl_core:emerald"},
|
||
|
{"mcl_core:emerald", "mcl_core:ironblock", "mcl_core:emerald"},
|
||
|
{"mcl_core:emerald", "mcl_core:emerald", "mcl_core:emerald"},
|
||
|
}
|
||
|
})
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
-- chat command
|
||
|
core.register_chatcommand("emeraldbank", {
|
||
|
params = S("<player> <num>"),
|
||
|
description = S("Add player emeralds in bank account, also can use negative numbers"),
|
||
|
privs = {server=true},
|
||
|
func = function(name, param)
|
||
|
local playername, num = param:match("([^ ]+) (.+)")
|
||
|
local player
|
||
|
if playername and num then
|
||
|
player = core.get_player_by_name(playername)
|
||
|
end
|
||
|
if player and num then
|
||
|
local player_meta = player:get_meta()
|
||
|
local bankemeralds = player_meta:get_int("emeraldbank:emerald")
|
||
|
player_meta:set_int("emeraldbank:emerald", bankemeralds+num)
|
||
|
core.chat_send_player(name, S("Emeralds in @1 bank account: @2", playername, bankemeralds+num) )
|
||
|
return true
|
||
|
end
|
||
|
return false
|
||
|
end
|
||
|
})
|