Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local storage
- local save
- local api = {}
- local protectSlotHook = function()
- return not storage.protectedSlots[turtle.getSelectedSlot() - 1]
- end
- installPreExecutionHook(turtle, "drop", "lib:protectSlot", protectSlotHook, true)
- installPreExecutionHook(turtle, "dropUp", "lib:protectSlot", protectSlotHook, true)
- installPreExecutionHook(turtle, "dropDown", "lib:protectSlot", protectSlotHook, true)
- installPreExecutionHook(turtle, "place", "lib:protectSlot", protectSlotHook, true)
- installPreExecutionHook(turtle, "placeUp", "lib:protectSlot", protectSlotHook, true)
- installPreExecutionHook(turtle, "placeDown", "lib:protectSlot", protectSlotHook, true)
- function api.protectSlot(slot, protectorId)
- local slots = storage.protectedSlots[slot]
- if not slots then
- slots = { protectors = {}, protectorCount = 0 }
- storage.protectedSlots[slot] = slots
- end
- if not slots.protectors[protectorId] then
- slots.protectors[protectorId] = true
- slots.protectorCount = slots.protectorCount + 1
- end
- save()
- end
- api.isSlotProtected = {
- pars = { 1, 2 },
- fct = function(slot, id)
- if not storage.protectedSlots[slot] then
- return false
- end
- if id and not storage.protectedSlots[slot].protectors[id] then
- return false
- end
- return true
- end
- }
- function api.unprotectSlot(slot, protectorId)
- local slots = storage.protectedSlots[slot]
- if not slots[slot] then
- error("slot " .. slot .. " is not protected")
- elseif not slots[slot].protectors[protectorId] then
- error("slot " .. slot .. " is not protected by '" .. protectorId .. "'")
- end
- local slotInfo = slots[slot]
- slotInfo.protectors[protectorId] = nil
- slotInfo.protectorCount = slotInfo.protectorCount - 1
- if slotInfo.protectorCount == 0 then
- slots[slot] = nil
- end
- save()
- end
- return {
- name = "protectSlot",
- api = api,
- onInitStorage = function(_storage, _save)
- storage = _storage
- save = _save
- if not storage.protectedSlots then
- storage.protectedSlots = {}
- end
- end
- }
Add Comment
Please, Sign In to add comment