|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.remove_spawn_chunks; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
| 4 | +import net.minecraft.core.BlockPos; |
| 5 | +import net.minecraft.server.level.ServerLevel; |
| 6 | +import net.minecraft.server.level.TicketType; |
| 7 | +import net.minecraft.world.entity.Entity; |
| 8 | +import net.minecraft.world.level.ChunkPos; |
| 9 | +import org.spongepowered.asm.mixin.Mixin; |
| 10 | +import org.spongepowered.asm.mixin.injection.At; |
| 11 | + |
| 12 | +@Mixin(Entity.class) |
| 13 | +public class EntityMixin { |
| 14 | + /** |
| 15 | + * @author embeddedt |
| 16 | + * @reason If the spawn chunks are not loaded, end portals linking to the overworld will teleport entities into |
| 17 | + * the void at the spawn position, which is not ideal. To solve this, we create a PORTAL ticket if the expected |
| 18 | + * overworld chunk is missing. |
| 19 | + */ |
| 20 | + @ModifyExpressionValue(method = "findDimensionEntryPoint", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;getSharedSpawnPos()Lnet/minecraft/core/BlockPos;"), require = 0) |
| 21 | + private BlockPos mfix$triggerChunkloadAtSpawnPos(BlockPos spawnPos, ServerLevel destination) { |
| 22 | + // Only apply this change if the overworld is the destination |
| 23 | + if (destination.dimension() == ServerLevel.OVERWORLD) { |
| 24 | + // No ticket is required if the chunk happens to already be loaded |
| 25 | + if(!destination.hasChunk(spawnPos.getX() >> 4, spawnPos.getZ() >> 4)) { |
| 26 | + // Create a portal ticket. While we could just load the chunk once, it would immediately unload on the |
| 27 | + // next tick, causing churn. The ticket will keep it loaded for a few seconds which should give high |
| 28 | + // performance for farms pumping things through portals frequently. |
| 29 | + BlockPos key = spawnPos.immutable(); |
| 30 | + destination.getChunkSource().addRegionTicket(TicketType.PORTAL, new ChunkPos(key), 3, key); |
| 31 | + // Wait for the chunk to be loaded, as adding the ticket is asynchronous |
| 32 | + destination.getChunk(key); |
| 33 | + } |
| 34 | + } |
| 35 | + return spawnPos; |
| 36 | + } |
| 37 | +} |
0 commit comments