income update

This commit is contained in:
Nathaniel Freeman 2021-10-13 14:23:24 +02:00
parent 38ffd38201
commit 6d4dbba152
4 changed files with 64 additions and 167 deletions

@ -39,7 +39,7 @@ User /pay command. Pay emeralds to another player:
Admin command to add emeralds in bank accounts:
```
/emeraldbank <player> <num>
/emeralds <player> <num>
```
If num is 0 return emeralds in player account.

50
income.lua Normal file

@ -0,0 +1,50 @@
-- 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
-- (at your option) 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 income_count = tonumber(core.settings:get("emeraldbank.income_count")) or 1
local income_period = tonumber(core.settings:get("emeraldbank.income_period")) or 1800
-- income
if income_enabled then
local timer = 0
core.register_globalstep(
function(dtime)
timer = timer + dtime;
if timer >= income_period then
timer = 0
for _, player in ipairs(core.get_connected_players()) do
if not player or player.is_fake_player then return end
local meta = player:get_meta()
local name = player:get_player_name()
local bankemeralds = meta:get_int("emeraldbank:emerald")
local total = bankemeralds+income_count
meta:set_int("emeraldbank:emerald", total)
core.chat_send_player(name, S("You have earned @1 emeralds in your bank account!", income_count) )
end
end
end
)
end

176
init.lua

@ -7,7 +7,7 @@
-- 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.
-- (at your option) any later version.
-- Emeraldbank is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -20,174 +20,24 @@
local S = core.get_translator(core.get_current_modname())
local bankcraft = core.settings:get_bool("emeraldbank.craft") or true
local income_enabled = core.settings:get_bool("emeraldbank.income_enabled", true)
local income_count = tonumber(core.settings:get("emeraldbank.income_count")) or 1
local income_period = tonumber(core.settings:get("emeraldbank.income_period")) or 1800
local maxbankgive = tonumber(core.settings:get("emeraldbank.max_bank_give")) or 100
local modpath = core.get_modpath(core.get_current_modname())
emeraldbank = {}
function emeraldbank.keep(player, itemstack)
local meta = player:get_meta()
local bankemeralds = meta:get_int("emeraldbank:emerald") -- if nil "get_int()" return 0 Magic!
local name = player:get_player_name()
local itemname = itemstack:get_name()
local itemcount = itemstack:get_count()
if itemname == "mcl_core:emerald" then
itemstack:take_item(itemcount)
meta:set_int("emeraldbank:emerald", bankemeralds+itemcount)
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds+itemcount) )
return true
end
if itemname == "mcl_core:emeraldblock" then
itemstack:take_item(itemcount*9)
meta:set_int("emeraldbank:emerald", bankemeralds+itemcount*9)
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds+itemcount*9) )
return true
end
core.chat_send_player(name, S("You need keep emeralds or emeraldblocks in your hand!") )
return false
end
function emeraldbank.take(player)
local meta = player:get_meta()
local bankemeralds = meta:get_int("emeraldbank:emerald")
local name = player:get_player_name()
local pos = player:get_pos()
local num = 1
if bankemeralds >= 1 then
if bankemeralds >= 10 then
num = 10
end
meta:set_int("emeraldbank:emerald", bankemeralds-num)
core.add_item(pos, "mcl_core:emerald "..num)
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
emeraldbank.player_inv = "list[current_player;main;0,4.5;9,3;9]"..
mcl_formspec.get_itemslot_bg(0,4.5,9,3)..
"list[current_player;main;0,7.74;9,1;]"..
mcl_formspec.get_itemslot_bg(0,7.74,9,1)..
"listring[current_player;main]"
-- 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 = {pickaxey=1, handy=1, building_block=1, enderman_takable=1},
sounds = mcl_sounds.node_sound_metal_defaults(),
_mcl_blast_resistance = 5,
_mcl_hardness = 1,
local income_enabled = core.settings:get_bool("emeraldbank.income_enabled", true)
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
emeraldbank.keep(clicker, itemstack)
end,
on_punch = function(pos, node, puncher, pointed_thing)
emeraldbank.take(puncher)
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
-- user /pay chat command
core.register_chatcommand("pay", {
params = "<player> <num>",
description = S("Pay money to other player. Transfer your emeralds to another bank account."),
func = function(name, param)
local player1 = core.get_player_by_name(name)
local playername2, stringnum = param:match("([^ ]+) (.+)")
local player2
local num = tonumber(stringnum)
if playername2 and num then
player2 = core.get_player_by_name(playername2)
end
if player2 and num then
local meta1 = player1:get_meta()
local meta2 = player2:get_meta()
local bankemeralds1 = meta1:get_int("emeraldbank:emerald")
local bankemeralds2 = meta2:get_int("emeraldbank:emerald")
if num > 0 then
if bankemeralds1 >= num then
meta1:set_int("emeraldbank:emerald", bankemeralds1-num)
meta2:set_int("emeraldbank:emerald", bankemeralds2+num)
core.chat_send_player(name, S("Pay Successfully! You have transferred @1 Emeralds. Now you have: @2", num, bankemeralds1-num) )
core.chat_send_player(playername2, S("Pay Successfully! You've gotten @1 Emeralds. Now you have: @2", num, bankemeralds2+num) )
else
core.chat_send_player(name, S("Not enough Emeralds in your account") )
end
else
core.chat_send_player(name, S("Invalid pay") )
end
return true
end
return false
end
})
-- admin chat command
core.register_chatcommand("emeraldbank", {
params = "<player> <num>",
description = S("Admin Command! Add player emeralds in bank account, also can use negative numbers"),
privs = {server=true},
func = function(name, param)
local playername, stringnum = param:match("([^ ]+) (.+)")
local player
local num = tonumber(stringnum)
if playername and num then
player = core.get_player_by_name(playername)
end
if player and num then
local meta = player:get_meta()
local bankemeralds = meta:get_int("emeraldbank:emerald")
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
})
-- income
if income_enabled then
local timer = 0
core.register_globalstep(
function(dtime)
timer = timer + dtime;
if timer >= income_period then
timer = 0
for _, player in ipairs(core.get_connected_players()) do
if not player or player.is_fake_player then return end
local meta = player:get_meta()
local name = player:get_player_name()
local bankemeralds = meta:get_int("emeraldbank:emerald")
local bankgive = math.floor(bankemeralds^(1/3))
if bankgive > maxbankgive then bankgive = maxbankgive end
local earned = income_count+bankgive
local total = bankemeralds+earned
dofile(modpath .. "/income.lua")
end
meta:set_int("emeraldbank:emerald", total)
core.chat_send_player(name, S("You have earned @1 emeralds in your bank account!", earned) )
end
end
end
)
end
dofile(modpath .. "/bank.lua")
dofile(modpath .. "/shop.lua")
dofile(modpath .. "/commands.lua")

@ -10,6 +10,3 @@ emeraldbank.income_count (number of emeralds) int 1
# Length of time (in seconds) between checking if a user should get income
emeraldbank.income_period (time between income) int 1800
# Bank will give some of money extra in function of emeralds in bank account. That is the max.
emeraldbank.max_bank_give (max extra emeralds) int 100