Having Golems Use a Random Item

Discussion in 'Programming' started by MyrddinE, Jun 9, 2014.

  1. MyrddinE

    MyrddinE Active Member

    Joined:
    Dec 4, 2011
    Messages:
    220
    Likes Received:
    69
    I have a tree farm, and I found it very difficult to get the farm to plant random saplings. Most methods of planting start from the beginning and move on from there, and there is no way I could see to either fill a chest with random items, or select random items from the chest to plant.

    So I wrote it. This goes on a computer next to the chest (alter the side to suit), and simple swaps the item in slot one (which is where golems, pipes, and machines typically pull from) with another stack in the chest, every two minutes. I use a golem, but this should work for nearly any automation where you want to pull a random item.

    Code:
    chest = peripheral.wrap('right')
    max = chest.getInventorySize()
     
    while true do
      slot = math.floor(math.random(max-1)+1)
      chest.swapStacks(slot,1)
      print("Swapped 1 with "..slot)
      sleep(120)
    end