Minecraft – Computercraft trading machine script

minecraft-computercraftminecraft-feed-the-beastminecraft-java-edition

I was trying to program a computercraft trading machine which would let the player select an item after he paid a fixed price. This is my current setup:
backfront

This is my current program code:

sort = peripheral.warp("top")
term.redirect(peripheral.warp("back"))
print("Welcome at Alexanders record shop!")
print("Every music disc costs 10 diamonds.")
while dia<10 do
print("You have paid"..dia.."Diamonds.")
end

My questions:

  1. How do I count and extract the diamonds?
  2. How can the player select his item via the redstone buttons?

Best Answer

To check how many of a given item is in an inventory in a given direction, you need to use the sorter.list(direction) function, where direction is an integer corresponding to the direction of the inventory in relationship to the sorter (0 for down, 1 for up, 2 for -Z, 3 for +Z, 4 for -X and 5 for +X). This function returns a table, so you've got to use a loop to look through it. I implemented a checkInput() function like so:

function checkInput()
  inChest = sorter.list(inputDirection)
  for id,count in pairs(inChest) do
    if id==currency then
      return count
    end
  end
  return 0
end

Where inputDirection is the direction of the ender chest, and currency is the item ID of the desired payment item (in this case, 264). I did it this way so you can configure the payment later without having to change it everywhere. That returns hoe many diamonds, if any, are in the ender chest.

You can extract items from an inventory and move them to another inventory using the sorter function sorter.extract(inputDirection, id, outputDirection, count). So, for example, to trade 10 diamonds for a specified record item, you could do something like this (after verifying that payment is there, and the record is in stock):

sorter.extract(inputDirection, currency, paymentDirection, price)
sorter.extract(stockDirection, item, inputDirection, 1)

As for the buttons, the use of Advanced Monitors allows the use of touchscreens. I used Direwolf20's Button API, found on pastebin here (use pastebin get S8x0K3ui button to download it directly from the ComputerCraft computer). It can be quite confusing, though. Direwolf has a tutorial for it here, which explains what each function does.

The core function that you're going to be using is button.setTable(name, func, xmin, xmax, ymin, ymax), where func is the name of the function that you want the button to perform when you push it. You need to do this for each button you want to have and use. For example:

button.setTable("^", cursorUp, true, 26, 27, 5, 8)
button.setTable("v", cursorDown, true, 26, 27, 13, 16)
button.setTable("Purchase", purchase, true, 10, 19, 18, 19)

(those positionings happen to work well on a 3x3 monitor setup).

I used a cursorPos variable to track where the cursor was, and modified that variable accordingly with cursorUp() and cursorDown(). The function purchase() uses the current cursor position as a reference to which item is selected, and makes the trade.

Monitor touch is a pullEvent that can be used to trigger an action. Like so:

while true do
  display()
  event, side, x, y = os.pullEvent("monitor_touch")
  button.checkxy(x,y)
end

I am very sorry that this answer is probably a tad longwinded and confusing. ComputerCraft can do that sometimes. In my tinkering with the various APIs needed, I actually wrote a storefront script, which can be found on pastebin here. Whether you want to use that, or look at it a little then figure it out on your own, go ahead. The latter is a much better practice in my opinion.

You may want to check out the ComputerCraft Wiki for more on the API involved. Be sure to check out Direwolf's tutorial on the Button API as well, since he's much better at explaining his code than I am. And, finally, though the documentation is a little scarce, there is some documentation on MiscPeripherals on the ComputerCraft forums, found here.