Switch to player has creative privilege check instead of creative mode.

Thanks to @Skaapdev
Closes #9 and #10
master
Alexander Weber 2020-08-19 07:58:52 +02:00
parent a66b9a7299
commit 943fe836a1
3 changed files with 36 additions and 11 deletions

View File

@ -152,4 +152,9 @@ function maininv.get(playername)
return self
end
-- Check if player has creative privilege.
function maininvClass:get_has_creative()
return minetest.is_creative_enabled(self.playername)
end
return maininv

View File

@ -1,7 +1,3 @@
if not minetest.setting_getbool("creative_mode") then
return
end
local cache = smart_inventory.cache
local ui_tools = smart_inventory.ui_tools
@ -78,6 +74,7 @@ end
-- Page layout definition
-----------------------------------------------------
local function creative_callback(state)
local player = state.location.rootState.location.player
-- build up UI controller
@ -277,6 +274,10 @@ local function creative_callback(state)
ui_controller:restore()
end
local function player_has_creative(state)
return state.param.invobj:get_has_creative()
end
-----------------------------------------------------
-- Register page in smart_inventory
-----------------------------------------------------
@ -285,5 +286,22 @@ smart_inventory.register_page({
tooltip = "The creative way to get items",
icon = "smart_inventory_creative_button.png",
smartfs_callback = creative_callback,
is_visible_func = player_has_creative,
sequence = 15
})
-- Redefinition for sfinv method maybe called from other mods
function sfinv.set_player_inventory_formspec(player, context)
local playername = player:get_player_name()
local page_state = smart_inventory.get_page_state("creative", playername)
if page_state then
local state = page_state.location.parentState
local has_creative = player_has_creative(state)
state:get("creative_button"):setVisible(has_creative)
if not has_creative then
state:get("crafting_button"):submit(nil, playername)
end
state:show()
end
end

View File

@ -12,9 +12,9 @@ local filter = smart_inventory.filter
local cache = smart_inventory.cache
local ui_tools = smart_inventory.ui_tools
local txt = smart_inventory.txt
local creative = minetest.setting_getbool("creative_mode")
local function update_grid(state, listname)
local player_has_creative = state.param.invobj:get_has_creative()
-- Update the users inventory grid
local list = {}
state.param["player_"..listname.."_list"] = list
@ -69,7 +69,7 @@ local function update_grid(state, listname)
end
-- add all usable in creative available armor to the main list
if listname == "main" and creative == true then
if listname == "main" and player_has_creative == true then
if smart_inventory.armor_mod then
for _, itemdef in pairs(cache.cgroups["armor"].items) do
if not list_dedup[itemdef.name] and not itemdef.groups.not_in_creative_inventory then
@ -225,6 +225,7 @@ local function update_page(state)
end
local function move_item_to_armor(state, item)
local player_has_creative = state.param.invobj:get_has_creative()
local name = state.location.rootState.location.player
local player = minetest.get_player_by_name(name)
local inventory = player:get_inventory()
@ -232,7 +233,7 @@ local function move_item_to_armor(state, item)
-- get item to be moved to armor inventory
local itemstack, itemname, itemdef
if creative == true then
if player_has_creative == true then
itemstack = ItemStack(item.item)
itemname = item.item
else
@ -273,7 +274,7 @@ local function move_item_to_armor(state, item)
end
-- handle put backs in non-creative to not lost items
if creative == false then
if player_has_creative == false then
inventory:set_stack("main", item.stack_index, itemstack)
for _, stack in ipairs(removed_items) do
stack = inventory:add_item("main", stack)
@ -304,7 +305,7 @@ local function move_item_to_clothing(state, item)
clothing:set_player_clothing(player)
state.param.player_clothing_data = clothes_ordered
-- handle put backs in non-creative to not lost items
if creative == false then
if player_has_creative == false then
local itemstack = inventory:get_stack("main", item.stack_index)
itemstack:take_item()
inventory:set_stack("main", item.stack_index, itemstack)
@ -313,6 +314,7 @@ local function move_item_to_clothing(state, item)
end
local function move_item_to_inv(state, item)
local player_has_creative = state.param.invobj:get_has_creative()
local name = state.location.rootState.location.player
local player = minetest.get_player_by_name(name)
local inventory = player:get_inventory()
@ -320,7 +322,7 @@ local function move_item_to_inv(state, item)
if cache.cgroups["armor"] and cache.cgroups["armor"].items[item.item] then
local armor_inv = minetest.get_inventory({type="detached", name=name.."_armor"})
local itemstack = armor_inv:get_stack("armor", item.stack_index)
if creative == true then
if player_has_creative == true then
-- trash armor item in creative
itemstack = ItemStack("")
else
@ -332,7 +334,7 @@ local function move_item_to_inv(state, item)
elseif cache.cgroups["clothing"] and cache.cgroups["clothing"].items[item.item] then
local clothes = state.param.player_clothing_data
if creative ~= true and clothes[item.stack_index] then
if player_has_creative ~= true and clothes[item.stack_index] then
local itemstack = inventory:add_item("main", ItemStack(clothes[item.stack_index]))
if itemstack:is_empty() then
clothes[item.stack_index] = nil