bentobox/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory....

82 lines
3.4 KiB
Java
Raw Normal View History

package world.bentobox.bentobox.database.json;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
Implemeted Blueprint bundles and blueprints (#672) * A prototype for Blueprint bundles and blueprints This stores blueprints inside bundles. Each bundle can have up to 3 blueprints defines by the World.Environment. This is not a finished manager. It just handles all the saving and loading side of things. I thought this would help you so you can then concentrate on the UI. * WIP: Copy blocks to Blueprint done. * WIP Pasting done. * WIP: Added BlueprintsManager to ultimately replace SchemsManager. * Moved blueprint suffix and changed to .blu * Fixed unit test. * Now tested and working. Integrated with new island and resetting island. If there are no blueprint bundles or blueprints then a default bedrock set will be made and put in the game mode addon's blueprints folder. Still to do: enable schems to be loaded and pasted for legacy support. Add blueprints and a bundle to GameModeAddons like BSkyBlock. * Renamed most of the classes * Cleaned up clipboard and paster. * Further cleanup on blueprint clipboard and paster. * Merged blueprint classes into one package. * Put Blueprint data objects in their own package. Isolated schems classes for later removal. * Renamed admin command classes and changed locale files. * More clean up to remove schems * Schem to blueprints converter done. Converts schems to blueprint bundles and sets up a default set. Tested the happy-path. Need to do more testing on edge cases. * Added basic UI for development. Fixed bug with schem conversion. * Adds permissions into the blueprints. Fixes tests, cleans up some naming * Added IslandCreationPanel and created BlueprintManagementPanel * Fixed JSONDatabaseHandler's constructor being public * Made the Blueprints button in ManagementPanel open the Blueprint management panel * Fixed tests and ignored one (NPE)
2019-05-15 20:16:41 +02:00
import org.bukkit.util.Vector;
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;
import world.bentobox.bentobox.versions.ServerCompatibility;
/**
* 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-07-11 22:55:17 +02:00
* @param plugin plugin
*/
public BentoboxTypeAdapterFactory(BentoBox plugin) {
this.plugin = plugin;
}
/* (non-Javadoc)
* @see com.google.gson.TypeAdapterFactory#create(com.google.gson.Gson, com.google.gson.reflect.TypeToken)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@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();
} else if (Biome.class.isAssignableFrom(rawType) && !ServerCompatibility.getInstance().isVersion(ServerCompatibility.ServerVersion.V1_17_1)) { // TODO: Any better way ?
return (TypeAdapter<T>) new BiomeTypeAdapter();
} else if (Enum.class.isAssignableFrom(rawType)) {
return new EnumTypeAdapter(rawType);
} 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();
Implemeted Blueprint bundles and blueprints (#672) * A prototype for Blueprint bundles and blueprints This stores blueprints inside bundles. Each bundle can have up to 3 blueprints defines by the World.Environment. This is not a finished manager. It just handles all the saving and loading side of things. I thought this would help you so you can then concentrate on the UI. * WIP: Copy blocks to Blueprint done. * WIP Pasting done. * WIP: Added BlueprintsManager to ultimately replace SchemsManager. * Moved blueprint suffix and changed to .blu * Fixed unit test. * Now tested and working. Integrated with new island and resetting island. If there are no blueprint bundles or blueprints then a default bedrock set will be made and put in the game mode addon's blueprints folder. Still to do: enable schems to be loaded and pasted for legacy support. Add blueprints and a bundle to GameModeAddons like BSkyBlock. * Renamed most of the classes * Cleaned up clipboard and paster. * Further cleanup on blueprint clipboard and paster. * Merged blueprint classes into one package. * Put Blueprint data objects in their own package. Isolated schems classes for later removal. * Renamed admin command classes and changed locale files. * More clean up to remove schems * Schem to blueprints converter done. Converts schems to blueprint bundles and sets up a default set. Tested the happy-path. Need to do more testing on edge cases. * Added basic UI for development. Fixed bug with schem conversion. * Adds permissions into the blueprints. Fixes tests, cleans up some naming * Added IslandCreationPanel and created BlueprintManagementPanel * Fixed JSONDatabaseHandler's constructor being public * Made the Blueprints button in ManagementPanel open the Blueprint management panel * Fixed tests and ignored one (NPE)
2019-05-15 20:16:41 +02:00
} else if (Vector.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new VectorTypeAdapter();
} else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
// This covers a lot of Bukkit objects
return (TypeAdapter<T>) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class));
}
return null;
}
}