Get Coordinates of command source

I want to create a mysql home Plugin. I found out, that there are functions like the teleporthelper, but I cant use them in combination with a command source.

Please can you show me how it would look, if I want to get the coordinates from a command sorce and save x,y,z in 3 variables ?

you would need 4 variables actually…1 for the world, 3 for the coordinates, to do it that way…

thats great :slight_smile: by the way - can you say me how i perform this ? ^^

Presumedly the only command-souce making a ‘home’ would be a player… you’re not worrying about the console - no physical presence - or a command-block issuing the command. Or another plugin…


if (src instanceof Player){
Player player = (Player) src;
Location pLoc = player.getLocation();
World world = (World) pLoc.getExtent();
String worldname = world.getName();
int x=pLoc.getBlockX();

…etc for y, z coords

Though really, if you are just using a key/value system : “HomeX” for “PlayerX” = “LocationX”… you dont need to break the location down into all of its individual smallest compoents for storing and then build it back up again, unless you will have reasons for wanting x, y , z as separate fields in the database for a reason, or worlds to be separate.

You can store the location object as a string, or serialize it. Or you can pull the world part out from a location, and the coordinate-vector part out of a location, and store world as one field, and the position vector as another, which is less messy strings than a location object string…

thanks :slight_smile: the idea is, that i want (as u said) to store homes in my mysql db ^^
when its possible, i will store the location as it is in my db :smile:

I have another question ^^
When i stored it, and the player runs something like /home, how can i teleport the player to the saved location ?

Another question:

When I create the new Location with the 4 vars → location = new Location(var,var, var, var) , right ?
Greets :slight_smile:

I believe that’s correct. You then change the location of the player with
player.setLocation(Location location)

And don’t forget to offer() it back to the server once it’s changed! I believe this will result in this code snippet:
offer(player.setLocation(Location location))


ok, so after i changed the location of a player, i just hafe to run this:
offer(player.setLocation(Location location)) ? ^^ (with my variables)

why i have to do this ? :slight_smile:

Think of it as forcing a refresh to assert that it is not an illegal movement at mach 7

1 Like

You’ll notice that the contents of the offer() is the location setting in the first snippet, so you don’t need both lines :slight_smile:

I’m not entirely sure why you have to do this, but perhaps the player object you manipulate is a snapshot of the dynamic one used by the game itself, to ensure your variables don’t dynamically change while you’re working on them? I didn’t make it, and I don’t know Java, but I’ve been on these forums for long enough to pick up some how’s. The why’s I leave to the Sponge team :stuck_out_tongue:

I think this is a part of the Data API, so perhaps @gabizou would be able to enlighten us?

yeah, i know, that i don’t have to use the code again, but I hope too, that someone can explain offer() :smiley:

wait what? You aren’t offering anything back… player.setLocation(Location) works just fine. The only things you offer are DataManipulators (and soon Values).

1 Like

In that case, I stand corrected. Offer() isn’t necessary. :stuck_out_tongue:

good to know :slight_smile: