How do I copy a player's coordinates into a Lodestone?
-
I'm trying to make a manhunt compass using lodestone NBT, avoiding /setworldspawn issues, but I'm having trouble copying the runner’s position array data to the compass'
LodestonePos
NBT tag, considering they are in different formats, and this information will be constantly changing and being updated every tick.Runner’s Position Format (Doubles):
Pos{0.0d, 0.0d, 0.0d}
Compass's Lodestone Position Format (Must be integers.):
LodestonePos{X: 0, Y: 0, Z: 0}
I've tried using item modifiers,
/item modify
,/execute store result storage
,/data modify storage
, to no avail. There doesn't seem to be a way to select array positions in paths, and simply setting the value from the player's position won’t work because they are in different formats.
-
Copying the item into the player's inventory
A great post has already been made regarding this: https://gaming.stackexchange.com/questions/376895/how-can-i-modify-an-item-while-it-is-inside-the-players-inventory
Setting the lodestone position
These should suffice:
execute store result block Items[0].tag.LodestonePos.X int 1 run data get entity @p[] Pos[0]
execute store result block Items[0].tag.LodestonePos.Y int 1 run data get entity @p[<however you’re selecting the runner>] Pos[1]
execute store result block Items[0].tag.LodestonePos.Z int 1 run data get entity @p[<however you’re selecting the runner>] Pos[2]
Now, why does this work?
The player's
Pos
attribute is an array ofdouble
s (high-precision decimal values), whereas the compass's LodestonePos is anObject
with attributesX
,Y
, andZ
, which are allint
s (whole numbers between -231 and 231-1).
Why the developers decided to have different implementations of coordinates is beyond me, but what this means for us is that we have to make three seperate assignments; one for each axis.First, we get the player's position
Let's use
@p
for simplicitydata get @p Pos
this will return something like
[0d, 3.14d, 12345.678d]
, which is an array of the X, Y, and Z coordinates respectively.data get @p Pos[0]
This is just the X coordinate
Now let's store a value into an item
Let's use 0 0 0 as the location of a shulker box with a single compass in it
execute store result block 0 0 0 Items[0].LodestonePos.X ...
This is accessing the X coordinate of the
LodestonePos
attribute of the first item in a shulker box at0 0 0
, presuming one exists.breaking down the execute command
Now we have
execute store result block Items[0].tag.LodestonePos.X int 1 run data get entity @p[] Pos[0]
which will store the player's x position into the lodestone compass's X attribute.
Let's look at it piece by piece
# using the 'store' directive indicates you want to put a value somewhere execute store
you want to store the value you get, not just whether-or-not it succeeds
result
you want to put the result into a TileEntity (aka Block Entity) like a chest or shulker box
block
#the location of the tile entity
0 0 0the NBT path of the tile entity you want to edit
Items[0].tag.LodestonePos.X
indicates the resulting value should be converted to type
int
int
the scale of the resulting value (just multiplication)
1
you want to run a command to get a value
run
the command that should return the desired value
data get entity @p[<however you’re selecting the runner>]
Making it work for X, Y, and Z
This is relatively simple. Just replace
Pos[0]
withPos[1]
for the Y-coodinate, orPos[2]
for the Z-coordinate. Then ReplaceLodestonePos.X
withLodestonePos.Y
andLodestonePos.Z
, and there you have the 3 different commands