2

Using UUIDs to execute functions

RADI0R 4/5/22 9:35 am
2k
4/6/2022 11:27 am
So I know I can read any entity's UUID with /data get entity.

But what I'm looking for is a way to use this data. For example...

I want to create a datapack, in which any player can select a mob and later on I want to run a command as this selected mob. It also needs to work on other players, in a "player selects a player" scenario.
I could make this work with /tag command, adding a tag to the selected entity, but I need a unique tag each time a player selects anything. And I need this datapack to work on servers, allowing multiple players to select their mob at the same time. NOTE: A player in this datapack cannot select multiple entities at once. Only one at a time.

The solutiuon I came up with would be to save the UUID of the selected entity somewhere, and later on do:
/execute as @e[nbt={UUID:[ -somehow insert stored UUID here- ]}] run ...
But I have no idea how to do that.

Is there a way to do this with UUIDs? Or is there a different solution? Please help.
Posted by
RADI0R
Level 43 : Master Loremaster
60

  Have something to say?

JoinSign in

7

Kefaku
04/05/2022 10:22 am
Level 45 : Master Nerd
I think I know how to do this, but I'll first have to test it myself when I'm at home (soon), to make sure I'm not telling you something that is not working.
2
RADI0R
04/05/2022 11:30 am
Level 43 : Master Loremaster
Oh it's you again :DThanks a lot in advance man. What would I do without you...
2
Kefaku
04/05/2022 12:32 pm
Level 45 : Master Nerd
I found a solution. It's not very efficient, but it works and I'm sure you can come up with some way to improve it.
Feel free to ask me, if you don't understand something or need more help. :D

First of all, just inserting a stored value into a selector like you suggested ("/execute as @e[nbt={UUID:[ -somehow insert stored UUID here- ]}]") is not possible (to my knowledge).

Now to the solution I found: I first tried storing the UUID using '/execute store result storage', but I came across a problem that was similar to yours: While you needed a unique tag for each player, that method would need a unique storage for each player.
Then I noticed, that the UUID in the NBT of each entity basically is just an array of 4 integers. Since it's just integers, they can be stored in a normal scoreboard, which automatically creates a unique position for each entity that is given a score. Knowing that I added 4 scoreboards for the 4 integers that the UUID consists of:

scoreboard objectives add linkedUUID0 dummy
scoreboard objectives add linkedUUID1 dummy
scoreboard objectives add linkedUUID2 dummy
scoreboard objectives add linkedUUID3 dummy

These are named linkedUUID, because for each player they store the UUID of the entity that the players is linked to (=> wants to teleport to). I needed a way to store the UUID values in these scoreboards and came up with the following commands, which store the 4 UUID values of the nearest entity (@e; the distance=0.01.. is there to make sure it doesn't the select the player itself) in the linkedUUID score of the player that wants to be teleported to that entity later on (@p). Of course you can modify the selectors depending on what you need.

execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @p linkedUUID0 run data get entity @s UUID[​0]
execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @p linkedUUID1 run data get entity @s UUID[​1]
execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @p linkedUUID2 run data get entity @s UUID[​2]
execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @p linkedUUID3 run data get entity @s UUID[​3]

Because a player can only have 1 score your condition "A player in this datapack cannot select multiple entities at once. Only one at a time." is automatically met. Now I had scoreboards telling me what UUID a player wants to teleport to, but I needed 4 more scoreboards, which for the linked entities store their own UUID, so I can compare them and find the one to teleport to later:

scoreboard objectives add UUID0 dummy
scoreboard objectives add UUID1 dummy
scoreboard objectives add UUID2 dummy
scoreboard objectives add UUID3 dummy

Then the entity that is beeing linked to a player needs to store its own UUID to these UUID scoreboards. These commands should be executed simultaneously with the commands that store the linkedUUID to a player score:

execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @s UUID0 run data get entity @s UUID[​0]
execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @s UUID1 run data get entity @s UUID[​1]
execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @s UUID2 run data get entity @s UUID[​2]
execute at @p as @e[​limit=1,distance=0.01..,sort=nearest] store result score @s UUID3 run data get entity @s UUID[​3]

Finally, I just needed a way to teleport the player to the entity that matches the stored UUID. I just execute as every entity (@e) , if all its UUID scores (its own UUID) match the linkedUUID scores of the player that wants to teleport (@p):

execute at @p as @e[​distance=0.01..] if score @s UUID0 = @p linkedUUID0 if score @s UUID1 = @p linkedUUID1 if score @s UUID2 = @p linkedUUID2 if score @s UUID3 = @p linkedUUID3 run tp @p @s
4
Silabear
04/06/2022 11:04 am
He/Him • Level 70 : Legendary Guard Bear
you are epic
5
Kefaku
04/06/2022 11:27 am
Level 45 : Master Nerd
Thanks ^^
1
RADI0R
04/05/2022 4:54 pm
Level 43 : Master Loremaster
I'm honestly speechless. Not only is this a really smart solution I never would've thought of, but the biggest kudos for formulating it all in such a clean and organized way. Thank you for taking your time typing all that. It's amazing.
2
Kefaku
04/05/2022 11:29 pm
Level 45 : Master Nerd
Thanks for your appreciation. :D
Please tell me, if you found a way to improve it or make it more efficient. I'd really like to know a way, because currently comparing 4 scores for every entity in existence (in the tp command) is not very efficient.
2

Welcome