View difference between Paste ID: qcah5aB9 and e5072iZj
SHOW: | | - or go back to the newest paste.
1
local backgroundColor = colors.blue
2
local altBackgroundColor = colors.lightBlue
3
local greetingOne = "active link: "
4
--"Please choose your"
5
local greetingTwo = "no active link"
6
--"destination"
7
local timerDelay = 8
8
local refreshScreenDelay = 4
9
10
-- computer front points northward
11
local dirs = {front="north",left="east",back="south",right="west"}
12
local dir = nil
13
14
local mon = peripheral.find("monitor")
15
16
local chest = nil
17
local slot = nil
18
19
for i,v in pairs(rs.getSides()) do
20
  if peripheral.getType(v) == "ender_chest" then
21
    chest = peripheral.wrap(v)
22
    dir = dirs[v]
23
  elseif peripheral.getType(v) == "book_receptacle" then
24
    slot = peripheral.wrap(v)
25
  end
26
end
27
28
if not chest or not dir or not slot or not mon then print("Error while trying to find chest, slot or monitor"); error() end
29-
local chestSlot = nil
29+
30
function removeActiveBook()
31
  if slot.getStackInSlot(1) then 
32
    if slot.pushItem(dir,1,1) ~= 1 then print("Error while moving book to chest") end
33-
print("remove ",chestSlot)
33+
34-
    if chestSlot then 
34+
35-
      if slot.pushItemIntoSlot(dir,1,1,chestSlot) ~=1 then 
35+
36-
        if slot.pushItem(dir,1,1) ~= 1 then print("Error while moving book into chest"); error() end
36+
37
sleep(1)
38-
      chestSlot = nil
38+
39-
    else 
39+
local nDest = nil 
40-
      if slot.pushItem(dir,1,1) ~= 1 then print("Error while moving book to chest"); error() end
40+
--4
41
local destTable = {}
42
--local destNames = {}
43
local timer = nil
44
45
function padStr(str, len)
46
  if #str > len then do return string.sub(str,1,len) end
47
  else do return str .. string.rep(' ', len - #str) end
48-
local nDest = 4
48+
49
end
50
51
function waitForTouchEvent()
52
  local refreshScreenTimer = os.startTimer(refreshScreenDelay)
53
  repeat
54
    event, side, monX, monY = os.pullEvent() --"monitor_touch")
55
    if event == "timer" and (side == timer or side == refreshScreenTimer) then
56
      if side == timer then removeActiveBook() end
57
      drawUserInterface()
58
      if side == refreshScreenTimer then refreshScreenTimer = os.startTimer(refreshScreenDelay) end
59
    end
60
  until event == "monitor_touch"
61-
    if event == "timer" and side == timer then
61+
  --removeActiveBook()
62
  --drawUserInterface()
63
  if nDest > 0 and (monY-3 > 0) and (monY-3 <= nDest) then
64
    --if slot.pullItem(dir,destTable[monY - 3][1],1) ~= 1 then print("Error while moving book to slot") end
65
    local destination = destTable[monY-3][1][2]
66-
  removeActiveBook()
66+
    drawUserInterface()
67
    if destination == destTable[monY-3][1][2] then 
68-
    chestSlot = destTable[monY - 3]
68+
69-
    print("place ",chestSlot)
69+
      for _,destinationID in pairs(destTable[monY - 3]) do if slot.pullItem(dir,destinationID[1],1) == 1 then break end end
70-
    if slot.pullItem(dir,chestSlot,1) ~= 1 then chestSlot = nil; print("Error while moving book to slot") end
70+
      timer = os.startTimer(timerDelay)
71-
    timer = os.startTimer(timerDelay)
71+
72
  else
73
    removeActiveBook()
74
  end
75
end
76
77-
  local books = chest.getAllStacks()
77+
78
  destTable = {}
79
 -- destNames = {}
80
  local items = chest.getAllStacks()
81
  local books = {}
82
  for n,item in pairs(items) do
83
    if item.rawName == "item.myst.linkbook" then
84
      if not books[item.destination] then
85
        books[item.destination] = {n}
86
      else
87
        books[item.destination][#(books[item.destination])+1] = n
88
      end
89
    end
90
  end
91
  local sortedBooks = {}
92
  for n in pairs(books) do table.insert(sortedBooks,n) end
93
  table.sort(sortedBooks)
94-
  for n,book in pairs(books) do
94+
95-
    if book.rawName == "item.myst.linkbook" then
95+
96-
      i = i + 1
96+
97-
      destTable[i] = n
97+
98-
      mon.setCursorPos(1, i + 3)
98+
99-
      if (i % 2 == 0) then
99+
100-
        mon.setBackgroundColor(altBackgroundColor)
100+
101
    mon.write(greetingOne)
102-
        mon.setBackgroundColor(backgroundColor)
102+
103
    mon.write(active.destination)
104-
      name = padStr(book.destination,monX)
104+
105-
      mon.write(name)        
105+
106
  end
107
  --
108
  --mon.write(greetingTwo)
109
  nDest = 0
110
  local i = 0
111
  for _,book in ipairs(sortedBooks) do
112
    i = i + 1
113
    destTable[i] = {books[book],book}
114
    mon.setCursorPos(1, i + 3)
115
    if (i % 2 == 0) then
116
      mon.setBackgroundColor(altBackgroundColor)
117
    else
118
      mon.setBackgroundColor(backgroundColor)
119
    end
120
   -- destNames[#destNames+1] = book
121
    name = padStr(book,monX)
122
    mon.write(name)        
123
  end
124
  if not i then 
125
    mon.setCursorPos(1, 4)
126
    mon.write("No Entries")
127
  end
128
  nDest = #destTable
129
end
130
131
while true do
132
  drawUserInterface()
133
  waitForTouchEvent()
134
end