From 4b3286f28f9b2d176f0a4407766b9c4940ced1f6 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Mon, 25 Mar 2024 05:51:11 +0100 Subject: [PATCH] Move Gamemode code to new mcl_gamemode mod --- .luacheckrc | 1 + mods/HUD/mcl_experience/init.lua | 9 +++++ mods/HUD/mcl_experience/mod.conf | 2 +- mods/HUD/mcl_inventory/init.lua | 66 +------------------------------ mods/HUD/mcl_inventory/mod.conf | 2 +- mods/PLAYER/mcl_gamemode/API.md | 27 +++++++++++++ mods/PLAYER/mcl_gamemode/init.lua | 64 ++++++++++++++++++++++++++++++ mods/PLAYER/mcl_gamemode/mod.conf | 1 + mods/PLAYER/mcl_meshhand/init.lua | 2 + mods/PLAYER/mcl_meshhand/mod.conf | 2 +- 10 files changed, 109 insertions(+), 67 deletions(-) create mode 100644 mods/PLAYER/mcl_gamemode/API.md create mode 100644 mods/PLAYER/mcl_gamemode/init.lua create mode 100644 mods/PLAYER/mcl_gamemode/mod.conf diff --git a/.luacheckrc b/.luacheckrc index c75295608..deec6da1a 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -105,6 +105,7 @@ globals = { "mcl_formspec", "mcl_formspec_prepend", "mcl_furnaces", + "mcl_gamemode", "mcl_grindstone", "mcl_hbarmor", "mcl_heads", diff --git a/mods/HUD/mcl_experience/init.lua b/mods/HUD/mcl_experience/init.lua index a7b5c15e8..7e9c8b78e 100644 --- a/mods/HUD/mcl_experience/init.lua +++ b/mods/HUD/mcl_experience/init.lua @@ -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", diff --git a/mods/HUD/mcl_experience/mod.conf b/mods/HUD/mcl_experience/mod.conf index f9c6b0313..df0b67db1 100644 --- a/mods/HUD/mcl_experience/mod.conf +++ b/mods/HUD/mcl_experience/mod.conf @@ -1,4 +1,4 @@ name = mcl_experience author = oilboi description = eXPerience mod -depends = mcl_init +depends = mcl_init, mcl_gamemode diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index c9fc88db5..72f36885d 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -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("[] []"), - 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) diff --git a/mods/HUD/mcl_inventory/mod.conf b/mods/HUD/mcl_inventory/mod.conf index 4366a5b13..e2c81eedc 100644 --- a/mods/HUD/mcl_inventory/mod.conf +++ b/mods/HUD/mcl_inventory/mod.conf @@ -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 diff --git a/mods/PLAYER/mcl_gamemode/API.md b/mods/PLAYER/mcl_gamemode/API.md new file mode 100644 index 000000000..6cc4b7a3d --- /dev/null +++ b/mods/PLAYER/mcl_gamemode/API.md @@ -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. diff --git a/mods/PLAYER/mcl_gamemode/init.lua b/mods/PLAYER/mcl_gamemode/init.lua new file mode 100644 index 000000000..a64875d33 --- /dev/null +++ b/mods/PLAYER/mcl_gamemode/init.lua @@ -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("[] []"), + 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 +}) diff --git a/mods/PLAYER/mcl_gamemode/mod.conf b/mods/PLAYER/mcl_gamemode/mod.conf new file mode 100644 index 000000000..6929ccbb4 --- /dev/null +++ b/mods/PLAYER/mcl_gamemode/mod.conf @@ -0,0 +1 @@ +name = mcl_gamemode diff --git a/mods/PLAYER/mcl_meshhand/init.lua b/mods/PLAYER/mcl_meshhand/init.lua index 6e654a732..6295356d8 100644 --- a/mods/PLAYER/mcl_meshhand/init.lua +++ b/mods/PLAYER/mcl_meshhand/init.lua @@ -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("", { diff --git a/mods/PLAYER/mcl_meshhand/mod.conf b/mods/PLAYER/mcl_meshhand/mod.conf index 687932514..fd10c259c 100644 --- a/mods/PLAYER/mcl_meshhand/mod.conf +++ b/mods/PLAYER/mcl_meshhand/mod.conf @@ -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