Skip to content

Commit

Permalink
Material Type adapter (#2253)
Browse files Browse the repository at this point in the history
* Fix powermockito test dependencies.

* Add Material gson type adapter to handle old Material enums

In this case GRASS -> SHORT_GRASS
  • Loading branch information
tastybento authored Jan 2, 2024
1 parent 0352cfd commit e8e1d61
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 19 deletions.
44 changes: 25 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,31 @@
</repositories>

<dependencies>
<!-- Mockito (Unit testing) This goes at the top to ensure the dependencies are accurate. -->
<!-- This is required for PowerMockito to work and must be placed before it -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.30.2-GA</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
<!-- Spigot API -->
<dependency>
<groupId>org.spigotmc</groupId>
Expand All @@ -212,25 +237,6 @@
<artifactId>bstats-bukkit</artifactId>
<version>${bstats.version}</version>
</dependency>
<!-- Mockito (Unit testing) -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<!-- Database -->
<dependency>
<groupId>org.mongodb</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
Expand All @@ -23,6 +24,7 @@
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.MaterialTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.PotionEffectTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.VectorTypeAdapter;
import world.bentobox.bentobox.database.json.adapters.WorldTypeAdapter;
Expand Down Expand Up @@ -55,6 +57,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (Location.class.isAssignableFrom(rawType)) {
// Use our current location adapter for backward compatibility
return (TypeAdapter<T>) new LocationTypeAdapter();
} else if (Material.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new MaterialTypeAdapter();
} else if (Biome.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new BiomeTypeAdapter();
} else if (Enum.class.isAssignableFrom(rawType)) {
Expand Down
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);
}
}

0 comments on commit e8e1d61

Please sign in to comment.