View difference between Paste ID: WajGr46K and EW0RMSBp
SHOW: | | - or go back to the newest paste.
1
function exchange(list,y,page)
2
    w,h = term.getSize()
3
    y=y+(h-1)*(page-1)
4
    local l = list[y]
5
    if y<=#list then
6
        invcheck = "execute unless entity @p[nbt={Inventory:[{id:\""..l[3].."\",Count:"..l[4].."b}]}] run "
7
        checkstr = "execute if entity @p[nbt={Inventory:[{id:\""..l[3].."\",Count:"..l[4].."b}]}] run "
8
        checkmoney = "execute if entity @p[scores={money="..l[2].."..}] run "
9
        invcheckmoney = "execute unless entity @p[scores={money="..l[2].."..}] run "
10
        if l[1] == "buy" then
11
            exec(checkmoney.."give @p "..l[3].." "..l[4])
12
            exec(checkmoney.."tellraw @p [{\"text\":\"Success! (You had \",\"color\":\"green\"},{\"score\":{\"name\":\"*\",\"objective\":\"money\"},\"color\":\"green\"},{\"text\":\"$)\",\"color\":\"green\"}]")
13
            exec(invcheckmoney.."tellraw @p [{\"text\":\"Not enough money! (You have \",\"color\":\"red\"},{\"score\":{\"name\":\"*\",\"objective\":\"money\"},\"color\":\"red\"},{\"text\":\"$)\",\"color\":\"red\"}]")
14
            exec(checkmoney.."scoreboard players remove @p money "..l[2])
15
        else
16
            exec(checkstr.."scoreboard players add @p money "..l[2])
17
            exec(checkstr.."tellraw @p [{\"text\":\"Success! (You have \",\"color\":\"green\"},{\"score\":{\"name\":\"*\",\"objective\":\"money\"},\"color\":\"green\"},{\"text\":\"$)\",\"color\":\"green\"}]")
18
            exec(invcheck.."tellraw @p [{\"text\":\"Not enough items!\",\"color\":\"red\"}]")
19
            exec(checkstr.."clear @p "..l[3].." "..l[4])
20
        end
21
        exec(checkstr.."clear @p "..l[1].." "..l[2])
22
    end
23
end
24
function draw(list,page)
25
    term.redirect(peripheral.wrap("top"))
26
    peripheral.wrap("top").setTextScale(0.5)
27
    term.clear()
28
    term.setCursorPos(1,1)
29
    w,h = term.getSize()
30
    pages = 1
31
    if h>#list then
32
        pages = math.floor(#list/(h-1))
33
    end
34
    for i=1+(h-1)*(page-1),(h-1)*page do
35
        if i>#list then break end
36
        if list[i][1] == "sell" then
37
            print(list[i][4].." "..list[i][3].." -> "..list[i][2].."$")
38
        else
39
            print(list[i][2].."$ -> "..list[i][4].." "..list[i][3])
40
        end
41
    end
42
    term.setCursorPos(1,h)
43
    write("<<<")
44
    term.setCursorPos(math.floor(w/2),h)
45
    write(page)
46
    term.setCursorPos(w-2,h)
47
    write(">>>")
48
end
49
--------------+
50
page = 1   --+#
51
table = {} --+#
52
--------------+
53
table[1] = {"sell",42,"minecraft:dirt",1}
54
table[2] = {"buy",69,"minecraft:amethyst_shard",1}
55
---------------
56
draw(table,page)
57
while true do
58
    name,side,x,y = os.pullEvent("monitor_touch")
59
    w,h = term.getSize()
60
    if y<h then
61
        exchange(table,y,page)
62
    else
63
        if x<w/2 then
64
            page=page-1
65
            if page<1 then
66
                page = math.ceil(#table/(h-1))
67
            end
68
        else
69
            page=page+1
70
            if page>math.ceil(#table/(h-1)) then
71
                page = 1
72
            end
73
        end
74
    end
75
    draw(table,page)
76
end