Save extent to configuration file

I’m currently working on a warp plugin. As I am fairly new to coding plugins, and the Java language in general, I would like to apologize for my lack of knowledge or understanding. Since warps is a simple concept, I decided to challenge myself by making my own warp plugin.

Anyways,… here’s the issue:

When setting the player’s location, I first have to define a new location. This requires me to enter the extent.
It looks like this:

Location loc = new Location(Extent, Vector3d);

Here is how I teleport the player:

theplayer.setLocationAndRotation(loc, Vector3dRotation);

This is what my configuration file content looks like:

warp=[
"(70.59157315636821, 80.61555682537467, -39.230587424297724)",
"(-278.83740234375, 24.300010681152344, 0.0)",
"net.minecraft.world.WorldServer@62c0418b",
"Owner: serubi",
"ID: (ID goes here)"
]

The first line is obviously the coordinates, the second line is the rotation, and the third line is the player’s extent (toString).

I’m not sure if I’m doing it wrong, but can I transform this extent string, to the extent needed for the location?


Please excuse me, if my code is bad.

Help is much appreciated :blush:

Instead of storing toString(), try storing player.getWorld().getName() or player.getWorld().getUniqueId(), and to access it later, use game.getServer(),getWorld(UUID or WorldName).get()

The .get() after getWorld() is because .getWorld() returns an Optional.

Edit: I’d just like to mention that in many cases, .toString() returns the object’s position in RAM, so it doesnt always mean what you might expect.

2 Likes

Perfect! It surely did the trick.

I’m just wondering why returning an ‘Optional’ works (since .get() returns World). If you don’t mind, please explain.

Optional would be absent if the world didn’t exist.

Here is an example

Optional<World> w = game.getServer().getWorld(/* world name */);
if (w.isPresent()) {
    // Then the world exists, so we can now do...
    World world = w.get();

    // I can now do some code here, with the specified world.
} 
1 Like

Also, Optional doesn’t throw a NPE when there’s nothing inside.

It should probably be added that using the UUID to identify (and store) a world is recommended over using the name, because administrators can change a world’s name by renaming the world’s folder or using a rename command provided by a world management plugin.

1 Like

Good point. I’ll keep this in mind for future plugins :slight_smile:

just a protip
Java 8 has its own version of the Optional class, but we use Google’s optional. if something’s weird with you using optionals, check if you have the right one imported.

(plus, you shouldn’t be building in j8 anyway, use j7 at most for now)

Unless it’s being released to the public, why we he use an EOL version?