Storing a player's location and teleporting the player back to it later in Java Minecraft



  • I'm looking for a way to store a player's current location (probably in the scoreboard as separate X, Y and Z) and teleport the player to that location later. I'm not completely familiar with the execute command, but I think this should be possible with the store sub-command.

    The methods I found online involved an intermediary entity, whose location is modified first, then the player is teleported to that entity, and finally the entity is killed so the identifier can be reused. The problem I have with this method is that the target location has to be loaded, which I cannot ensure.

    I'm looking for as simple as possible a solution that I can put in my own data packs, not an external data pack. Can anyone provide me with the method and exact commands to achieve this?



  • You can do this with scoreboards but the solution is kind of long

    First, setup the scoreboards with:

    scoreboard objectives add xpos dummy
    

    scoreboard objectives add ypos dummy

    scoreboard objectives add zpos dummy

    Then, at any time you can do:

    execute as  run execute store result score @s xpos run data get entity @s Pos[0]
    

    execute as run execute store result score @s ypos run data get entity @s Pos[1]

    execute as run execute store result score @s zpos run data get entity @s Pos[2]

    This saves your position as a score

    Unfortunately, in order to teleport back to those coordinates non-manually, it takes a lot of complicated math functions, but here goes. I will paste the two I have and the Y part should be easy.

    You will need the following four blocks of commands, for both x and z, and their positive and negative. The same thing should work for Y meaning you would only need one block of commands if you only plan to teleport to locations above y0, but a second is necessary for it to be compatible with the new negative areas. NOTE: I do not have the y value one made, but you should be able to make it reletively easily by changing the values.

    But first:

    execute as  store result score @s tpX run data get entity @s Pos[0]
    

    execute as store result score @s tpY run data get entity @s Pos[1]

    execute as store result score @s tpZ run data get entity @s Pos[2]

    tp 0 0 0

    execute as @s[scores={tpX=..-1}] at @s run function minecraft:tpx_neg

    execute as @s[scores={tpX=1..}] at @s run function minecraft:tpx_pos

    execute as @s[scores={tpY=..-1}] at @s run function minecraft:tpy_neg

    execute as @s[scores={tpY=1..}] at @s run function minecraft:tpy_pos

    execute as @s[scores={tpZ=..-1}] at @s run function minecraft:tpz_neg

    execute as @s[scores={tpZ=1..}] at @s run function minecraft:tpz_pos

    tpx_neg

    scoreboard players set @s tpConst -1
    scoreboard players operation @s tpX *= @s tpConst
    

    #Teleports the player 1 block if it needs, this is all just binary
    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-1 ~ ~

    #Teleport two blocks, rides off the const of 2 before
    scoreboard players operation @s tpVar = @s tpX
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-2 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 4
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-4 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 8
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-8 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 16
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-16 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 32
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-32 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 64
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-64 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 128
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-128 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 256
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-256 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 512
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-512 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 1024
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-1024 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 2048
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-2048 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 4096
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-4096 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 8192
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-8192 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 16384
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-16384 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 32768
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-32768 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 65536
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-65536 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 131072
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-131072 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 262144
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-262144 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 524288
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-524288 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 1048576
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-1048576 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 2097152
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-2097152 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 4194304
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-4194304 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 8388608
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-8388608 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 16777216
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-16777216 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 33554432
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-33554432 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 67108864
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-67108864 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 134217728
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-134217728 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 268435456
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-268435456 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 536870912
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-536870912 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 1073741824
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~-1073741824 ~ ~

    scoreboard players set @s tpConst -1
    scoreboard players operation @s tpX *= @s tpConst

    tpx_pos

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~1 ~ ~
    

    #Teleport two blocks, rides off the const of 2 before
    scoreboard players operation @s tpVar = @s tpX
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~2 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 4
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~4 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 8
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~8 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 16
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~16 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 32
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~32 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 64
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~64 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 128
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~128 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 256
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~256 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 512
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~512 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 1024
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~1024 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 2048
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~2048 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 4096
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~4096 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 8192
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~8192 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 16384
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~16384 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 32768
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~32768 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 65536
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~65536 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 131072
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~131072 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 262144
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~262144 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 524288
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~524288 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 1048576
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~1048576 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 2097152
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~2097152 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 4194304
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~4194304 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 8388608
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~8388608 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 16777216
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~16777216 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 33554432
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~33554432 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 67108864
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~67108864 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 134217728
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~134217728 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 268435456
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~268435456 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 536870912
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~536870912 ~ ~

    scoreboard players operation @s tpVar = @s tpX
    scoreboard players set @s tpConst 1073741824
    scoreboard players operation @s tpVar /= @s tpConst
    scoreboard players set @s tpConst 2
    scoreboard players operation @s tpVar %= @s tpConst
    execute as @s[scores={tpVar=1}] at @s run tp @s ~1073741824 ~ ~

    CONTINUED IN PART 2 I RAN OUT OF SPACE



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2