Commandes aléatoires

Il peut parfois être utile de générer de l’aléatoire dans Minecraft pour envoyer un texte à un joueur, permettre à un pnj de proposer diverses offres, ou encore modifier la réaction du jeu en fonction de l’heure.

Pour cet exemple, un message aléatoire sera envoyé au joueur lançant la commande. Les messages choisis sont 16 messages parmi les 400 pouvant s’afficher au lancement de Minecraft (sur le logo).

Messages

Tout d’abord, je commence par créer un datapack. Je créé ensuite le dossier data/mangarmor/functions/random qui contiendra les fonctions de ce projet. Enfin, je créé le dossier message qui contiendra 16 fonctions numérotées de 01 à 16 et qui enverront chacune un message différent au joueur. Voici un exemple avec la fonction mangarmor:random/message/01 :

				
					tellraw @s ["",{"text":"Aléatoire : ","color":"gold"},"Vu à la TV !"]
				
			

Ensuite, je créé l’objectif random dans le scoreboard avec une function init qui sera lancée au chargement du datapack (en l’ajoutant au tag minecraft:load). Je t’invite à lire mes précédents articles si tu ne sais pas comment faire. Le datapack est également disponible en fin d’article. L’objectif random servira à enregistrer les variables du datapack.

				
					# Création de l'objectif pour enregistrer les variables
scoreboard objectives add random dummy "Aléatoire"

# Message pour confirmer le chargement du datapack
tellraw @a ["",{"text":"Aléatoire :","color":"gold"}," datapack chargé"]
				
			

Incrémentation

La première méthode est sans doute la plus simple : incrémenter une variable à chaque utilisation pour modifier la prochaine réaction. Le message ne sera donc pas aléatoire mais l’incrémentation permet un message différent à chaque appel.

Je créé ensuite la fonction next qui se content d’incrémenter la variable et envoyer un message en fonction de sa valeur. Enfin, si la valeur est 16, on la passe à 0 afin de revenir au premier message.

				
					scoreboard players add next random 1

execute if score next random matches 1 run function mangarmor:random/message/01
execute if score next random matches 2 run function mangarmor:random/message/02
execute if score next random matches 3 run function mangarmor:random/message/03
execute if score next random matches 4 run function mangarmor:random/message/04
execute if score next random matches 5 run function mangarmor:random/message/05
execute if score next random matches 6 run function mangarmor:random/message/06
execute if score next random matches 7 run function mangarmor:random/message/07
execute if score next random matches 8 run function mangarmor:random/message/08
execute if score next random matches 9 run function mangarmor:random/message/09
execute if score next random matches 10 run function mangarmor:random/message/10
execute if score next random matches 11 run function mangarmor:random/message/11
execute if score next random matches 12 run function mangarmor:random/message/12
execute if score next random matches 13 run function mangarmor:random/message/13
execute if score next random matches 14 run function mangarmor:random/message/14
execute if score next random matches 15 run function mangarmor:random/message/15
execute if score next random matches 16 run function mangarmor:random/message/16

execute if score next random matches 16 run scoreboard players set next random 0
				
			

Heure du jour

Une autre solution est de déterminer le message en fonction de l’heure en jeu. La commande time nous permet d’obtenir une valeur entre 0 et 24000. J’enregistre la valeur obtenue dans la variable time de l’objectif random et envoie un message en fonction de la valeur.

				
					execute store result score time random run time query daytime

execute if score time random matches 0..1500 run function mangarmor:random/message/01
execute if score time random matches 1500..3000 run function mangarmor:random/message/02
execute if score time random matches 3000..4500 run function mangarmor:random/message/03
execute if score time random matches 4500..6000 run function mangarmor:random/message/04
execute if score time random matches 6000..7500 run function mangarmor:random/message/05
execute if score time random matches 7500..9000 run function mangarmor:random/message/06
execute if score time random matches 9000..10500 run function mangarmor:random/message/07
execute if score time random matches 10500..12000 run function mangarmor:random/message/08
execute if score time random matches 12000..13500 run function mangarmor:random/message/09
execute if score time random matches 13500..15000 run function mangarmor:random/message/10
execute if score time random matches 15000..16500 run function mangarmor:random/message/11
execute if score time random matches 16500..18000 run function mangarmor:random/message/12
execute if score time random matches 18000..19500 run function mangarmor:random/message/13
execute if score time random matches 19500..21000 run function mangarmor:random/message/14
execute if score time random matches 21000..22500 run function mangarmor:random/message/15
execute if score time random matches 22500..24000 run function mangarmor:random/message/16
				
			

Particule

Une autre méthode consiste à générer une particule et à utiliser son identifiant unique (UUID) comme nombre aléatoire. Je commence par enregistrer la variable max qui contiendra la valeur maximum du nombre aléatoire. Ensuite, je génère une particule et récupère son UUID. J’effectue ensuite un modulo pour obtenir un nombre entre 0 et le maximum souhaité (non compris). Enfin, pour obtenir un nombre entre 1 et 16, j’ajoute 1. Il ne me reste plus qu’à afficher le message correspondant et à détruire la particule.

				
					scoreboard players set max random 16

# Génèration d'une entité pour obtenir un nombre aléatoire
summon minecraft:area_effect_cloud ~ ~ ~ {Tags:["random_math"],Age:1}
execute store result score math random run data get entity @e[type=area_effect_cloud,tag=random_math,limit=1] UUID[0]

# Calcul d'un nombre dans l'écart souhaité (entre 1 et 16)
scoreboard players operation math random %= max random
scoreboard players add math random 1
kill @e[type=area_effect_cloud,tag=random_math]

execute if score math random matches 1 run function mangarmor:random/message/01
execute if score math random matches 2 run function mangarmor:random/message/02
execute if score math random matches 3 run function mangarmor:random/message/03
execute if score math random matches 4 run function mangarmor:random/message/04
execute if score math random matches 5 run function mangarmor:random/message/05
execute if score math random matches 6 run function mangarmor:random/message/06
execute if score math random matches 7 run function mangarmor:random/message/07
execute if score math random matches 8 run function mangarmor:random/message/08
execute if score math random matches 9 run function mangarmor:random/message/09
execute if score math random matches 10 run function mangarmor:random/message/10
execute if score math random matches 11 run function mangarmor:random/message/11
execute if score math random matches 12 run function mangarmor:random/message/12
execute if score math random matches 13 run function mangarmor:random/message/13
execute if score math random matches 14 run function mangarmor:random/message/14
execute if score math random matches 15 run function mangarmor:random/message/15
execute if score math random matches 16 run function mangarmor:random/message/16

# Détruction de l'entité
kill @e[type=minecraft:area_effect_cloud,name=random_math]
				
			

Bedrock edition

Les versions Bedrock de Minecraft disposent d’une commande bien utile : le random est disponible dans le scoreboard. Ainsi, pour générer un nombre aléatoire entre 1 et 16, il suffit d’écrire cette ligne :

				
					scoreboard players random bedrock random 1 16

				
			

Espérons que cette fonctionnalité apparaisse prochainement dans l’édition Java.

Téléchargement

Pour aller plus loin

Voici d’autres articles qui pourraient t’intéresser :

Minecraft

Boutique : monnaie virtuelle

Les vendeurs de Minecraft utilisent l’émeraude comme monnaie du jeu. Nous allons voir dans cet article comment utiliser une monnaie plus sécurisée car dématérialisée.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.

Sommaire

Partager

Facebook
Twitter