function emeraldbank.add_emeralds()

This commit is contained in:
Nathaniel Freeman 2021-10-20 17:39:17 +02:00
parent 178d60b689
commit 0ddd130cdb
2 changed files with 23 additions and 20 deletions

@ -24,22 +24,31 @@ local S = core.get_translator(core.get_current_modname())
local bankcraft = core.settings:get_bool("emeraldbank.craft") or true local bankcraft = core.settings:get_bool("emeraldbank.craft") or true
function emeraldbank.keep(player, itemstack) function emeraldbank.add_emeralds(player, num)
if not player then return false end
local meta = player:get_meta() local meta = player:get_meta()
local bankemeralds = meta:get_int("emeraldbank:emerald") -- if nil "get_int()" return 0 Magic! local bankemeralds = meta:get_int("emeraldbank:emerald") -- if nil "get_int()" return 0 Magic!
local name = player:get_player_name() local name = player:get_player_name()
if num then
meta:set_int("emeraldbank:emerald", bankemeralds+num)
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds+num) )
return true
end
return false
end
function emeraldbank.keep(player, itemstack)
local itemname = itemstack:get_name() local itemname = itemstack:get_name()
local itemcount = itemstack:get_count() local itemcount = itemstack:get_count()
local name = player:get_player_name()
if itemname == "mcl_core:emerald" then if itemname == "mcl_core:emerald" then
itemstack:take_item(itemcount) itemstack:take_item(itemcount)
meta:set_int("emeraldbank:emerald", bankemeralds+itemcount) emeraldbank.add_emeralds(player, itemcount)
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds+itemcount) )
return true return true
end end
if itemname == "mcl_core:emeraldblock" then if itemname == "mcl_core:emeraldblock" then
itemstack:take_item(itemcount*9) itemstack:take_item(itemcount)
meta:set_int("emeraldbank:emerald", bankemeralds+itemcount*9) emeraldbank.add_emeralds(player, itemcount*9)
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds+itemcount*9) )
return true return true
end end
core.chat_send_player(name, S("You need keep emeralds or emeraldblocks in your hand!") ) core.chat_send_player(name, S("You need keep emeralds or emeraldblocks in your hand!") )
@ -56,9 +65,8 @@ function emeraldbank.take(player)
if bankemeralds >= 10 then if bankemeralds >= 10 then
num = 10 num = 10
end end
meta:set_int("emeraldbank:emerald", bankemeralds-num) emeraldbank.add_emeralds(player, -num)
core.add_item(pos, "mcl_core:emerald "..num) core.add_item(pos, "mcl_core:emerald "..num)
core.chat_send_player(name, S("Emeralds in Bank: @1", bankemeralds-num) )
return true return true
end end
core.chat_send_player(name, S("Not enough Emeralds in your account") ) core.chat_send_player(name, S("Not enough Emeralds in your account") )

@ -29,6 +29,8 @@ core.register_chatcommand("pay", {
description = S("Pay money to other player. Transfer your emeralds to another bank account."), description = S("Pay money to other player. Transfer your emeralds to another bank account."),
func = function(name, param) func = function(name, param)
local player1 = core.get_player_by_name(name) local player1 = core.get_player_by_name(name)
local meta1 = player1:get_meta()
local bankemeralds1 = meta1:get_int("emeraldbank:emerald")
local playername2, stringnum = param:match("([^ ]+) (.+)") local playername2, stringnum = param:match("([^ ]+) (.+)")
local player2 local player2
local num = tonumber(stringnum) local num = tonumber(stringnum)
@ -36,16 +38,12 @@ core.register_chatcommand("pay", {
player2 = core.get_player_by_name(playername2) player2 = core.get_player_by_name(playername2)
end end
if player2 and num then 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 num > 0 then
if bankemeralds1 >= num then if bankemeralds1 >= num then
meta1:set_int("emeraldbank:emerald", bankemeralds1-num) core.chat_send_player(name, S("Pay Successfully! You have transferred @1 Emeralds." , num) )
meta2:set_int("emeraldbank:emerald", bankemeralds2+num) core.chat_send_player(playername2, S("Pay Successfully! You've gotten @1 Emeralds.", num) )
core.chat_send_player(name, S("Pay Successfully! You have transferred @1 Emeralds. Now you have: @2", num, bankemeralds1-num) ) emeraldbank.add_emeralds(player1, -num)
core.chat_send_player(playername2, S("Pay Successfully! You've gotten @1 Emeralds. Now you have: @2", num, bankemeralds2+num) ) emeraldbank.add_emeralds(player2, num)
else else
core.chat_send_player(name, S("Not enough Emeralds in your account") ) core.chat_send_player(name, S("Not enough Emeralds in your account") )
end end
@ -71,10 +69,7 @@ core.register_chatcommand("emeralds", {
player = core.get_player_by_name(playername) player = core.get_player_by_name(playername)
end end
if player and num then if player and num then
local meta = player:get_meta() emeraldbank.add_emeralds(player, num)
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 return true
end end
return false return false