Skip to content

Commit ed50765

Browse files
authored
Addres Biome's changes in Minecraft 1.18. (#1893)
Since Minecraft 1.18 renamed and removed a lot of biomes, some addons that stored them in the database may require migration. This Adapter will fix that as now BentoBox itself will do migration for all Biome.class objects.
1 parent 2e6ef59 commit ed50765

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.bukkit.Location;
66
import org.bukkit.World;
7+
import org.bukkit.block.Biome;
78
import org.bukkit.configuration.serialization.ConfigurationSerializable;
89
import org.bukkit.inventory.ItemStack;
910
import org.bukkit.potion.PotionEffectType;
@@ -16,14 +17,8 @@
1617

1718
import world.bentobox.bentobox.BentoBox;
1819
import world.bentobox.bentobox.api.flags.Flag;
19-
import world.bentobox.bentobox.database.json.adapters.BukkitObjectTypeAdapter;
20-
import world.bentobox.bentobox.database.json.adapters.EnumTypeAdapter;
21-
import world.bentobox.bentobox.database.json.adapters.FlagTypeAdapter;
22-
import world.bentobox.bentobox.database.json.adapters.ItemStackTypeAdapter;
23-
import world.bentobox.bentobox.database.json.adapters.LocationTypeAdapter;
24-
import world.bentobox.bentobox.database.json.adapters.PotionEffectTypeAdapter;
25-
import world.bentobox.bentobox.database.json.adapters.VectorTypeAdapter;
26-
import world.bentobox.bentobox.database.json.adapters.WorldTypeAdapter;
20+
import world.bentobox.bentobox.database.json.adapters.*;
21+
2722

2823
/**
2924
* Allocates type adapters based on class type.
@@ -52,6 +47,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
5247
if (Location.class.isAssignableFrom(rawType)) {
5348
// Use our current location adapter for backward compatibility
5449
return (TypeAdapter<T>) new LocationTypeAdapter();
50+
} else if (Biome.class.isAssignableFrom(rawType)) {
51+
return (TypeAdapter<T>) new BiomeTypeAdapter();
5552
} else if (Enum.class.isAssignableFrom(rawType)) {
5653
return new EnumTypeAdapter(rawType);
5754
} else if (ItemStack.class.isAssignableFrom(rawType)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)