Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- #深夜のComputerCrafting60分一本勝負
- -- お題: 「turtle.inspect()」または「turtle.getItemDetail()」を使ったプログラム
- -- 2015/01/30 23:00スタート by hevo2
- --
- -- turtle.getItemDetail(slot_num)
- -- nil or
- -- { count=1, name="minecraft:dirt", damage=0}
- -- #############################################################
- -- sortInventory()
- -- インベントリの中のアイテムを整理する。
- -- #############################################################
- -- いままで作った関数持ってくるよ。
- -- インベントリ内にバラバラに入っているアイテムをできるだけまとめる
- -- turtle.transferTo(slot [, quantity]), 1/27作
- function collectItem()
- local selected_slot = turtle.getSelectedSlot()
- for i=1,16 do
- local detail = turtle.getItemDetail(i)
- if detail then -- スロットになにかアイテムある
- for j=i,16 do
- local detail2 = turtle.getItemDetail(j)
- if detail2 and detail["name"]==detail2["name"] and detail["damage"]==detail2["damage"] then
- turtle.select(j)
- turtle.transferTo(i)
- end
- end
- else -- スロットにアイテムない
- end
- end
- turtle.select(selected_slot)
- end
- -- turtle.getItemCounrt(slot_num) , 1/27作
- -- 発展→ getItemCountAll(item_name, damage) => アイテム総数
- function getItemCountAll(item_name, damage)
- local count = 0
- for i=1,16 do
- local detail = turtle.getItemDetail(i)
- if detail then -- スロットになにかアイテムある
- if string.match(detail["name"],item_name) then -- アイテム名がマッチする
- if damage == nil or damage==detail["damage"] then -- ダメージ値の指定があり、それが一致する
- count = count + detail["count"]
- end
- else -- アイテム名がマッチしない
- end
- else -- スロットにアイテムない
- end
- end
- return count
- end
- -- selectItem(name, damage) , 1/26作
- -- スロット内を探し、name(damage値指定があるときはそれも)とマッチするアイテムを選択する。
- function selectItem(name,damage)
- -- local selected_slot = turtle.getSelectedSlot()
- for i=1,16 do
- local item = turtle.getItemDetail(i)
- if type(item)=="table" then -- スロットに何かアイテム
- -- print("slot=",i," ",item["name"]," ",item["damage"])
- if string.match(item["name"],name) then -- 指定アイテムの名前と一致
- if damage==nil or damage == item["damage"] then -- ダメージ値指定がない、または指定と一致
- turtle.select(i)
- return true, item["count"]
- else -- 名前は一致したがダメージ値が違う
- -- do nothing
- end
- else -- 指定アイテムの名前と不一致
- -- do nothing
- end
- else -- スロット内にアイテムなし
- -- do nothing
- end
- end
- -- turtle.select(selected_slot)
- return false, "No item to match"
- end
- -- スロット番号の小さいほうから探し、空っぽのスロットを選択する。
- function selectEmptySlot()
- for i=1, 16 do
- local item = turtle.getItemDetail(i)
- if item == nil then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- function changeItems(slot0, slot1, tmp_slot)
- if turtle.getItemCount(tmp_slot)>0 then return false, "tmp_slot must be empty." end
- if turtle.getItemCount(slot1)>0 then -- 移動先が埋まっている
- turtle.select(slot1)
- turtle.transferTo(tmp_slot) -- slot1のアイテムを退避
- turtle.select(slot0)
- turtle.transferTo(slot1) -- slot0のアイテムをslot1に移動
- turtle.select(tmp_slot)
- turtle.transferTo(slot0) -- 退避スロットのアイテムをslot1に移動
- else
- turtle.select(slot0)
- turtle.transferTo(slot1) -- slot0のアイテムをslot1に移動
- end
- end
- -- sortInventory()
- function sortInventory()
- local tmp_slot = 16
- collectItem() -- まずは整理しようね。
- -- スロット16(tmp_slot)があいてない場合はエラー返して終了するよ
- if turtle.getItemCount(tmp_slot) > 0 then return false, "Slot "..tmp_slot.." must be empty." end
- -- インベントリアイテムの情報を収集するよ
- local items = {}
- for i=1,15 do
- local item = turtle.getItemDetail(i)
- if item then -- スロットにアイテムあります。
- local item_label = item["name"]..(item["damage"]<10 and "0"..item["damage"] or item["damage"])
- -- "minecraft:dirt00" のように名前とダメージ値を連結したラベルを作って集めるよ。
- table.insert(items, item_label)
- else
- -- スロットは空です。
- end
- end
- table.sort(items) -- 文字列順にソートじゃい。
- for i,sort_item_label in ipairs(items) do
- sort_item_name = string.sub(sort_item_label, 1,-3) -- 文字列の、頭~後ろから3番目まで取り出すと名前
- sort_item_damage = tonumber(string.sub(sort_item_label, -2)) -- 文字列の、後ろから3番目~最後まで取り出すとダメージ値
- selectItem(sort_item_name, sort_item_damage)
- local slot0 = turtle.getSelectedSlot()
- --print(slot0..'->'..i)
- changeItems(slot0, i, tmp_slot)
- end
- end
- print(sortInventory())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement