enderpro100

Item Filter System

Jun 23rd, 2023 (edited)
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.81 KB | Gaming | 0 0
  1. --- Program : Item Filter System
  2. --- Author : LightKnight51
  3. --- Last modification : 23/06/2023
  4.  
  5. --- Utils API
  6. local libraryName = "MarquitoLuaUtils"
  7. local libraryCode = "mVTSwvw1"
  8.  
  9. -- Drop items in slot in specific side
  10. function GetDropItemsFunction(side)
  11.     local turtleDropFunc = nil
  12.     if side == "up" then
  13.         turtleDropFunc = turtle.dropUp
  14.     elseif side == "down" then
  15.         turtleDropFunc = turtle.dropDown
  16.     elseif side == "front" then
  17.         turtleDropFunc = turtle.drop
  18.     end
  19.     return turtleDropFunc
  20. end
  21.  
  22. -- Drop every items accepted down, and other up (it's for a item's trash)
  23. function FilterItems(excludingItems, dropAcceptedFunction, dropExcludedFunction)
  24.     -- Array size
  25.     local arraySize = table.getn(excludingItems)
  26.    
  27.     -- Loop each items inside the turtle
  28.     for m = 1, 16  do
  29.         local isAcceptedItem = true
  30.         -- Get item name in current slot
  31.         local turtleCurrentItemName = nil
  32.         if turtle.getItemDetail(m) ~= nil then
  33.             turtleCurrentItemName = turtle.getItemDetail(m).name
  34.         end
  35.        
  36.         if turtleCurrentItemName ~= nil and arraySize ~= nil and arraySize > 0 then
  37.             for s = 1, arraySize do
  38.                 if string.find(turtleCurrentItemName, excludingItems[s]) then
  39.                     -- If the item in the slot match with an exluding item, we drop up
  40.                     isAcceptedItem = false
  41.                     break
  42.                 end
  43.             end
  44.  
  45.             turtle.select(m)
  46.             if isAcceptedItem then
  47.                 -- Item accepted
  48.                 dropAcceptedFunction()
  49.             else
  50.                 -- Item excluded
  51.                 dropExcludedFunction()
  52.             end
  53.         end
  54.     end
  55. end
  56.  
  57. -- Checks items exit sides
  58. function CheckExitSides(itemsAcceptedExitSide, itemsExcludedExitSide, canStartFilter)
  59.     -- Check accepted exit side
  60.     if itemsAcceptedExitSide == nil or itemsAcceptedExitSide == "" then
  61.         canStartFilter = false
  62.         MarquitoLuaUtils.SetConfValue("items_accepted_exit_side", "")
  63.     elseif itemsAcceptedExitSide ~= "up" and itemsAcceptedExitSide ~= "down" and itemsAcceptedExitSide ~= "front" then
  64.         canStartFilter = false
  65.         MarquitoLuaUtils.SetConfValue("items_accepted_exit_side", "")
  66.         print("Accepted items exit side's values authorized are up, down or front")
  67.     end
  68.     -- Check refused exit side
  69.     if itemsExcludedExitSide == nil or itemsExcludedExitSide == "" then
  70.         canStartFilter = false
  71.         MarquitoLuaUtils.SetConfValue("items_excluded_exit_side", "")
  72.     elseif itemsExcludedExitSide ~= "up" and itemsExcludedExitSide ~= "down" and itemsExcludedExitSide ~= "front" then
  73.         canStartFilter = false
  74.         MarquitoLuaUtils.SetConfValue("items_excluded_exit_side", "")
  75.         print("Excluded items exit side's values authorized are up, down or front")
  76.     end
  77.  
  78.     return canStartFilter
  79. end
  80.  
  81. -- Initialize the item filter system
  82. function InitItemFilterSystem()
  83.     local canStartFilter = true
  84.     -- Set the items sorting list from the conf
  85.     local itemsString = tostring(MarquitoLuaUtils.GetConfValue("items_excluded"))
  86.     -- Items accepted exit side
  87.     local itemsAcceptedExitSide = tostring(MarquitoLuaUtils.GetConfValue("items_accepted_exit_side"))
  88.     -- Items excluded exit side
  89.     local itemsExcludedExitSide = tostring(MarquitoLuaUtils.GetConfValue("items_excluded_exit_side"))
  90.  
  91.     -- Check excluded item list
  92.     local items = {}
  93.     if itemsString ~= nil and itemsString ~= "" then
  94.         -- Array of items refused
  95.         items = MarquitoLuaUtils.Split(itemsString, ",")
  96.  
  97.         if not MarquitoLuaUtils.IsAnArray(items) then
  98.             canStartFilter = false
  99.         end
  100.     else
  101.         canStartFilter = false
  102.         MarquitoLuaUtils.SetConfValue("items_excluded", "")
  103.     end
  104.     -- Checks items exit sides
  105.     canStartFilter = CheckExitSides(itemsAcceptedExitSide, itemsExcludedExitSide, canStartFilter)
  106.  
  107.     -- If checks are correct, we can start the filter
  108.     if canStartFilter then
  109.         local dropAcceptedFunction = GetDropItemsFunction(itemsAcceptedExitSide)
  110.         local dropExcludedFunction = GetDropItemsFunction(itemsExcludedExitSide)
  111.         -- Show items excluded for debug
  112.         print("Items excludeds : " .. itemsString)
  113.         while true do
  114.             FilterItems(items, dropAcceptedFunction, dropExcludedFunction)
  115.             sleep(0.05)
  116.         end
  117.     end
  118. end
  119.  
  120. -- Download program
  121. function DownloadProgram(pastebinCode, programName)
  122.     if not fs.exists(programName) then
  123.         shell.run("pastebin get " .. pastebinCode .. " " .. programName)
  124.     end
  125. end
  126.  
  127. -- We need this library for other programs work
  128. DownloadProgram(libraryCode, libraryName)
  129. os.loadAPI("MarquitoLuaUtils")
  130. -- Initialize the item filter system
  131. InitItemFilterSystem()
Advertisement
Add Comment
Please, Sign In to add comment