diff --git a/init.lua b/init.lua index 4662ef2..eddc045 100644 --- a/init.lua +++ b/init.lua @@ -8,7 +8,7 @@ dofile(modpath.."/xp.lua") --variable used for time keeping for updating xp time = 0 -local get_specialInfo = function(player, specialty) +local get_specialInfo = function(name, specialty) local formspec = "size[8,8]" -- size of the formspec page .."button_exit[0,0;0.75,0.5;close;X]" -- back to main inventory .."button[2,0;2,0.5;miner;Miner]" @@ -18,7 +18,7 @@ local get_specialInfo = function(player, specialty) .."button[2,3;2,0.5;builder;Builder]" .."list[current_player;main;0,4;8,4;]" if(specialty ~= "") then - formspec = formspec.."label[4,0;XP: "..specialties.players[player:get_player_name()][specialty].."]"..specialties.skills[specialty].menu + formspec = formspec.."label[4,0;XP: "..specialties.players[name][specialty].."]"..specialties.skills[specialty].menu end return formspec end @@ -27,19 +27,12 @@ minetest.register_on_leaveplayer(function(player)--Called if on a server, if sin specialties.updateXP(player:get_player_name()) end) ---Initial Files Created -minetest.register_on_newplayer(function(player) - for skill,_ in pairs(specialties.skills) do - specialties.writeXP(player:get_player_name(), skill, 0) - end -end) - --Initial XP Extraction --optimizes the amount of calls to files minetest.register_chatcommand("spec", { description = "Show Specialties menu", func = function(name, param) - minetest.show_formspec(name, "specialties:spec", get_specialInfo(minetest.get_player_by_name(name), "")) + minetest.show_formspec(name, "specialties:spec", get_specialInfo(name, "")) end, }) minetest.register_on_joinplayer(function(player) @@ -51,28 +44,27 @@ minetest.register_on_joinplayer(function(player) player:get_inventory():set_size("buildtrash", 1) name = player:get_player_name() specialties.players[name] = {} - for skill,_ in pairs(specialties.skills) do - specialties.players[name][skill] = specialties.readXP(name, skill) - end + specialties.players[name] = specialties.readXP(name) end) -local function show_formspec(player, specialty) - minetest.show_formspec(player, "specialties:spec", get_specialInfo(minetest.get_player_by_name(player), specialty)) +local function show_formspec(name, specialty) + minetest.show_formspec(name, "specialties:spec", get_specialInfo(name, specialty)) end --Skill Events local function healTool(player, list, specialty, cost) - tool = player:get_inventory():get_list(list)[1] + local tool = player:get_inventory():get_list(list)[1] if tool:get_name():find(":"..list) == nil then return end + local name = player:get_player_name() if tool:get_wear() ~= 0 and specialties.healAmount[tool:get_name()] ~= nil then - if specialties.changeXP(player:get_player_name(), specialty, -cost) then + if specialties.changeXP(name, specialty, -cost) then tool:add_wear(-specialties.healAmount[tool:get_name()]) player:get_inventory():set_stack(list, 1, tool) end end - show_formspec(player:get_player_name(), specialty) + show_formspec(name, specialty) end local function upgradeTool(player, list, specialty, cost) - tool = player:get_inventory():get_list(list)[1] + local tool = player:get_inventory():get_list(list)[1] if tool:get_name():find(":"..list) == nil then return end if specialties.upgradeTree[tool:get_name()] ~= nil then if specialties.changeXP(player:get_player_name(), specialty, -cost) then @@ -86,7 +78,8 @@ local function addSpecial2Tool(player, skill, list, specialty, cost) local toolname = tool:get_name() if toolname:find(":"..list) == nil then return end if toolname:find("_"..skill) ~= nil then return end - if specialties.changeXP(player:get_player_name(), specialty, -cost) then + local name = player:get_player_name() + if specialties.changeXP(name, specialty, -cost) then local def = tool:get_definition() local colonpos = toolname:find(":") local modname = toolname:sub(0,colonpos-1) @@ -94,7 +87,7 @@ local function addSpecial2Tool(player, skill, list, specialty, cost) local name = toolname.."_"..skill player:get_inventory():set_stack(list, 1, name) end - show_formspec(player:get_player_name(), specialty) + show_formspec(name, specialty) end local function doTransfer(player, list, factor) @@ -102,14 +95,15 @@ end --GUI Events minetest.register_on_player_receive_fields(function(player, formname, fields) + local name = player:get_player_name() if fields.specialties then - show_formspec(player:get_player_name(), "") + show_formspec(name, "") return end --MINER if fields.miner then - show_formspec(player:get_player_name(), "miner") + show_formspec(name, "miner") return end if fields.healpick then healTool(player, "pick", "miner", 100) end @@ -118,7 +112,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) --LUMBERJACK if fields.lumberjack then - show_formspec(player:get_player_name(), "lumberjack") + show_formspec(name, "lumberjack") return end if fields.healaxe then healTool(player, "axe", "lumberjack", 100) end @@ -127,7 +121,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) --DIGGER if fields.digger then - show_formspec(player:get_player_name(), "digger") + show_formspec(name, "digger") return end if fields.healshovel then healTool(player, "shovel", "digger", 100) end @@ -136,7 +130,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) --FARMER if fields.farmer then - show_formspec(player:get_player_name(), "farmer") + show_formspec(name, "farmer") return end if fields.healhoe then healTool(player, "hoe", "farmer", 100) end @@ -145,7 +139,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) --BUILDER if fields.builder then - show_formspec(player:get_player_name(), "builder") + show_formspec(name, "builder") return end if fields.dorefill then doTransfer(player, "refill", 1) end @@ -156,24 +150,33 @@ end) --XP Events minetest.register_on_dignode(function(pos, oldnode, digger) - if(digger == nil) then + if digger == nil then return end - if(digger:get_wielded_item():is_empty())then + if digger:get_wielded_item():is_empty() then return end local tool = digger:get_wielded_item():get_name() local name = digger:get_player_name() - if(tool:find("pick") ~= nil)then + if tool:find("pick") ~= nil then specialties.changeXP(name, "miner", 1) end - if(tool:find("axe") ~= nil)then + if tool:find("axe") ~= nil then specialties.changeXP(name, "lumberjack", 1) + if tool:find("feller") ~= nil and minetest.get_item_group(oldnode.name, "tree") ~= 0 then + local y = 1 + local abovepos = {x=pos.x,y=pos.y+y,z=pos.z} + while minetest.get_node(abovepos).name == oldnode.name do + minetest.dig_node(abovepos) + y = y+1 + abovepos = {x=pos.x,y=pos.y+y,z=pos.z} + end + end end - if(tool:find("shovel") ~= nil)then + if tool:find("shovel") ~= nil then specialties.changeXP(name, "digger", 1) end - if(oldnode.name:find("farming") ~= nil) then + if oldnode.name:find("farming") ~= nil then specialties.changeXP(name, "farmer", 5) end end) @@ -181,12 +184,12 @@ minetest.register_on_placenode(function(pos, newnode, placer, oldnode) specialties.changeXP(placer:get_player_name(), "builder", 1) end) minetest.register_globalstep(function(dtime) - if(time+dtime < 10) then + if time+dtime < 10 then time = time+dtime else time = 0 - for key in pairs(specialties.players)do - specialties.updateXP(key) + for player in pairs(specialties.players)do + specialties.writeXP(player) end end end) \ No newline at end of file