I want to do random command from a list to run but in datapack and without taking space from a world also for a tick
Minecraft-Java-Edition-Datapacks – How to Run Random Command through Datapack
minecraft-commandsminecraft-data-packsminecraft-java-edition
Related Topic
- Minecraft – How do i run a minecraft datapack function when a player has a specific item in their inventory? (minecraft 1.15)
- Minecraft – Why does /trigger have no objectives/commands after installing a datapack that has /trigger commands
- Minecraft Java Edition – How to Check for Something Every Tick but Only Run a Command One Time When the Condition Is Met
Best Answer
1.15+
My solution uses an nbt array, grabs a random number, takes the value at index that was generated, and runs the command in a command block.
Preparation in Minecraft
So in preparation, you will need an array in a storage which we’ll call
rcg
with each command as a value. Make sure the commands are in strings. You can do that with the following command.If you ever want to add another command, the process is simple. All you need to do is append the command. The process is what follows.
Now, we need a
Impulse Command Block
set toNeeds Redstone
to run the command. We’ll put the command block at coordinate0 0 0
.Preparation Outside Minecraft
Now, we need a random number generator. Cloud Wolf has a random number generator that can use a range. The rng comes with trig as well although we won’t need trig for this. The download link is here. Now you need to install the datapack into your world and then you are ready to go.
Core Commands
Now, we get to the commands. So in a new function, we need a command to retrieve the length of the command array and store it in the range max scoreboard value. We’ll call this function
main
. You can do that with the following command.Now, we run the function for the rng.
Next, we set a new nbt array to the original one to iterate through the array later.
Now, we need a new function to remove the first value from the
itercommands
array, remove 1 from the generated number, then run the function again unless the out value is 0. We’ll name this functionfindatindex
The following commands does this.Now, we run this new function unless out is 0.
Next, we take the first value in the
itercommands
array and store it in the command block’s command.Next, we power the command block by using one of multiple methods. 1: We modify the command block to set always active on. 2: We modify the command block to set powered to true. 3: We set a redstone block next to it. I will go with the 3rd.
Finally, we schedule another function for 1 tick in the future, to remove the redstone block. We’ll call it
rcg_end
.There you go. You are finished. Please let me know if you have any bugs or questions.