Minecraft – Giving players one arrow every two seconds, possibly with /replaceitem

minecraft-commandsminecraft-java-edition

So, I need o give players one arrow every two seconds, but with a maximum of two arrows in the inventory. To do this, I tried adding a scoreboard objective called
ArrowN and setting the score to 1 if the player has one arrow and to 2 if the player has two arrow. Then every two seconds replacing with two arrows if the player has an ArrowN score of 1 and not replacing if he has a score of 2. Now, I couldn't get how to set a score of 0 for a player that has no arrows, to replace him one arrow. Something like this just works for a half:

scoreboard objectives set @a[score_ArrowN=!1,score_ArrowN=!2] ArrowN 0

I think operation could be useful, but i can't use that command.

Note: it must be multiplayer friendly

Edit:probably using /give is a lot easier but I don't like having arrows popping on your face every two seconds

Best Answer

Part of the solution is to use the /stats command to count the number of arrows in a players inventory using the /clear command, and set that result to a scoreboard objective for the player. Then it's pretty straight forward command-block-fu to give arrows when needed, as well as take them away. Let's see how I did it.

First things first, though, I'm doing this in 1.9, but the commands will work fine in 1.8, with a little adjustment and a 20Hz fill clock.

We need 2 scoreboard objectives: the arrow count (which you've named ArrowN, so I'll keep that in my example), and a timer variable.

/scoreboard objectives add ArrowN dummy
/scoreboard objectives add Timer dummy

Next, we need a chain of command blocks, starting with a repeating command block, and followed by several chain command blocks. Set the repeating command block to "Needs Redstone", at least for now, and the rest to "Always Activate". The first two commands, in order, are:

/scoreboard players remove #Timer Timer 1
/clear @a minecraft:arrow -1 0

#Timer is a dummy player that will never show up in scoreboard displays, and he will keep track of when to hand out arrows. It might be a good idea to initially set his Timer score to 40, but that's not strictly necessary. The second command will count all the arrows someone has in their inventory, independent of the number of stacks. Unfortunately, this doesn't do much for us until we get that count into the scoreboard. To do this, stand on the command block and enter the following command (through chat):

/stats block ~ ~-1 ~ set QueryResult @a ArrowN

So, whenever the clear command gets run, the count that it finds will be placed in that players ArrowN score.

The next three commands manage the 2 second timer and dole out the arrows. In 1.9, the first is unconditional, while the other 2 are conditional. For 1.8, you can use comparators off each side of the first command block, but this is a little inelegant.

/scoreboard players test #Timer Timer * 0
/scoreboard players set #Timer Timer 40
/give @a[score_ArrowN=1] minecraft:arrow

The first command tests to see if the timer has reached 0, and if so, resets it (the second command) and gives all players an arrow if they have at most 1 arrow already (the third command).

Now, we just need to remove arrows from people who have too many. These commands are all unconditional so that they run every game tick:

/clear @a[score_ArrowN_min=3] minecraft:arrow -1 1
/clear @a[score_ArrowN_min=12] minecraft:arrow -1 9
/clear @a[score_ArrowN_min=66] minecraft:arrow -1 54

These three commands will remove a total of 1 arrow if the player has between 3 and 10, 10 arrows if they have between 12 and 66, and a whole stack if they have 66 or more. You can expand on this (and add finer intervals) if you find arrows aren't being removed fast enough, but it should take less than a second in most cases.