Is there a way to set up a shop with scoreboard commands, that you can sell items at?

I'm aware of a way to buy items with a Money system with couple commands. But can you sell items to gain money?

Here is what I used for a buying system:

/scoreboard objectives add Money dummy (name)

Set Display:

/scoreboard objectives setdisplay sidebar Money

Changing Player Score:

Set Money amount:

/scoreboard players set @p Money 10

Add Money:

/scoreboard players add @p Money 10

Remove Money:

/scoreboard players remove @p Money 10

Add New Player to Money System:

Add Objective:

/scoreboard objectives add newplayer dummy

/scoreboard players add @p[score_newplayer_min=0,score_newplayer­=1] Money 100

/scoreboard players set @p newplayer 2

Then you buy:

/scoreboard players remove @p[r=5,score_Money_min=10] Money 10

/give @p[r=5,score_Money_min=10] minecraft:cobblestone 3

But can you sell items to gain money?


The big problem with taking items from players is verifying they have the minimum amount before using the clear command. Here is a method to do that. This solution uses a scoreboard and command stats to determine if a player has a minimum amount of items. It could be used to sell/trade items with players.

First you need to manually create a scoreboard objective:

/scoreboard objectives add ITEMS dummy

This will use four command blocks, all chained. I used a button on the impulse block so the player could initiate. The first block uses the clear command with a value of 0. It does nothing but allows command stats to determine how many items were affected/looked at. The second block stores how many items were affected in the objective ITEMS. This objective now has total number of the specified item in the players inventory. The third block clears 100 custom named paper from the inventory only if the player has at least 100. The fourth block would then be used to output to the player(give currency/items).

Blocks

First block is Impulse Unconditional Needs Redstone with command:

clear @p minecraft:diamond 0 0

Second block is Chain Conditional Always Active with command:

stats block ~1 ~ ~ set AffectedItems @p ITEMS

Third block is Chain Conditional Always Active with command:

clear @p[score_ITEMS_min=100] minecraft:diamond 0 1 

Fourth block is Chain Conditional Always Active it's command:

scoreboard players add @p Money 10

All this to see if the player has at least the specified amount before using the clear command. The clear command, when used alone, will succeed if the player has even one of the specified item in their inventory.

This is someone else's idea. I just verified it actually works, and tweaked the commands a little. Here is the page I found the original commands on.