View difference between Paste ID: bZ5YGHyy and HxtJDny9
SHOW: | | - or go back to the newest paste.
1-
os.loadAPI("GraphicsAPI")
1+
os.unloadAPI("button")
2
sleep(0.5)
3
os.loadAPI("button") 
4-
term.redirect(mon)
4+
5
rednet.open("back")
6-
GraphicsAPI.resetSurface()
6+
7-
GraphicsAPI.resetScreen()
7+
items = {}
8
9-
screenWidth, screenHeight = term.getSize()
9+
menu = "index"
10-
print("X:" ..x .." Y:" .. y)
10+
11-
tileX = math.floor(screenWidth / 8)
11+
label = ""
12-
tileY = math.floor(screenHeight / 5)
12+
inputName = ""
13
intputId = ""
14-
print("tileX:" ..tileX .." tileY:" ..tileY)
14+
intputMeta = ""
15
16-
topMenu = {
16+
indexLetter = ""
17-
  {name = "back", text = "", x = 1, y = 1, width = tileX, height = tileY, color = colors.red},
17+
currentPage = 1 --Page number in letter index
18-
  {name = "title", text = "", x = tileX + 1, y = 1, width = tileX * 6, height = tileY, color = colors.blue},
18+
hasNextPage = false
19-
  {name = "next", text = "", x = tileX * 7 + 1, y = 1, width = tileX, height = tileY, color = colors.red},
19+
20-
  {name = "a", text = "A", x = 1, y = tileY + 1, width = tileX, height = tileY, color = colors.green},
20+
currentItem = ""
21-
  {name = "b", text = "B", x = tileX * 2 + 1, y = tileY + 1, width = tileX, height = tileY, color = colors.cyan},
21+
withdrawAmount = ""
22-
  {name = "c", text = "C", x = tileX * 3 + 1, y = tileY + 1, width = tileX, height = tileY, color = colors.green},
22+
craftAmount = ""
23-
  {name = "d", text = "D", x = tileX * 4 + 1, y = tileY + 1, width = tileX, height = tileY, color = colors.cyan},
23+
24-
 -- {name = "e", text = "E", x = tileX * 5 + 1, y = tileY + 1, width = tileX, height = tileY, color = colors.green},
24+
function save(table, name)
25-
--  {name = "f", text = "F", x = tileX * 6 + 1, y = tileY + 1, width = tileX, height = tileY, color = colors.cyan},
25+
  local file = fs.open(name, "w")
26-
 -- {name = "g", text = "G", x = tileX * 7 + 1, y = tileY + 1, width = tileX, height = tileY, color = colors.green},
26+
  file.write(textutils.serialize(table))
27-
 -- {name = "h", text = "H", x = tileX * 8 + 1, y = tileY + 1, width = tileX, height = tileY, color = colors.cyan}
27+
  file.close()
28-
}
28+
29
30-
for i=1, #topMenu do
30+
function load(name)
31-
  GraphicsAPI.drawRect({topMenu[i]["x"], topMenu[i]["y"]}, {topMenu[i]["x"] + topMenu[i]["width"], topMenu[i]["y"] + topMenu[i]["height"]}, topMenu[i]["color"], topMenu[i]["color"])
31+
  local file = fs.open(name, "r")
32
  if file ~= nil then
33
    local data = file.readAll()
34-
--GraphicsAPI.drawRect({1, 1}, {9, 6}, colors.red, colors.red)
34+
    file.close()
35-
--GraphicsAPI.drawRect({10, 1}, {73, 6}, colors.blue, colors.blue)
35+
    return textutils.unserialize(data)
36-
--GraphicsAPI.drawRect({74, 1}, {82, 6}, colors.red, colors.red)
36+
  else
37-
term.restore();
37+
    return nil
38-
GraphicsAPI.update()
38+
  end
39
end
40
41
function split(pString, pPattern) --("A_Small_Dog", "_")
42
  local Table = {}
43
  local fpat = "(.-)" .. pPattern
44
  local last_end = 1
45
  local s, e, cap = pString:find(fpat, 1)
46
  while s do
47
    if s ~= 1 or cap ~= "" then
48
      table.insert(Table, cap)
49
    end
50
    last_end = e + 1
51
    s, e, cap = pString:find(fpat, last_end)
52
  end
53
  if last_end <= #pString then
54
    cap = pString:sub(last_end)
55
    table.insert(Table, cap)
56
  end
57
  return Table
58
end
59
60
function getReply(uuid)
61
  id, message = rednet.receive()
62
  m = split(message, " ")
63
  if m[1] == "inv" then
64
    if m[2] == "replyAmount" then
