TransWikia.com

how to run random command through datapack?

Arqade Asked on January 8, 2021

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

2 Answers

I found something similar but more easier for who that want to do

the generator

https://misode.github.io/loot-table/

what I do after is
(in main function) for running command:

/execute if score @s RANDOM matches <any number in range that not used>  run function <namespace>:<function>  

(in the end of the function)for not repeated commands:

/scoreboard players set @s RANDOM 0

Correct answer by panda on January 8, 2021

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.

data modify storage rcg commands set value [<values>]

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.

data modify storage rcg commands append value “<command>”

Now, we need a Impulse Command Block set to Needs Redstone to run the command. We’ll put the command block at coordinate 0 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.

execute store result score in1 math run data get storage rcg commands

Now, we run the function for the rng.

function math:rng/range

Next, we set a new nbt array to the original one to iterate through the array later.

data modify storage rcg itercommands set from storage rcg commands

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 function findatindex The following commands does this.

data remove storage rcg itercommands[0]
scoreboard players remove out math 1
execute unless score out math matches 0 run function <namespace>:findatindex

Now, we run this new function unless out is 0.

execute unless score out math matches 0 run function <namespace>:findatindex

Next, we take the first value in the itercommands array and store it in the command block’s command.

data modify block 0 0 0 Command set from storage rcg itercommands[0]

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.

setblock 0 0 -1 redstone_block

Finally, we schedule another function for 1 tick in the future, to remove the redstone block. We’ll call it rcg_end.

schedule function <namespace>:rcg_end

There you go. You are finished. Please let me know if you have any bugs or questions.

Answered by randomuser922 on January 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP