Fix and rewirte of some buggy function

This commit is contained in:
davedevils 2015-07-01 07:17:09 +02:00
parent 2a51d901c4
commit f836f85170
3 changed files with 65 additions and 43 deletions

View file

@ -1,22 +1,24 @@
inven = {} inven = {}
CREATIVE_FORMSPEC = "";
SURVIVAL_FORMSPEC = "";
function inventory.creative_inv(player) function inventory.creative_inv(player)
local name = player:get_player_name() local name = player:get_player_name()
CREATIVE_FORMSPEC = "invsize[11,9.75;]".. CREATIVE_FORMSPEC = "invsize[11,9.75;]"..
--"background[-0.25,1;10.5,8;inventory_creative_inventory_bg.png]".. --"background[-0.25,1;10.5,8;inventory_creative_inventory_bg.png]"..
"button[9.5,0;1.5,1.5;creative_search;Search]".. "button[9.5,0;1.5,1.5;creative_search;Search]"..
"list[detached:"..name.."_armor;armor_head;0.25,1.25;1,1;]".. "list[detached:"..name.."_armor;armor_head;0.25,1.25;1,1;]"..
"list[detached:"..name.."_armor;armor_torso;0.25,2.5;1,1;]".. "list[detached:"..name.."_armor;armor_torso;0.25,2.5;1,1;]"..
"list[detached:"..name.."_armor;armor_legs;2.75,1.25;1,1;]".. "list[detached:"..name.."_armor;armor_legs;2.75,1.25;1,1;]"..
"list[detached:"..name.."_armor;armor_feet;2.75,2.5;1,1;]".. "list[detached:"..name.."_armor;armor_feet;2.75,2.5;1,1;]"..
"image[1.3,1;1.5,3;player.png]".. "image[1.3,1;1.5,3;player.png]"..
"list[current_player;main;0,4;9,4;9]".. "list[current_player;main;0,4;9,4;9]"..
"list[current_player;main;0,7.75;9,1;]".. "list[current_player;main;0,7.75;9,1;]"..
"list[detached:creative_trash;main;9.1,7.75;1,1;]".. "list[detached:creative_trash;main;9.1,7.75;1,1;]"..
"button[9.15,6;1,1;clear_inventory;Clear]".. "button[9.15,6;1,1;clear_inventory;Clear]"..
"button[9.5,8.75;1.5,1.5;creative_survival;Survival]" "button[9.5,8.75;1.5,1.5;creative_survival;Survival]"
player:get_inventory():set_width("main", 9) player:get_inventory():set_width("main", 9)
player:get_inventory():set_size("main", 36) player:get_inventory():set_size("main", 36)
player:set_inventory_formspec(CREATIVE_FORMSPEC) player:set_inventory_formspec(CREATIVE_FORMSPEC)
end end

View file

@ -1,13 +1,5 @@
--Configuration file for Inventory --Configuration file for Inventory
-- Set "Creative" for all Player have creatif , set "Survival" for all player in normal mode
Default_Mode = "Survival" Default_Mode = "Survival"
--Mode players are in when they join for the first time. Possible values: Creative, Survival. CASE SENSITIVE
--IMPORTANT: This formspec must be valid. If you want compatibility with other inventory-changing mods
--or games, you must change the above to the formspec definition for that mod and remove it
--from their mod.
--NOTE: At the moment, the creative formspec cannot be changed, as it is required to use variables inside the mod.
--You can try to change it, but it probably will not work. The same is true for a survival inventory with variables,
--such as inventory_plus. It is still possible to change the SURVIVAL_FORMSPEC variable inside the code to cope
--with this, but it will not be easy.

View file