65
      if tonumber(m[3]) == tonumber(uuid) then
66
        return m[4]
67
      end
68
    end
69
  end
70
  return nil
71
end
72
73
function selectIndex(selected)
74
  print("Selected " ..selected .." in " ..menu)
75
  button.flash(selected)
76
    
77
  if selected == "Menu" then
78
    menu = "index"
79
    currentPage = 0
80
    drawMenu()
81
  
82
  elseif menu == "index" then
83
    if selected == "Add Item" then
84
      inputName = ""
85
      menu = "addItemName"
86
      drawKeyboard("Enter Item Name: ")
87
      selected = ""
88
    elseif string.len(selected) == 1 and menu == "index" then
89
      currentPage = 1
90
      hasNextPage = false
91
      menu = "itemList"
92
      indexLetter = selected
93
      drawList(selected)
94
    end
95
  
96
  elseif menu == "itemList" then
97
    if selected == "Prev" then
98
      if currentPage > 1 then
99
        currentPage = currentPage - 1
100
        drawList(indexLetter)
101
      else
102
        c = string.byte(indexLetter) - 1
103
        if c >= 65 then
104
          currentPage = 1
105
          hasNextPage = false
106
          menu = "itemList"
107
          indexLetter = string.char(c)
108
          drawList(string.char(c))
109
        else
110
          currentPage = 1
111
          hasNextPage = false
112
          hasNextPage = false
113
          menu = "itemList"
114
          indexLetter = "Z"
115
          drawList("Z")
116
        end
117
      end
118
    elseif selected == "Next" then
119
      if hasNextPage then
120
        print("Has next")
121
        currentPage = currentPage + 1
122
        drawList(indexLetter)
123
      else
124
        print("No Next next")
125
        c = string.byte(indexLetter) + 1
126
        if c <= 90 then
127
          currentPage = 1
128
          hasNextPage = false
129
          menu = "itemList"
130
          indexLetter = string.char(c)
131
          drawList(string.char(c))
132
        else
133
          currentPage = 1
134
          hasNextPage = false
135
          menu = "itemList"
136
          indexLetter = "A"
137
          drawList("A")
138
        end
139
      end
140
    else
141
      print("Clicked item")
142
      menu = "item"
143
      currentItem = selected
144
      drawItem(selected)
145
    end
146
    
147
  elseif menu == "item" then
148
    if selected == "Withdraw" then
149
      withdrawAmount = ""
150
      menu = "withdraw"
151
      drawNumpad("Enter Amount: ")
152
      selected = ""
153
    end
154
  
155
  elseif menu == "addItemName" then
156
    print("In Add Item Name")
157
    if selected ~= "Backspace"  and selected ~= "Menu" and string.len(selected) == 1 then
158
      mon.setCursorPos(2, 8)
159
      mon.clearLine()
160
      inputName = inputName ..string.lower(selected)
161
      mon.write(label ..inputName)
162
      selected = ""
163
    elseif selected == "Backspace" then
164
      mon.setCursorPos(2, 8)
165
      mon.clearLine()
166
      inputName = string.sub(inputName, 1, string.len(inputName) - 1)
167
      mon.write(label ..inputName)
168
      selected = ""
169
    elseif selected == "OK" and menu == "addItemName" then
170
      inputId = ""
171
      drawNumpad("Enter Item ID:")
172
      menu = "addItemId"
173
    end
174
    
175
  elseif menu == "withdraw" then
176
    if selected ~= "Backspace" and selected ~= "Menu" and string.len(selected) == 1 then
177
      mon.setCursorPos(2, 8)
178
      mon.clearLine()
179
      if withdrawAmount == nil then
180
        withdrawAmount = string.lower(selected)
181
      else
182
        withdrawAmount = withdrawAmount ..string.lower(selected)
183
      end
184
      mon.write(label ..withdrawAmount)
185
      selected = ""
186
    elseif selected == "Backspace" then
187
      mon.setCursorPos(2, 8)
188
      mon.clearLine()
189
      withdrawAmount = string.sub(withdrawAmount, 1, string.len(withdrawAmount) - 1)
190
      mon.write(label ..withdrawAmount)
191
      selected = ""
192
    elseif selected == "OK" and menu == "withdraw" then
193
      uuid = tostring(items[currentItem]["id"] + items[currentItem]["meta"] * 32768)
194
      amount = nil
195
  
196
      rednet.broadcast("inv getAmount " ..uuid)
197
      while amount == nil do
198
        amount = getReply(uuid)
199
      end
200
  
201
      if tonumber(amount) > tonumber(withdrawAmount) + 1 then
202
        rednet.broadcast("inv withdraw " ..uuid .." " ..withdrawAmount)
203
        drawItem(currentItem)
204
        menu = "item"
205
      else
206
        button.clearTable()
207
        button.setTable("Menu", selectIndex, 2, 80, 2, 4)
208
        button.label(2, 8, "Not enough items to withdraw!")
209
        button.label(2, 9, tostring(amount - 1) .." " ..currentItem .."(s) left in storage.")
210
        button.screen()
211
      end
212
      selected = ""
213
    end
214
    
215
  elseif menu == "addItemId" then
216
    print("In Add Item Name")
217
    if selected ~= "Backspace" and selected ~= "Menu" and string.len(selected) == 1 then
218
      print("In Charachet")
219
      mon.setCursorPos(2, 8)
220
      mon.clearLine()
221
      if inputId == nil then
222
        inputId = string.lower(selected)
223
      else
224
        inputId = inputId ..string.lower(selected)
225
      end
226
      mon.write(label ..inputId)
227
      selected = ""
228
    elseif selected == "Backspace" then
229
      mon.setCursorPos(2, 8)
230
      mon.clearLine()
231
      inputId = string.sub(inputId, 1, string.len(inputId) - 1)
232
      mon.write(label ..inputId)
233
      selected = ""
234
    elseif selected == "OK" and menu == "addItemId" then
235
      inputMeta = ""
236
      drawNumpad("Enter Item Meta:")
237
      menu = "addItemMeta"
238
      selected = ""
239
    end
240
  
241
  elseif menu == "addItemMeta" then
242
    print("In Add Item Meta")
243
    if selected ~= "Backspace" and selected ~= "Menu" and string.len(selected) == 1 then
244
      mon.setCursorPos(2, 8)
245
      mon.clearLine()
246
      if inputMeta == nil then
247
        inputMeta = string.lower(selected)
248
      else
249
        inputMeta = inputMeta ..string.lower(selected)
250
      end
251
      mon.write(label ..inputMeta)
252
      selected = ""
253
    elseif selected == "Backspace" then
254
      mon.setCursorPos(2, 8)
255
      mon.clearLine()
256
      inputMeta = string.sub(inputMeta, 1, string.len(inputMeta) - 1)
257
      mon.write(label ..inputMeta)
258
      selected = ""
259
    elseif selected == "OK" and menu == "addItemMeta" then
260
      createItem()
261
    end
262
  end
263
end
264
265
function pairsByKeys (t, f)
266
  local a = {}
267
  for n in pairs(t) do table.insert(a, n) end
268
    table.sort(a, f)
269
    local i = 0      -- iterator variable
270
    local iter = function ()   -- iterator function
271
    i = i + 1
272
    if a[i] == nil then return nil
273
      else return a[i], t[a[i]]
274
    end
275
  end
276
  return iter
277
end
278
279
function sortList(list)
280
  sortedList = {}
281
  for key, value in pairsByKeys(list) do
282
    sortedList[key] = value
283
  end
284
  
285
  return sortedList
286
end
287
288
function drawItem(name)
289
  button.clearTable()
290
  
291
  button.setTable("Prev", selectIndex, 2, 10, 2, 4)
292
  button.setTable("Menu", selectIndex, 12, 70, 2, 4)
293
  button.setTable("Next", selectIndex, 72, 80, 2, 4)
294
  
295
  button.setTable("Craft", selectIndex, 2, 40, 26, 32)
296
  button.setTable("Withdraw", selectIndex, 42, 80, 26, 32)
297
  
298
  id = items[name]["id"]
299
  meta = items[name]["meta"]
300
  uuid = id + meta * 32768
301
  amount = nil
302
  
303
  rednet.broadcast("inv getAmount " ..uuid)
304
  while amount == nil do
305
    amount = getReply(uuid)
306
  end
307
  
308
  button.label(2, 6, "Item: " ..name)
309
  button.label(2, 10, "ID: " ..tostring(id))
310
  button.label(2, 14, "Meta: " ..tostring(meta))
311
  button.label(2, 18, "UUID: " ..tostring(uuid))
312
  button.label(2, 22, "Amount: " ..amount)
313
  
314
  button.screen()
315
end
316
317
function addItem(name, id, meta)
318
  if name == nil then
319
    print("ITEM NAME IS NULL")
320
  end
321
  
322
  print("NAME: " ..name)
323
  
324
  items[name] = {}
325
  items[name]["id"] = tonumber(id)
326
  items[name]["meta"] = tonumber(meta)
327
  
328
  itemName = ""
329
  itemId = ""
330
  itemMeta = ""
331
  
332
  items = sortList(items)
333
  
334
  save(items, "dictionary")
335
end
336
337
function createItem()
338
  button.clearTable()
339
  
340
  addItem(inputName, inputId, inputMeta)
341
  
342
  button.setTable("Menu", selectIndex, 2, 80, 2, 4)
343
  
344
  button.label(2, 8, "Added " ..inputName .." to item dictionary!")
345
  
346
  for k, v in pairs(items) do
347
    print(k)
348
  end
349
  
350
  menu = "addedItem"
351
  
352
  button.screen()
353
end
354
355
function drawList(letter)
356
  print("Drawing Item List!")
357
  button.clearTable()
358
  
359
  button.setTable("Prev", selectIndex, 2, 10, 2, 4)
360
  button.setTable("Menu", selectIndex, 12, 70, 2, 4)
361
  button.setTable("Next", selectIndex, 72, 80, 2, 4)
362
  
363
 
364
  lastLetter = ""
365
  totalItemCount = 0
366
  itemsInColumn = 0
367
  column = 1
368
  pageItemIsOn = 1
369
  hasNextPage = false
370
  
371
  
372
  
373
  for k, v in pairsByKeys(items) do
374
    --print("Subbed: " .. string.sub(k, 1, 1))
375
    if lastLetter == string.lower(letter) and string.sub(k, 1, 1) ~= string.lower(letter) then
376
      print("Breaking")
377
      break
378
    elseif string.sub(k, 1, 1) == string.lower(letter) then
379
      pageItemIsOn = math.ceil(totalItemCount / 14)
380
      --print("pageItemIsOn: " ..pageItemIsOn .. " Current Page: " ..currentPage)
381
      if pageItemIsOn < 1 then
382
        pageItemIsOn = 1
383
      end
384
385
      --print("Page " ..currentPage)
386
      if pageItemIsOn == currentPage then
387
        if itemsInColumn == 7 then
388
          itemsInColumn = 0;
389
          if column == 1 then
390
            column = 2
391
          else
392
            column = 1
393
          end
394
        end
395
      
396
        if column == 1 then
397
         -- print("Adding to column 1")
398
          button.setTable(k, selectIndex, 2, 40, 6 + itemsInColumn * 4, 6 + itemsInColumn * 4 + 2)
399
        else
400
          --print("Adding to column 2")
401
          button.setTable(k, selectIndex, 42, 80, 6 + itemsInColumn * 4, 6 + itemsInColumn * 4 + 2)
402
        end
403
        itemsInColumn = itemsInColumn + 1
404
      elseif pageItemIsOn > currentPage then
405
        hasNextPage = true
406
      end
407
      
408
      totalItemCount = totalItemCount + 1
409
      lastLetter = string.lower(letter)
410
    end
411
  end
412
  
413
  print("Next? ", hasNextPage)
414
  button.screen()
415
end
416
417
function drawMenu()
418
  button.clearTable()
419
  
420
  button.setTable("Menu", selectIndex, 2, 80, 2, 4)
421
  
422
  button.setTable("A", selectIndex, 2, 10, 6, 11)
423
  button.setTable("B", selectIndex, 12, 20, 6, 11)
424
  button.setTable("C", selectIndex, 22, 30, 6, 11)
425
  button.setTable("D", selectIndex, 32, 40, 6, 11)
426
  button.setTable("E", selectIndex, 42, 50, 6, 11)
427
  button.setTable("F", selectIndex, 52, 60, 6, 11)
428
  button.setTable("G", selectIndex, 62, 70, 6, 11)
429
  button.setTable("H", selectIndex, 72, 80, 6, 11)
430
  
431
  button.setTable("I", selectIndex, 2, 10, 13, 18)
432
  button.setTable("J", selectIndex, 12, 20, 13, 18)
433
  button.setTable("K", selectIndex, 22, 30, 13, 18)
434
  button.setTable("L", selectIndex, 32, 40, 13, 18)
435
  button.setTable("M", selectIndex, 42, 50, 13, 18)
436
  button.setTable("N", selectIndex, 52, 60, 13, 18)
437
  button.setTable("O", selectIndex, 62, 70, 13, 18)
438
  button.setTable("P", selectIndex, 72, 80, 13, 18)
439
  
440
  button.setTable("Q", selectIndex, 2, 10, 20, 25)
441
  button.setTable("R", selectIndex, 12, 20, 20, 25)
442
  button.setTable("S", selectIndex, 22, 30, 20, 25)
443
  button.setTable("T", selectIndex, 32, 40, 20, 25)
444
  button.setTable("U", selectIndex, 42, 50, 20, 25)
445
  button.setTable("V", selectIndex, 52, 60, 20, 25)
