Restrict creative inventories to player

This commit is contained in:
Wuzzy 2019-08-31 19:16:09 +02:00
parent beb95a21a6
commit 0f8757ccba

View File

@ -3,6 +3,9 @@ local F = minetest.formspec_escape
creative = {}
creative.creative_inventories = {}
creative.creative_sizes = {}
local playerdata = {}
local form = default.ui.get_page("default:2part")
@ -13,12 +16,12 @@ form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
default.ui.register_page("creative:creative", form)
creative.creative_inventory_size = 0
creative.slots_num = 7*4
-- Create detached creative inventory after loading all mods
minetest.register_on_mods_loaded(function()
local inv = minetest.create_detached_inventory("creative", {
-- Create detached creative inventory for player
local function create_creative_inventory(player)
local player_name = player:get_player_name()
local inv = minetest.create_detached_inventory("creative_"..player_name, {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
if minetest.settings:get_bool("creative_mode") then
return count
@ -45,7 +48,7 @@ minetest.register_on_mods_loaded(function()
minetest.log("action", player:get_player_name().." takes "..dump(stack:get_name()).." from creative inventory")
end
end,
})
}, player_name)
local creative_list = {}
for name,def in pairs(minetest.registered_items) do
if (not def.groups.not_in_creative_inventory or def.groups.not_in_creative_inventory == 0)
@ -58,11 +61,13 @@ minetest.register_on_mods_loaded(function()
for _,itemstring in ipairs(creative_list) do
inv:add_item("main", ItemStack(itemstring))
end
creative.creative_inventory_size = #creative_list
end)
creative.creative_inventories[player_name] = inv
creative.creative_sizes[player_name] = inv:get_size("main")
return inv
end
-- Create the trash field
local trash = minetest.create_detached_inventory("creative_trash", {
local trash = minetest.create_detached_inventory("creative_!trash", {
-- Allow the stack to be placed and remove it in on_put()
-- This allows the creative inventory to restore the stack
allow_put = function(inv, listname, index, stack, player)
@ -78,23 +83,27 @@ local trash = minetest.create_detached_inventory("creative_trash", {
})
trash:set_size("main", 1)
creative.get_creative_formspec = function(player, start_i, pagenum)
pagenum = math.floor(pagenum)
local pagemax = math.floor((creative.creative_inventory_size-1) / (creative.slots_num) + 1)
local player_name = player:get_player_name()
if not creative.creative_inventories[player_name] then
create_creative_inventory(player)
end
local size = creative.creative_sizes[player_name]
local pagemax = math.floor((size-1) / (creative.slots_num) + 1)
return
"list[detached:creative;main;0.25,0.25;7,4;"..tostring(start_i).."]"..
"list[detached:creative_"..player_name..";main;0.25,0.25;7,4;"..tostring(start_i).."]"..
"label[7.5,0.75;"..F(S("@1/@2", pagenum, pagemax)).."]"..
default.ui.button(7.25, 1.25, 1, 1, "creative_prev", "<<")..
default.ui.button(7.25, 2.25, 1, 1, "creative_next", ">>")..
default.ui.get_itemslot_bg(0.25, 0.25, 7,4)..
"image[7.25,3.25;1,1;creative_trash_icon.png]"..
"list[detached:creative_trash;main;7.25,3.25;1,1;]"..
"list[detached:creative_!trash;main;7.25,3.25;1,1;]"..
default.ui.get_itemslot_bg(7.25, 3.25, 1,1)..
"listring[current_player;main]"..
"listring[detached:creative_trash;main]"..
"listring[detached:creative;main]"..
"listring[detached:creative_!trash;main]"..
"listring[detached:creative_"..player_name..";main]"..
"listring[current_player;main]"
end
@ -106,7 +115,7 @@ end
local get_page_and_start_i = function(playername)
init_playerdata(playername)
local page = playerdata[playername].page
local page = playerdata[playername].page or 1
local start_i = (page - 1) * creative.slots_num
return page, start_i
end
@ -130,8 +139,12 @@ minetest.register_on_joinplayer(function(player)
return
end
init_playerdata(player:get_player_name())
if not creative.creative_inventories[player:get_player_name()] then
create_creative_inventory(player)
end
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
playerdata[player:get_player_name()] = nil
end)
@ -161,13 +174,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
start_i = start_i + creative.slots_num
page = page + 1
end
if start_i >= creative.creative_inventory_size then
if start_i >= creative.creative_sizes[playername] then
start_i = start_i - creative.slots_num
page = page - 1
end
playerdata[playername].page = page
if start_i < 0 or start_i >= creative.creative_inventory_size then
if start_i < 0 or start_i >= creative.creative_sizes[playername] then
start_i = 0
page = 1
end