Advertisement
broken-arrow

Untitled

Nov 6th, 2021
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import com.google.gson.Gson;
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.Location;
  4. import org.bukkit.World;
  5. import org.mineacademy.fo.SerializeUtil;
  6.  
  7. import java.lang.reflect.Type;
  8. import java.util.*;
  9.  
  10. public class ConvertJson {
  11.  
  12.  
  13.     public static <T> String convertToJsonList(String key, List<T> arrayList) {
  14.         Map<String, List<String>> maps = new HashMap<>();
  15.         Gson gson = new Gson();
  16.         List<String> serilizedList = new ArrayList<>();
  17.         if (arrayList != null)
  18.             for (T list : arrayList) {
  19.                 if (list instanceof Location)
  20.                     serilizedList.add(SerializeUtil.serializeLoc((Location) list));
  21.                 if (list instanceof UUID)
  22.                     serilizedList.add(String.valueOf(list));
  23.  
  24.             }
  25.         maps.put(key, serilizedList);
  26.  
  27.         return gson.toJson(maps);
  28.     }
  29.  
  30.     public static <T> ArrayList<T> convertFromJsonList(Class<T> classof, String Inputmap) {
  31.         ArrayList<T> arrayList = new ArrayList<>();
  32.         Gson gson = new Gson();
  33.         final Map<String, List<Object>> map = gson.fromJson(Inputmap, (Type) Map.class);
  34.         if (map != null) {
  35.             Map<String, List<Object>> mapList = new HashMap<>(map);
  36.             for (Map.Entry<String, List<Object>> entry : mapList.entrySet())
  37.                 for (Object deserilizedList : entry.getValue()) {
  38.  
  39.                     if (classof == Location.class && isLocation(deserilizedList) != null)
  40.                         arrayList.add((T) isLocation(deserilizedList));
  41.                     if (classof == UUID.class)
  42.                         arrayList.add((T) UUID.fromString(deserilizedList.toString()));
  43.                 }
  44.         }
  45.         return arrayList;
  46.     }
  47.  
  48.     public static Location isLocation(Object raw) {
  49.         String[] parts;
  50.         if (!raw.toString().contains(" "))
  51.             return null;
  52.         else {
  53.             int length = (parts = raw.toString().split(" ")).length;
  54.             if (length == 4) {
  55.                 final String world = parts[0];
  56.                 final World bukkitWorld = Bukkit.getWorld(world);
  57.                 if (bukkitWorld == null)
  58.                     return null;
  59.                 if (!parts[1].matches("[-+]?\\d+") && !parts[2].matches("[-+]?\\d+") && !parts[3].matches("[-+]?\\d+"))
  60.                     return null;
  61.                 else {
  62.                     int x = Integer.parseInt(parts[1]), y = Integer.parseInt(parts[2]), z = Integer.parseInt(parts[3]);
  63.                     return new Location(bukkitWorld, x, y, z);
  64.                 }
  65.             }
  66.         }
  67.         return null;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement