|
| 1 | +// |
| 2 | +// Created by BONNe |
| 3 | +// Copyright - 2021 |
| 4 | +// |
| 5 | + |
| 6 | + |
| 7 | +package world.bentobox.bentobox.database.json.adapters; |
| 8 | + |
| 9 | + |
| 10 | +import com.google.gson.TypeAdapter; |
| 11 | +import com.google.gson.stream.JsonReader; |
| 12 | +import com.google.gson.stream.JsonToken; |
| 13 | +import com.google.gson.stream.JsonWriter; |
| 14 | +import org.bukkit.block.Biome; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * Minecraft 1.18 reworked their biomes, and a lot of things were renamed or removed. |
| 23 | + * This adapter will address these changes in each database object, instead of manually fining it |
| 24 | + * per implementation. |
| 25 | + */ |
| 26 | +public final class BiomeTypeAdapter extends TypeAdapter<Biome> |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Map that contains string value to the actual Biome enum object. |
| 30 | + */ |
| 31 | + final Map<String, Biome> biomeMap; |
| 32 | + |
| 33 | + public BiomeTypeAdapter() { |
| 34 | + this.biomeMap = new HashMap<>(); |
| 35 | + |
| 36 | + // Put in current values. |
| 37 | + Arrays.stream(Biome.values()).forEach(biome -> this.biomeMap.put(biome.name(), biome)); |
| 38 | + |
| 39 | + // Put in renamed biomes values. |
| 40 | + this.biomeMap.put("TALL_BIRCH_FOREST", Biome.OLD_GROWTH_BIRCH_FOREST); |
| 41 | + this.biomeMap.put("GIANT_TREE_TAIGA", Biome.OLD_GROWTH_PINE_TAIGA); |
| 42 | + this.biomeMap.put("GIANT_SPRUCE_TAIGA", Biome.OLD_GROWTH_SPRUCE_TAIGA); |
| 43 | + this.biomeMap.put("SNOWY_TUNDRA", Biome.SNOWY_PLAINS); |
| 44 | + this.biomeMap.put("JUNGLE_EDGE", Biome.SPARSE_JUNGLE); |
| 45 | + this.biomeMap.put("STONE_SHORE", Biome.STONY_SHORE); |
| 46 | + this.biomeMap.put("MOUNTAINS", Biome.WINDSWEPT_HILLS); |
| 47 | + this.biomeMap.put("WOODED_MOUNTAINS", Biome.WINDSWEPT_FOREST); |
| 48 | + this.biomeMap.put("GRAVELLY_MOUNTAINS", Biome.WINDSWEPT_GRAVELLY_HILLS); |
| 49 | + this.biomeMap.put("SHATTERED_SAVANNA", Biome.WINDSWEPT_SAVANNA); |
| 50 | + this.biomeMap.put("WOODED_BADLANDS_PLATEAU", Biome.WOODED_BADLANDS); |
| 51 | + |
| 52 | + // Put in removed biomes values. BONNe chose some close enough values. |
| 53 | + this.biomeMap.put("SNOWY_MOUNTAINS", Biome.WINDSWEPT_HILLS); |
| 54 | + this.biomeMap.put("DESERT_HILLS", Biome.WINDSWEPT_HILLS); |
| 55 | + this.biomeMap.put("MOUNTAIN_EDGE", Biome.WINDSWEPT_HILLS); |
| 56 | + this.biomeMap.put("SNOWY_TAIGA_HILLS", Biome.WINDSWEPT_HILLS); |
| 57 | + this.biomeMap.put("TAIGA_HILLS", Biome.WINDSWEPT_HILLS); |
| 58 | + this.biomeMap.put("TAIGA_MOUNTAINS", Biome.WINDSWEPT_HILLS); |
| 59 | + this.biomeMap.put("SNOWY_TAIGA_MOUNTAINS", Biome.WINDSWEPT_HILLS); |
| 60 | + this.biomeMap.put("WOODED_HILLS", Biome.WINDSWEPT_FOREST); |
| 61 | + this.biomeMap.put("SWAMP_HILLS", Biome.WINDSWEPT_FOREST); |
| 62 | + this.biomeMap.put("DARK_FOREST_HILLS", Biome.WINDSWEPT_FOREST); |
| 63 | + this.biomeMap.put("JUNGLE_HILLS", Biome.SPARSE_JUNGLE); |
| 64 | + this.biomeMap.put("MODIFIED_JUNGLE", Biome.SPARSE_JUNGLE); |
| 65 | + this.biomeMap.put("MODIFIED_JUNGLE_EDGE", Biome.SPARSE_JUNGLE); |
| 66 | + this.biomeMap.put("BAMBOO_JUNGLE_HILLS", Biome.SPARSE_JUNGLE); |
| 67 | + this.biomeMap.put("BIRCH_FOREST_HILLS", Biome.OLD_GROWTH_BIRCH_FOREST); |
| 68 | + this.biomeMap.put("TALL_BIRCH_HILLS", Biome.OLD_GROWTH_BIRCH_FOREST); |
| 69 | + this.biomeMap.put("GIANT_TREE_TAIGA_HILLS", Biome.OLD_GROWTH_PINE_TAIGA); |
| 70 | + this.biomeMap.put("GIANT_SPRUCE_TAIGA_HILLS", Biome.OLD_GROWTH_SPRUCE_TAIGA); |
| 71 | + this.biomeMap.put("MUSHROOM_FIELD_SHORE", Biome.MUSHROOM_FIELDS); |
| 72 | + this.biomeMap.put("BADLANDS_PLATEAU", Biome.BADLANDS); |
| 73 | + this.biomeMap.put("MODIFIED_WOODED_BADLANDS_PLATEAU", Biome.BADLANDS); |
| 74 | + this.biomeMap.put("MODIFIED_BADLANDS_PLATEAU", Biome.BADLANDS); |
| 75 | + this.biomeMap.put("SHATTERED_SAVANNA_PLATEAU", Biome.SAVANNA_PLATEAU); |
| 76 | + this.biomeMap.put("DESERT_LAKES", Biome.DESERT); |
| 77 | + this.biomeMap.put("MODIFIED_GRAVELLY_MOUNTAINS", Biome.WINDSWEPT_GRAVELLY_HILLS); |
| 78 | + this.biomeMap.put("DEEP_WARM_OCEAN", Biome.DEEP_LUKEWARM_OCEAN); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Biome read(JsonReader input) throws IOException |
| 83 | + { |
| 84 | + if (JsonToken.NULL.equals(input.peek())) { |
| 85 | + input.nextNull(); |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + return this.biomeMap.get(input.nextString().toUpperCase()); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void write(JsonWriter output, Biome enumValue) throws IOException { |
| 94 | + output.value(enumValue != null ? enumValue.name() : null); |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments