Player Mobs

Player Mobs

1M Downloads

Spawn in the surface like real player

PlaIsMe opened this issue ยท 1 comments

commented

Is there anyway to make the player mob entity spawn in the surface like real player ?
I'm making a mod that use player mobs but they only spawn in cave like monster

I tried to do this but it not work, 1.18.2

@Mod.EventBusSubscriber(modid = MyMod.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class PlayerMobsSpawnOverride {
    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public static void commonSetup(FMLCommonSetupEvent event) {
        event.enqueueWork(() -> {
            LOGGER.info("[MOD DEBUG]: patching playermob spawning");
            SpawnPlacements.register(
                    EntityRegistry.PLAYER_MOB_ENTITY.get(),
                    SpawnPlacements.Type.ON_GROUND,
                    Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, Monster::checkAnyLightMonsterSpawnRules);
        });
    }
}
commented

Update: I added mixin for this, looking for better solution

@Inject(method = "finalizeSpawn", at = @At("RETURN"))
    private void teleportToSurfaceIfUnderground(ServerLevelAccessor world, DifficultyInstance difficulty, MobSpawnType reason, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag dataTag, CallbackInfoReturnable<SpawnGroupData> cir) {
        Entity self = (Entity) (Object) this;

        BlockPos pos = self.blockPosition();
        int currentY = pos.getY();
        int surfaceY = world.getHeightmapPos(Heightmap.Types.WORLD_SURFACE, pos).getY();

        if (currentY < surfaceY - 5 && world.getRandom().nextFloat() < 0.6F) {
            BlockPos surfacePos = new BlockPos(pos.getX(), surfaceY, pos.getZ());
            self.setPos(surfacePos.getX() + 0.5, surfacePos.getY(), surfacePos.getZ() + 0.5);
        }
}