SNiPeRzCiinema

Irons Spell Books scroll clean up

Nov 5th, 2025 (edited)
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. local me = peripheral.find("me_bridge")
  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.getItems()
  21.     local itemsToTrash = {}
  22.  
  23.     -- Scan all items and track highest level per spell ID
  24.     for _, item in ipairs(items) do
  25.         if item.name == "irons_spellbooks:scroll" then
  26.             local nbt = item.components["irons_spellbooks:spell_container"].data
  27.             -- Since there's only one spell per scroll, take the first (and only) entry
  28.            local data = nbt[1]
  29.            if data then
  30.                local id = data.id
  31.                local level = data.level
  32.                if not spells[id] or spells[id].highest < level then
  33.                    spells[id] = {highest = level}
  34.                end
  35.                -- Mark for potential trashing if not the highest
  36.                table.insert(itemsToTrash, {item = item, id = id, level = level})
  37.            end
  38.        end
  39.    end
  40.  
  41.    -- Now, trash items that are not the highest level for their spell ID
  42.    for _, trashData in ipairs(itemsToTrash) do
  43.        local spell = spells[trashData.id]
  44.        if trashData.level < spell.highest then
  45.            print(textutils.formatTime(os.time())..": Removing spell: " .. formatScrollName(trashData.item.displayName, trashData.level .. "/" .. spell.highest))
  46.            me.exportItem(trashData.item, trashPeripheral)
  47.        end
  48.    end
  49. end
  50.  
  51.  
  52. -- Function to handle termination
  53. local function handleTermination()
  54.    running = false
  55.    print(textutils.formatTime(os.time())..": Shutting down spell management system...")
  56. end
  57.  
  58. -- Check for ME bridge
  59. if not me then
  60.    error("ME Bridge peripheral not found!")
  61. end
  62.  
  63. -- Initial cleanup
  64. print(textutils.formatTime(os.time())..": Performing initial spell cleanup...")
  65. local success, err = pcall(cleanSpells)
  66. if not success then
  67.    print(textutils.formatTime(os.time())..": Initial error in cleanSpells: " .. tostring(err))
  68. end
  69.  
  70. -- The main loop: wait for timer or termination
  71. while running do
  72.    local timer = os.startTimer(check_interval)
  73.    local event = os.pullEvent()
  74.    if event == "timer" then
  75.        local success, err = pcall(cleanSpells)
  76.        if not success then
  77.            print(textutils.formatTime(os.time())..": Error in cleanSpells: " .. tostring(err))
  78.        end
  79.    elseif event == "terminate" then
  80.        handleTermination()
  81.        break
  82.    end
  83. end
  84.  
Advertisement
Add Comment
Please, Sign In to add comment