How can I get a player's location and stock it in tree differents ints [Spigot 1.15.2]

Viewed 1559

I want to create a command that gets the coordinates of the player who executed the command and stock it in three int (X Y Z) and to be able to use them in another command to teleport a player to those coordinates (it's a /spawn command).

1 Answers

You need to create a CommandExecutor, explained over here. You can get the Location fragments (X Y Z) from the Location of the Player. Then you need to save it somewhere to access it when the other command is executed, for example in a Hashmap to link the location to the player.

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (sender instanceof Player) {
        Player player = (Player) sender;
        // get location and location fragments
        Location loc = player.getLocation();

        double x = loc.getX();
        double y = loc.getY();
        double z = loc.getZ();
        // convert to int (information loss)
        int xInt = (int)x;
        int yInt = (int)y;
        int zInt = (int)z;
    }
} 
Related