Advertisement
eniallator

Ender Chest Chooser

Aug 28th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. -- Ender Chest chooser
  2. local screenDim = {}
  3. screenDim.x, screenDim.y = term.getSize()
  4. local players = {eniallator = 3584, YaEyezOnMe = 2816, eriyo2000 = 267}
  5. local numPlayers = 3
  6. local selectedPlayer = "no one"
  7. local turtleID = 48
  8. local boxWidth = math.floor(screenDim.x /numPlayers +0.5)
  9. local displayTab = {}
  10. local nameDisplayLimit = boxWidth -2
  11. local community = {name = "Community", id = 1536}
  12. community.button = {screenDim.x -#community.name -1, 1, screenDim.x, 1, colours.lime, community.name, community.name}
  13.  
  14. rednet.open("right")
  15. term.setBackgroundColor(colours.black)
  16. shell.run("clear")
  17.  
  18. local function makeDisplayTab()
  19.   for k,v in pairs(players) do
  20.     table.insert(displayTab,{#displayTab *boxWidth +2, 3, (#displayTab +1) *boxWidth -1, screenDim.y -1, colours.blue, k:sub(1, nameDisplayLimit), k})
  21.   end
  22. end
  23.  
  24. makeDisplayTab()
  25.  
  26. -- tab format {x1,y1,x2,y2,background colour, substringed app name (for display purposes), original app name}
  27. local function fillArea(tab)
  28.   term.setBackgroundColor(tab[5])
  29.  
  30.   for i=0,tab[4]-tab[2] do
  31.     local j = 0
  32.  
  33.     while j <= tab[3]-tab[1] do
  34.       term.setCursorPos(tab[1]+j,tab[2]+i)
  35.  
  36.       if tab[6] and math.floor(j-((tab[3]-tab[1])/2)+#tab[6]/2) == 0 and math.floor(i-(tab[4]-tab[2])/2) == 0 then
  37.         -- If statement seeing whether the iterators have hit the place where it should be writing the word out
  38.  
  39.         term.write(tab[6])
  40.         j = j + #tab[6]
  41.       else
  42.  
  43.         j = j + 1
  44.         term.write(" ")
  45.       end
  46.     end
  47.   end
  48. end
  49.  
  50. local function displayScreen()
  51.   fillArea({1,1,screenDim.x,1,colours.black})
  52.   term.setCursorPos(1,1)
  53.   term.write("Selected: " .. selectedPlayer)
  54.   fillArea(community.button)
  55.  
  56.   for i=1,#displayTab do
  57.     fillArea(displayTab[i])
  58.   end
  59. end
  60.  
  61. local function detectClick(tab, x, y)
  62.   if tab[1] <= x and tab[3] >= x and tab[2] <= y and tab[4] >= y then
  63.     return tab[7]
  64.   end
  65. end
  66.  
  67. local function clickDetectDisplay(x, y)
  68.   for i=1, #displayTab do
  69.     if detectClick(displayTab[i], x, y) then
  70.       return displayTab[i][7]
  71.     end
  72.   end
  73.  
  74.   return false
  75. end
  76.  
  77. local function checkDone()
  78.   local done = false
  79.  
  80.   repeat
  81.     local received = {}
  82.     received.id, received.msg = rednet.receive()
  83.  
  84.     if received.id == turtleID and received.msg == "done" then
  85.       done = true
  86.     end
  87.  
  88.   until done
  89. end
  90.  
  91. while true do
  92.   makeDisplayTab()
  93.   displayScreen()
  94.  
  95.   if justSent then
  96.     checkDone()
  97.     justSent = false
  98.   end
  99.  
  100.   local event = {}
  101.   event.type, event.button, event.x, event.y = os.pullEvent()
  102.  
  103.   if event.type == "mouse_click" or event.type == "monitor_touch" then
  104.     local clickedPlayer = clickDetectDisplay(event.x, event.y)
  105.  
  106.     if clickedPlayer then
  107.       selectedPlayer = clickedPlayer
  108.       rednet.send(turtleID, players[clickedPlayer])
  109.       justSent = true
  110.  
  111.     elseif detectClick(community.button, event.x, event.y) then
  112.       selectedPlayer = community.name
  113.       rednet.send(turtleID, community.id)
  114.       justSent = true
  115.     end
  116.   end
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement