Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Port Structures #317

Merged
merged 2 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/main/java/com/gildedgames/aether/Aether.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ public Aether() {
modEventBus.addListener(this::commonSetup);
modEventBus.addListener(this::curiosSetup);
modEventBus.addListener(this::dataSetup);

IEventBus forgeBus = MinecraftForge.EVENT_BUS;
forgeBus.addListener(EventPriority.NORMAL, AetherStructures::addDimensionalSpacing);

DeferredRegister<?>[] registers = {
AetherBlocks.BLOCKS,
Expand All @@ -94,8 +91,7 @@ public Aether() {
AetherSoundEvents.SOUNDS,
AetherContainerTypes.CONTAINERS,
AetherTileEntityTypes.TILE_ENTITIES,
AetherRecipes.RECIPE_SERIALIZERS,
AetherStructures.STRUCTURES
AetherRecipes.RECIPE_SERIALIZERS
};

for (DeferredRegister<?> register : registers) {
Expand All @@ -108,6 +104,8 @@ public Aether() {

ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, AetherConfig.COMMON_SPEC);
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, AetherConfig.CLIENT_SPEC);

AetherStructureIngress.registerEvents(modEventBus);
}

public void commonSetup(FMLCommonSetupEvent event) {
Expand All @@ -122,9 +120,6 @@ public void commonSetup(FMLCommonSetupEvent event) {

AetherFeatures.registerConfiguredFeatures();

AetherStructures.registerStructures();
AetherStructures.registerConfiguredStructures();

AetherEntityTypes.registerSpawnPlacements();

AetherItems.registerAbilities();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gildedgames.aether.common.registry;

import com.gildedgames.aether.Aether;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.data.worldgen.PlainVillagePools;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.feature.ConfiguredStructureFeature;
import net.minecraft.world.level.levelgen.feature.configurations.JigsawConfiguration;

public class AetherConfiguredStructures {
public static ConfiguredStructureFeature<?, ?> CONFIGURED_BRONZE_DUNGEON = AetherStructures.BRONZE_DUNGEON.get()
.configured(new JigsawConfiguration(() -> PlainVillagePools.START, 0));
public static ConfiguredStructureFeature<?, ?> CONFIGURED_GOLD_DUNGEON = AetherStructures.GOLD_DUNGEON.get()
.configured(new JigsawConfiguration(() -> PlainVillagePools.START, 0));

public static void registerConfiguredStructures() {
Registry<ConfiguredStructureFeature<?, ?>> registry = BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE;
Registry.register(registry, new ResourceLocation(Aether.MODID, "configured_bronze_dungeon"), CONFIGURED_BRONZE_DUNGEON);
Registry.register(registry, new ResourceLocation(Aether.MODID, "configured_gold_dungeon"), CONFIGURED_GOLD_DUNGEON);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockMatchTest;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;
import net.minecraftforge.registries.DeferredRegister;
Expand All @@ -34,7 +35,7 @@ public class AetherFeatures
// public static final RegistryObject<Feature<NoneFeatureConfiguration>> CRYSTAL_TREE = FEATURES.register("crystal_tree", () -> new CrystalTreeFeature(NoneFeatureConfiguration.CODEC));
// public static final RegistryObject<Feature<NoneFeatureConfiguration>> HOLIDAY_TREE = FEATURES.register("holiday_tree", () -> new HolidayTreeFeature(NoneFeatureConfiguration.CODEC));
//
// public static final RegistryObject<Feature<NoneFeatureConfiguration>> HOLYSTONE_SPHERE = FEATURES.register("holystone_sphere", () -> new HolystoneSphereFeature(NoneFeatureConfiguration.CODEC)); // This is for Gold Dungeons
public static final RegistryObject<Feature<NoneFeatureConfiguration>> HOLYSTONE_SPHERE = FEATURES.register("holystone_sphere", () -> new HolystoneSphereFeature(NoneFeatureConfiguration.CODEC)); // This is for Gold Dungeons

public static final class States
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.gildedgames.aether.common.registry;

import com.gildedgames.aether.Aether;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.mojang.serialization.Codec;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.levelgen.FlatLevelSource;
import net.minecraft.world.level.levelgen.StructureSettings;
import net.minecraft.world.level.levelgen.feature.ConfiguredStructureFeature;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
import net.minecraft.world.level.levelgen.feature.configurations.StructureFeatureConfiguration;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;

import java.util.HashMap;
import java.util.Map;

// FIXME: This class is needed because registering structures within the Minecraft core codebase is god-awful right now
// I'm not a fan of littering the main Aether mod class with this mess, so that's why these are here instead
@Deprecated // This class is NOT going to stay.
public final class AetherStructureIngress {
public static void registerEvents(IEventBus modEventBus) {
AetherStructures.STRUCTURES.register(modEventBus);
modEventBus.addListener(AetherStructureIngress::setup);

IEventBus forgeBus = MinecraftForge.EVENT_BUS;
forgeBus.addListener(EventPriority.NORMAL, AetherStructureIngress::addDimensionalSpacing);
//forgeBus.addListener(EventPriority.NORMAL, /*StructureFeature*/::setupStructureSpawns);
}

public static void setup(final FMLCommonSetupEvent event) {
event.enqueueWork(() -> {
AetherStructures.initStructures();
AetherConfiguredStructures.registerConfiguredStructures();
});
}

/**
* Tells the chunkgenerator which biomes our structure can spawn in.
* Will go into the world's chunkgenerator and manually add our structure spacing.
* If the spacing is not added, the structure doesn't spawn.
*
* Use this for dimension blacklists for your structure.
* (Don't forget to attempt to remove your structure too from the map if you are blacklisting that dimension!)
* (It might have your structure in it already.)
*
* Basically use this to make absolutely sure the chunkgenerator can or cannot spawn your structure.
*/
public static void addDimensionalSpacing(final WorldEvent.Load event) {
if (event.getWorld() instanceof ServerLevel serverLevel && Aether.MODID.equals(serverLevel.dimension().location().getNamespace())) {
ChunkGenerator chunkGenerator = serverLevel.getChunkSource().getGenerator();

StructureSettings worldStructureConfig = chunkGenerator.getSettings();

HashMap<StructureFeature<?>, HashMultimap<ConfiguredStructureFeature<?, ?>, ResourceKey<Biome>>> aetherStructure2BiomeMap = new HashMap<>();

for(Map.Entry<ResourceKey<Biome>, Biome> biomeEntry : serverLevel.registryAccess().ownedRegistryOrThrow(Registry.BIOME_REGISTRY).entrySet()) {
var biomeKey = biomeEntry.getKey();
if (Aether.MODID.equals(biomeKey.location().getNamespace())) { // If the biome has our namespace on it, add it
associateBiomeToConfiguredStructure(aetherStructure2BiomeMap, AetherConfiguredStructures.CONFIGURED_BRONZE_DUNGEON, biomeKey);
associateBiomeToConfiguredStructure(aetherStructure2BiomeMap, AetherConfiguredStructures.CONFIGURED_GOLD_DUNGEON, biomeKey);
}
}

ImmutableMap.Builder<StructureFeature<?>, ImmutableMultimap<ConfiguredStructureFeature<?, ?>, ResourceKey<Biome>>> tempStructureToMultiMap = ImmutableMap.builder();
worldStructureConfig.configuredStructures.entrySet().stream().filter(entry -> !aetherStructure2BiomeMap.containsKey(entry.getKey())).forEach(tempStructureToMultiMap::put);

aetherStructure2BiomeMap.forEach((key, value) -> tempStructureToMultiMap.put(key, ImmutableMultimap.copyOf(value)));

worldStructureConfig.configuredStructures = tempStructureToMultiMap.build();

Map<StructureFeature<?>, StructureFeatureConfiguration> tempMap = new HashMap<>(worldStructureConfig.structureConfig());
tempMap.putIfAbsent(AetherStructures.BRONZE_DUNGEON.get(), StructureSettings.DEFAULTS.get(AetherStructures.BRONZE_DUNGEON.get()));
tempMap.putIfAbsent(AetherStructures.GOLD_DUNGEON.get(), StructureSettings.DEFAULTS.get(AetherStructures.GOLD_DUNGEON.get()));
worldStructureConfig.structureConfig = tempMap;
}
}

/**
* Helper method that handles setting up the map to multimap relationship to help prevent issues.
*/
private static void associateBiomeToConfiguredStructure(Map<StructureFeature<?>, HashMultimap<ConfiguredStructureFeature<?, ?>, ResourceKey<Biome>>> aetherStructureToMultiMap, ConfiguredStructureFeature<?, ?> configuredStructureFeature, ResourceKey<Biome> biomeRegistryKey) {
aetherStructureToMultiMap.putIfAbsent(configuredStructureFeature.feature, HashMultimap.create());
HashMultimap<ConfiguredStructureFeature<?, ?>, ResourceKey<Biome>> configuredStructureToBiomeMultiMap = aetherStructureToMultiMap.get(configuredStructureFeature.feature);
if(configuredStructureToBiomeMultiMap.containsValue(biomeRegistryKey)) {
Aether.LOGGER.error("""
Detected 2 ConfiguredStructureFeatures that share the same base StructureFeature trying to be added to same biome. One will be prevented from spawning.
This issue happens with vanilla too and is why a Snowy Village and Plains Village cannot spawn in the same biome because they both use the Village base structure.
The two conflicting ConfiguredStructures are: {}, {}
The biome that is attempting to be shared: {}
""",
BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE.getId(configuredStructureFeature),
BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE.getId(configuredStructureToBiomeMultiMap.entries().stream().filter(e -> e.getValue() == biomeRegistryKey).findFirst().get().getKey()),
biomeRegistryKey
);
}
else{
configuredStructureToBiomeMultiMap.put(configuredStructureFeature, biomeRegistryKey);
}
}

private AetherStructureIngress() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,60 @@
import com.gildedgames.aether.common.world.structure.GoldDungeonStructure;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.levelgen.FlatLevelSource;
import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorSettings;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;
import net.minecraft.data.worldgen.PlainVillagePools;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
import net.minecraft.world.level.levelgen.feature.ConfiguredStructureFeature;
import net.minecraft.world.level.levelgen.feature.configurations.JigsawConfiguration;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
import net.minecraft.world.level.levelgen.StructureSettings;
import net.minecraft.world.level.levelgen.feature.configurations.StructureFeatureConfiguration;
import net.minecraft.server.level.ServerLevel;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.HashMap;
import java.util.Map;

public class AetherStructures
{
public class AetherStructures {
public static final DeferredRegister<StructureFeature<?>> STRUCTURES = DeferredRegister.create(ForgeRegistries.STRUCTURE_FEATURES, Aether.MODID);

// public static final RegistryObject<StructureFeature<NoneFeatureConfiguration>> BRONZE_DUNGEON = STRUCTURES.register("bronze_dungeon", () -> new BronzeDungeonStructure(NoneFeatureConfiguration.CODEC));
// public static final RegistryObject<StructureFeature<NoneFeatureConfiguration>> GOLD_DUNGEON = STRUCTURES.register("gold_dungeon", () -> new GoldDungeonStructure(NoneFeatureConfiguration.CODEC));
public static final RegistryObject<StructureFeature<JigsawConfiguration>> BRONZE_DUNGEON = STRUCTURES.register("bronze_dungeon", BronzeDungeonStructure::new);
public static final RegistryObject<StructureFeature<JigsawConfiguration>> GOLD_DUNGEON = STRUCTURES.register("gold_dungeon", GoldDungeonStructure::new);

public static final class ConfiguredStructures
{
// public static final ConfiguredStructureFeature<?, ?> BRONZE_DUNGEON = AetherStructures.BRONZE_DUNGEON.get().configured(FeatureConfiguration.NONE);
// public static final ConfiguredStructureFeature<?, ?> GOLD_DUNGEON = AetherStructures.GOLD_DUNGEON.get().configured(FeatureConfiguration.NONE);
public static void initStructures() {
setupMapSpacingAndLand(AetherStructures.BRONZE_DUNGEON.get(), new StructureFeatureConfiguration(6, 4, 16811681), true);
setupMapSpacingAndLand(AetherStructures.GOLD_DUNGEON.get(), new StructureFeatureConfiguration(24, 12, 120320420), false);
}

public static void registerStructures() {
// setupStructure(BRONZE_DUNGEON.get(), new StructureFeatureConfiguration(6, 4, 16811681), false);
// setupStructure(GOLD_DUNGEON.get(), new StructureFeatureConfiguration(24, 12, 120320420), false);
}

public static void registerConfiguredStructures() {
// Registry.register(BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE, new ResourceLocation(Aether.MODID, "bronze_dungeon"), BRONZE_DUNGEON.get().configured(FeatureConfiguration.NONE));
// Registry.register(BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE, new ResourceLocation(Aether.MODID, "gold_dungeon"), GOLD_DUNGEON.get().configured(FeatureConfiguration.NONE));

// FlatLevelGeneratorSettings.STRUCTURE_FEATURES.put(BRONZE_DUNGEON.get(), ConfiguredStructures.BRONZE_DUNGEON);
// FlatLevelGeneratorSettings.STRUCTURE_FEATURES.put(GOLD_DUNGEON.get(), ConfiguredStructures.GOLD_DUNGEON);
}
public static <F extends StructureFeature<?>> void setupMapSpacingAndLand(F structure, StructureFeatureConfiguration structureFeatureConfiguration, boolean transformSurroundingLand) {
StructureFeature.STRUCTURES_REGISTRY.put(structure.getRegistryName().toString(), structure);

public static void addDimensionalSpacing(final WorldEvent.Load event) {
if (event.getWorld() instanceof ServerLevel) {
ServerLevel serverWorld = (ServerLevel)event.getWorld();
if (transformSurroundingLand)
StructureFeature.NOISE_AFFECTING_FEATURES = ImmutableList.<StructureFeature<?>>builder()
.addAll(StructureFeature.NOISE_AFFECTING_FEATURES)
.add(structure)
.build();

if (serverWorld.getChunkSource().getGenerator() instanceof FlatLevelSource && serverWorld.dimension().equals(Level.OVERWORLD)) {
return;
}
StructureSettings.DEFAULTS = ImmutableMap.<StructureFeature<?>, StructureFeatureConfiguration>builder()
.putAll(StructureSettings.DEFAULTS)
.put(structure, structureFeatureConfiguration)
.build();

// Map<StructureFeature<?>, StructureFeatureConfiguration> tempMap = new HashMap<>(serverWorld.getChunkSource().generator.getSettings().structureConfig());
// tempMap.put(AetherStructures.BRONZE_DUNGEON.get(), StructureSettings.DEFAULTS.get(AetherStructures.BRONZE_DUNGEON.get()));
// tempMap.put(AetherStructures.GOLD_DUNGEON.get(), StructureSettings.DEFAULTS.get(AetherStructures.GOLD_DUNGEON.get()));
// serverWorld.getChunkSource().generator.getSettings().structureConfig = tempMap;
}
BuiltinRegistries.NOISE_GENERATOR_SETTINGS.entrySet().stream().map(Map.Entry::getValue).forEach(settings -> AetherStructures.attachStructureToNoiseGen(structure, structureFeatureConfiguration, settings));
}

private static <F extends StructureFeature<?>> void setupStructure(F structure, StructureFeatureConfiguration structureSeparationSettings, boolean transformSurroundingLand) {
StructureFeature.STRUCTURES_REGISTRY.put(structure.getRegistryName().toString(), structure);
public static void attachStructureToNoiseGen(StructureFeature<?> structure, StructureFeatureConfiguration structureFeatureConfiguration, NoiseGeneratorSettings settings) {
Map<StructureFeature<?>, StructureFeatureConfiguration> structureMap = settings.structureSettings().structureConfig();

// if (transformSurroundingLand) {
// StructureFeature.NOISE_AFFECTING_FEATURES =
// ImmutableList.<StructureFeature<?>>builder()
// .addAll(StructureFeature.NOISE_AFFECTING_FEATURES)
// .add(structure)
// .build();
// }
//
// StructureSettings.DEFAULTS =
// ImmutableMap.<StructureFeature<?>, StructureFeatureConfiguration>builder()
// .putAll(StructureSettings.DEFAULTS)
// .put(structure, structureSeparationSettings)
// .build();
if (structureMap instanceof ImmutableMap) {
Map<StructureFeature<?>, StructureFeatureConfiguration> tempMap = new HashMap<>(structureMap);
tempMap.put(structure, structureFeatureConfiguration);
settings.structureSettings().structureConfig = tempMap;
} else {
structureMap.put(structure, structureFeatureConfiguration);
}
}
}
Loading