melzneni

TurtiLib protectSlot

Aug 18th, 2023 (edited)
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. local storage
  2. local save
  3. local api = {}
  4.  
  5. local protectSlotHook = function()
  6. return not storage.protectedSlots[turtle.getSelectedSlot() - 1]
  7. end
  8.  
  9. installPreExecutionHook(turtle, "drop", "lib:protectSlot", protectSlotHook, true)
  10. installPreExecutionHook(turtle, "dropUp", "lib:protectSlot", protectSlotHook, true)
  11. installPreExecutionHook(turtle, "dropDown", "lib:protectSlot", protectSlotHook, true)
  12. installPreExecutionHook(turtle, "place", "lib:protectSlot", protectSlotHook, true)
  13. installPreExecutionHook(turtle, "placeUp", "lib:protectSlot", protectSlotHook, true)
  14. installPreExecutionHook(turtle, "placeDown", "lib:protectSlot", protectSlotHook, true)
  15.  
  16. function api.protectSlot(slot, protectorId)
  17. local slots = storage.protectedSlots[slot]
  18. if not slots then
  19. slots = { protectors = {}, protectorCount = 0 }
  20. storage.protectedSlots[slot] = slots
  21. end
  22. if not slots.protectors[protectorId] then
  23. slots.protectors[protectorId] = true
  24. slots.protectorCount = slots.protectorCount + 1
  25. end
  26. save()
  27. end
  28.  
  29. api.isSlotProtected = {
  30. pars = { 1, 2 },
  31. fct = function(slot, id)
  32. if not storage.protectedSlots[slot] then
  33. return false
  34. end
  35. if id and not storage.protectedSlots[slot].protectors[id] then
  36. return false
  37. end
  38. return true
  39. end
  40. }
  41.  
  42. function api.unprotectSlot(slot, protectorId)
  43. local slots = storage.protectedSlots[slot]
  44.  
  45. if not slots[slot] then
  46. error("slot " .. slot .. " is not protected")
  47. elseif not slots[slot].protectors[protectorId] then
  48. error("slot " .. slot .. " is not protected by '" .. protectorId .. "'")
  49. end
  50. local slotInfo = slots[slot]
  51. slotInfo.protectors[protectorId] = nil
  52. slotInfo.protectorCount = slotInfo.protectorCount - 1
  53. if slotInfo.protectorCount == 0 then
  54. slots[slot] = nil
  55. end
  56. save()
  57. end
  58.  
  59. return {
  60. name = "protectSlot",
  61. api = api,
  62. onInitStorage = function(_storage, _save)
  63. storage = _storage
  64. save = _save
  65. if not storage.protectedSlots then
  66. storage.protectedSlots = {}
  67. end
  68. end
  69. }
  70.  
Add Comment
Please, Sign In to add comment