Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- state enums
- local STATE_NONE = "NONE"
- local STATE_STEEL = "Hardened Steel"
- local STATE_NITRO = "Nitro Crystal"
- local STATE_ENDER = "Ender Core"
- local STATE_BLAZE = "Blazing Crystal Block"
- local STATE_NIOTIC = "Niotic Crystal"
- local STATE_SPIRIT = "Spirited Crystal"
- -- item -> state table
- -- this table will be used to determine what item we are currently crafting
- -- and what items need to be transfered in order to craft that item
- local state_from_item_table = {
- -- energized steel
- ["minecraft:iron_ingot"]=STATE_STEEL,
- ["minecraft:gold_ingot"]=STATE_STEEL,
- -- nitro
- ["minecraft:redstone_block"]=STATE_NITRO,
- ["minecraft:nether_star"]=STATE_NITRO,
- ["powah:blazing_crystal_block"]=STATE_NITRO,
- -- ender core
- ["minecraft:ender_eye"]=STATE_ENDER,
- ["powah:dielectric_casing"]=STATE_ENDER,
- ["powah:capacitor_basic_tiny"]=STATE_ENDER,
- -- blaze crystal
- ["botania:blaze_block"]=STATE_BLAZE,
- -- niotic crystal
- ["minecraft:diamond"]=STATE_NIOTIC,
- -- spirited crystal
- ["minecraft:emerald"]=STATE_SPIRIT
- }
- -- item table describing what items are required to craft in each state
- local state_item_recipies = {
- [STATE_STEEL] = {
- ["minecraft:gold_ingot"] = 1,
- ["minecraft:iron_ingot"] = 1
- },
- [STATE_NITRO] = {
- ["minecraft:redstone_block"] = 2,
- ["powah:blazing_crystal_block"] = 1,
- ["minecraft:nether_star"] = 1
- },
- [STATE_ENDER] = {
- ["minecraft:ender_eye"] = 1,
- ["powah:dielectric_casing"] = 1,
- ["powah:capacitor_basic_tiny"] = 1
- },
- [STATE_BLAZE] = {
- ["botania:blaze_block"] = 1
- },
- [STATE_NIOTIC] = {
- ["minecraft:diamond"] = 1
- },
- [STATE_SPIRIT] = {
- ["minecraft:emerald"] = 1
- }
- }
- ---------------------------------
- -- DO NOT EDIT BELOW THIS LINE --
- ---------------------------------
- -- the current state of the system
- cur_state = STATE_NONE
- -- helper function for cloning the item table
- function table.clone(org)
- local tbl = {}
- for k,v in pairs(org) do
- tbl[k] = v
- end
- return tbl
- end
- -- helper function for len of table
- function table.count(tbl)
- local count = 0
- for _ in pairs(tbl) do
- count = count + 1
- end
- return count
- end
- -- determine what state we should enter
- -- based on what item we find in the chest
- function determineStateFromItem(item)
- local state = state_from_item_table[item]
- if state == nil then
- return STATE_NONE
- else
- return state
- end
- end
- -- find where the chest is
- local chest = peripheral.find("minecraft:chest")
- local orb = peripheral.find("powah:energizing_orb")
- -- waits for the orb's inventory to be empty then
- -- signals that the orb is ready to craft another item
- function waitForReset()
- while true do
- if table.count(orb.list()) == 0 then
- break
- end
- end
- cur_state = STATE_NONE
- end
- -- transfers the requested number of items from the slot in the chest to the orb,
- -- if there is not enough items in the stack to meet the demands of the recipe
- -- the value in the recipe will be decremented by the amount we could transfer
- function transferItem(itemList, slot, item)
- -- # items we need to transfer
- local count = itemList[item.name]
- -- try to transfer the items
- local itemsTransfered = chest.pushItems(peripheral.getName(orb), slot, count)
- -- number of items left required to craft the recipe
- local itemsLeft = count - itemsTransfered
- -- if 0 then we have enough of this item and can stop searching for it,
- -- otherwise set to the amount left required to craft
- if itemsLeft <= 0 then
- itemList[item.name] = nil
- else
- itemList[item.name] = itemsLeft
- end
- end
- -- transfers the list of items from the chest to the orb
- -- this will remove items from the list
- function transferItems(itemList)
- -- loop until all items are transfered
- while next(itemList) ~= nil do
- -- check every slot in the chest for any of the items we need to craft the current recipe
- for slot, item in pairs(chest.list()) do
- if itemList[item.name] ~= nil then
- transferItem(itemList, slot, item)
- end
- end
- end
- end
- -- constantly check the chest for items, 1 second sleeps between
- function waitForItems()
- while cur_state == STATE_NONE do
- for slot, item in pairs(chest.list()) do
- cur_state = determineStateFromItem(item.name)
- if cur_state ~= STATE_NONE then
- break
- end
- end
- -- prevent looping forever with zero delay!
- if cur_state == STATE_NONE then
- os.sleep(1)
- end
- end
- end
- -- print out program info
- print("Running with recipies:")
- printed_recipes = {}
- for k, v in pairs(state_from_item_table) do
- if printed_recipes[v] == nil then
- printed_recipes[v] = true
- print(("\t%s = {"):format(v))
- for item, count in pairs(state_item_recipies[v]) do
- print(("\t\t%d\t%s"):format(count, item))
- end
- print("\t}")
- end
- end
- print("System started successfully!")
- print("")
- print("--------------------------------------")
- print("-- Mr. Fancy Dan's Crafting Manager --")
- print("--------------------------------------")
- print("")
- -- main loop
- while true do
- if state_item_recipies[cur_state] ~= nil then
- -- if we are in a state which has a recipe
- local items = table.clone(state_item_recipies[cur_state])
- -- transfer the required items
- transferItems(items)
- -- then wait for the reset signal which tells us we are done crafting that item
- waitForReset()
- else
- if cur_state ~= STATE_NONE then
- print(("Unknown state '%d' detected"):format(cur_state))
- else
- -- if we don't have a recipe we are in the none state, so we should wait for the chest to
- -- have an item which we can use to craft
- waitForItems()
- end
- end
- os.sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement