Advertisement
hassansyyid

Untitled

Aug 4th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 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.     @Override
  18.     public void write(JsonWriter out, Location location) throws IOException
  19.     {
  20.         if (location == null)
  21.         {
  22.             out.nullValue();
  23.             return;
  24.         }
  25.  
  26.         out.beginObject();
  27.  
  28.         if (location.getExtent() instanceof World)
  29.         {
  30.             out.name("world");
  31.             out.value(((World) location.getExtent()).getName());
  32.         }
  33.         out.name("x");
  34.         out.value(location.getX());
  35.  
  36.         out.name("y");
  37.         out.value(location.getY());
  38.  
  39.         out.name("z");
  40.         out.value(location.getZ());
  41.  
  42.         out.endObject();
  43.  
  44.     }
  45.  
  46.     @Override
  47.     public Location read(JsonReader in) throws IOException
  48.     {
  49.         if (in.peek() == JsonToken.NULL)
  50.         {
  51.             return null;
  52.         }
  53.  
  54.         in.beginObject();
  55.  
  56.         in.nextName();
  57.         String worldName = in.nextString();
  58.  
  59.         in.nextName();
  60.         double x = in.nextDouble();
  61.  
  62.         in.nextName();
  63.         double y = in.nextDouble();
  64.  
  65.         in.nextName();
  66.         double z = in.nextDouble();
  67.  
  68.         in.endObject();
  69.  
  70.         if (Main.game.getServer().getWorld(worldName).isPresent())
  71.         {
  72.             Location location = new Location(Main.game.getServer().getWorld(worldName).get(), x, y, z);
  73.             return location;
  74.         }
  75.         else
  76.         {
  77.             return null;
  78.         }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement