Move Gamemode code to new mcl_gamemode mod

This commit is contained in:
AFCMS 2024-03-25 05:51:11 +01:00 committed by ryvnf
parent 20c4739dea
commit 4b3286f28f
10 changed files with 109 additions and 67 deletions

View File

@ -105,6 +105,7 @@ globals = {
"mcl_formspec",
"mcl_formspec_prepend",
"mcl_furnaces",
"mcl_gamemode",
"mcl_grindstone",
"mcl_hbarmor",
"mcl_heads",

View File

@ -254,6 +254,15 @@ minetest.register_on_mods_loaded(function()
table.sort(mcl_experience.on_add_xp, function(a, b) return a.priority < b.priority end)
end)
mcl_gamemode.register_on_gamemode_change(function(p, old_gm, gm)
if gm == "survival" then
mcl_experience.setup_hud(p)
mcl_experience.update(p)
elseif gm == "creative" then
mcl_experience.remove_hud(p)
end
end)
minetest.register_chatcommand("set_xp", {
privs = { debug = true },
description = "Set experience of current player",

View File

@ -1,4 +1,4 @@
name = mcl_experience
author = oilboi
description = eXPerience mod
depends = mcl_init
depends = mcl_init, mcl_gamemode

View File

@ -1,12 +1,7 @@
mcl_inventory = {}
local S = minetest.get_translator("mcl_inventory")
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/creative.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/survival.lua")
--local mod_player = minetest.get_modpath("mcl_player")
--local mod_craftguide = minetest.get_modpath("mcl_craftguide")
---Returns a single itemstack in the given inventory to the main inventory, or drop it when there's no space left.
local function return_item(itemstack, dropper, pos, inv)
if dropper:is_player() then
@ -234,63 +229,6 @@ end
mcl_player.register_on_visual_change(mcl_inventory.update_inventory_formspec)
local old_is_creative_enabled = minetest.is_creative_enabled
function minetest.is_creative_enabled(name)
if old_is_creative_enabled(name) then return true end
if not name then return false end
assert(type(name) == "string", "minetest.is_creative_enabled requires a string (the playername) argument. This is likely an error in a non-mineclonia mod.")
local p = minetest.get_player_by_name(name)
if p then
return p:get_meta():get_string("gamemode") == "creative"
end
return false
end
local gamemodes = {
"survival",
"creative"
}
function mcl_inventory.player_set_gamemode(p,g)
local m = p:get_meta()
m:set_string("gamemode",g)
if g == "survival" then
mcl_experience.setup_hud(p)
mcl_experience.update(p)
elseif g == "creative" then
mcl_experience.remove_hud(p)
end
mcl_meshhand.update_player(p)
mcl_gamemode.register_on_gamemode_change(function(p, old_gm, gm)
set_inventory(p)
end
minetest.register_chatcommand("gamemode",{
params = S("[<gamemode>] [<player>]"),
description = S("Change gamemode (survival/creative) for yourself or player"),
privs = { server = true },
func = function(n,param)
-- Full input validation ( just for @erlehmann <3 )
local p
local args = param:split(" ")
if args[2] ~= nil then
p = minetest.get_player_by_name(args[2])
n = args[2]
else
p = minetest.get_player_by_name(n)
end
if not p then
return false, S("Player not online")
end
if args[1] ~= nil and table.indexof(gamemodes, args[1]) == -1 then
return false, S("Gamemode @1 does not exist.", args[1])
elseif args[1] ~= nil then
mcl_inventory.player_set_gamemode(p,args[1])
end
--Result message - show effective game mode
local gm = p:get_meta():get_string("gamemode")
if gm == "" then gm = gamemodes[1] end
return true, S("Gamemode for player @1: @2", n, gm)
end
})
end)

View File

@ -1,5 +1,5 @@
name = mcl_inventory
author = BlockMen
description = Adds the player inventory and creative inventory.
depends = mcl_init, mcl_formspec, mcl_enchanting, mcl_meshhand
depends = mcl_init, mcl_formspec, mcl_enchanting, mcl_meshhand, mcl_gamemode
optional_depends = mcl_armor, mcl_brewing, mcl_potions, mcl_enchanting, mcl_craftguide, mcl_player

View File

@ -0,0 +1,27 @@
# `mcl_gamemode`
## `mcl_gamemode.gamemodes`
List of availlable gamemodes.
Currently `{"survival", "creative"}`
## `mcl_gamemode.get_gamemode(player)`
Get the player's gamemode.
Returns "survival" or "creative".
## `mcl_gamemode.set_gamemode(player, gamemode)`
Set the player's gamemode.
gamemode: "survival" or "creative"
## `mcl_gamemode.register_on_gamemode_change(function(player, old_gamemode, new_gamemode))`
Register a function that will be called when `mcl_gamemode.set_gamemode` is called.
## `mcl_gamemode.registered_on_gamemode_change`
Map of registered on_gamemode_change.

View File

@ -0,0 +1,64 @@
local S = minetest.get_translator("mcl_gamemode")
mcl_gamemode = {
gamemodes = {
"survival",
"creative",
},
registered_on_gamemode_change = {}
}
function mcl_gamemode.register_on_gamemode_change(func)
table.insert(mcl_gamemode.registered_on_gamemode_change, func)
end
local old_is_creative_enabled = minetest.is_creative_enabled
function minetest.is_creative_enabled(name)
if old_is_creative_enabled(name) then return true end
if not name then return false end
assert(type(name) == "string", "minetest.is_creative_enabled requires a string (the playername) argument. This is likely an error in a non-mineclonia mod.")
local p = minetest.get_player_by_name(name)
if p then
return p:get_meta():get_string("gamemode") == "creative"
end
return false
end
function mcl_gamemode.get_gamemode(p)
return minetest.is_creative_enabled(p:get_player_name()) and "creative" or "survival"
end
function mcl_gamemode.set_gamemode(p, gm)
if table.indexof(mcl_gamemode.gamemodes, gm) == -1 then return false end
local old_gm = mcl_gamemode.get_gamemode(p)
p:get_meta():set_string("gamemode", gm)
for _, func in ipairs(mcl_gamemode.registered_on_gamemode_change) do
func(p, old_gm, gm)
end
return true
end
minetest.register_chatcommand("gamemode",{
params = S("[<gamemode>] [<player>]"),
description = S("Change gamemode (survival/creative) for yourself or player"),
privs = { server = true },
func = function(n,param)
local p
local args = param:split(" ")
if args[2] ~= nil then
p = minetest.get_player_by_name(args[2])
n = args[2]
else
p = minetest.get_player_by_name(n)
end
if not p then
return false, S("Player not online")
end
if mcl_gamemode.set_gamemode(p, args[1]) == false then
return false, S("Failed to set Gamemode @1 for player @2", args[1], p:get_player_name())
end
--Result message - show effective game mode
return true, S("Gamemode for player @1: @2", n, mcl_gamemode.get_gamemode(p))
end
})

View File

@ -0,0 +1 @@
name = mcl_gamemode

View File

@ -106,6 +106,8 @@ else
minetest.register_on_joinplayer(mcl_meshhand.update_player)
end
mcl_gamemode.register_on_gamemode_change(mcl_meshhand.update_player)
-- This is needed to deal damage when punching mobs
-- with random items in hand in survival mode
minetest.override_item("", {

View File

@ -1,5 +1,5 @@
name = mcl_meshhand
author = jordan4ibanez
description = Applies the player skin texture to the hand.
depends = mcl_tools, mcl_player
depends = mcl_tools, mcl_player, mcl_gamemode
optional_depends = mcl_skins, mcl_custom_skins