Advertisement
astracerus

Organized Teleporter

Dec 22nd, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.39 KB | Gaming | 0 0
  1. --[[
  2. MIT License
  3.  
  4. Copyright (c) 2024 astracerus
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. ]]
  24.  
  25. --Config
  26. DESTINATION_NAME = "ars_nouveau:repository_1"
  27. teleporter_inventory = peripheral.wrap(DESTINATION_NAME)
  28. SOURCE_NAME = "sophisticatedstorage:chest_3"
  29. source_inventory = peripheral.wrap(SOURCE_NAME)
  30. SHELL_NAME="teleport"
  31.  
  32. --Globals
  33. monitor = peripheral.find("monitor")
  34. storage = peripheral.find("nbtStorage")
  35. completion = require "cc.completion"
  36.  
  37. -- monitor globals
  38. VIEWING_LOCATIONS = true
  39. VIEWING_TAGS = false
  40. viewing_current = VIEWING_LOCATIONS
  41. ENTRIES_PER_SCREEN = 8
  42. curr_display_pagination_idx = 1
  43.  
  44. buttons = {}
  45.  
  46. --COMMAND LIST
  47. CMD_LIST = "list_locations"
  48. CMD_REFRESH = "refresh_locations"
  49. CMD_TAG_LIST = "tag_list"
  50. CMD_CREATE_TAG = "tag_create"
  51. CMD_DELETE_TAG = "tag_delete"
  52. CMD_TAG_LOC = "tag_location"
  53. CMD_UNTAG_LOC = "untag_location"
  54. CMD_CURR_DEST = "current_destination"
  55. CMD_CURR_TAG = "current_tag"
  56. CMD_SET_DEST = "set_destination"
  57. CMD_CLEAR_DEST = "clear_destination"
  58. CMD_SET_CURR_TAG = "set_curr_tag"
  59. CMD_TEST = "test_input"
  60. CMD_HELP = "help"
  61.  
  62.  
  63. COMMANDS = {CMD_HELP, CMD_LIST, CMD_TEST, CMD_TAG_LIST, CMD_CREATE_TAG, CMD_DELETE_TAG, CMD_REFRESH, CMD_TAG_LOC, CMD_UNTAG_LOC, CMD_SET_CURR_TAG, CMD_CURR_TAG, CMD_CURR_DEST,CMD_SET_DEST, CMD_CLEAR_DEST}
  64. HELP_COMMANDS = {'all'}
  65. for _,command in ipairs(COMMANDS) do
  66.     table.insert(HELP_COMMANDS, command)
  67. end
  68. --Constants
  69. ALL_TAG = "All"
  70. ITEM_IN_TELEPORTER_INVENTORY = 0
  71. NOT_FOUND = -1
  72.  
  73.  
  74. --Bootstrapping Functions
  75. function getCurrentLoc()
  76.     if teleporter_inventory.getItemDetail(1) == nil then
  77.         return nil
  78.     end
  79.     return teleporter_inventory.getItemDetail(1).displayName
  80. end
  81.  
  82. function stubOutAllTag()
  83.     local current_tags = storage.read()
  84.     if current_tags[ALL_TAG] == nil then
  85.         current_tags[ALL_TAG] = {}
  86.         storage.writeTable(current_tags)
  87.     end
  88. end
  89.  
  90.  
  91. --Global variables
  92. current_tag = ALL_TAG
  93. current_loc = getCurrentLoc()
  94. stubOutAllTag()
  95.  
  96. --Core logic functions
  97. function createTag(tag_to_create)
  98.     if string.lower(tag_to_create) == string.lower(ALL_TAG) then
  99.         return false, "special tag, can't create/destroy"
  100.     end
  101.     local current_tags = storage.read()
  102.     for k,v in pairs(current_tags) do
  103.         if string.lower(k) == tag_to_create then
  104.             return false, "tag already exists"
  105.         end
  106.     end
  107.     current_tags[tag_to_create] = {}
  108.     local success, err_msg = storage.writeTable(current_tags)
  109.     return success, err_msg
  110. end
  111.  
  112. function deleteTag(tag_to_delete)
  113.     if string.lower(tag_to_create) == string.lower(ALL_TAG) then
  114.         return false, "special tag, can't create/destroy"
  115.     end
  116.     local current_tags = storage.read()
  117.     for k,v in pairs(current_tags) do
  118.         if string.lower(k) == string.lower(tag_to_delete) then
  119.             current_tags[tag_to_delete] = nil
  120.             local success, err_msg = storage.writeTable(current_tags)
  121.             return success, err_msg
  122.         end
  123.     end
  124.     return false, "tag doesn't exist"
  125. end
  126.  
  127. function listTags()
  128.     local current_tags = storage.read()
  129.     local ret_list = {}
  130.     for k,v in pairs(current_tags) do
  131.         table.insert(ret_list, k)
  132.     end
  133.     table.sort(ret_list)
  134.     return ret_list
  135. end
  136.  
  137. function refreshLocations()
  138.     local all_locs = {}
  139.     if teleporter_inventory.getItemDetail(1) ~= nil then
  140.         all_locs[teleporter_inventory.getItemDetail(1).displayName] = 1
  141.     end
  142.     for slot, item in pairs(source_inventory.list()) do
  143.         if all_locs[source_inventory.getItemDetail(slot).displayName] ~= nil then
  144.             return false, "Duplicates detected across both inventories"
  145.         else
  146.             all_locs[source_inventory.getItemDetail(slot).displayName] = 1
  147.         end
  148.     end
  149.     table.sort(all_locs)
  150.     local current_tags = storage.read()
  151.     current_tags[ALL_TAG] = all_locs
  152.     return storage.writeTable(current_tags)
  153. end
  154.  
  155. function listLocations()
  156.     local current_tags = storage.read()
  157.     local ret_table = {}
  158.     for k,v in pairs(current_tags[current_tag]) do
  159.         table.insert(ret_table, k)
  160.     end
  161.     table.sort(ret_table)
  162.     return ret_table
  163. end
  164.  
  165. function listAllLocations()
  166.     local current_tags = storage.read()
  167.     local ret_table = {}
  168.     for k,v in pairs(current_tags[ALL_TAG]) do
  169.         table.insert(ret_table, k)
  170.     end
  171.     table.sort(ret_table)
  172.     return ret_table
  173. end
  174.  
  175. function tagLocation(tag, location)
  176.     local current_tags = storage.read()
  177.     local actual_tag = nil
  178.     for value, _ in pairs(current_tags) do
  179.         if string.lower(value) == string.lower(tag) then
  180.             actual_tag = value
  181.             break
  182.         end
  183.     end
  184.     if actual_tag == nil then return false, "tag doesn't exist" end
  185.     for key, _ in pairs(current_tags[ALL_TAG]) do
  186.         if string.lower(key) == string.lower(location) then
  187.             -- check to see if location already in tag
  188.             if current_tags[actual_tag][key] ~= nil then
  189.                 return false, "location already in tag"
  190.             end
  191.             current_tags[actual_tag][key] = 1
  192.             return storage.writeTable(current_tags)
  193.         end
  194.     end
  195.     return false, "location doesn't exist"
  196. end
  197.  
  198. function untagLocation(tag, location)
  199.     local current_tags = storage.read()
  200.     local actual_tag = nil
  201.     for value, _ in pairs(current_tags) do
  202.         if string.lower(value) == string.lower(tag) then
  203.             actual_tag = value
  204.             break
  205.         end
  206.     end
  207.     if actual_tag == nil then return false, "tag doesn't exist" end
  208.     for key, value in pairs(current_tags[actual_tag]) do
  209.         if string.lower(key) == string.lower(location) then
  210.             current_tags[actual_tag][key] = nil
  211.             return storage.writeTable(current_tags)
  212.         end
  213.     end
  214.     return false, "location doesn't exist in tag"
  215. end
  216.  
  217. function setCurrentTag(tag)
  218.     local current_tags = storage.read()
  219.     for value, _  in pairs(current_tags) do
  220.         if string.lower(value) == string.lower(tag) then
  221.             current_tag = value
  222.             return true, nil
  223.         end
  224.     end
  225.     return false, "tag doesn't exist"
  226. end
  227.  
  228. function currentTag()
  229.     return current_tag
  230. end
  231.  
  232. function currentDestination()
  233.     if teleporter_inventory.getItemDetail(1) == nil then
  234.         return nil
  235.     else
  236.         return teleporter_inventory.getItemDetail(1).displayName
  237.     end
  238. end
  239.  
  240. function clearDestination()
  241.     if teleporter_inventory.getItemDetail(1) == nil then
  242.         return true
  243.     end
  244.     transfered_items = source_inventory.pullItems(DESTINATION_NAME, 1)
  245.     current_loc = getCurrentLoc()
  246.     return transfered_items == 1
  247. end
  248.  
  249.  
  250.  
  251. function getSlotForName(name)
  252.     if teleporter_inventory.getItemDetail(1) ~= nil and teleporter_inventory.getItemDetail(1).displayName == name then
  253.         return ITEM_IN_TELEPORTER_INVENTORY
  254.     end
  255.     for slot, item in pairs(source_inventory.list()) do
  256.         if source_inventory.getItemDetail(slot).displayName == name then
  257.             return slot
  258.         end
  259.     end
  260.     return NOT_FOUND
  261. end
  262.  
  263. function setDestination(dest_name)
  264.     local slot = getSlotForName(dest_name)
  265.     if slot == ITEM_IN_TELEPORTER_INVENTORY then
  266.         return true, nil
  267.     end
  268.     if slot == NOT_FOUND then
  269.         return false, "destination not found"
  270.     end
  271.     local cleared_destination = clearDestination()
  272.     if not cleared_destination then
  273.         return false, "unable to clean out destination"
  274.     end
  275.     source_inventory.pushItems(DESTINATION_NAME, slot, 1, 1)
  276.     current_loc = getCurrentLoc()
  277.     return true, nil
  278. end
  279.  
  280. --Monitor Functions
  281. --idea, have button class with 3 states -> currently selected, enabled, disabled
  282. --define it with (x,y) start, length, text inside, and onTouch callback
  283.  
  284. ACTIVE_COLOR = colors.green
  285. ENABLED_COLOR = colors.lightGray
  286. DISABLED_COLOR = colors.gray
  287.  
  288. function resetMonitor()
  289.     monitor.setBackgroundColor(colors.black)
  290.     monitor.clear()
  291. end
  292.  
  293. --this was initially implemented with the thought that the touchpoint term redirects would interfere with the shell
  294. --which I now believe was in error, but I'm not reimplementing it with touchpoint
  295.  
  296. local Button = {
  297.     x = 1,
  298.     y = 1,
  299.     length = 1,
  300.     text = "",
  301.     color = ENABLED_COLOR,
  302.     onClick = function(self) end,
  303.     inArea = function(self,x,y)
  304.         return self.y == y and (x < self.x + self.length and x >= self.x)
  305.     end,
  306.     draw = function(self)
  307.         monitor.setBackgroundColor(self.color)
  308.         monitor.setCursorPos(self.x, self.y)
  309.         --get the text length right
  310.         display_text = ""
  311.         if string.len(self.text) > self.length then
  312.             display_text = string.sub(self.text, 1, self.length)
  313.         elseif string.len(self.text) == self.length then
  314.             display_text = self.text
  315.         else
  316.             display_text = self.text
  317.             while string.len(display_text) < self.length do
  318.                 display_text = " " .. display_text .. " "
  319.             end
  320.             if string.len(display_text) > self.length then
  321.                 display_text = string.sub(display_text, 1, string.len(display_text) - 1)
  322.             end
  323.         end
  324.         monitor.write(display_text)
  325.     end
  326. }
  327.  
  328. function Button:new(o)
  329.     o = o or {} -- create object if user does not provide one
  330.     setmetatable(o, self)
  331.     self.__index = self
  332.     return o
  333. end
  334.  
  335. function drawUI()
  336.     local width, height = monitor.getSize()
  337.     local topline = ""
  338.     local entries = {}
  339.     local entry_on_click = function(self) end
  340.     local view_other_text = ""
  341.     local view_other_mode = VIEWING_LOCATIONS
  342.     buttons = {}
  343.     total_entries = 0
  344.     current_entry = ""
  345.     if viewing_current == VIEWING_LOCATIONS then
  346.         if current_tag == ALL_TAG then
  347.             topline = "Locations"
  348.         else
  349.             topline = "Locations Tagged " .. current_tag
  350.         end
  351.         local locations = listLocations()
  352.         total_entries = #locations
  353.         entries = {unpack(locations, curr_display_pagination_idx, curr_display_pagination_idx + ENTRIES_PER_SCREEN - 1)}
  354.         entry_on_click = function(self) setDestination(self.text) end
  355.         view_other_text = "View Tags"
  356.         view_other_mode = VIEWING_TAGS
  357.         current_entry = current_loc
  358.     else
  359.         topline = "Tags"
  360.         local tags = listTags()
  361.         total_entries = #tags
  362.         entries = {unpack(tags, curr_display_pagination_idx, curr_display_pagination_idx + ENTRIES_PER_SCREEN - 1)}
  363.         entry_on_click = function(self) setCurrentTag(self.text) end
  364.         view_other_text = "View Locations"
  365.         view_other_mode = VIEWING_LOCATIONS
  366.         current_entry = current_tag
  367.     end
  368.  
  369.     --setup buttons
  370.     for i=1,ENTRIES_PER_SCREEN do
  371.         if entries[i] ~= nil then
  372.             local button_color = colors.black
  373.             if current_entry == entries[i] then
  374.                 button_color = ACTIVE_COLOR
  375.             else
  376.                 button_color = ENABLED_COLOR
  377.             end
  378.             local button = Button:new({x=1, y=2*i+1,length=width, text=entries[i], color=button_color,  onClick=entry_on_click})
  379.             table.insert(buttons, button)
  380.         end
  381.     end
  382.     local arrow_key_y = (2 * ENTRIES_PER_SCREEN) + 3
  383.     if curr_display_pagination_idx ~= 1 then --if the left button should be enabled
  384.         local left_arrow_button = Button:new({x=1, y=arrow_key_y, length=1, text="<", color=ENABLED_COLOR, onClick=function(self) curr_display_pagination_idx = curr_display_pagination_idx - ENTRIES_PER_SCREEN end})
  385.         table.insert(buttons, left_arrow_button)
  386.     else
  387.         local left_arrow_button = Button:new({x=1, y=arrow_key_y, length=1, text="<", color=DISABLED_COLOR, onClick=function(self) end})
  388.         table.insert(buttons, left_arrow_button)
  389.     end
  390.  
  391.  
  392.     if (curr_display_pagination_idx + ENTRIES_PER_SCREEN - 1) < total_entries then
  393.         local right_arrow_button = Button:new({x=width, y=arrow_key_y, length=1, text=">", color=ENABLED_COLOR, onClick=function(self) curr_display_pagination_idx = curr_display_pagination_idx + ENTRIES_PER_SCREEN end})
  394.         table.insert(buttons, right_arrow_button)
  395.     else
  396.         local right_arrow_button = Button:new({x=width, y=arrow_key_y, length=1, text=">", color=DISABLED_COLOR, onClick=function(self) end})
  397.         table.insert(buttons, right_arrow_button)
  398.     end
  399.  
  400.     local view_other_button = Button:new({x=3, y=arrow_key_y, length=width-4, text=view_other_text, color=ENABLED_COLOR, onClick=function(self)
  401.         viewing_current = view_other_mode
  402.         curr_display_pagination_idx = 1
  403.         end})
  404.     table.insert(buttons, view_other_button)
  405.  
  406.     --draw screen
  407.     resetMonitor()
  408.     monitor.setCursorPos(width/2 - string.len(topline)/2, 1)
  409.     monitor.write(topline)
  410.     for _,button in ipairs(buttons) do
  411.         button:draw()
  412.     end
  413. end
  414.  
  415.  
  416.  
  417. function monitorThread()
  418.     drawUI()
  419.     while true do
  420.         local event, side, x, y = os.pullEvent("monitor_touch")
  421.         --do Event Processing
  422.         for _, button in ipairs(buttons) do
  423.             if button:inArea(x, y) then
  424.                 button:onClick()
  425.             end
  426.         end
  427.         --redraw UI
  428.         drawUI()
  429.     end
  430. end
  431.  
  432. -- Shell Helper functions
  433. function shellPrompt()
  434.     write(string.format("(%s)> ", SHELL_NAME))
  435. end
  436.  
  437. function cmdOutput(cmd_name, output)
  438.     print(string.format("%s", output))
  439. end
  440.  
  441. function getTrimmedString(input)
  442.     --trim user input
  443.     local start_idx = 1
  444.     for i=1,string.len(input) do
  445.         testChar = string.sub(input, i, i)
  446.         if string.match(testChar, "%s") == nil then
  447.             start_idx = i
  448.             break
  449.         end
  450.     end
  451.  
  452.     local final_idx = string.len(input)
  453.     for i=string.len(input),1,-1 do
  454.         testChar = string.sub(input, i, i)
  455.         if string.match(testChar, "%s") == nil then
  456.             final_idx = i
  457.             break
  458.         end
  459.     end  
  460.  
  461.     return string.sub(input, start_idx, final_idx)
  462. end
  463.  
  464. function cmdInput(cmd_name, request_string, completion_function)
  465.     write(string.format("(%s)> ", request_string))
  466.     local user_input = read(nil, nil, completion_function)
  467.    
  468.  
  469.     local trimmed_user_input = getTrimmedString(user_input)
  470.     if string.match(trimmed_user_input, "^%s+$") then
  471.         print("User input must contain non whitespace characters")
  472.         trimmed_user_input = cmdInput(cmd_name, request_string, completion_function)
  473.     end
  474.     return trimmed_user_input
  475. end
  476.  
  477. function writeDividingLine()
  478.     width, height = term.getSize()
  479.     for i=1,width do
  480.         term.write("-")
  481.     end
  482. end
  483.  
  484. --Shell Functions
  485. function helpCmd()
  486.     local command_to_help_with = string.lower(cmdInput(CMD_HELP, "command to help with (all if unsure)", function(text) return completion.choice(text, HELP_COMMANDS) end))
  487.     if command_to_help_with == "all" then
  488.         local output = "list of commands -> "
  489.         for _,command in pairs(COMMANDS) do
  490.             output = output .. command .. ", "
  491.         end
  492.         output = string.sub(output, 1, string.len(output)-2)
  493.         cmdOutput(CMD_HELP, output)
  494.     elseif command_to_help_with == CMD_TEST then
  495.         cmdOutput(CMD_HELP, "test -> test to see what your input looks like to the shell(this is a development command)")
  496.     elseif string.lower(cmd) == CMD_CREATE_TAG then
  497.         cmdOutput(CMD_HELP, "create a tag")
  498.     elseif string.lower(cmd) == CMD_DELETE_TAG then
  499.         cmdOutput(CMD_HELP, "delete a tag")
  500.     elseif string.lower(cmd) == CMD_TAG_LIST then
  501.         cmdOutput(CMD_HELP, "list current tags")
  502.     elseif string.lower(cmd) == CMD_LIST then
  503.         cmdOutput(CMD_HELP, "list all locations under the current tag")
  504.     elseif string.lower(cmd) == CMD_REFRESH then
  505.         cmdOutput(CMD_HELP, "refresh -> refresh the locations under the " .. ALL_TAG .. " tag")
  506.     elseif string.lower(cmd) == CMD_TAG_LOC then
  507.         cmdOutput(CMD_HELP, "add a location to a tag")
  508.     elseif string.lower(cmd) == CMD_UNTAG_LOC then
  509.         cmdOutput(CMD_HELP, "remove a location from a tag")
  510.     elseif string.lower(cmd) == CMD_SET_CURR_TAG then
  511.         cmdOutput(CMD_HELP, "set the current tag for autocomplete and listing purposes")
  512.     elseif string.lower(cmd) == CMD_CURR_TAG then
  513.         cmdOutput(CMD_HELP, "get the current tag for autocomplete and listing purposes")
  514.     elseif string.lower(cmd) == CMD_CURR_DEST then
  515.         cmdOutput(CMD_HELP, "get the current destination you are teleporting to ")
  516.     elseif string.lower(cmd) == CMD_CLEAR_DEST then
  517.         cmdOutput(CMD_HELP, "clear the current destination you are teleporting to ")
  518.     elseif string.lower(cmd) == CMD_SET_DEST then
  519.         cmdOutput(CMD_HELP, "set the current destination you are teleporting to")
  520.     else
  521.         cmdOutput(SHELL_NAME, "Invalid Command")
  522.     end
  523. end
  524.  
  525. function createTagCmd()
  526.     local tag = cmdInput(CMD_CREATE_TAG, "tag to create")
  527.     local success, err_msg = createTag(tag)
  528.     if success then
  529.         cmdOutput(CMD_CREATE_TAG, "Tag " .. tag .. " created successfully")
  530.     else
  531.         cmdOutput(CMD_CREATE_TAG, "Tag creation failed, reason -> " .. err_msg)
  532.     end
  533. end
  534.  
  535. function deleteTagCmd()
  536.     local tag = cmdInput(CMD_DELETE_TAG, "tag to delete", function(text) return completion.choice(text, listTags()) end)
  537.     local success, err_msg = deleteTag(tag)
  538.     if success then
  539.         cmdOutput(CMD_DELETE_TAG, "Tag " .. tag .. " deleteted successfully")
  540.     else
  541.         cmdOutput(CMD_DELETE_TAG, "Tag deletion failed, reason -> " .. err_msg)
  542.     end
  543. end
  544.  
  545. function listTagsCmd()
  546.     local tagList = listTags()
  547.     for _, tag in pairs(tagList) do
  548.         cmdOutput(CMD_TAG_LIST, tag)
  549.     end
  550. end
  551.  
  552. function listLocationsCmd()
  553.     locs = listLocations()
  554.     for _,loc in pairs(locs) do
  555.         cmdOutput(CMD_LIST, loc)
  556.     end
  557. end
  558.  
  559. function refreshLocationsCmd()
  560.     success, err_msg = refreshLocations()
  561.     if success then
  562.         cmdOutput(CMD_REFRESH, "Locations refreshed successfully")
  563.     else
  564.         cmdOutput(CMD_REFRESH, "Failed to refresh locations, reason -> " .. err_msg)
  565.     end
  566. end
  567.  
  568. function tagLocationCmd()
  569.     local tag = cmdInput(CMD_TAG_LOC, "tag to add location to", function(text) return completion.choice(text, listTags()) end)
  570.     local location = cmdInput(CMD_TAG_LOC, "location to tag", function(text) return completion.choice(text, listAllLocations()) end)
  571.     local success, err_msg = tagLocation(tag, location)
  572.     if success then
  573.         cmdOutput(CMD_TAG_LOC, "Location " .. location .. " added to tag " .. tag)
  574.     else
  575.         cmdOutput(CMD_TAG_LOC, "Failed to tag location, reason -> " .. err_msg)
  576.     end
  577. end
  578.  
  579. function untagLocationCmd()
  580.     local tag = cmdInput(CMD_UNTAG_LOC, "tag to remove location from", function(text) return completion.choice(text, listTags()) end)
  581.     local location = cmdInput(CMD_UNTAG_LOC, "location to untag", function(text) return completion.choice(text, listAllLocations()) end)
  582.     local success, err_msg = untagLocation(tag, location)
  583.     if success then
  584.         cmdOutput(CMD_UNTAG_LOC, "Location " .. location .. " removed from tag " .. tag)
  585.     else
  586.         cmdOutput(CMD_UNTAG_LOC, "Failed to untag location, reason -> " .. err_msg)
  587.     end
  588. end
  589.  
  590. function setCurrentTagCmd()
  591.     local tag = cmdInput(CMD_SET_CURR_TAG, "tag to switch to", function(text) return completion.choice(text, listTags()) end)
  592.     local success, err_msg = setCurrentTag(tag)
  593.     if success then
  594.         cmdOutput(CMD_SET_CURR_TAG, "Tag set to " .. tag)
  595.     else
  596.         cmdOutput(CMD_SET_CURR_TAG, "Failed to set current tag, reason -> " .. err_msg)
  597.     end
  598. end
  599.  
  600. function currentTagCmd()
  601.     cmdOutput(CMD_CURR_TAG, "Current tag is " .. current_tag)
  602. end
  603.  
  604. function currentDestinationCmd()
  605.     local current_destination = currentDestination()
  606.     if current_destination == nil then
  607.         cmdOutput(CMD_CURR_DEST, "No Destination Set")
  608.     else
  609.         cmdOutput(CMD_CURR_DEST, "Destination set to " .. current_destination)
  610.     end
  611. end
  612.  
  613. function clearDestinationCmd()
  614.     local cleared_destination = clearDestination()
  615.     if cleared_destination then
  616.         cmdOutput(CMD_CLEAR_DEST, "Destination cleared successfully")
  617.     else
  618.         cmdOutput(CMD_CLEAR_DEST, "Failed to clear destination; check source inventory for overfill")
  619.     end
  620. end
  621.  
  622. function setDestinationCmd()
  623.     local destination_name = cmdInput(CMD_SET_DEST, "destination to tp to", function(text) return completion.choice(text, listLocations()) end)
  624.     local success, err_msg = setDestination(destination_name)
  625.     if success then
  626.         cmdOutput(CMD_SET_DEST, "Set teleporter destination to " .. destination_name .. " successfully")
  627.     else
  628.         cmdOutput(CMD_SET_DEST, "Failed to set teleport destination, reason -> " .. err_msg)
  629.     end
  630. end
  631.    
  632.  
  633. function shellThread()
  634.     while true do
  635.         shellPrompt()
  636.         local cmd = getTrimmedString(read(nil, nil, function(text) return completion.choice(text, COMMANDS) end))
  637.         writeDividingLine()
  638.         if string.lower(cmd) == CMD_TEST then
  639.             user_input = cmdInput(CMD_TEST, "test characters", nil)
  640.             cmdOutput(CMD_TEST, string.format("[%s]",user_input))
  641.         elseif string.lower(cmd) == CMD_HELP then
  642.             helpCmd()
  643.         elseif string.lower(cmd) == CMD_CREATE_TAG then
  644.             createTagCmd()
  645.         elseif string.lower(cmd) == CMD_DELETE_TAG then
  646.             deleteTagCmd()
  647.         elseif string.lower(cmd) == CMD_TAG_LIST then
  648.             listTagsCmd()
  649.         elseif string.lower(cmd) == CMD_LIST then
  650.             listLocationsCmd()
  651.         elseif string.lower(cmd) == CMD_REFRESH then
  652.             refreshLocationsCmd()
  653.         elseif string.lower(cmd) == CMD_TAG_LOC then
  654.             tagLocationCmd()
  655.         elseif string.lower(cmd) == CMD_UNTAG_LOC then
  656.             untagLocationCmd()
  657.         elseif string.lower(cmd) == CMD_SET_CURR_TAG then
  658.             setCurrentTagCmd()
  659.         elseif string.lower(cmd) == CMD_CURR_TAG then
  660.             currentTagCmd()
  661.         elseif string.lower(cmd) == CMD_CURR_DEST then
  662.             currentDestinationCmd()
  663.         elseif string.lower(cmd) == CMD_CLEAR_DEST then
  664.             clearDestinationCmd()
  665.         elseif string.lower(cmd) == CMD_SET_DEST then
  666.             setDestinationCmd()
  667.         else
  668.             cmdOutput(SHELL_NAME, "Invalid Command")
  669.         end
  670.         writeDividingLine()
  671.         --fully reset the monitor each time a command is run
  672.         curr_display_pagination_idx = 1
  673.         drawUI()
  674.     end
  675. end
  676.  
  677.  
  678. --actually kick off the two threads
  679. parallel.waitForAll(shellThread, monitorThread)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement