Adopted Forge's new world type system

This commit is contained in:
Adubbz 2020-12-08 12:25:09 +11:00
parent 51f5cb1b6d
commit cca10ac408
9 changed files with 87 additions and 186 deletions

View file

@ -7,7 +7,7 @@ mod_version=13.0.0
minecraft_version=1.16.4
minecraft_version_toml=16
forge_version=35.0.2
forge_version=35.1.10
forge_version_toml=35
forge_group=net.minecraftforge
mappings_version=2-1.16.2

View file

@ -3,10 +3,8 @@ package biomesoplenty.client.handler;
import biomesoplenty.init.ModBiomes;
import biomesoplenty.init.ModConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.CreateWorldScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.WorldOptionsScreen;
import net.minecraft.client.gui.screen.WorldSelectionScreen;
import net.minecraft.client.gui.screen.*;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.GuiOpenEvent;
@ -30,8 +28,23 @@ public class GuiEventHandler
if (ModConfig.ClientConfig.useWorldType.get() && gui instanceof CreateWorldScreen && prevScreen instanceof WorldSelectionScreen)
{
WorldOptionsScreen optionsScreen = ((CreateWorldScreen)gui).worldGenSettingsComponent;
optionsScreen.preset = Optional.of(ModBiomes.biomeGeneratorTypeScreenBOP);
optionsScreen.preset = Optional.of(findBopBiomeGeneratorTypeScreen());
optionsScreen.settings = optionsScreen.preset.get().create(optionsScreen.registryHolder, optionsScreen.settings.seed(), optionsScreen.settings.generateFeatures(), optionsScreen.settings.generateBonusChest());
}
}
private static BiomeGeneratorTypeScreens findBopBiomeGeneratorTypeScreen()
{
for (BiomeGeneratorTypeScreens screen : BiomeGeneratorTypeScreens.PRESETS)
{
TranslationTextComponent desc = (TranslationTextComponent)screen.description;
if (desc.getKey().equals("generator.minecraft.biomesoplenty"))
{
return screen;
}
}
throw new RuntimeException("Failed to locate biomesoplenty biome generator type screen!");
}
}

View file

@ -1,39 +0,0 @@
/*******************************************************************************
* Copyright 2014-2020, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.world;
import net.minecraft.client.gui.screen.BiomeGeneratorTypeScreens;
import net.minecraft.util.registry.DynamicRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.DimensionSettings;
import net.minecraft.world.gen.settings.DimensionGeneratorSettings;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class BOPBiomeGeneratorTypeScreen extends BiomeGeneratorTypeScreens
{
public BOPBiomeGeneratorTypeScreen()
{
super("biomesoplenty");
}
@Override
protected ChunkGenerator generator(Registry<Biome> biomeRegistry, Registry<DimensionSettings> dimensionSettingsRegistry, long seed)
{
return BOPWorldTypeUtil.makeOverworld(biomeRegistry, dimensionSettingsRegistry, seed);
}
@Override
public DimensionGeneratorSettings create(DynamicRegistries.Impl registries, long seed, boolean generateFeatures, boolean generateBonusChest)
{
return BOPWorldTypeUtil.createDimensionGeneratorSettings(registries, seed, generateFeatures, generateBonusChest);
}
}

View file

@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright 2014-2020, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.world;
import net.minecraft.util.registry.DynamicRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.DimensionType;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.DimensionSettings;
import net.minecraft.world.gen.NoiseChunkGenerator;
import net.minecraft.world.gen.settings.DimensionGeneratorSettings;
import net.minecraftforge.common.world.ForgeWorldType;
public class BOPWorldType extends ForgeWorldType
{
public BOPWorldType()
{
super(null);
}
@Override
public ChunkGenerator createChunkGenerator(Registry<Biome> biomeRegistry, Registry<DimensionSettings> dimensionSettingsRegistry, long seed, String generatorSettings)
{
return new NoiseChunkGenerator(new BOPBiomeProvider(seed, biomeRegistry), seed, () -> dimensionSettingsRegistry.getOrThrow(DimensionSettings.OVERWORLD));
}
@Override
public DimensionGeneratorSettings createSettings(DynamicRegistries dynamicRegistries, long seed, boolean generateFeatures, boolean generateBonusChest, String generatorSettings)
{
Registry<Biome> biomeRegistry = dynamicRegistries.registryOrThrow(Registry.BIOME_REGISTRY);
Registry<DimensionSettings> dimensionSettingsRegistry = dynamicRegistries.registryOrThrow(Registry.NOISE_GENERATOR_SETTINGS_REGISTRY);
Registry<DimensionType> dimensionTypeRegistry = dynamicRegistries.registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY);
return new DimensionGeneratorSettings(seed, generateFeatures, generateBonusChest, DimensionGeneratorSettings.withOverworld(dimensionTypeRegistry, BOPDimensionType.bopDimensions(biomeRegistry, dimensionSettingsRegistry, seed), createChunkGenerator(biomeRegistry, dimensionSettingsRegistry, seed, null)));
}
}

View file

@ -1,113 +0,0 @@
/*******************************************************************************
* Copyright 2014-2020, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.world;
import biomesoplenty.core.BiomesOPlenty;
import com.google.common.collect.Lists;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.dedicated.ServerProperties;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.registry.DynamicRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.WorldGenRegistries;
import net.minecraft.world.Dimension;
import net.minecraft.world.DimensionType;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.provider.NetherBiomeProvider;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.DimensionSettings;
import net.minecraft.world.gen.NoiseChunkGenerator;
import net.minecraft.world.gen.settings.DimensionGeneratorSettings;
import net.minecraft.world.storage.ServerWorldInfo;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
public class BOPWorldTypeUtil
{
private static boolean isServerLevelTypeBop(DedicatedServer server)
{
String levelType = Optional.ofNullable((String)server.getProperties().properties.get("level-type")).map((str) -> str.toLowerCase(Locale.ROOT)).orElse("default");
return levelType.equals("biomesoplenty") || levelType.equals("biomesop");
}
// Derived from Dimension.stable
public static boolean isUsingBopWorldType(DimensionGeneratorSettings settings)
{
List<Map.Entry<RegistryKey<Dimension>, Dimension>> dimensions = Lists.newArrayList(settings.dimensions().entrySet());
Map.Entry<RegistryKey<Dimension>, Dimension> dimensionEntry0 = dimensions.get(0);
Map.Entry<RegistryKey<Dimension>, Dimension> dimensionEntry1 = dimensions.get(1);
Map.Entry<RegistryKey<Dimension>, Dimension> dimensionEntry2 = dimensions.get(2);
// BoP uses the standard dimension layout
if (dimensionEntry0.getKey() != Dimension.OVERWORLD || dimensionEntry1.getKey() != Dimension.NETHER && dimensionEntry2.getKey() != Dimension.END)
{
return false;
}
Dimension overworld = dimensionEntry0.getValue();
Dimension nether = dimensionEntry1.getValue();
Dimension end = dimensionEntry2.getValue();
// Ensure noise chunk generators are used in all dimensions
if (!(overworld.generator() instanceof NoiseChunkGenerator) || !(nether.generator() instanceof NoiseChunkGenerator) || !(end.generator() instanceof NoiseChunkGenerator))
{
return false;
}
// Ensure our nether and overworld biome providers are being used
if (!(overworld.generator().getBiomeSource() instanceof BOPBiomeProvider) || !(nether.generator().getBiomeSource() instanceof BOPNetherBiomeProvider || nether.generator().getBiomeSource() instanceof NetherBiomeProvider))
{
return false;
}
return true;
}
public static ChunkGenerator makeOverworld(Registry<Biome> biomes, Registry<DimensionSettings> noiseGeneratorSettings, long seed)
{
return new NoiseChunkGenerator(new BOPBiomeProvider(seed, biomes), seed, () -> noiseGeneratorSettings.getOrThrow(DimensionSettings.OVERWORLD));
}
public static DimensionGeneratorSettings createDimensionGeneratorSettings(DynamicRegistries registries, long seed, boolean generateFeatures, boolean generateBonusChest)
{
Registry<Biome> biomeRegistry = registries.registryOrThrow(Registry.BIOME_REGISTRY);
Registry<DimensionSettings> dimensionSettingsRegistry = registries.registryOrThrow(Registry.NOISE_GENERATOR_SETTINGS_REGISTRY);
Registry<DimensionType> dimensionTypeRegistry = registries.registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY);
return new DimensionGeneratorSettings(seed, generateFeatures, generateBonusChest, DimensionGeneratorSettings.withOverworld(dimensionTypeRegistry, BOPDimensionType.bopDimensions(biomeRegistry, dimensionSettingsRegistry, seed), makeOverworld(biomeRegistry, dimensionSettingsRegistry, seed)));
}
public static void setupForDedicatedServer(DedicatedServer server)
{
// Ensure we are using the bop world type
if (!isServerLevelTypeBop(server))
return;
ServerProperties properties = server.getProperties();
BiomesOPlenty.logger.info("Setting world generator settings to biomesoplenty");
if (server.getWorldData() instanceof ServerWorldInfo)
{
ServerWorldInfo worldInfo = (ServerWorldInfo) server.getWorldData();
// Only replace the worldGenSettings if it doesn't appear to already be the bop world type
if (!isUsingBopWorldType(worldInfo.worldGenSettings))
{
worldInfo.worldGenSettings = createDimensionGeneratorSettings(server.registryAccess(), worldInfo.worldGenSettings.seed(), worldInfo.worldGenSettings.generateFeatures(), worldInfo.worldGenSettings.generateBonusChest());
}
}
// Replace the world gen settings in server.properties
if (!isUsingBopWorldType(properties.worldGenSettings))
{
properties.worldGenSettings = createDimensionGeneratorSettings(server.registryAccess(), properties.worldGenSettings.seed(), properties.worldGenSettings.generateFeatures(), properties.worldGenSettings.generateBonusChest());
}
}
}

View file

@ -8,21 +8,13 @@
package biomesoplenty.core;
import biomesoplenty.common.world.BOPWorldTypeUtil;
import biomesoplenty.init.*;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.util.registry.DynamicRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;
import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ForgeRegistries;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -43,7 +35,6 @@ public class BiomesOPlenty
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::commonSetup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::loadComplete);
MinecraftForge.EVENT_BUS.addListener(this::serverStarting);
ModBiomes.setup();
ModConfig.setup();
@ -63,15 +54,4 @@ public class BiomesOPlenty
proxy.init();
ModCompatibility.setup();
}
private void serverStarting(final FMLServerAboutToStartEvent event)
{
// Only apply hackery to dedicated servers
if (!(event.getServer() instanceof DedicatedServer))
{
return;
}
BOPWorldTypeUtil.setupForDedicatedServer((DedicatedServer)event.getServer());
}
}

View file

@ -17,16 +17,17 @@ import biomesoplenty.common.biome.nether.VisceralHeapBiome;
import biomesoplenty.common.biome.nether.WitheredAbyssBiome;
import biomesoplenty.common.biome.overworld.*;
import biomesoplenty.common.util.biome.BiomeUtil;
import biomesoplenty.common.world.BOPBiomeGeneratorTypeScreen;
import biomesoplenty.common.world.BOPBiomeProvider;
import biomesoplenty.common.world.BOPNetherBiomeProvider;
import biomesoplenty.common.world.BOPWorldType;
import biomesoplenty.core.BiomesOPlenty;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import net.minecraft.client.gui.screen.BiomeGeneratorTypeScreens;
import net.minecraft.entity.villager.VillagerType;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.BiomeColors;
@ -39,6 +40,11 @@ import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.GameData;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import java.util.List;
import java.util.Map;
@ -48,7 +54,7 @@ import static biomesoplenty.api.biome.BOPBiomes.*;
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModBiomes
{
public static BOPBiomeGeneratorTypeScreen biomeGeneratorTypeScreenBOP;
public static BOPWorldType bopWorldType = new BOPWorldType();
public static Multimap<Integer, WeightedSubBiome> subBiomes = HashMultimap.create();
public static List<Integer> islandBiomeIds = Lists.newArrayList();
@ -58,9 +64,6 @@ public class ModBiomes
{
if (FMLEnvironment.dist == Dist.CLIENT)
{
biomeGeneratorTypeScreenBOP = new BOPBiomeGeneratorTypeScreen();
BiomeGeneratorTypeScreens.PRESETS.add(biomeGeneratorTypeScreenBOP);
ColorResolver grassColorResolver = BiomeColors.GRASS_COLOR_RESOLVER;
ColorResolver foliageColorResolver = BiomeColors.FOLIAGE_COLOR_RESOLVER;
ColorResolver waterColorResolver = BiomeColors.WATER_COLOR_RESOLVER;
@ -105,6 +108,21 @@ public class ModBiomes
};
}
// Obtain the game data logger and disable it temporarily
Logger gameDataLogger = (Logger)LogManager.getLogger(GameData.class);
Level oldLevel = gameDataLogger.getLevel();
gameDataLogger.setLevel(Level.OFF);
// Register our world type
// We intentionally use the minecraft namespace so we continue using "biomesoplenty" in server.properties
// This is markedly better than the alternative of biomesoplenty:biomesoplenty.
// We do this with GameData logging disabled to prevent people whining at us.
bopWorldType.setRegistryName(new ResourceLocation("biomesoplenty"));
ForgeRegistries.WORLD_TYPES.register(bopWorldType);
// Re-enable the game data logger
gameDataLogger.setLevel(oldLevel);
// Register biome providers
Registry.register(Registry.BIOME_SOURCE, "biomesoplenty_overworld", BOPBiomeProvider.CODEC);
Registry.register(Registry.BIOME_SOURCE, "biomesoplenty_nether", BOPNetherBiomeProvider.CODEC);

View file

@ -17,6 +17,6 @@ logoFile="bop_logo.png"
[[dependencies.biomesoplenty]]
modId="forge"
mandatory=true
versionRange="[35.0.2,)"
versionRange="[35.1.10,)"
ordering="NONE"
side="BOTH"

View file

@ -4,7 +4,7 @@
"advancements.biomesoplenty.all_biomes.title": "Wanderer",
"advancements.biomesoplenty.all_biomes.description": "Discover every biome in Biomes O' Plenty",
"generator.biomesoplenty": "Biomes O' Plenty",
"generator.minecraft.biomesoplenty": "Biomes O' Plenty",
"itemGroup.biomesoplenty": "Biomes O' Plenty",
"biome.biomesoplenty.alps": "Alps",