Advertisement
hassansyyid

Untitled

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