team-abnormals / environmental

Enhances the Overworld with new foliage and wildlife and introduces some simple biomes
https://www.curseforge.com/minecraft/mc-mods/environmental
Other
57 stars 40 forks source link

1.19.2 Freezes when used with Repurposed Structures #419

Open tatoyoda600 opened 1 week ago

tatoyoda600 commented 1 week ago

Was using this mod in a modpack alongside the Repurposed Structures mod in a modpack and the game froze indefinitely upon loading a chunk with one of its structures in it, the Underground Bastion. I narrowed the modpack down to just 4 mods and filed an issue with the Repurposed Structures mod (TelepathicGrunt/RepurposedStructures#340), but the creator informed me that it's an issue with a section of this mod's code: https://github.com/team-abnormals/environmental/blob/464409ecbfc7668b4703bdfe50e557bfbaf61c80/src/main/java/com/teamabnormals/environmental/core/mixin/MobMixin.java#L46

This issue appears to happen because they include mobs in their structures, and has caused this same issue before alongside the Structure Gel API (https://gitlab.com/modding-legacy/structure-gel-api/-/issues/36).

Was able to narrow it down to the following mod combination:

Steps I used to consistently repeat the issue:

  1. Seed: -7123539784518700980
  2. Locate Underground Bastion: /locate structure #repurposed_structures:explorer_maps/bastion_underground
  3. TP to Underground Bastion: /tp @s 2384 ~ -3888

No crash report because it never reaches the crash. Game log looks fine, it freezes without ever logging anything useful, so the last thing in all my logs is always the result of the /locate command, followed by the logs for force quitting Minecraft when I decide to use Task Manager on it:

[net.minecraft.server.MinecraftServer/]: tatoyoda600 joined the game
[net.minecraft.advancements.AdvancementList/]: Loaded 19 advancements
[net.minecraft.client.gui.components.ChatComponent/]: [System] [CHAT] The nearest #repurposed_structures:explorer_maps/bastion_underground (repurposed_structures:bastion_underground) is at [2384, ~, -3888] (3696 blocks away)
[net.minecraft.client.Minecraft/]: Stopping!

The launcher just tells me that the game was closed forcefully:

Process crashed with exit code 1
tatoyoda600 commented 1 week ago

Don't know anything about Minecraft modding, so I don't know the repercussions of the following code, if any, but I was able to get the game to stop freezing by editing MobMixin.java to have the code below. https://github.com/team-abnormals/environmental/blob/464409ecbfc7668b4703bdfe50e557bfbaf61c80/src/main/java/com/teamabnormals/environmental/core/mixin/MobMixin.java#L46

The code below is based heavily on the code Structure Gel API began using after the issue cropped up there.

#

New imports added:

import net.minecraft.server.level.WorldGenRegion;
import net.minecraft.world.level.StructureManager;
import net.minecraft.world.level.ServerLevelAccessor;

Modification of the populateDefaultEquipmentSlots function:

@Inject(method = "populateDefaultEquipmentSlots", at = @At("TAIL"))
private void populateDefaultEquipmentSlots(RandomSource random, DifficultyInstance difficulty, CallbackInfo info) {
int difficultyChance = difficulty.getDifficulty().getId() + 1;
if (this.getLevel() instanceof ServerLevelAccessor levelAccessor) {
    if (levelAccessor.hasChunkAt(this.blockPosition())) {
        StructureManager structureManager = levelAccessor instanceof ServerLevel sl ? sl.structureManager() : levelAccessor instanceof WorldGenRegion wg ? levelAccessor.getLevel().structureManager().forWorldGenRegion(wg) : null;
        Optional<Named<Structure>> structures = levelAccessor.registryAccess().registryOrThrow(Registry.STRUCTURE_REGISTRY).getTag(EnvironmentalStructureTags.HAS_HEALER_POUCH);
        if (structureManager != null && structures.isPresent()) {
            boolean valid = false;
            for (Holder<Structure> structure : structures.get()) {
                if (structure != null && structureManager.getStructureAt(this.blockPosition(), structure.value()).isValid()) {
                    valid = true;
                    break;
                }
            }

            if (valid && random.nextDouble() < difficultyChance * 0.01F) {
                this.setItemSlot(EquipmentSlot.CHEST, new ItemStack(EnvironmentalItems.HEALER_POUCH.get()));
                this.armorDropChances[EquipmentSlot.CHEST.getIndex()] = 1.0F;
            }
        }
    }
}
}