Minecraft command block: allow user on server to spawn one animal (per user)

minecraft-commandsminecraft-java-edition

I'd like to create a quest where a user can activate a command block that will spawn one ocelot, but limit it so that each player can activate said command only once.

I've got the command to spawn one ocelot, which is:

/summon ocelot ~ ~1 ~ {PersistenceRequired:1}

but I don't know how to limit it so that each player can only do it only once. I found the solution to something similar, but I don't know how to integrate that into spawning animals rather than giving items.

TL;DR: How would I use a command block to allow each player to summon an ocelot only once?

Best Answer

Scoreboards!

You can track a user with scoreboards. The main gist is:

If a user has an hasOcelot score of 1 presses the button, do nothing.
If a user has an hasOcelot score of 0 presses the button, spawn an Ocelot. Give the current player who pressed the button a score for hasOcelot of 1.

Where hasOcelot is a score of type 'dummy' that you use to keep tabs on who has spawned an Ocelot and who hasn't.

You can use the /testfor command with the radius and score selectors to see if the player fits into the criteria.


tl;dr

Chain command blocks like this:

/testfor @p[r=2,score_hasOcelot_max=0] (Check if user hasn't spawned)
/summon ~ ~1 ~ minecraft:Ocelot (Summon if the previous /testfor was true)
/scoreboards players set @p[r=2,score_hasOcelot_max=0] hasOcelot 1 (mark him as having an ocelot spawned, continuation of chain)

Note: I might've messed up some selectors due to changes between 1.9 and now.