Jump to content

Module:Minecraft/Mob

From Wikibooks, open books for an open world
local p = {}

p.get_probable_value = function(frame)
  if tonumber(frame.args.damage)>0 or tonumber(frame.args.danger)>0 then
    if frame.args.value=="spawning" then return "In the overworld, in low light levels." end
  end
  if frame.args.value=="spawning" then return "In the Overworld, on grass, in high light levels." end
  return ""
end

p.build_page = function(frame)
  --Check errors
  if not frame.args.health or #frame.args.health==0 or (tonumber(frame.args.health) and tonumber(frame.args.health)<1) then error('[[Category:Book:Minecraft/AttentionNeeded]]The variable "health" was expected. If it was defined, it might be lower than 1. Please correct to a number larger than 0') end

  local nocat=false
  if frame.args.nocat and #frame.args.nocat>0 then nocat=true end

  local out = ""

  --[[
    This module will be used inside the book "Minecraft". This function is the main function, and creates the top of all mob pages.
    This function will go through the following steps:
       1. Make infobox
       2. Find categories
       3. Return the output
  ]]--


  --============= Make infobox =============
  -- Get main category, used here and elsewhere
  local main_cat = 'Uncategorized'
  if frame.args.category then
    main_cat = frame.args.category
  end
  -- Get danger text
  local danger_text = ''
  if frame.args.danger and #frame.args.danger>0 then
    danger_text = frame.args.danger .. '/10'
  end
  -- Make infobox
  local infobox = frame:expandTemplate{
    title='Infobox',
    args={
     bodystyle   = "width:58%;float:left;background-color:#fff",
     headerstyle = "background-color:#ddf",
     labelstyle = "background-color:#ccf",
     header1     = frame.args.name,
     label2      = 'Health',
     data2       = frame.args.health,
     label3      = 'Defence',
     data3       = frame.args.defence,
     label4      = 'Damage',
     data4       = frame.args.damage,
     label5      = 'Danger',
     data5       = danger_text,
     label6      = 'Spawn rule',
     data6       = frame.args.spawning,
     label7      = 'Category',
     data7       = '[[:Category:Book:Minecraft/' .. main_cat .. '|' .. main_cat .. ']]',
    }
  }


  -- ============== Categories =============
  local cats=""
  if not nocat then
    cats = '[[Category:Book:Minecraft/Mobs]] [[Category:Book:Minecraft/' .. main_cat ..']]'
    if main_cat=="Uncategorized" then cats=cats+"[[Category:Book:Minecraft/AttentionNeeded]]" end
    local types = ''
    if frame.args.types then
     types='[[Category:Book:Minecraft/' ..

      mw.ustring.gsub(

      -- Replace double commas (2)
      mw.ustring.gsub(

      -- Remove whitespace
      mw.ustring.gsub(frame.args.types,' ',','),

      ',,', ','), -- (2)

      ',', ']][[Category:Book:Minecraft/') .. ']]'
     out = out .. types
    end
  end

  --================== Output =================
  out = out .. infobox .. cats;
  return out;
end

return p