446
  button.setTable("W", selectIndex, 62, 70, 20, 25)
447
  button.setTable("X", selectIndex, 72, 80, 20, 25)
448
  
449
  button.setTable("Y", selectIndex, 2, 10, 27, 32)
450
  button.setTable("Z", selectIndex, 12, 20, 27, 32)
451
  button.setTable("Add Item", selectIndex, 22, 80, 27, 32)
452
  
453
  button.screen()
454
end
455
456
function drawKeyboard(tempLabel)
457
  button.clearTable()
458
  
459
  label = tempLabel
460
  
461
  button.setTable("Menu", selectIndex, 2, 80, 2, 4)
462
  
463
  button.setTable("A", selectIndex, 2, 10, 12, 17)
464
  button.setTable("B", selectIndex, 12, 20, 12, 17)
465
  button.setTable("C", selectIndex, 22, 30, 12, 17)
466
  button.setTable("D", selectIndex, 32, 40, 12, 17)
467
  button.setTable("E", selectIndex, 42, 50, 12, 17)
468
  button.setTable("F", selectIndex, 52, 60, 12, 17)
469
  button.setTable("G", selectIndex, 62, 70, 12, 17)
470
  button.setTable("H", selectIndex, 72, 80, 12, 17)
471
  
472
  button.setTable("I", selectIndex, 2, 10, 19, 24)
473
  button.setTable("J", selectIndex, 12, 20, 19, 24)
474
  button.setTable("K", selectIndex, 22, 30, 19, 24)
475
  button.setTable("L", selectIndex, 32, 40, 19, 24)
476
  button.setTable("M", selectIndex, 42, 50, 19, 24)
477
  button.setTable("N", selectIndex, 52, 60, 19, 24)
478
  button.setTable("O", selectIndex, 62, 70, 19, 24)
479
  button.setTable("P", selectIndex, 72, 80, 19, 24)
480
  
481
  button.setTable("Q", selectIndex, 2, 10, 26, 28)
482
  button.setTable("R", selectIndex, 12, 20, 26, 28)
483
  button.setTable("S", selectIndex, 22, 30, 26, 28)
484
  button.setTable("T", selectIndex, 32, 40, 26, 28)
485
  button.setTable("U", selectIndex, 42, 50, 26, 28)
486
  
487
  button.setTable("V", selectIndex, 2, 10, 30, 32)
488
  button.setTable("W", selectIndex, 12, 20, 30, 32)
489
  button.setTable("X", selectIndex, 22, 30, 30, 32)
490
  button.setTable("Y", selectIndex, 32, 40, 30, 32)
491
  button.setTable("Z", selectIndex, 42, 50, 30, 32)
492
  
493
  button.setTable(" ", selectIndex, 52, 60, 26, 32)
494
  button.setTable("Backspace", selectIndex, 62, 70, 26, 32)
495
  button.setTable("OK", selectIndex, 72, 80, 26, 32)
496
  
497
  mon.setCursorPos(2, 8)
498
  mon.write(label)
499
  
500
  button.screen()
501
end
502
503
function drawNumpad(tempLabel)
504
  button.clearTable()
505
  
506
  label = tempLabel
507
  
508
  button.setTable("Menu", selectIndex, 2, 80, 2, 4)
509
  
510
  button.setTable("0", selectIndex, 2, 10, 13, 32)
511
  
512
  button.setTable("1", selectIndex, 12, 20, 13, 18)
513
  button.setTable("2", selectIndex, 22, 30, 13, 18)
514
  button.setTable("3", selectIndex, 32, 40, 13, 18)
515
  
516
  button.setTable("4", selectIndex, 12, 20, 20, 25)
517
  button.setTable("5", selectIndex, 22, 30, 20, 25)
518
  button.setTable("6", selectIndex, 32, 40, 20, 25)
519
 
520
  button.setTable("7", selectIndex, 12, 20, 27, 32)
521
  button.setTable("8", selectIndex, 22, 30, 27, 32)
522
  button.setTable("9", selectIndex, 32, 40, 27, 32)
523
  
524
  button.setTable("Backspace", selectIndex, 42, 60, 13, 32)
525
  button.setTable("OK", selectIndex, 62, 80, 13, 32)
526
  
527
  mon.setCursorPos(2, 8)
528
  mon.write(label)
529
  
530
  button.screen()
531
end
532
533
function getClick()
534
  event, side, x, y = os.pullEvent("monitor_touch")
535
  button.checkxy(x, y)
536
end
537
538
items = load("dictionary")
539
540
if items == nil then
541
  items = {}
542
end
543
544
drawMenu()
545
546
while true do
547
  getClick()
548
end