Added BoundingBoxTypeAdapter for database storage

Renamed other adpaters to be TypeAdapters for consistency.
This commit is contained in:
tastybento 2019-05-08 15:33:40 -07:00
parent 4e8a1071f2
commit 91b650bdee
6 changed files with 64 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.BoundingBox;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@ -12,11 +13,12 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseConnector;
import world.bentobox.bentobox.database.json.adapters.FlagAdapter;
import world.bentobox.bentobox.database.json.adapters.BoundingBoxTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.FlagTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.ItemStackTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.LocationAdapter;
import world.bentobox.bentobox.database.json.adapters.LocationTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.PotionEffectTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.WorldAdapter;
import world.bentobox.bentobox.database.json.adapters.WorldTypeAdapter;
/**
* Abstract class that handles insert/select-operations into/from a database.
@ -47,11 +49,12 @@ public abstract class AbstractJSONDatabaseHandler<T> extends AbstractDatabaseHan
// enableComplexMapKeySerialization - forces GSON to use TypeAdapters even for Map keys
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization().setPrettyPrinting();
// Register adapters
builder.registerTypeAdapter(Location.class, new LocationAdapter()) ;
builder.registerTypeAdapter(World.class, new WorldAdapter());
builder.registerTypeAdapter(Flag.class, new FlagAdapter(plugin));
builder.registerTypeAdapter(Location.class, new LocationTypeAdapter()) ;
builder.registerTypeAdapter(World.class, new WorldTypeAdapter());
builder.registerTypeAdapter(Flag.class, new FlagTypeAdapter(plugin));
builder.registerTypeAdapter(PotionEffectType.class, new PotionEffectTypeAdapter());
builder.registerTypeAdapter(ItemStack.class, new ItemStackTypeAdapter());
builder.registerTypeAdapter(BoundingBox.class, new BoundingBoxTypeAdapter());
// Keep null in the database
builder.serializeNulls();
// Allow characters like < or > without escaping them

View File

@ -0,0 +1,46 @@
package world.bentobox.bentobox.database.json.adapters;
import java.io.IOException;
import org.bukkit.util.BoundingBox;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
public class BoundingBoxTypeAdapter extends TypeAdapter<BoundingBox> {
@Override
public void write(JsonWriter out, BoundingBox box) throws IOException {
if (box == null) {
out.nullValue();
return;
}
out.beginArray();
out.value(box.getMinX());
out.value(box.getMinY());
out.value(box.getMinZ());
out.value(box.getMaxX());
out.value(box.getMaxY());
out.value(box.getMaxZ());
out.endArray();
}
@Override
public BoundingBox read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
in.beginArray();
double minX = in.nextDouble();
double minY = in.nextDouble();
double minZ = in.nextDouble();
double maxX = in.nextDouble();
double maxY = in.nextDouble();
double maxZ = in.nextDouble();
in.endArray();
return new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
}
}

View File

@ -10,11 +10,11 @@ import com.google.gson.stream.JsonWriter;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
public class FlagAdapter extends TypeAdapter<Flag> {
public class FlagTypeAdapter extends TypeAdapter<Flag> {
private BentoBox plugin;
public FlagAdapter(BentoBox plugin) {
public FlagTypeAdapter(BentoBox plugin) {
this.plugin = plugin;
}

View File

@ -11,7 +11,7 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
public class LocationAdapter extends TypeAdapter<Location> {
public class LocationTypeAdapter extends TypeAdapter<Location> {
@Override
public void write(JsonWriter out, Location location) throws IOException {

View File

@ -10,7 +10,7 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
public class WorldAdapter extends TypeAdapter<World> {
public class WorldTypeAdapter extends TypeAdapter<World> {
@Override
public void write(JsonWriter out, World value) throws IOException {

View File

@ -65,12 +65,12 @@ public class FlagAdapterTest {
@Test
public void testFlagAdapter() {
new FlagAdapter(plugin);
new FlagTypeAdapter(plugin);
}
@Test
public void testWriteJsonWriterFlag() throws IOException {
FlagAdapter fa = new FlagAdapter(plugin);
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
JsonWriter out = mock(JsonWriter.class);
Flag value = Flags.ANIMAL_SPAWN;
fa.write(out, value);
@ -79,7 +79,7 @@ public class FlagAdapterTest {
@Test
public void testWriteJsonWriterFlagNull() throws IOException {
FlagAdapter fa = new FlagAdapter(plugin);
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
JsonWriter out = mock(JsonWriter.class);
Flag value = null;
fa.write(out, value);
@ -88,7 +88,7 @@ public class FlagAdapterTest {
@Test
public void testReadJsonReaderNull() throws IOException {
FlagAdapter fa = new FlagAdapter(plugin);
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
JsonReader reader = mock(JsonReader.class);
Mockito.when(reader.peek()).thenReturn(JsonToken.NULL);
Flag flag = fa.read(reader);
@ -98,7 +98,7 @@ public class FlagAdapterTest {
@Test
public void testReadJsonReader() throws IOException {
FlagAdapter fa = new FlagAdapter(plugin);
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
JsonReader reader = mock(JsonReader.class);
Mockito.when(reader.peek()).thenReturn(JsonToken.STRING);
Mockito.when(reader.nextString()).thenReturn("ANIMAL_SPAWN");