hassansyyid

Untitled

Aug 4th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package io.github.hsyyid.adminshop.utils;
  2.  
  3. import io.github.hsyyid.adminshop.Main;
  4.  
  5. import java.io.IOException;
  6.  
  7. import org.spongepowered.api.world.Location;
  8. import org.spongepowered.api.world.World;
  9.  
  10. import com.google.gson.TypeAdapter;
  11. import com.google.gson.stream.JsonReader;
  12. import com.google.gson.stream.JsonToken;
  13. import com.google.gson.stream.JsonWriter;
  14.  
  15. public class LocationAdapter extends TypeAdapter<Location>
  16. {
  17.  
  18.     @Override
  19.     public void write(JsonWriter out, Location location) throws IOException
  20.     {
  21.         if (location == null)
  22.         {
  23.             out.nullValue();
  24.             return;
  25.         }
  26.  
  27.         out.beginObject();
  28.  
  29.         if (location.getExtent() instanceof World)
  30.         {
  31.             out.name("world");
  32.             out.value(((World) location.getExtent()).getUniqueId().toString());
  33.         }
  34.         out.name("x");
  35.         out.value(location.getX());
  36.  
  37.         out.name("y");
  38.         out.value(location.getY());
  39.  
  40.         out.name("z");
  41.         out.value(location.getZ());
  42.  
  43.         out.endObject();
  44.  
  45.     }
  46.  
  47.     @Override
  48.     public Location read(JsonReader in) throws IOException
  49.     {
  50.         if (in.peek() == JsonToken.NULL)
  51.         {
  52.             return null;
  53.         }
  54.  
  55.         in.beginObject();
  56.  
  57.         in.nextName();
  58.         String worldID = in.nextString();
  59.  
  60.         in.nextName();
  61.         double x = in.nextDouble();
  62.  
  63.         in.nextName();
  64.         double y = in.nextDouble();
  65.  
  66.         in.nextName();
  67.         double z = in.nextDouble();
  68.  
  69.         in.endObject();
  70.  
  71.         if (Main.game.getServer().getWorld(worldID).isPresent())
  72.         {
  73.             Location location = new Location(Main.game.getServer().getWorld(worldID).get(), x, y, z);
  74.             return location;
  75.         }
  76.         else
  77.         {
  78.             return null;
  79.         }
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment