-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix powermockito test dependencies. * Add Material gson type adapter to handle old Material enums In this case GRASS -> SHORT_GRASS
- Loading branch information
1 parent
0352cfd
commit e8e1d61
Showing
3 changed files
with
85 additions
and
19 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
56 changes: 56 additions & 0 deletions
56
src/main/java/world/bentobox/bentobox/database/json/adapters/MaterialTypeAdapter.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,56 @@ | ||
package world.bentobox.bentobox.database.json.adapters; | ||
|
||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.bukkit.Material; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
|
||
/** | ||
* Minecraft 1.20 changed GRASS to SHORT_GRASS. This class provides and backwards compatibility when loading | ||
* databased files stored with previous versions. It can be extended in the future if further enum changes are made. | ||
* @author tastybento | ||
* @since 2.0.0 | ||
*/ | ||
public final class MaterialTypeAdapter extends TypeAdapter<Material> | ||
{ | ||
/** | ||
* Map that contains string value to the actual Material enum object. | ||
*/ | ||
final Map<String, Material> materialMap; | ||
|
||
public MaterialTypeAdapter() { | ||
this.materialMap = new HashMap<>(); | ||
|
||
// Put in current values. | ||
Arrays.stream(Material.values()).forEach(mat -> this.materialMap.put(mat.name(), mat)); | ||
|
||
// Put in renamed material values. | ||
this.materialMap.put("GRASS", Material.SHORT_GRASS); | ||
} | ||
|
||
@Override | ||
public Material read(JsonReader input) throws IOException | ||
{ | ||
if (JsonToken.NULL.equals(input.peek())) { | ||
input.nextNull(); | ||
return null; | ||
} | ||
|
||
return this.materialMap.get(input.nextString().toUpperCase()); | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter output, Material enumValue) throws IOException { | ||
output.value(enumValue != null ? enumValue.name() : null); | ||
} | ||
} | ||
|