package me.galaran.bukkitutils.__utils_project_name__;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

public class LocUtils {

    public static String toStringFull(Location loc) {
        return String.format(Locale.US, "%s, pitch: %.2f, yaw: %.2f", toStringWorldXYZ(loc), loc.getPitch(), loc.getYaw());
    }

    public static String toStringWorldXYZ(Location loc) {
        return loc.getWorld().getName() + ": " + toStringXYZ(loc);
    }

    public static String toStringXYZ(Location loc) {
        return String.format(Locale.US, "[%.2f %.2f %.2f]", loc.getX(), loc.getY(), loc.getZ());
    }

    public static boolean closerThan(Location loc1, Location loc2, double distance) {
        if (!loc1.getWorld().equals(loc2.getWorld())) {
            return false;
        } else {
            return loc1.distance(loc2) <= distance;
        }
    }

    public static Map<String, Object> serialize(Location loc) {
        if (loc == null) return null;

        Map<String, Object> locData = new LinkedHashMap<String, Object>();
        locData.put("x", loc.getX());
        locData.put("y", loc.getY());
        locData.put("z", loc.getZ());
        locData.put("world", loc.getWorld().getName());
        locData.put("pitch", loc.getPitch());
        locData.put("yaw", loc.getYaw());
        return locData;
    }

    public static Location deserialize(ConfigurationSection section) {
        World world = Bukkit.getServer().getWorld(section.getString("world"));
        if (world == null) {
            throw new IllegalArgumentException("Non-existent world: " + section.getString("world"));
        }

        Location result = new Location(world, section.getDouble("x"), section.getDouble("y"), section.getDouble("z"));
        result.setPitch((float) section.getDouble("pitch", 0));
        result.setYaw((float) section.getDouble("yaw", 0));
        return result;
    }
}

@@ -1474,32 +1474,36 @@
  locData;%0A    %7D%0A
+    
 %0A    public stat
@@ -1530,39 +1530,88 @@
 ize(
-ConfigurationSection section) %7B
+Map%3CString, Object%3E map) %7B%0A        String worldName = (String) map.get(%22world%22);
 %0A   
@@ -1661,34 +1661,17 @@
 rld(
-section.getString(%22
 world
-%22)
+Name
 );%0A 
@@ -1774,34 +1774,17 @@
 %22 + 
-section.getString(%22
 world
-%22)
+Name
 );%0A 
@@ -1842,227 +1842,582 @@
 rld,
- section.getDouble(%22x%22), section.getDouble(%22y%22), section.getDouble(%22z%22));%0A        result.setPitch((float) section.getDouble(%22pitch%22, 0));%0A        result.setYaw((float) section.getDouble(%22yaw%22, 0));%0A        return result
+%0A                ((Number) map.get(%22x%22)).doubleValue(),%0A                ((Number) map.get(%22y%22)).doubleValue(),%0A                ((Number) map.get(%22z%22)).doubleValue());%0A        %0A        if (map.containsKey(%22pitch%22)) %7B%0A            result.setPitch(((Number) map.get(%22pitch%22)).floatValue());%0A        %7D%0A        if (map.containsKey(%22yaw%22)) %7B%0A            result.setYaw(((Number) map.get(%22yaw%22)).floatValue());%0A        %7D%0A        %0A        return result;%0A    %7D%0A%0A    public static Location deserialize(ConfigurationSection section) %7B%0A        return deserialize(section.getValues(false))
 ;%0A  

Deserialize location: map