1

Conditional Command Execution via Tags

Torphedo 6/5/20 4:51 pm history
516
9/24/2020 10:57 pm
Hi, I'm building a datapack, and I'm trying to run commands conditionally. I am aware that this can be done using scoreboards, and this is my current setup. But, I'm trying to make things fairly plug & play, so I'd like to be able to use tags because they can be added and removed easily and without any setup. As far as I know, there's no way to run a command the first time when a pack is loaded, and I think if I were to try to automate the setup, it would do it either every tick, or every time I use the command that requires a tag/scoreboard. I don't want to flood my chat with failure messages from the scoreboard already existing. Also, my main goals are efficiency, accessibility, and minimalism of my code. My datapack is just a JSON defining the tick variable, and a tick.mcfunction file that does everything.

Info about my datapack (not entirely relevant to the question, but gives context on why I'm doing things the way I am)

If you are curious, I'm working on running my creations out of a book. You click on options in the book, and it runs commands that create various fun items. The one that requires a tag or scoreboard is called BarrierFly. There's an on and an off function, which change the player score. the datapack executes at player with that score, and hopefully soon, the tag. BarrierFly does what it sounds like. It places a platform of barriers under you (the fill replaces air only) and erases the ones you leave behind as you walk. I'm also trying to make it multiplayer compatible, which hasn't been much of an issue yet.

If you have an idea for how I could better do any of this, I'm not attached to my current method, so feel free to suggest things! I always enjoy making things more efficient and effective!


Thanks,
Torphedo


edit: I got it, thanks Fleevoid

edit 2: The code was switched to tags a while ago, and it works great. I may upgrade in the future to custom armor stand blocks as mentioned in the comments.

edit 3: I can't find a good way to implement the armor stands, but suggestions are welcome!
Posted by
Torphedo
Level 14 : Journeyman Explorer
1

  Have something to say?

JoinSign in

10

One_Nose
06/18/2020 1:08 pm
He/Him • Level 59 : Grandmaster Dragonborn Hero
The ON function:
tag @s add barrier_flyThe OFF function:
tag @s remove barrier_flyCause every player with BarrierFly on to say hi:
execute as @a[tag=barrier_fly] run say hiRemember to replace the "as" with "at" if you want the position instead of the target.

Hope it helped! If you have any questions or issues feel free to comment below.
2
Bertiecrafter
06/08/2020 10:21 am
Level 72 : Legendary System
Probably too late, but there is a minecraft:load tag that runs all listed functions on /reload. Very useful if you want to only run things once.
2
Torphedo
06/06/2020 12:30 am
Level 14 : Journeyman Explorer
The BarrierFly code, currently using scoreboards:

# cleanup
execute at @p run fill ~2 ~-1 ~2 ~-2 ~-1 ~2 minecraft:air replace minecraft:barrier
execute at @p run fill ~2 ~-1 ~2 ~2 ~-1 ~-2 minecraft:air replace minecraft:barrier
execute at @p run fill ~-2 ~ ~-2 ~2 ~ ~2 minecraft:air replace minecraft:barrier
execute at @p run fill ~-2 ~-2 ~-2 ~2 ~-2 ~2 minecraft:air replace minecraft:barrier
execute at @p run fill ~-2 ~-1 ~-2 ~-2 ~-1 ~2 minecraft:air replace minecraft:barrier
execute at @p run fill ~-2 ~-1 ~-2 ~2 ~-1 ~-2 minecraft:air replace minecraft:barrier
# platform
execute if score @p BarrierFly matches 1 run execute at @p run fill ~-1 ~-1 ~-1 ~1 ~-1 ~1 minecraft:barrier replace air
1
One_Nose
06/18/2020 1:26 pm
He/Him • Level 59 : Grandmaster Dragonborn Hero
An expert advice: instead of placing barriers, summon tagged invisible armor stands:
execute at @a[tag=barrier_fly] if block ~ ~-1 ~ minecraft:air run summon minecraft:armor_stand ~ ~-1 ~ {Tags:["barrier_fly_armor_stand"],Invisible:1b,Marker:1b}If there is a barrier_fly player above, they will make a barrier where they are:
execute at @e[tag=barrier_fly_armor_stand] positioned ~ ~1 ~ if entity @a[tag=barrier_fly,distance=..1] run setblock ~ ~-1 ~ minecraft:barrierElse, they will kill the block and themselves.
execute at @e[tag=barrier_fly_armor_stand] positioned ~ ~1 ~ unless entity @a[tag=barrier_fly,distance=..1] run setblock ~ ~-1 ~ minecraft:air
execute as @e[tag=barrier_fly_armor_stand] at @s positioned ~ ~1 ~ unless entity @a[tag=barrier_fly,distance=..1] run kill

That is how custom blocks work. In doing this you make a new block that acts exactly as a barrier, but disappears if there is no barrier_fly player above him. Doing this ensures that your data pack won't break regular barriers. It also ensures all barriers are destroyed, for example if you jump your code might retain the barriers. If you understood this, you are one step from making big datapacks. If not or you have any issues, you can comment me and I will try to help you.
2
Torphedo
09/22/2020 2:57 pm
Level 14 : Journeyman Explorer
oh, also, there is already a line of code to handle jumping and one for going downwards by breaking the one under you in creative.
1
Torphedo
08/11/2020 8:21 pm
Level 14 : Journeyman Explorer
I had resolved it, but this looks interesting, I may try that at some point!
2
Torphedo
09/24/2020 10:57 pm
Level 14 : Journeyman Explorer
history
I implemented this, and it works, but it doesn't handle speed as well and even regular sprinting can outrun it, causing you to drop a block occasionally. Any ideas? The easiest solution would be to make it a 3x3 ring of armor stands around the player, which is why my original can handle a comparably large amount of speed. The barriers also don't disappear properly, which I think is because by the time the command goes into effect, the player is no longer in a place where the barrier would get erased. Just a theory, I'll do some more testing tomorrow.

Edit: Even if this is properly implemented, barriers would still vanish behind you if you walk on them. I'll be reverting this unless anyone has a way to make this work.
1
HoboMaggot
06/05/2020 9:25 pm
Level 55 : Grandmaster Blob
Wait, can you iterate the point of this post again? Is it a question or a statement?
2
Torphedo
06/05/2020 9:27 pm
Level 14 : Journeyman Explorer
Sorry, I got stuff sorted out. It was a question about player tags, but I got it. I finished and posted the datapack mentioned
2
Torphedo
06/05/2020 5:32 pm
Level 14 : Journeyman Explorer
I was referring to the /tag command. However, the function tag here looks really promising!

https://minecraft.gamepedia.com/Tag
1

Welcome