daily pastebin goal
87%
SHARE
TWEET

SummonMobTower0_1

hevohevo Feb 22nd, 2015 216 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- #######################################
  2. -- Summon MobTower
  3. --   required Command Computer in ComputerCraft 1.7 or over.
  4. -- version 0.1, License MIT
  5. -- Twitter: @hevohevo
  6.  
  7. -- #######################################
  8. -- Functions
  9. local function tbl2TagStr(tbl)
  10.   local str = string.gsub(textutils.serializeJSON(tbl),"\"", "")
  11.   return str
  12. end
  13.  
  14. local function head(array)
  15.   return array[1]
  16. end
  17.  
  18. local function rest(array)
  19.   return {select(2,unpack(array))}
  20. end
  21.  
  22. local function _makeOptsTbl(array)
  23.   local tmp_tbl = {}
  24.  
  25.   if type(array)~="table" or #array==0 then
  26.     return nil
  27.   else
  28.     tmp_tbl=head(array)
  29.     tmp_tbl["Riding"]=_makeOptsTbl(rest(array))
  30.     return tmp_tbl
  31.   end
  32. end
  33.  
  34. -- 上から順番に
  35. -- tbl = {{id="Slime",Size=1},{id="Slime",Size=1.5},{id="Slime",Size=2}}
  36. function summonMobTower(tbl, x,y,z)
  37.   assert(tbl[1] and tbl[1]["id"], "invalid arguments")
  38.   local top_mob = tbl[1]
  39.   local data_tags = {}
  40.   for k,v in pairs(top_mob) do
  41.     if k ~= "id" then
  42.       data_tags[k]=v
  43.     end
  44.   end
  45.   if #tbl>1 then
  46.     data_tags["Riding"]=_makeOptsTbl(rest(tbl))
  47.   end
  48.   local text = string.format("summon %s %d %d %d %s", top_mob["id"], x,y,z, tbl2TagStr(data_tags))
  49.   print(text)
  50.   commands.exec(text)
  51. end
  52.  
  53. -- #######################################
  54. -- Main
  55.  
  56. -- 上から順に配列で指定
  57. -- opts = {{id="Slime",Size=1, CustomName:"hoge"},
  58. --         {id="Slime",Size=1},
  59. --         {id="Villager"}}
  60. -- summonMobTower(opts, x,y,z)
  61.  
  62. function Test()
  63.   local cx,cy,cz = commands.getBlockPosition()
  64.   local x, y, z = cx, cy+1, cz
  65.   local tower_tbl = {}
  66.   for i=3,0,-0.2 do
  67.     table.insert(tower_tbl, {id="Slime",Size=i})
  68.   end
  69.   summonMobTower(tower_tbl, x,y,z)
  70. end
  71.  
  72. for i=1,20 do
  73.   Test()
  74.   os.sleep(1)
  75. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top