|
| 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