mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-25 02:27:44 +01:00
Added BoundingBoxTypeAdapter for database storage
Renamed other adpaters to be TypeAdapters for consistency.
This commit is contained in:
parent
4e8a1071f2
commit
91b650bdee
@ -4,6 +4,7 @@ import org.bukkit.Location;
|
|||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.potion.PotionEffectType;
|
import org.bukkit.potion.PotionEffectType;
|
||||||
|
import org.bukkit.util.BoundingBox;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
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.api.flags.Flag;
|
||||||
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
|
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
|
||||||
import world.bentobox.bentobox.database.DatabaseConnector;
|
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.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.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.
|
* 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
|
// enableComplexMapKeySerialization - forces GSON to use TypeAdapters even for Map keys
|
||||||
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization().setPrettyPrinting();
|
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization().setPrettyPrinting();
|
||||||
// Register adapters
|
// Register adapters
|
||||||
builder.registerTypeAdapter(Location.class, new LocationAdapter()) ;
|
builder.registerTypeAdapter(Location.class, new LocationTypeAdapter()) ;
|
||||||
builder.registerTypeAdapter(World.class, new WorldAdapter());
|
builder.registerTypeAdapter(World.class, new WorldTypeAdapter());
|
||||||
builder.registerTypeAdapter(Flag.class, new FlagAdapter(plugin));
|
builder.registerTypeAdapter(Flag.class, new FlagTypeAdapter(plugin));
|
||||||
builder.registerTypeAdapter(PotionEffectType.class, new PotionEffectTypeAdapter());
|
builder.registerTypeAdapter(PotionEffectType.class, new PotionEffectTypeAdapter());
|
||||||
builder.registerTypeAdapter(ItemStack.class, new ItemStackTypeAdapter());
|
builder.registerTypeAdapter(ItemStack.class, new ItemStackTypeAdapter());
|
||||||
|
builder.registerTypeAdapter(BoundingBox.class, new BoundingBoxTypeAdapter());
|
||||||
// Keep null in the database
|
// Keep null in the database
|
||||||
builder.serializeNulls();
|
builder.serializeNulls();
|
||||||
// Allow characters like < or > without escaping them
|
// Allow characters like < or > without escaping them
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -10,11 +10,11 @@ import com.google.gson.stream.JsonWriter;
|
|||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
import world.bentobox.bentobox.api.flags.Flag;
|
import world.bentobox.bentobox.api.flags.Flag;
|
||||||
|
|
||||||
public class FlagAdapter extends TypeAdapter<Flag> {
|
public class FlagTypeAdapter extends TypeAdapter<Flag> {
|
||||||
|
|
||||||
private BentoBox plugin;
|
private BentoBox plugin;
|
||||||
|
|
||||||
public FlagAdapter(BentoBox plugin) {
|
public FlagTypeAdapter(BentoBox plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
|
|
@ -11,7 +11,7 @@ import com.google.gson.stream.JsonReader;
|
|||||||
import com.google.gson.stream.JsonToken;
|
import com.google.gson.stream.JsonToken;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
public class LocationAdapter extends TypeAdapter<Location> {
|
public class LocationTypeAdapter extends TypeAdapter<Location> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, Location location) throws IOException {
|
public void write(JsonWriter out, Location location) throws IOException {
|
@ -10,7 +10,7 @@ import com.google.gson.stream.JsonReader;
|
|||||||
import com.google.gson.stream.JsonToken;
|
import com.google.gson.stream.JsonToken;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
public class WorldAdapter extends TypeAdapter<World> {
|
public class WorldTypeAdapter extends TypeAdapter<World> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, World value) throws IOException {
|
public void write(JsonWriter out, World value) throws IOException {
|
@ -65,12 +65,12 @@ public class FlagAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFlagAdapter() {
|
public void testFlagAdapter() {
|
||||||
new FlagAdapter(plugin);
|
new FlagTypeAdapter(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWriteJsonWriterFlag() throws IOException {
|
public void testWriteJsonWriterFlag() throws IOException {
|
||||||
FlagAdapter fa = new FlagAdapter(plugin);
|
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
|
||||||
JsonWriter out = mock(JsonWriter.class);
|
JsonWriter out = mock(JsonWriter.class);
|
||||||
Flag value = Flags.ANIMAL_SPAWN;
|
Flag value = Flags.ANIMAL_SPAWN;
|
||||||
fa.write(out, value);
|
fa.write(out, value);
|
||||||
@ -79,7 +79,7 @@ public class FlagAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWriteJsonWriterFlagNull() throws IOException {
|
public void testWriteJsonWriterFlagNull() throws IOException {
|
||||||
FlagAdapter fa = new FlagAdapter(plugin);
|
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
|
||||||
JsonWriter out = mock(JsonWriter.class);
|
JsonWriter out = mock(JsonWriter.class);
|
||||||
Flag value = null;
|
Flag value = null;
|
||||||
fa.write(out, value);
|
fa.write(out, value);
|
||||||
@ -88,7 +88,7 @@ public class FlagAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReadJsonReaderNull() throws IOException {
|
public void testReadJsonReaderNull() throws IOException {
|
||||||
FlagAdapter fa = new FlagAdapter(plugin);
|
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
|
||||||
JsonReader reader = mock(JsonReader.class);
|
JsonReader reader = mock(JsonReader.class);
|
||||||
Mockito.when(reader.peek()).thenReturn(JsonToken.NULL);
|
Mockito.when(reader.peek()).thenReturn(JsonToken.NULL);
|
||||||
Flag flag = fa.read(reader);
|
Flag flag = fa.read(reader);
|
||||||
@ -98,7 +98,7 @@ public class FlagAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReadJsonReader() throws IOException {
|
public void testReadJsonReader() throws IOException {
|
||||||
FlagAdapter fa = new FlagAdapter(plugin);
|
FlagTypeAdapter fa = new FlagTypeAdapter(plugin);
|
||||||
JsonReader reader = mock(JsonReader.class);
|
JsonReader reader = mock(JsonReader.class);
|
||||||
Mockito.when(reader.peek()).thenReturn(JsonToken.STRING);
|
Mockito.when(reader.peek()).thenReturn(JsonToken.STRING);
|
||||||
Mockito.when(reader.nextString()).thenReturn("ANIMAL_SPAWN");
|
Mockito.when(reader.nextString()).thenReturn("ANIMAL_SPAWN");
|
||||||
|
Loading…
Reference in New Issue
Block a user