2019-05-09 03:21:40 +02:00
package world.bentobox.bentobox.database.json ;
import java.util.Map ;
import org.bukkit.Location ;
import org.bukkit.World ;
2021-12-19 17:49:29 +01:00
import org.bukkit.block.Biome ;
2019-05-09 03:21:40 +02:00
import org.bukkit.configuration.serialization.ConfigurationSerializable ;
import org.bukkit.inventory.ItemStack ;
import org.bukkit.potion.PotionEffectType ;
2019-05-15 20:16:41 +02:00
import org.bukkit.util.Vector ;
2019-05-09 03:21:40 +02:00
import com.google.gson.Gson ;
import com.google.gson.TypeAdapter ;
import com.google.gson.TypeAdapterFactory ;
import com.google.gson.reflect.TypeToken ;
import world.bentobox.bentobox.BentoBox ;
import world.bentobox.bentobox.api.flags.Flag ;
2022-01-22 22:14:57 +01:00
import world.bentobox.bentobox.database.json.adapters.BiomeTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.BukkitObjectTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.EnumTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.FlagTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.ItemStackTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.LocationTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.PotionEffectTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.VectorTypeAdapter ;
import world.bentobox.bentobox.database.json.adapters.WorldTypeAdapter ;
2022-01-02 02:34:51 +01:00
import world.bentobox.bentobox.versions.ServerCompatibility ;
2021-12-19 17:49:29 +01:00
2019-05-09 03:21:40 +02:00
/ * *
* Allocates type adapters based on class type .
*
* @author tastybento
*
* /
public class BentoboxTypeAdapterFactory implements TypeAdapterFactory {
2021-09-18 17:15:15 +02:00
final BentoBox plugin ;
2019-05-09 03:21:40 +02:00
/ * *
2019-07-11 22:55:17 +02:00
* @param plugin plugin
2019-05-09 03:21:40 +02:00
* /
public BentoboxTypeAdapterFactory ( BentoBox plugin ) {
this . plugin = plugin ;
}
/ * ( non - Javadoc )
* @see com . google . gson . TypeAdapterFactory # create ( com . google . gson . Gson , com . google . gson . reflect . TypeToken )
* /
2021-05-29 02:24:51 +02:00
@SuppressWarnings ( { " unchecked " , " rawtypes " } )
2019-05-09 03:21:40 +02:00
@Override
public < T > TypeAdapter < T > create ( Gson gson , TypeToken < T > type ) {
Class < ? > rawType = type . getRawType ( ) ;
if ( Location . class . isAssignableFrom ( rawType ) ) {
// Use our current location adapter for backward compatibility
return ( TypeAdapter < T > ) new LocationTypeAdapter ( ) ;
2022-01-02 02:34:51 +01:00
} else if ( Biome . class . isAssignableFrom ( rawType ) & & ! ServerCompatibility . getInstance ( ) . isVersion ( ServerCompatibility . ServerVersion . V1_17_1 ) ) { // TODO: Any better way ?
2021-12-19 17:49:29 +01:00
return ( TypeAdapter < T > ) new BiomeTypeAdapter ( ) ;
2021-05-29 02:24:51 +02:00
} else if ( Enum . class . isAssignableFrom ( rawType ) ) {
return new EnumTypeAdapter ( rawType ) ;
2019-05-09 03:21:40 +02:00
} else if ( ItemStack . class . isAssignableFrom ( rawType ) ) {
// Use our current location adapter for backward compatibility
return ( TypeAdapter < T > ) new ItemStackTypeAdapter ( ) ;
} else if ( Flag . class . isAssignableFrom ( rawType ) ) {
return ( TypeAdapter < T > ) new FlagTypeAdapter ( plugin ) ;
} else if ( PotionEffectType . class . isAssignableFrom ( rawType ) ) {
return ( TypeAdapter < T > ) new PotionEffectTypeAdapter ( ) ;
} else if ( World . class . isAssignableFrom ( rawType ) ) {
return ( TypeAdapter < T > ) new WorldTypeAdapter ( ) ;
2019-05-15 20:16:41 +02:00
} else if ( Vector . class . isAssignableFrom ( rawType ) ) {
return ( TypeAdapter < T > ) new VectorTypeAdapter ( ) ;
2019-05-09 03:21:40 +02:00
} else if ( ConfigurationSerializable . class . isAssignableFrom ( rawType ) ) {
// This covers a lot of Bukkit objects
return ( TypeAdapter < T > ) new BukkitObjectTypeAdapter ( gson . getAdapter ( Map . class ) ) ;
}
return null ;
}
}