@ -2,7 +2,14 @@ local path = minetest.get_modpath(minetest.get_current_modname())
local filepath = minetest.get_worldpath() local filepath = minetest.get_worldpath()
local function save_player_data() CREATIVE_SEARCH_ITEMS = ""
local creative_type = "search"
filepath = minetest.get_worldpath()
se = {}
function save_player_data()
local file = io.open(filepath .. "/playerdata.txt", "w") local file = io.open(filepath .. "/playerdata.txt", "w")
file:write(minetest.serialize(playerdata)) file:write(minetest.serialize(playerdata))
file:close() file:close()
@ -20,6 +27,8 @@ function load_player_data()
return {} return {}
end end
inventory = {} inventory = {}
inventory.inventory_size = 0 inventory.inventory_size = 0
pagenum = 0 pagenum = 0
@ -29,21 +38,34 @@ dofile(path.."/config.txt")
dofile(path.."/api.lua") dofile(path.."/api.lua")
dofile(path.."/workbench.lua") dofile(path.."/workbench.lua")
minetest.register_on_joinplayer(function(player) local function save_newplayer(pname)
pname = player:get_player_name()
playerdata = load_player_data()
if not playerdata[pname] then if not playerdata[pname] then
playerdata[pname] = {} playerdata[pname] = {}
playerdata[pname]['isPlayer'] = true
playerdata[pname]['gamemode'] = Default_Mode playerdata[pname]['gamemode'] = Default_Mode
save_player_data() save_player_data()
minetest.after(1, function() load_player_data() end)
playerdata = load_player_data()
end
end
minetest.register_on_joinplayer(function(player)
local pname = player:get_player_name()
local playerdata = load_player_data()
if not playerdata[pname] then
playerdata[pname] = {}
playerdata[pname]['isPlayer'] = true
playerdata[pname]['gamemode'] = Default_Mode
save_player_data()
end end
if not playerdata[pname]['gamemode'] then if not playerdata[pname]['gamemode'] then
playerdata[pname]['gamemode'] = Default_Mode playerdata[pname]['gamemode'] = Default_Mode
save_player_data() save_player_data()
playerdata = load_player_data() playerdata = load_player_data()
minetest.after(0.3, function() updategamemode(pname, "0") end) minetest.after(1, function() updategamemode(pname, "0") end)
else else
minetest.after(0.3, function() updategamemode(pname, "0") end) minetest.after(1, function() updategamemode(pname, "0") end)
end end
end) end)
@ -62,7 +84,7 @@ local trash = minetest.create_detached_inventory("creative_trash", {
trash:set_size("main", 1) trash:set_size("main", 1)
creative_list = {} local creative_list = {}
for name,def in pairs(minetest.registered_items) do 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) if (not def.groups.not_in_creative_inventory or def.groups.not_in_creative_inventory == 0)
and def.description and def.description ~= "" then and def.description and def.description ~= "" then
@ -116,13 +138,16 @@ end)
function updategamemode(pname, status) function updategamemode(pname, status)
playerdata = load_player_data() playerdata = load_player_data()
if not status then if not status then
print(pname.." has switched to "..playerdata[pname]['gamemode'].." Mode.") print(pname.." has switched to "..playerdata[pname]['gamemode'].." Mode.")
minetest.chat_send_all(pname.." has switched to "..playerdata[pname]['gamemode'].." Mode.") minetest.chat_send_all(pname.." has switched to "..playerdata[pname]['gamemode'].." Mode.")
end
print(playerdata[pname])
if playerdata[pname] == nil then
save_newplayer(pname)
end end
if playerdata[pname]['gamemode'] == "Creative" then if playerdata[pname]['gamemode'] == "Creative" then
local player = minetest.env:get_player_by_name(pname) local player = minetest.env:get_player_by_name(pname)
inventory.set_player_formspec(player, 1, 1)
inventory.set_player_formspec(player, 1, 1)
else else
local player = minetest.env:get_player_by_name(pname) local player = minetest.env:get_player_by_name(pname)
@ -132,13 +157,14 @@ function updategamemode(pname, status)
end end
inventory.set_player_formspec = function(player, start_i, pagenum) inventory.set_player_formspec = function(player, start_i, pagenum)
playerdata = load_player_data() playerdata = load_player_data()
if playerdata[player:get_player_name()]['gamemode'] == "Creative" or creative_type == "default" then if playerdata[player:get_player_name()]['gamemode'] == "Creative" then
inventory.creative_inv(player) inventory.creative_inv(player)
inventory.hotbar(player) inventory.hotbar(player)
end end
if creative_type == "search" then
pagenum = math.floor(pagenum) if creative_type == "search" and playerdata[player:get_player_name()]['gamemode'] == "Creative" then
pagemax = math.floor((inventory.inventory_size-1) / (9*3) + 1) local pagenum = math.floor(pagenum)
local pagemax = math.floor((inventory.inventory_size-1) / (9*3) + 1)
CREATIVE_SEARCH_ITEMS = "invsize[10,7;]".. CREATIVE_SEARCH_ITEMS = "invsize[10,7;]"..
"background[-0.22,-0.25;10.8,7.7;creative_inventory_bg.png]".. "background[-0.22,-0.25;10.8,7.7;creative_inventory_bg.png]"..
"button[8,0;1.5,1;creative_search;Search]".. "button[8,0;1.5,1;creative_search;Search]"..
@ -158,7 +184,7 @@ playerdata = load_player_data()
end end
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if not playerdata[pname]['gamemode'] == "Creative" then if playerdata[player:get_player_name()]['gamemode'] == "Survival" then
return return
end end
-- Figure out current page from formspec -- Figure out current page from formspec
@ -202,10 +228,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
inventory.set_player_formspec(player, start_i, start_i / (9*3) + 1) inventory.set_player_formspec(player, start_i, start_i / (9*3) + 1)
end) end)
local gm_priv = false
if minetest.setting_getbool("creative_mode")==false then if minetest.setting_getbool("creative_mode")==false then
local gm_priv = true gm_priv = true
elseif minetest.setting_getbool("creative_mode")==true then elseif minetest.setting_getbool("creative_mode")==true then
local gm_priv = false gm_priv = false
end end
minetest.register_chatcommand('gamemode',{ minetest.register_chatcommand('gamemode',{