1
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!
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!
10
The ON function:
Hope it helped! If you have any questions or issues feel free to comment below.
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.
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.
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
# 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
An expert advice: instead of placing barriers, summon tagged invisible armor stands:
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.
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 killThat 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.
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.
I had resolved it, but this looks interesting, I may try that at some point!
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.
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.
Wait, can you iterate the point of this post again? Is it a question or a statement?
Sorry, I got stuff sorted out. It was a question about player tags, but I got it. I finished and posted the datapack mentioned
I was referring to the /tag command. However, the function tag here looks really promising!
https://minecraft.gamepedia.com/Tag
https://minecraft.gamepedia.com/Tag
