Minecraft: How to make a command block shop that allows players to sell items

minecraft-commandsminecraft-java-edition

I am making a map that relies on currency and other custom things like a shop. But the issue is that I'd like the player to be able to sell for example 16 chorus fruits for 20 Coins. Now keep in mind I'm building the map on the latest snapshot of Minecraft 1.9 and thus I'm using the brand new command blocks. There are some limitations to what I can use. For example I'm only able to use a total of 4 command block for every item because of lack of space.

And if you have a way of making it possible I'd love you to explain the command and how it works so that I can have a better understanding of what I'm doing.

EDIT: I'm aware that I'll probably have to use /testfor and I found this online:

/testfor @p[r=10] {Inventory:[{id:"minecraft:emerald",Count:2b}]}

keep in mind I have no idea how to make it so that I can /testfor a certain amount of an item. The youtuber who posted it as dragnoz so take a look there for even more info.

Best Answer

The issue with checking for the Count tag is that it checks for an exact match rather than minimum. The player must have a stack of exactly 2 emeralds in a slot in their inventory, otherwise they cannot match.

To circumvent that, you will have to use CommandStats, which are a set of triggers that can set a target's scoreboard score based on the success of the command. For example, the "SuccessCount" trigger sets a score equal to the number of times a command was successfully processed (such as /testfor @e[type=Creeper] resulting in a score equal to the number of creepers).

For item detection, "AffectedItems" will be used.

Prerequisites

Objective to hold each player's "AffectedItems" return value.

/scoreboard objectives add ITEMS dummy

In order for CommandStats to modify a target's score, that target must be tracked prior. This may need to run on a clock in the event new players can join at any time.

/scoreboard players add @a ITEMS 0

Clock commands

The following will need to be run on a clock.

CommandStat trigger applied to all players, who will set their own "ITEMS" score based on commands that they run. This needs to run on a clock because it is being removed during detection.

/stats entity @a set AffectedItems @a[c=1] ITEMS

Detection

The following must be run in numerical order each time you want the player to sell an item. You may need to change target selectors to target a specific player, such as using x/y/z/r parameters depending on your setup.

  1. Cause players to use /clear to remove 0 of the specified item. While this does not remove any items, the stored "AffectedItems" trigger will receive the number of items that could have been cleared, which would be equal to the number of emeralds across the player's inventory. Their "ITEMS" score will be set to that number.

    /execute @p[x=0,y=0,z=0,r=0] ~ ~ ~ /clear @a[c=1] minecraft:emerald 0 0
    
  2. If a player had at least 16 emeralds ("ITEMS" score of 16+), then remove their "AffectedItems" trigger to preserve their score. If a /give or /clear command is used instead, their "ITEMS" score will be modified and thus can no longer reliably be used.

    /stats entity @p[x=0,y=0,z=0,r=0,score_ITEMS_min=16] clear AffectedItems
    
  3. Provide the player with necessary item if they have an "ITEMS" score of 16+, which means they have 16+ emeralds.

    /give @p[x=0,y=0,z=0,r=0,score_ITEMS_min=16] minecraft:stone
    
  4. Remove the 16 emeralds from the player's inventory.

    /clear @p[x=0,y=0,z=0,r=0,score_ITEMS_min=16] minecraft:emerald 0 16
    

Summary

You will repeat the "Detection" command set for each item you want to sell. Make sure to change the coordinates for each section.

CommandStats guide.

General info on commands.