Advertisement
hevohevo

ComputerCrafting2015-01-27

Jan 27th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. -- #深夜のComputerCrafting60分一本勝負
  2. -- お題: 「turtle.inspect()」または「turtle.getItemDetail()」を使ったプログラム
  3. -- 2015/01/27 23:00スタート by hevo2
  4. -- 23:44 終了
  5.  
  6. -- turtle.getItemDetail(slot_num)
  7. -- nil or
  8. -- { count=1, name="minecraft:dirt", damage=0}
  9.  
  10.  
  11. -- ##############################################################
  12. -- turtle.getItemCounrt(slot_num)
  13. -- 発展→ turtle.getItemCountAll(item_name, damage) => 個数
  14.  
  15.  
  16. function getItemCountAll(item_name, damage)
  17.   local count = 0
  18.  
  19.   for i=1,16 do
  20.     local detail = turtle.getItemDetail(i)
  21.     if detail then  -- スロットになにかアイテムある
  22.       if string.match(detail["name"],item_name) then  -- アイテム名がマッチする
  23.         if damage == nil or damage==detail["damage"] then  -- ダメージ値の指定があり、それが一致する
  24.           count = count + detail["count"]
  25.         end
  26.       else  -- アイテム名がマッチしない
  27.       end
  28.     else  -- スロットにアイテムない
  29.       -- do nothing
  30.     end
  31.   end
  32.  
  33.   return count
  34. end
  35.  
  36. -- 名前(ダメージ値も可)を指定して、インベントリ内に合計いくつそのアイテムが存在するか数える
  37. -- print(getItemCountAll("dirt",0))
  38. -- print(getItemCountAll("dirt"))
  39. -- print(getItemCountAll("stone"))
  40. -- print(getItemCountAll("stone",5))
  41.  
  42. -- ##############################################################
  43. -- インベントリ内にバラバラに入っているアイテムをできるだけまとめる
  44. -- turtle.transferTo(slot [, quantity])
  45. function collectItem()
  46.   local selected_slot = turtle.getSelectedSlot()
  47.   for i=1,16 do
  48.     local detail = turtle.getItemDetail(i)
  49.     if detail then -- スロットになにかアイテムある
  50.       for j=i,16 do
  51.         local detail2 = turtle.getItemDetail(j)
  52.         if detail2 and detail["name"]==detail2["name"] and detail["damage"]==detail2["damage"] then
  53.           turtle.select(j)
  54.           turtle.transferTo(i)
  55.         end
  56.       end
  57.     else -- スロットにアイテムない
  58.     end
  59.   end
  60.   turtle.select(selected_slot)
  61. end
  62.  
  63. -- collectItem()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement