Advertisement
Guest User

api.lua

a guest
May 2nd, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.47 KB | None | 0 0
  1.  
  2. armor_api = {
  3.     player_hp = {},
  4. }
  5.  
  6. armor_api.get_armor_textures = function(self, player)
  7.     if not player then
  8.         return
  9.     end
  10.     local name = player:get_player_name()
  11.     local textures = {}
  12.     local player_inv = player:get_inventory()
  13.     for _,v in ipairs({"head", "torso", "legs", "feet"}) do
  14.         local stack = player_inv:get_stack("armor_"..v, 1)
  15.         if stack:get_definition().groups["armor_"..v] then
  16.             local item = stack:get_name()
  17.             textures[v] = item:gsub("%:", "_")..".png"
  18.         end
  19.     end
  20.     return textures
  21. end
  22.  
  23. armor_api.set_player_armor = function(self, player)
  24.     if not player then
  25.         return
  26.     end
  27.     local name = player:get_player_name()
  28.     local player_inv = player:get_inventory()
  29.     local armor_level = 0
  30.     for _,v in ipairs({"head", "torso", "legs", "feet"}) do
  31.         local stack = player_inv:get_stack("armor_"..v, 1)
  32.         local armor = stack:get_definition().groups["armor_"..v] or 0
  33.         armor_level = armor_level + armor
  34.     end
  35.     local armor_groups = {fleshy=100}
  36.     if armor_level > 0 then
  37.         armor_groups.level = math.floor(armor_level / 20)
  38.         armor_groups.fleshy = 100 - armor_level
  39.     end
  40.     player:set_armor_groups(armor_groups)
  41.     uniskins:update_player_visuals(player)
  42. end
  43.  
  44. armor_api.update_armor = function(self, player)
  45.     if not player then
  46.         return
  47.     end
  48.     local name = player:get_player_name()
  49.     local hp = player:get_hp()
  50.     if hp == nil or hp == 0 or hp == self.player_hp[name] then
  51.         return
  52.     end
  53.     if self.player_hp[name] > hp then
  54.         local player_inv = player:get_inventory()
  55.         local armor_inv = minetest.get_inventory({type="detached", name=name.."_outfit"})
  56.         if armor_inv == nil then
  57.             return
  58.         end
  59.         local heal_max = 0
  60.         for _,v in ipairs({"head", "torso", "legs", "feet"}) do
  61.             local stack = armor_inv:get_stack("armor_"..v, 1)
  62.             if stack:get_count() > 0 then
  63.                 local use = stack:get_definition().groups["armor_use"] or 0
  64.                 local heal = stack:get_definition().groups["armor_heal"] or 0
  65.                 local item = stack:get_name()
  66.                 stack:add_wear(use)
  67.                 armor_inv:set_stack("armor_"..v, 1, stack)
  68.                 player_inv:set_stack("armor_"..v, 1, stack)
  69.                 if stack:get_count() == 0 then
  70.                     local desc = minetest.registered_items[item].description
  71.                     if desc then
  72.                         minetest.chat_send_player(name, "Your "..desc.." got destroyed!")
  73.                     end            
  74.                     self:set_player_armor(player)
  75.                 end
  76.                 heal_max = heal_max + heal
  77.             end
  78.         end
  79.         if heal_max > math.random(100) then
  80.             player:set_hp(self.player_hp[name])
  81.             return
  82.         end    
  83.     end
  84.     self.player_hp[name] = hp
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement