Advertisement
SNiPeRzCiinema

Iron's Spells & CC: Tweaked Filtering

Apr 16th, 2025 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | Gaming | 0 0
  1. local me = peripheral.find("meBridge")
  2. local trashPeripheral = "minecraft:chest_0"
  3.  
  4. local check_interval = 20
  5. local running = true
  6.  
  7. print(textutils.formatTime(os.time())..": Spell management system started. Checking every " .. check_interval .. " seconds.")
  8.  
  9. local function trim(string)
  10.     return string:gsub("^%s*(.-)%s*$", "%1")
  11. end
  12. local function formatScrollName(name, level)
  13.     name = trim(name:gsub("%[", ""):gsub("Scroll]", ""))
  14.     return string.format("%s (Level %s)", name, level)
  15. end
  16.  
  17. -- Function to clean up spells in the ME system
  18. local function cleanSpells()
  19.     local spells = {}
  20.     local items = me.listItems()
  21.  
  22.     -- Get the items in the ME system
  23.     for _, item in ipairs(items) do  -- Use _ instead of i since i is unused, use item directly
  24.         -- Check if the item is a spell scroll
  25.         if item.name == "irons_spellbooks:scroll" then
  26.             local nbt = item.components["irons_spellbooks:spell_container"].data
  27.  
  28.             -- Get the spell data from the NBT
  29.             for _, data in ipairs(nbt) do
  30.                 local id = data.id
  31.                 local level = data.level
  32.                 local count = item.count
  33.                
  34.                 -- If the spell is not in the list, add it
  35.                 if spells[id] == nil then
  36.                     spells[id] = {
  37.                         highest = level,
  38.                         highest_count = count,
  39.                         list = {}
  40.                     }
  41.                 end
  42.  
  43.                 -- Check if the spell is a higher level, if so, update the highest level
  44.                 if spells[id].highest < level then
  45.                     spells[id].highest = level
  46.                     spells[id].highest_count = count
  47.                 end
  48.  
  49.                 -- Catalogue the spell
  50.                 table.insert(spells[id].list, {item = item, id = id, level = level, count = count})
  51.             end
  52.         end
  53.     end
  54.  
  55.  
  56.     -- Go through the spells and only keep the spells that are either the highest level or 1 below the highest level
  57.     for id, spell in pairs(spells) do
  58.         local keep_level = spell.highest
  59.         local amount_of_highest_level = spell.highest_count
  60.        
  61.         -- Calculate the threshold level to keep
  62.         local threshold = amount_of_highest_level <= 1 and keep_level - 1 or keep_level
  63.        
  64.         -- Process lower level spells for removal
  65.         for _, data in ipairs(spell.list) do
  66.             if data.level < threshold then
  67.                 print(textutils.formatTime(os.time())..": Removing spell: " .. formatScrollName(data.item.displayName, data.level.."/"..spell.highest))
  68.                 me.exportItemToPeripheral(data.item, trashPeripheral)
  69.             end
  70.         end
  71.     end
  72. end
  73.  
  74.  
  75. -- Function to handle termination
  76. local function handleTermination()
  77.     running = false
  78.     print(textutils.formatTime(os.time())..": Shutting down spell management system...")
  79. end
  80.  
  81. -- Register termination handler
  82. os.pullEvent = os.pullEventRaw  -- Prevent terminate event from being blocked
  83.  
  84. -- The main loop
  85. while running do
  86.     local success, error = pcall(cleanSpells)
  87.     if not success then
  88.         print(textutils.formatTime(os.time())..": Error in cleanSpells: " .. tostring(error))
  89.     end
  90.    
  91.     -- Check for terminate event
  92.     local timer = os.startTimer(check_interval)
  93.     local event, param = os.pullEvent()
  94.     if event == "terminate" then
  95.         handleTermination()
  96.     end
  97. end
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement