Skip to content

Commit

Permalink
Avoid/catch problems setting up a StructureBlockRegistry (#592)
Browse files Browse the repository at this point in the history
Fixes a problem that @akisephila encountered. When an exception is
thrown in `ILandType.registerBlocks()`, the error ended up not being
logged, thus making it harder to find the actual problem. This PR fixes
this issue in two ways:
- Uses by `TitleLandType.isAspectCompatible()` have been replaced by
terrain land type tags.
- When the structure block registry is set up for `LandGenSettings`, any
exceptions are now caught and logged.
  • Loading branch information
kirderf1 authored Apr 13, 2024
1 parent a063d13 commit 4527014
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"minestuck:heat"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"minestuck:heat"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ protected void addTags(HolderLookup.Provider provider)
this.tag(SAND).add(LandTypes.SAND.get(), LandTypes.RED_SAND.get(), LandTypes.LUSH_DESERTS.get());
this.tag(SANDSTONE).add(LandTypes.SANDSTONE.get(), LandTypes.RED_SANDSTONE.get());
this.tag(IS_DESOLATE).addTags(SAND, SANDSTONE, ROCK);
this.tag(IS_DANGEROUS).add(LandTypes.HEAT.get());
this.tag(IS_FLUID_IMPORTANT).add(LandTypes.HEAT.get());
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/mraof/minestuck/util/MSTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public static class TerrainLandTypes
public static final TagKey<TerrainLandType> SAND = tag("sand");
public static final TagKey<TerrainLandType> SANDSTONE = tag("sandstone");
public static final TagKey<TerrainLandType> IS_DESOLATE = tag("is_desolate");
public static final TagKey<TerrainLandType> IS_DANGEROUS = tag("is_dangerous");
public static final TagKey<TerrainLandType> IS_FLUID_IMPORTANT = tag("is_fluid_important");

private static TagKey<TerrainLandType> tag(String name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public final class LandGenSettings
{
this.landTypes = landTypes;

blockRegistry = new StructureBlockRegistry();
landTypes.getTerrain().registerBlocks(blockRegistry);
landTypes.getTitle().registerBlocks(blockRegistry);
blockRegistry = StructureBlockRegistry.init(landTypes);

landTypes.getTerrain().setGenSettings(this);
landTypes.getTitle().setGenSettings(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mraof.minestuck.block.MSBlocks;
import com.mraof.minestuck.world.gen.LandChunkGenerator;
import com.mraof.minestuck.world.lands.LandTypePair;
import net.minecraft.core.Direction;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
Expand All @@ -16,13 +17,16 @@
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockStateMatchTest;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;
import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public final class StructureBlockRegistry
{
private static final Logger LOGGER = LogManager.getLogger();

private static final Map<String, BlockEntry> staticRegistry = new HashMap<>();
private static final Map<Block, String> templateBlockMap = new HashMap<>();
Expand Down Expand Up @@ -130,6 +134,21 @@ public static StructureBlockRegistry getOrDefault(ChunkGenerator generator)
else return defaultRegistry;
}

public static StructureBlockRegistry init(LandTypePair landTypes)
{
try
{
StructureBlockRegistry blockRegistry = new StructureBlockRegistry();
landTypes.getTerrain().registerBlocks(blockRegistry);
landTypes.getTitle().registerBlocks(blockRegistry);
return blockRegistry;
} catch(RuntimeException e)
{
LOGGER.error("Caught exception while setting up StructureBlockRegistry.", e);
return defaultRegistry;
}
}

private static class BlockEntry
{
//TODO define restriction with a set of block state properties instead of base class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mraof.minestuck.entity.MSEntityTypes;
import com.mraof.minestuck.util.MSSoundEvents;
import com.mraof.minestuck.util.MSTags;
import com.mraof.minestuck.world.biome.LandBiomeSetType;
import com.mraof.minestuck.world.biome.LandBiomeType;
import com.mraof.minestuck.world.gen.feature.MSPlacedFeatures;
Expand All @@ -13,7 +14,6 @@
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.material.Fluids;

public class FrogsLandType extends TitleLandType
{
Expand Down Expand Up @@ -61,9 +61,7 @@ public void addBiomeGeneration(LandBiomeGenBuilder builder, StructureBlockRegist
@Override
public boolean isAspectCompatible(TerrainLandType otherType)
{
StructureBlockRegistry registry = new StructureBlockRegistry();
otherType.registerBlocks(registry);
return !registry.getBlockState("ocean").getFluidState().is(Fluids.LAVA);
return !otherType.is(MSTags.TerrainLandTypes.IS_DANGEROUS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mraof.minestuck.block.MSBlocks;
import com.mraof.minestuck.util.MSSoundEvents;
import com.mraof.minestuck.util.MSTags;
import com.mraof.minestuck.world.biome.LandBiomeSetType;
import com.mraof.minestuck.world.biome.LandBiomeType;
import com.mraof.minestuck.world.gen.LandGenSettings;
Expand All @@ -15,7 +16,6 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.Vec3;

public class PulseLandType extends TitleLandType
Expand Down Expand Up @@ -66,9 +66,7 @@ public void addBiomeGeneration(LandBiomeGenBuilder builder, StructureBlockRegist
@Override
public boolean isAspectCompatible(TerrainLandType otherType)
{
StructureBlockRegistry registry = new StructureBlockRegistry();
otherType.registerBlocks(registry);
return !registry.getBlockState("ocean").getFluidState().is(Fluids.LAVA); //Lava is likely a too important part of the terrain aspect to be replaced
return !otherType.is(MSTags.TerrainLandTypes.IS_FLUID_IMPORTANT);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mraof.minestuck.world.lands.title;

import com.mraof.minestuck.util.MSSoundEvents;
import com.mraof.minestuck.util.MSTags;
import com.mraof.minestuck.world.biome.LandBiomeSetType;
import com.mraof.minestuck.world.biome.LandBiomeType;
import com.mraof.minestuck.world.gen.feature.MSPlacedFeatures;
Expand All @@ -10,7 +11,6 @@
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.material.Fluids;

public class RabbitsLandType extends TitleLandType
{
Expand Down Expand Up @@ -44,9 +44,7 @@ public void addBiomeGeneration(LandBiomeGenBuilder builder, StructureBlockRegist
@Override
public boolean isAspectCompatible(TerrainLandType otherType)
{
StructureBlockRegistry registry = new StructureBlockRegistry();
otherType.registerBlocks(registry);
return !registry.getBlockState("ocean").getFluidState().is(Fluids.LAVA);
return !otherType.is(MSTags.TerrainLandTypes.IS_DANGEROUS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mraof.minestuck.block.MSBlocks;
import com.mraof.minestuck.util.MSSoundEvents;
import com.mraof.minestuck.util.MSTags;
import com.mraof.minestuck.world.biome.LandBiomeSetType;
import com.mraof.minestuck.world.biome.LandBiomeType;
import com.mraof.minestuck.world.gen.LandGenSettings;
Expand All @@ -13,7 +14,6 @@
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.Vec3;

public class ThoughtLandType extends TitleLandType
Expand Down Expand Up @@ -60,9 +60,7 @@ public void addBiomeGeneration(LandBiomeGenBuilder builder, StructureBlockRegist
@Override
public boolean isAspectCompatible(TerrainLandType otherType)
{
StructureBlockRegistry registry = new StructureBlockRegistry();
otherType.registerBlocks(registry);
return !registry.getBlockState("ocean").getFluidState().is(Fluids.LAVA);
return !otherType.is(MSTags.TerrainLandTypes.IS_FLUID_IMPORTANT);
}

@Override
Expand Down

0 comments on commit 4527014

Please sign in to comment.