Resuscitate BiomeDictionary using RegistryKeys instead of biome references.

This commit is contained in:
LexManos 2020-10-15 12:43:18 -07:00
parent 51fa230e7b
commit 93226bbcff
2 changed files with 68 additions and 123 deletions

View file

@ -17,11 +17,11 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
/* Biomes are completely redone in 1.16.2, reevaluate
package net.minecraftforge.common; package net.minecraftforge.common;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -30,12 +30,12 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import net.minecraft.world.biome.Biomes; import net.minecraft.world.biome.Biomes;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.RegistryKey;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.*; import net.minecraft.world.biome.*;
import static net.minecraftforge.common.BiomeDictionary.Type.*; import static net.minecraftforge.common.BiomeDictionary.Type.*;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
public class BiomeDictionary public class BiomeDictionary
@ -45,8 +45,7 @@ public class BiomeDictionary
public static final class Type public static final class Type
{ {
private static final Map<String, Type> byName = new TreeMap<>();
private static final Map<String, Type> byName = new HashMap<String, Type>();
private static Collection<Type> allTypes = Collections.unmodifiableCollection(byName.values()); private static Collection<Type> allTypes = Collections.unmodifiableCollection(byName.values());
/*Temperature-based tags. Specifying neither implies a biome is temperate*/ /*Temperature-based tags. Specifying neither implies a biome is temperate*/
@ -104,8 +103,8 @@ public class BiomeDictionary
private final String name; private final String name;
private final List<Type> subTypes; private final List<Type> subTypes;
private final Set<Biome> biomes = new HashSet<Biome>(); private final Set<RegistryKey<Biome>> biomes = new HashSet<>();
private final Set<Biome> biomesUn = Collections.unmodifiableSet(biomes); private final Set<RegistryKey<Biome>> biomesUn = Collections.unmodifiableSet(biomes);
private Type(String name, Type... subTypes) private Type(String name, Type... subTypes)
{ {
@ -175,16 +174,15 @@ public class BiomeDictionary
} }
} }
private static final Map<ResourceLocation, BiomeInfo> biomeInfoMap = new HashMap<ResourceLocation, BiomeInfo>(); private static final Map<RegistryKey<Biome>, BiomeInfo> biomeInfoMap = new HashMap<>();
private static class BiomeInfo private static class BiomeInfo
{ {
private final Set<Type> types = new HashSet<Type>(); private final Set<Type> types = new HashSet<Type>();
private final Set<Type> typesUn = Collections.unmodifiableSet(this.types); private final Set<Type> typesUn = Collections.unmodifiableSet(this.types);
} }
public static void init() {}
static static
{ {
registerVanillaBiomes(); registerVanillaBiomes();
@ -194,10 +192,8 @@ public class BiomeDictionary
* Adds the given types to the biome. * Adds the given types to the biome.
* *
*/ */
public static void addTypes(Biome biome, Type... types) public static void addTypes(RegistryKey<Biome> biome, Type... types)
{ {
Preconditions.checkArgument(ForgeRegistries.BIOMES.containsValue(biome), "Cannot add types to unregistered biome %s", biome);
Collection<Type> supertypes = listSupertypes(types); Collection<Type> supertypes = listSupertypes(types);
Collections.addAll(supertypes, types); Collections.addAll(supertypes, types);
@ -216,7 +212,7 @@ public class BiomeDictionary
* *
*/ */
@Nonnull @Nonnull
public static Set<Biome> getBiomes(Type type) public static Set<RegistryKey<Biome>> getBiomes(Type type)
{ {
return type.biomesUn; return type.biomesUn;
} }
@ -226,9 +222,8 @@ public class BiomeDictionary
* *
*/ */
@Nonnull @Nonnull
public static Set<Type> getTypes(Biome biome) public static Set<Type> getTypes(RegistryKey<Biome> biome)
{ {
ensureHasTypes(biome);
return getBiomeInfo(biome).typesUn; return getBiomeInfo(biome).typesUn;
} }
@ -237,24 +232,18 @@ public class BiomeDictionary
* *
* @return returns true if a common type is found, false otherwise * @return returns true if a common type is found, false otherwise
*/ */
public static boolean areSimilar(Biome biomeA, Biome biomeB) public static boolean areSimilar(RegistryKey<Biome> biomeA, RegistryKey<Biome> biomeB)
{ {
for (Type type : getTypes(biomeA)) Set<Type> typesA = getTypes(biomeA);
{ Set<Type> typesB = getTypes(biomeB);
if (getTypes(biomeB).contains(type)) return typesA.stream().anyMatch(typesB::contains);
{
return true;
}
}
return false;
} }
/** /**
* Checks if the given type has been added to the given biome. * Checks if the given type has been added to the given biome.
* *
*/ */
public static boolean hasType(Biome biome, Type type) public static boolean hasType(RegistryKey<Biome> biome, Type type)
{ {
return getTypes(biome).contains(type); return getTypes(biome).contains(type);
} }
@ -263,89 +252,15 @@ public class BiomeDictionary
* Checks if any type has been added to the given biome. * Checks if any type has been added to the given biome.
* *
*/ */
public static boolean hasAnyType(Biome biome) public static boolean hasAnyType(RegistryKey<Biome> biome)
{ {
return !getBiomeInfo(biome).types.isEmpty(); return !getBiomeInfo(biome).types.isEmpty();
} }
/**
* Automatically adds appropriate types to a given biome based on certain heuristics.
* If a biome's types are requested and no types have been added to the biome so far, the biome's types
* will be determined and added using this method.
*
* /
public static void makeBestGuess(Biome biome)
{
Type type = Type.fromVanilla(biome.getCategory());
if (type != null)
{
BiomeDictionary.addTypes(biome, type);
}
if (biome.getDownfall() > 0.85f)
{
BiomeDictionary.addTypes(biome, WET);
}
if (biome.getDownfall() < 0.15f)
{
BiomeDictionary.addTypes(biome, DRY);
}
if (biome.getDefaultTemperature() > 0.85f)
{
BiomeDictionary.addTypes(biome, HOT);
}
if (biome.getDefaultTemperature() < 0.15f)
{
BiomeDictionary.addTypes(biome, COLD);
}
if (biome.isHighHumidity() && biome.getDepth() < 0.0F && (biome.getScale() <= 0.3F && biome.getScale() >= 0.0F))
{
BiomeDictionary.addTypes(biome, SWAMP);
}
if (biome.getDepth() <= -0.5F)
{
if (biome.getScale() == 0.0F)
{
BiomeDictionary.addTypes(biome, RIVER);
}
else
{
BiomeDictionary.addTypes(biome, OCEAN);
}
}
if (biome.getScale() >= 0.4F && biome.getScale() < 1.5F)
{
BiomeDictionary.addTypes(biome, HILLS);
}
if (biome.getScale() >= 1.5F)
{
BiomeDictionary.addTypes(biome, MOUNTAIN);
}
}
//Internal implementation //Internal implementation
private static BiomeInfo getBiomeInfo(Biome biome) private static BiomeInfo getBiomeInfo(RegistryKey<Biome> biome)
{ {
return biomeInfoMap.computeIfAbsent(biome.getRegistryName(), k -> new BiomeInfo()); return biomeInfoMap.computeIfAbsent(biome, k -> new BiomeInfo());
}
/**
* Ensure that at least one type has been added to the given biome.
* /
static void ensureHasTypes(Biome biome)
{
if (!hasAnyType(biome))
{
makeBestGuess(biome);
LOGGER.warn("No types have been added to Biome {}, types have been assigned on a best-effort guess: {}", biome.getRegistryName(), !getBiomeInfo(biome).types.isEmpty() ? getBiomeInfo(biome).types : "could not guess types");
}
} }
private static Collection<Type> listSupertypes(Type... types) private static Collection<Type> listSupertypes(Type... types)
@ -443,14 +358,43 @@ public class BiomeDictionary
addTypes(Biomes.ERODED_BADLANDS, HOT, DRY, SPARSE, MOUNTAIN, RARE, OVERWORLD); addTypes(Biomes.ERODED_BADLANDS, HOT, DRY, SPARSE, MOUNTAIN, RARE, OVERWORLD);
addTypes(Biomes.MODIFIED_WOODED_BADLANDS_PLATEAU, HOT, DRY, SPARSE, HILLS, RARE, OVERWORLD, PLATEAU, MODIFIED); addTypes(Biomes.MODIFIED_WOODED_BADLANDS_PLATEAU, HOT, DRY, SPARSE, HILLS, RARE, OVERWORLD, PLATEAU, MODIFIED);
addTypes(Biomes.MODIFIED_BADLANDS_PLATEAU, HOT, DRY, SPARSE, MOUNTAIN, RARE, OVERWORLD, PLATEAU, MODIFIED); addTypes(Biomes.MODIFIED_BADLANDS_PLATEAU, HOT, DRY, SPARSE, MOUNTAIN, RARE, OVERWORLD, PLATEAU, MODIFIED);
addTypes(Biomes.BAMBOO_JUNGLE, HOT, WET, RARE, JUNGLE, OVERWORLD);
addTypes(Biomes.BAMBOO_JUNGLE_HILLS, HOT, WET, RARE, JUNGLE, HILLS, OVERWORLD);
addTypes(Biomes.field_235252_ay_, HOT, DRY, NETHER);
addTypes(Biomes.field_235253_az_, HOT, DRY, NETHER, FOREST);
addTypes(Biomes.field_235250_aA_, HOT, DRY, NETHER, FOREST);
addTypes(Biomes.field_235251_aB_, HOT, DRY, NETHER);
if (DEBUG) if (DEBUG)
{ {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
buf.append("BiomeDictionary:\n"); buf.append("BiomeDictionary:\n");
Type.byName.forEach((name, type) -> buf.append(" ").append(type.name).append(": ").append(type.biomes.stream().map(b -> b.getRegistryName().toString()).collect(Collectors.joining(", "))).append('\n')); Type.byName.forEach((name, type) ->
buf.append(" ").append(type.name).append(": ")
.append(type.biomes.stream()
.map(RegistryKey::func_240901_a_)
.sorted((a,b) -> a.compareNamespaced(b))
.map(Object::toString)
.collect(Collectors.joining(", "))
)
.append('\n')
);
boolean missing = false;
List<RegistryKey<Biome>> all = StreamSupport.stream(ForgeRegistries.BIOMES.spliterator(), false)
.map(b -> RegistryKey.func_240903_a_(Registry.field_239720_u_, b.getRegistryName()))
.sorted().collect(Collectors.toList());
for (RegistryKey<Biome> key : all) {
if (!biomeInfoMap.containsKey(key)) {
if (!missing) {
buf.append("Missing:\n");
missing = true;
}
buf.append(" ").append(key.func_240901_a_()).append('\n');
}
}
LOGGER.debug(buf.toString()); LOGGER.debug(buf.toString());
} }
} }
} }
*/

View file

@ -127,6 +127,7 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook
MinecraftForge.EVENT_BUS.addListener(VillagerTradingManager::loadTrades); MinecraftForge.EVENT_BUS.addListener(VillagerTradingManager::loadTrades);
MinecraftForge.EVENT_BUS.register(MinecraftForge.INTERNAL_HANDLER); MinecraftForge.EVENT_BUS.register(MinecraftForge.INTERNAL_HANDLER);
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(this);
BiomeDictionary.init();
} }
public void preInit(FMLCommonSetupEvent evt) public void preInit(FMLCommonSetupEvent evt)