How to setup a scoreboard for items

I am making a map where if you jump on a pressure plate you get 1 paper, how can i make it so when I get 1 paper the scoreboard counts it and then displays it and also how do i make it so if I drop 1 paper, it then shows one less in the scoreboard


Solution 1:

You can use CommandStats to count the number of items in a player's inventory.

The /clear command will return an "AffectedItems" value equal to the number of items that were cleared. You can set the maximum amount to 0 to prevent items from being removed, which will then return the total number of items that player had in their inventory.

This is also multiplayer-friendly since each player can have their own score and stat assigned, and /execute can be used to cause players to run commands to trigger their own stat.

Prerequisites

Objective to store the "AffectedItems" value.

/scoreboard objectives add PaperCount dummy

Set that to display on the sidebar.

/scoreboard objectives setdisplay sidebar PaperCount

CommandStat to apply to players, who will target their own "PaperCount" score when running commands that return an "AffectedItems" value. If new players are able to join at any time, you may need to run this on a clock.

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

In order for CommandStats to modify a target's score, that target must be tracked on the scoreboard prior. If new players are able to join at any time, you may need to run this on a clock.

/scoreboard players add @a PaperCount 0

Clock commands

The following must be run on a clock.

  1. Cause players to clear 0 paper from their own inventories.

    /execute @a ~ ~ ~ /clear @a[c=1] minecraft:paper 0 0
    

The player will then have a "PaperCount" score equal to the number of paper they had in their inventory.