Skip to content

Commit

Permalink
Add dev error if spawn placements are missing for entities that spawn…
Browse files Browse the repository at this point in the history
… in-world (#689)
  • Loading branch information
ThatGravyBoat authored Jul 10, 2024
1 parent 4c4b8a5 commit fbee11a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
private static <T extends Mob> void register(
EntityType<T> p_21755_, SpawnPlacementType p_321847_, Heightmap.Types p_21757_, SpawnPlacements.SpawnPredicate<T> p_21758_
) {
@@ -73,7 +_,8 @@
@@ -73,7 +_,13 @@
EntityType<T> p_217075_, ServerLevelAccessor p_217076_, MobSpawnType p_217077_, BlockPos p_217078_, RandomSource p_217079_
) {
SpawnPlacements.Data spawnplacements$data = DATA_BY_TYPE.get(p_217075_);
- return spawnplacements$data == null || ((SpawnPredicate<T>)spawnplacements$data.predicate).test(p_217075_, p_217076_, p_217077_, p_217078_, p_217079_);
+ boolean vanillaResult = spawnplacements$data == null || spawnplacements$data.predicate.test((EntityType)p_217075_, p_217076_, p_217077_, p_217078_, p_217079_);
+ return net.neoforged.neoforge.event.EventHooks.checkSpawnPlacements(p_217075_, p_217076_, p_217077_, p_217078_, p_217079_, vanillaResult);
+ }
+
+ // Neo: Added to allow for checking if an entity has a spawn placement
+ public static boolean hasPlacement(EntityType<?> type) {
+ return DATA_BY_TYPE.containsKey(type);
}

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistrationInfo;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.gametest.framework.GameTestServer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.SpawnPlacements;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.storage.LevelResource;
import net.neoforged.fml.config.ConfigTracker;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.util.LogicalSidedProvider;
Expand Down Expand Up @@ -158,14 +167,25 @@ private static void runModifiers(final MinecraftServer server) {
.map(Holder::value)
.toList();

final Set<EntityType<?>> entitiesWithoutPlacements = new HashSet<>();

// Apply sorted biome modifiers to each biome.
final var biomeRegistry = registries.registryOrThrow(Registries.BIOME);
biomeRegistry.holders().forEach(biomeHolder -> {
final Biome biome = biomeHolder.value();
ensureProperSync(
biomeHolder.value().modifiableBiomeInfo()
biome.modifiableBiomeInfo()
.applyBiomeModifiers(biomeHolder, biomeModifiers, registries),
biomeHolder,
biomeRegistry);

final MobSpawnSettings mobSettings = biome.getMobSettings();
mobSettings.getSpawnerTypes().forEach(category -> {
mobSettings.getMobs(category).unwrap().forEach(data -> {
if (SpawnPlacements.hasPlacement(data.type)) return;
entitiesWithoutPlacements.add(data.type);
});
});
});
// Rebuild the indexed feature list
registries.registryOrThrow(Registries.LEVEL_STEM).forEach(levelStem -> {
Expand All @@ -176,5 +196,10 @@ private static void runModifiers(final MinecraftServer server) {
registries.registryOrThrow(Registries.STRUCTURE).holders().forEach(structureHolder -> {
structureHolder.value().modifiableStructureInfo().applyStructureModifiers(structureHolder, structureModifiers);
});

if (!entitiesWithoutPlacements.isEmpty() && !FMLLoader.isProduction()) {
LOGGER.error("The following entities have not registered to the RegisterSpawnPlacementsEvent, but a spawn entry was found. This will mean that the entity doesn't have restrictions on its spawn location, please register a spawn placement for the entity, you can register with NO_RESTRICTIONS if you don't want any restrictions."
+ entitiesWithoutPlacements.stream().map(EntityType::getKey).map(ResourceLocation::toString).collect(Collectors.joining("\n\t - ", "\n\t - ", "")));
}
}
}

0 comments on commit fbee11a

Please sign in to comment.