-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2614 from BentoBoxWorld/tag_serialization
Added GSON serialization for Tags
- Loading branch information
Showing
5 changed files
with
130 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/world/bentobox/bentobox/database/json/adapters/TagTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package world.bentobox.bentobox.database.json.adapters; | ||
|
||
import java.io.IOException; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Keyed; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.Tag; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
|
||
/** | ||
* @author tastybento | ||
* | ||
* @param <T> Tag class to be serialized | ||
*/ | ||
public final class TagTypeAdapter<E extends Keyed> extends TypeAdapter<Tag<E>> { | ||
private final TypeAdapter<String> stringAdapter; | ||
private final String registry; | ||
private final Class<E> elementType; | ||
|
||
public TagTypeAdapter(Gson gson, String registry, Class<E> elementType) { | ||
this.stringAdapter = gson.getAdapter(String.class); | ||
this.registry = registry; | ||
this.elementType = elementType; | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, Tag<E> value) throws IOException { | ||
if (value == null) { | ||
out.nullValue(); | ||
return; | ||
} | ||
stringAdapter.write(out, value.getKey().toString()); | ||
} | ||
|
||
@Override | ||
public Tag<E> read(JsonReader in) throws IOException { | ||
if (in.peek() == JsonToken.NULL) { | ||
in.nextNull(); | ||
return null; | ||
} | ||
|
||
String key = stringAdapter.read(in); | ||
NamespacedKey namespacedKey = NamespacedKey.fromString(key); | ||
if (namespacedKey == null) { | ||
throw new JsonParseException("Invalid tag key format: " + key); | ||
} | ||
|
||
Tag<E> tag = Bukkit.getTag(registry, namespacedKey, elementType); | ||
if (tag == null) { | ||
throw new JsonParseException("Unknown tag: " + key); | ||
} | ||
|
||
return tag; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/world/bentobox/bentobox/database/json/adapters/TagTypeAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package world.bentobox.bentobox.database.json.adapters; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
import org.bukkit.Fluid; | ||
import org.bukkit.Keyed; | ||
import org.bukkit.Material; | ||
import org.bukkit.Tag; | ||
import org.bukkit.entity.EntityType; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
public class TagTypeAdapterFactory implements TypeAdapterFactory { | ||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
@Override | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
Class<?> rawType = type.getRawType(); | ||
if (Tag.class.isAssignableFrom(rawType)) { | ||
// Get the generic type parameter of Tag<T> | ||
Type[] typeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments(); | ||
if (typeArguments.length > 0) { | ||
Class<? extends Keyed> tagType = (Class<? extends Keyed>) typeArguments[0]; | ||
|
||
// Determine the correct registry based on the tag type | ||
String registry; | ||
if (Material.class.equals(tagType)) { | ||
registry = Tag.REGISTRY_BLOCKS; | ||
} else if (EntityType.class.equals(tagType)) { | ||
registry = Tag.REGISTRY_ENTITY_TYPES; | ||
} else if (Fluid.class.equals(tagType)) { | ||
registry = Tag.REGISTRY_FLUIDS; | ||
} else { | ||
throw new IllegalArgumentException("Unsupported Tag type: " + tagType); | ||
} | ||
|
||
return (TypeAdapter<T>) new TagTypeAdapter(gson, registry, tagType); | ||
} | ||
} | ||
return null; | ||
} | ||
} |