Skip to content

Commit 387843b

Browse files
Added LocationSerializer
1 parent 54d42e9 commit 387843b

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

plugin-test/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<dependency>
7272
<groupId>com.zetaplugins</groupId>
7373
<artifactId>zetacore</artifactId>
74-
<version>1.11.2</version>
74+
<version>1.11.3</version>
7575
</dependency>
7676
</dependencies>
7777
</project>

zetacore/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.zetaplugins</groupId>
88
<artifactId>zetacore</artifactId>
9-
<version>1.11.2</version>
9+
<version>1.11.3</version>
1010
<packaging>jar</packaging>
1111

1212
<name>ZetaCore</name>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.zetaplugins.zetacore.util;
2+
3+
public class LocationDeserializationException extends Exception {
4+
public LocationDeserializationException(String message) {
5+
super(message);
6+
}
7+
8+
public LocationDeserializationException(String message, Throwable cause) {
9+
super(message, cause);
10+
}
11+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.zetaplugins.zetacore.util;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.Location;
5+
import org.bukkit.World;
6+
7+
/**
8+
* Utility class for serializing and deserializing Location objects to and from string format.
9+
*/
10+
public final class LocationSerializer {
11+
private LocationSerializer() {}
12+
13+
public interface WorldProvider {
14+
World getWorld(String name);
15+
}
16+
17+
public static final WorldProvider DEFAULT_PROVIDER = Bukkit::getWorld;
18+
19+
/**
20+
* Serializes a location into a string format: "worldName,x,y,z,yaw,pitch"
21+
* @param worldName The name of the world
22+
* @param x The x coordinate
23+
* @param y The y coordinate
24+
* @param z The z coordinate
25+
* @param yaw The yaw rotation
26+
* @param pitch The pitch rotation
27+
* @return The serialized location string
28+
*/
29+
public static String serializeLocation(String worldName, double x, double y, double z, float yaw, float pitch) {
30+
return worldName + "," + x + "," + y + "," + z + "," + yaw + "," + pitch;
31+
}
32+
33+
/**
34+
* Serializes a Location object into a string format: "worldName,x,y,z,yaw,pitch"
35+
* @param location The Location object to serialize
36+
* @return The serialized location string
37+
*/
38+
public static String serializeLocation(Location location) {
39+
return serializeLocation(location.getWorld().getName(), location.getX(), location.getY(), location.getZ(),
40+
location.getYaw(), location.getPitch());
41+
}
42+
43+
/**
44+
* Deserializes a location string back into a Location object. Expects format: "worldName,x,y,z,yaw,pitch"
45+
* @param serializedLocation The serialized location string
46+
* @return The deserialized Location object
47+
* @throws LocationDeserializationException if the string is malformed or the world does not exist
48+
*/
49+
public static Location deserializeLocation(String serializedLocation) throws LocationDeserializationException {
50+
return deserializeLocation(serializedLocation, DEFAULT_PROVIDER);
51+
}
52+
53+
/**
54+
* Deserializes a location string back into a Location object. Expects format: "worldName,x,y,z,yaw,pitch"
55+
* @param serializedLocation The serialized location string
56+
* @param provider The WorldProvider to use for fetching worlds
57+
* @return The deserialized Location object
58+
* @throws LocationDeserializationException if the string is malformed or the world does not exist
59+
*/
60+
public static Location deserializeLocation(String serializedLocation, WorldProvider provider) throws LocationDeserializationException {
61+
if (serializedLocation.isEmpty()) throw new LocationDeserializationException("Serialized location string is empty.");
62+
63+
String[] parts = serializedLocation.split(",");
64+
if (parts.length != 6) {
65+
throw new LocationDeserializationException("Invalid serialized location format. Expected 6 parts but got " + parts.length);
66+
}
67+
68+
String worldName = parts[0];
69+
World world = provider.getWorld(worldName);
70+
if (world == null) throw new LocationDeserializationException("World not found: " + worldName);
71+
72+
double x = Double.parseDouble(parts[1]);
73+
double y = Double.parseDouble(parts[2]);
74+
double z = Double.parseDouble(parts[3]);
75+
float yaw = Float.parseFloat(parts[4]);
76+
float pitch = Float.parseFloat(parts[5]);
77+
78+
return new Location(world, x, y, z, yaw, pitch);
79+
}
80+
}

0 commit comments

Comments
 (0)