Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Program : Item Filter System
- --- Author : LightKnight51
- --- Last modification : 23/06/2023
- --- Utils API
- local libraryName = "MarquitoLuaUtils"
- local libraryCode = "mVTSwvw1"
- -- Drop items in slot in specific side
- function GetDropItemsFunction(side)
- local turtleDropFunc = nil
- if side == "up" then
- turtleDropFunc = turtle.dropUp
- elseif side == "down" then
- turtleDropFunc = turtle.dropDown
- elseif side == "front" then
- turtleDropFunc = turtle.drop
- end
- return turtleDropFunc
- end
- -- Drop every items accepted down, and other up (it's for a item's trash)
- function FilterItems(excludingItems, dropAcceptedFunction, dropExcludedFunction)
- -- Array size
- local arraySize = table.getn(excludingItems)
- -- Loop each items inside the turtle
- for m = 1, 16 do
- local isAcceptedItem = true
- -- Get item name in current slot
- local turtleCurrentItemName = nil
- if turtle.getItemDetail(m) ~= nil then
- turtleCurrentItemName = turtle.getItemDetail(m).name
- end
- if turtleCurrentItemName ~= nil and arraySize ~= nil and arraySize > 0 then
- for s = 1, arraySize do
- if string.find(turtleCurrentItemName, excludingItems[s]) then
- -- If the item in the slot match with an exluding item, we drop up
- isAcceptedItem = false
- break
- end
- end
- turtle.select(m)
- if isAcceptedItem then
- -- Item accepted
- dropAcceptedFunction()
- else
- -- Item excluded
- dropExcludedFunction()
- end
- end
- end
- end
- -- Checks items exit sides
- function CheckExitSides(itemsAcceptedExitSide, itemsExcludedExitSide, canStartFilter)
- -- Check accepted exit side
- if itemsAcceptedExitSide == nil or itemsAcceptedExitSide == "" then
- canStartFilter = false
- MarquitoLuaUtils.SetConfValue("items_accepted_exit_side", "")
- elseif itemsAcceptedExitSide ~= "up" and itemsAcceptedExitSide ~= "down" and itemsAcceptedExitSide ~= "front" then
- canStartFilter = false
- MarquitoLuaUtils.SetConfValue("items_accepted_exit_side", "")
- print("Accepted items exit side's values authorized are up, down or front")
- end
- -- Check refused exit side
- if itemsExcludedExitSide == nil or itemsExcludedExitSide == "" then
- canStartFilter = false
- MarquitoLuaUtils.SetConfValue("items_excluded_exit_side", "")
- elseif itemsExcludedExitSide ~= "up" and itemsExcludedExitSide ~= "down" and itemsExcludedExitSide ~= "front" then
- canStartFilter = false
- MarquitoLuaUtils.SetConfValue("items_excluded_exit_side", "")
- print("Excluded items exit side's values authorized are up, down or front")
- end
- return canStartFilter
- end
- -- Initialize the item filter system
- function InitItemFilterSystem()
- local canStartFilter = true
- -- Set the items sorting list from the conf
- local itemsString = tostring(MarquitoLuaUtils.GetConfValue("items_excluded"))
- -- Items accepted exit side
- local itemsAcceptedExitSide = tostring(MarquitoLuaUtils.GetConfValue("items_accepted_exit_side"))
- -- Items excluded exit side
- local itemsExcludedExitSide = tostring(MarquitoLuaUtils.GetConfValue("items_excluded_exit_side"))
- -- Check excluded item list
- local items = {}
- if itemsString ~= nil and itemsString ~= "" then
- -- Array of items refused
- items = MarquitoLuaUtils.Split(itemsString, ",")
- if not MarquitoLuaUtils.IsAnArray(items) then
- canStartFilter = false
- end
- else
- canStartFilter = false
- MarquitoLuaUtils.SetConfValue("items_excluded", "")
- end
- -- Checks items exit sides
- canStartFilter = CheckExitSides(itemsAcceptedExitSide, itemsExcludedExitSide, canStartFilter)
- -- If checks are correct, we can start the filter
- if canStartFilter then
- local dropAcceptedFunction = GetDropItemsFunction(itemsAcceptedExitSide)
- local dropExcludedFunction = GetDropItemsFunction(itemsExcludedExitSide)
- -- Show items excluded for debug
- print("Items excludeds : " .. itemsString)
- while true do
- FilterItems(items, dropAcceptedFunction, dropExcludedFunction)
- sleep(0.05)
- end
- end
- end
- -- Download program
- function DownloadProgram(pastebinCode, programName)
- if not fs.exists(programName) then
- shell.run("pastebin get " .. pastebinCode .. " " .. programName)
- end
- end
- -- We need this library for other programs work
- DownloadProgram(libraryCode, libraryName)
- os.loadAPI("MarquitoLuaUtils")
- -- Initialize the item filter system
- InitItemFilterSystem()
Advertisement
Add Comment
Please, Sign In to add comment