Advertisement
hornedcommando

Minecraft Modem Send

May 3rd, 2024 (edited)
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | Gaming | 0 0
  1. --Work in Progress
  2. --Desc: A Modem program which attempts to send items from a connected inventory in the network to a different connected inventory in the network
  3.  
  4. --By: hornedcommando
  5.  
  6. function input()
  7.     while true do
  8.         write("from?\n")
  9.         fromInv = peripheral.wrap(read())
  10.         write("to?\n")
  11.         toInv = peripheral.wrap(read())
  12.         write("item? (Press Enter to specify none, if no item is specified all types will be sent)\n")
  13.         --TODO: probably needs an item map, don't want to have to type out the full item name, should be colloquial like "coal" or "iron"
  14.         item = read()
  15.         write("amount? (Press Enter to specify none, if no amount is specified all will be sent\n")
  16.         amount = tonumber(read())
  17.         break
  18.     end
  19. end
  20.  
  21. function send(from, to, item, amount)
  22.     --Check if from and to are valid peripherals
  23.     if not peripheral.isPresent(from) then
  24.         print("Error: No peripheral found on side " .. from)
  25.         return
  26.     end
  27.     if not peripheral.isPresent(to) then
  28.         print("Error: No peripheral found on side " .. to)
  29.         return
  30.     end
  31.     --Check if from has an inventory
  32.     local fromInv = peripheral.wrap(from)
  33.     if not fromInv or not fromInv.list then
  34.         print("Error: No inventory found on side " .. from)
  35.         return
  36.     end
  37.     --Check if to has an inventory
  38.     local toInv = peripheral.wrap(to)
  39.     if not toInv or not toInv.list then
  40.         print("Error: No inventory found on side " .. to)
  41.         return
  42.     end
  43.     --Check if the item exists in from
  44.     local itemSlot
  45.     for slot, itemData in pairs(fromInv.list()) do
  46.         if itemData.name == item then
  47.             itemSlot = slot
  48.             break
  49.         end
  50.     end
  51.     if not itemSlot then
  52.         print("Error: Item " .. item .. " not found in " .. from)
  53.         return
  54.     end
  55.  
  56.     --Check if there are enough items in from
  57.     local itemCount = fromInv.getItemCount(itemSlot)
  58.     if itemCount < amount then
  59.         print("Error: Not enough " .. item .. " in " .. from)
  60.         return
  61.     end
  62.  
  63.     -- Push items from from to to
  64.     fromInv.pushItems(peripheral.getName(to), itemSlot, amount)
  65.     print("Sent " .. amount .. " " .. item .. " from " .. from .. " to " .. to)
  66. end
  67.  
  68. input()
  69. send(fromInv, toInv, item, amount)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement