Advertisement
jkniest

rocket

May 18th, 2022 (edited)
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.48 KB | None | 0 0
  1. -- Consts
  2. local BTN_PADDING = 5
  3. local REDSTONE_SIDE = 'front'
  4. local ARMOR_REGULAR_SIDE = 'top'
  5. local ARMOR_SPACE_SIDE = 'right'
  6. local MAX_OXYGEN = 48000
  7.  
  8.  
  9. -- Data
  10. local rocketReady = false
  11. local spacesuiteEquipped = false
  12. local oxygenLevel = 100
  13.  
  14. -- Peripherals
  15. local monitor = peripheral.find('monitor')
  16. local inventoryManager = peripheral.find('inventoryManager')
  17.  
  18. if monitor == nil then
  19.     print('Error: You need to connect a monitor!')
  20.     return
  21. end
  22.  
  23. if inventoryManager == nil then
  24.     print('Error: You need to connect an inventory manager!')
  25.     return
  26. end
  27.  
  28.  
  29. -- Cached properties
  30. local monitorWidth, monitorHeight = monitor.getSize()
  31.  
  32.  
  33. -- Generic methods
  34. function resetMonitor()
  35.     monitor.clear()
  36.     monitor.setCursorPos(0, 0)
  37. end
  38.  
  39. function drawButton(label, y, color, parent)
  40.     local parentWidth = parent.getSize()
  41.  
  42.     local button = window.create(parent, BTN_PADDING, y, parentWidth - BTN_PADDING * 2, 3)
  43.     button.setBackgroundColor(color)
  44.     button.clear()
  45.  
  46.     local btnWidth = parentWidth - 10;
  47.     button.setCursorPos(1, 2)
  48.  
  49.     local remainingWidth = btnWidth - label:len()
  50.     button.write(string.rep(' ', remainingWidth / 2) .. label .. string.rep(' ', remainingWidth / 2))
  51.  
  52.     button.redraw();
  53. end
  54.  
  55.  
  56. -- Main logic
  57.  
  58. function drawLoop()
  59.     resetMonitor()
  60.    
  61.     -- Generate status string --
  62.     local statusLabel = 'Rocket inactive'
  63.     local statusColor = colors.red
  64.  
  65.     if rocketReady == true then
  66.         statusLabel = 'Rocket ready'
  67.         statusColor = colors.green
  68.     end
  69.  
  70.     --
  71.     -- Top  -> General information about rocket
  72.     --
  73.     monitor.setTextColor(statusColor)
  74.     monitor.setCursorPos(
  75.         monitorWidth / 2 - statusLabel:len() / 2,
  76.         3
  77.     )
  78.     monitor.write(statusLabel)
  79.    
  80.     if rocketReady == true and spacesuiteEquipped == false then
  81.         -- Draw a button to equip the spacesuite
  82.         drawButton('Equip spacesuite', 10, colors.green, monitor)
  83.     elseif rocketReady == false and spacesuiteEquipped == true then
  84.         -- Draw a button to equip regular armor
  85.         drawButton('Equip regular armor', 10, colors.green, monitor)
  86.     end
  87.  
  88.     if rocketReady == true and spacesuiteEquipped == true then
  89.         -- Draw oxygen level
  90.         monitor.setTextColor(colors.white)
  91.         monitor.setCursorPos(3, monitorHeight - 2)
  92.  
  93.         local oxygenLabel = 'Oxygen Level: ' .. oxygenLevel .. '%'
  94.         monitor.write(oxygenLabel)
  95.  
  96.         if spacesuiteEquipped == true then
  97.             local equippedLabel = 'Spacesuite equipped!'
  98.             monitor.setCursorPos(monitorWidth - 3 - equippedLabel:len(), monitorHeight - 2)
  99.             monitor.write(equippedLabel)
  100.         end
  101.     end
  102. end
  103.  
  104. function checkClick(x, y)
  105.     -- Check if the click is the start / stop load test button
  106.     if x >= BTN_PADDING and x < BTN_PADDING + monitorWidth - BTN_PADDING * 2
  107.     and y >= 10 and y < 13
  108.      then
  109.         if rocketReady == true and spacesuiteEquipped == false then
  110.             -- Equip space suite
  111.             -- First fetch the current armor
  112.             inventoryManager.removeItemFromPlayer(ARMOR_REGULAR_SIDE, 1, 39) -- Helmet
  113.             inventoryManager.removeItemFromPlayer(ARMOR_REGULAR_SIDE, 1, 38) -- Breastplate
  114.             inventoryManager.removeItemFromPlayer(ARMOR_REGULAR_SIDE, 1, 37) -- Pants
  115.             inventoryManager.removeItemFromPlayer(ARMOR_REGULAR_SIDE, 1, 36) -- Boots
  116.  
  117.             -- Then give the space suite
  118.             inventoryManager.addItemToPlayer(ARMOR_SPACE_SIDE, 1, 39) -- Helmet
  119.             inventoryManager.addItemToPlayer(ARMOR_SPACE_SIDE, 1, 38) -- Breastplate
  120.             inventoryManager.addItemToPlayer(ARMOR_SPACE_SIDE, 1, 37) -- Pants
  121.             inventoryManager.addItemToPlayer(ARMOR_SPACE_SIDE, 1, 36) -- Boots
  122.         elseif rocketReady == false and spacesuiteEquipped == true then
  123.             -- Equip regular armor
  124.             -- First fetch the current space armor
  125.             inventoryManager.removeItemFromPlayer(ARMOR_SPACE_SIDE, 1, 39) -- Helmet
  126.             inventoryManager.removeItemFromPlayer(ARMOR_SPACE_SIDE, 1, 38) -- Breastplate
  127.             inventoryManager.removeItemFromPlayer(ARMOR_SPACE_SIDE, 1, 37) -- Pants
  128.             inventoryManager.removeItemFromPlayer(ARMOR_SPACE_SIDE, 1, 36) -- Boots
  129.  
  130.             -- Then give the regular armor
  131.             inventoryManager.addItemToPlayer(ARMOR_REGULAR_SIDE, 1, 39) -- Helmet
  132.             inventoryManager.addItemToPlayer(ARMOR_REGULAR_SIDE, 1, 38) -- Breastplate
  133.             inventoryManager.addItemToPlayer(ARMOR_REGULAR_SIDE, 1, 37) -- Pants
  134.             inventoryManager.addItemToPlayer(ARMOR_REGULAR_SIDE, 1, 36) -- Boots
  135.         end
  136.     elseif x <= 2 and y <= 2 then
  137.         print("Dong")
  138.     end
  139.  
  140.     fetchData()
  141. end
  142.  
  143. function fetchData()
  144.     rocketReady = redstone.getInput(REDSTONE_SIDE)
  145.  
  146.     -- Check if the player is currently wearing the spacesuite
  147.     local armorPlate = inventoryManager.getArmor()[102]
  148.  
  149.     spacesuiteEquipped = false
  150.     if armorPlate.name == 'beyond_earth:netherite_space_suit' then
  151.         spacesuiteEquipped = true
  152.         oxygenLevel = math.floor((armorPlate.nbt['Energy']/MAX_OXYGEN)*100)
  153.     end
  154. end
  155.  
  156. fetchData()
  157.  
  158. while true do
  159.     drawLoop()
  160.  
  161.     local event, _, x, y = os.pullEvent()
  162.    
  163.     if event == 'monitor_touch' then
  164.         print('Clicked monitor')
  165.         checkClick(x, y)
  166.     elseif event == 'redstone' then
  167.         print('Changed redstone signal')
  168.         rocketReady = redstone.getInput(REDSTONE_SIDE)
  169.     end
  170. end
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement